1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-01 09:21:26 +03:00

util: return the remaining string in startswith()

This commit is contained in:
Lennart Poettering 2012-10-27 01:20:01 +02:00
parent ada45c785f
commit e8988fc2a2
2 changed files with 22 additions and 29 deletions

View File

@ -216,45 +216,38 @@ char* endswith(const char *s, const char *postfix) {
return (char*) s + sl - pl; return (char*) s + sl - pl;
} }
bool startswith(const char *s, const char *prefix) { char* startswith(const char *s, const char *prefix) {
size_t sl, pl; const char *a, *b;
assert(s); assert(s);
assert(prefix); assert(prefix);
sl = strlen(s); a = s, b = prefix;
pl = strlen(prefix); for (;;) {
if (*b == 0)
return (char*) a;
if (*a != *b)
return NULL;
if (pl == 0) a++, b++;
return true; }
if (sl < pl)
return false;
return memcmp(s, prefix, pl) == 0;
} }
bool startswith_no_case(const char *s, const char *prefix) { char* startswith_no_case(const char *s, const char *prefix) {
size_t sl, pl; const char *a, *b;
unsigned i;
assert(s); assert(s);
assert(prefix); assert(prefix);
sl = strlen(s); a = s, b = prefix;
pl = strlen(prefix); for (;;) {
if (*b == 0)
return (char*) a;
if (tolower(*a) != tolower(*b))
return NULL;
if (pl == 0) a++, b++;
return true; }
if (sl < pl)
return false;
for(i = 0; i < pl; ++i)
if (tolower(s[i]) != tolower(prefix[i]))
return false;
return true;
} }
bool first_word(const char *s, const char *word) { bool first_word(const char *s, const char *word) {

View File

@ -142,8 +142,8 @@ static inline bool isempty(const char *p) {
} }
char *endswith(const char *s, const char *postfix); char *endswith(const char *s, const char *postfix);
bool startswith(const char *s, const char *prefix); char *startswith(const char *s, const char *prefix);
bool startswith_no_case(const char *s, const char *prefix); char *startswith_no_case(const char *s, const char *prefix);
bool first_word(const char *s, const char *word); bool first_word(const char *s, const char *word);