diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2021-02-04 03:21:08 +0900 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2021-02-09 14:22:54 +0900 |
commit | e5bc5f1f5ac3595bfbe77d4896d6b7b22a0310a8 (patch) | |
tree | ebf3a88d73ad0a49008c9a441a58ad387702e900 /src/fundamental/string-util-fundamental.c | |
parent | 5f33b2300b1ce7749a686dd1699ea0818b45857c (diff) | |
download | systemd-e5bc5f1f5ac3595bfbe77d4896d6b7b22a0310a8.tar.gz |
fundamental: move several macros and functions into src/fundamental/
sd-boot has a copy of a subset of codes from libbasic. This makes
sd-boot share the code with libbasic, and dedup the code.
Note, startswith_no_case() is dropped from sd-boot, as
- it is not used,
- the previous implementation is not correct,
- gnu-efi does not have StrniCmp() or so.
Diffstat (limited to 'src/fundamental/string-util-fundamental.c')
-rw-r--r-- | src/fundamental/string-util-fundamental.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/fundamental/string-util-fundamental.c b/src/fundamental/string-util-fundamental.c new file mode 100644 index 0000000000..a94bce6e3e --- /dev/null +++ b/src/fundamental/string-util-fundamental.c @@ -0,0 +1,76 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#ifndef SD_BOOT +#include "macro.h" +#endif +#include "string-util-fundamental.h" + +sd_char *startswith(const sd_char *s, const sd_char *prefix) { + sd_size_t l; + + assert(s); + assert(prefix); + + l = strlen(prefix); + if (!strneq(s, prefix, l)) + return NULL; + + return (sd_char*) s + l; +} + +#ifndef SD_BOOT +sd_char *startswith_no_case(const sd_char *s, const sd_char *prefix) { + sd_size_t l; + + assert(s); + assert(prefix); + + l = strlen(prefix); + if (!strncaseeq(s, prefix, l)) + return NULL; + + return (sd_char*) s + l; +} +#endif + +sd_char* endswith(const sd_char *s, const sd_char *postfix) { + sd_size_t sl, pl; + + assert(s); + assert(postfix); + + sl = strlen(s); + pl = strlen(postfix); + + if (pl == 0) + return (sd_char*) s + sl; + + if (sl < pl) + return NULL; + + if (strcmp(s + sl - pl, postfix) != 0) + return NULL; + + return (sd_char*) s + sl - pl; +} + +sd_char* endswith_no_case(const sd_char *s, const sd_char *postfix) { + sd_size_t sl, pl; + + assert(s); + assert(postfix); + + sl = strlen(s); + pl = strlen(postfix); + + if (pl == 0) + return (sd_char*) s + sl; + + if (sl < pl) + return NULL; + + if (strcasecmp(s + sl - pl, postfix) != 0) + return NULL; + + return (sd_char*) s + sl - pl; +} |