*/
bool str_endswith(const char *const string, const char *const suffix);
+/**
+ * str_starts_with - indicate whether a string starts with a given character
+ * @string: string to test
+ * @needle: character to expect in string
+ * @ignore_chars: characters to ignore at start such as space and "\t"
+ */
+bool str_starts_with(const char *s, char needle, const char *ignore_chars);
+
#endif /* LABWC_STRING_HELPERS_H */
return strcmp(string + len_str - len_sfx, suffix) == 0;
}
+
+bool
+str_starts_with(const char *s, char needle, const char *ignore_chars)
+{
+ return (s + strspn(s, ignore_chars))[0] == needle;
+}
+
return 0;
}
-static bool
-starts_with_less_than(const char *s)
-{
- return (s + strspn(s, " \t\r\n"))[0] == '<';
-}
-
static int
handle_pipemenu_readable(int fd, uint32_t mask, void *_ctx)
{
}
/* Guard against badly formed data such as binary input */
- if (!starts_with_less_than(ctx->buf.data)) {
+ if (!str_starts_with(ctx->buf.data, '<', " \t\r\n")) {
wlr_log(WLR_ERROR, "expect xml data to start with '<'; abort pipemenu");
goto clean_up;
}
tests = [
'buf-simple',
+ 'str',
]
foreach t : tests
--- /dev/null
+// SPDX-License-Identifier: GPL-2.0-only
+#define _POSIX_C_SOURCE 200809L
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <string.h>
+#include <cmocka.h>
+#include "common/string-helpers.h"
+
+static void
+test_str_starts_with(void **state)
+{
+ (void)state;
+
+ assert_true(str_starts_with(" foo", 'f', " \t\r\n"));
+ assert_true(str_starts_with("f", 'f', " \t\r\n"));
+ assert_false(str_starts_with(" foo", '<', " "));
+}
+
+int main(int argc, char **argv)
+{
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_str_starts_with),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}