X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;ds=sidebyside;f=ccan%2Fstring%2Fstring.h;fp=ccan%2Fstring%2Fstring.h;h=f4997c69adbef7c08561040c64586235c2892ec2;hb=650c775ff00cccd03fc84e7789a03c51d9839004;hp=0000000000000000000000000000000000000000;hpb=c8acddea39d222312102952e91c32cfe4dd2cea0;p=ccan diff --git a/ccan/string/string.h b/ccan/string/string.h new file mode 100644 index 00000000..f4997c69 --- /dev/null +++ b/ccan/string/string.h @@ -0,0 +1,46 @@ +#ifndef CCAN_STRING_H +#define CCAN_STRING_H +#include +#include + +/** + * streq - Are two strings equal? + * @a: first string + * @b: first string + * + * This macro is arguably more readable than "!strcmp(a, b)". + * + * Example: + * if (streq(str, "")) + * printf("String is empty!\n"); + */ +#define streq(a,b) (strcmp((a),(b)) == 0) + +/** + * strstarts - Does this string start with this prefix? + * @str: string to test + * @prefix: prefix to look for at start of str + * + * Example: + * if (strstarts(str, "foo")) + * printf("String %s begins with 'foo'!\n", str); + */ +#define strstarts(str,prefix) (strncmp((str),(prefix),strlen(prefix)) == 0) + +/** + * strends - Does this string end with this postfix? + * @str: string to test + * @postfix: postfix to look for at end of str + * + * Example: + * if (strends(str, "foo")) + * printf("String %s end with 'foo'!\n", str); + */ +static inline bool strends(const char *str, const char *postfix) +{ + if (strlen(str) < strlen(postfix)) + return false; + + return streq(str + strlen(str) - strlen(postfix), postfix); +} +#endif /* CCAN_STRING_H */