]> git.mdlowis.com Git - proto/labwc.git/commitdiff
string-helpers.c: add str_starts_with()
authorJohan Malm <jgm323@gmail.com>
Tue, 20 Aug 2024 16:59:46 +0000 (17:59 +0100)
committerJohan Malm <johanmalm@users.noreply.github.com>
Wed, 18 Dec 2024 18:32:25 +0000 (18:32 +0000)
include/common/string-helpers.h
src/common/string-helpers.c
src/menu/menu.c
t/meson.build
t/str.c [new file with mode: 0644]

index e95b4e4a22d08fa155df00b3d77e897f5d60488c..b37da83bd3708960d99d9fdbe7a26ab78bfe7a85 100644 (file)
@@ -73,4 +73,12 @@ char *str_join(const char *const parts[],
  */
 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 */
index 10978507f176722eeef76ee5c6d0df73815b2ef0..a1d873dd15d2911da9fda8131bcb19d04bfba644 100644 (file)
@@ -170,3 +170,10 @@ str_endswith(const char *const string, const char *const suffix)
 
        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;
+}
+
index d1f85901ded8146be29a0cb198eef095302cff87..6a0f5ce83c8605dccb8998828b683ed396de37bf 100644 (file)
@@ -1419,12 +1419,6 @@ handle_pipemenu_timeout(void *_ctx)
        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)
 {
@@ -1468,7 +1462,7 @@ 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;
        }
index 5517b973c24a042573c590e65cbca6112907ff13..eb997fe299ac71db499a07b47fa94f2e646c4497 100644 (file)
@@ -11,6 +11,7 @@ test_lib = static_library(
 
 tests = [
   'buf-simple',
+  'str',
 ]
 
 foreach t : tests
diff --git a/t/str.c b/t/str.c
new file mode 100644 (file)
index 0000000..79debb1
--- /dev/null
+++ b/t/str.c
@@ -0,0 +1,28 @@
+// 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);
+}