mirror of
https://github.com/labwc/labwc.git
synced 2025-10-28 05:40:22 -04:00
string-helpers.c: add str_starts_with()
This commit is contained in:
parent
5db953aa89
commit
2446c46069
5 changed files with 45 additions and 7 deletions
|
|
@ -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 */
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ test_lib = static_library(
|
|||
|
||||
tests = [
|
||||
'buf-simple',
|
||||
'str',
|
||||
]
|
||||
|
||||
foreach t : tests
|
||||
|
|
|
|||
28
t/str.c
Normal file
28
t/str.c
Normal file
|
|
@ -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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue