mirror of
https://github.com/systemd/systemd.git
synced 2026-07-21 15:00:38 +00:00
string-util: add new strdupcspn()/strdupspn()
These combine strndup() + strspn()/strcspn() into one. There are a bunch of strndupa() calls that could use similar treatment (or should be converted to strdup[c]spn(), but this commit doesn't bother with that.
This commit is contained in:
@@ -1201,3 +1201,19 @@ size_t strspn_from_end(const char *str, const char *accept) {
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
char *strdupspn(const char *a, const char *accept) {
|
||||
if (isempty(a) || isempty(accept))
|
||||
return strdup("");
|
||||
|
||||
return strndup(a, strspn(a, accept));
|
||||
}
|
||||
|
||||
char *strdupcspn(const char *a, const char *reject) {
|
||||
if (isempty(a))
|
||||
return strdup("");
|
||||
if (isempty(reject))
|
||||
return strdup(a);
|
||||
|
||||
return strndup(a, strcspn(a, reject));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user