string.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #ifndef CCAN_STRING_H
  2. #define CCAN_STRING_H
  3. #include <string.h>
  4. #include <stdbool.h>
  5. /**
  6. * streq - Are two strings equal?
  7. * @a: first string
  8. * @b: first string
  9. *
  10. * This macro is arguably more readable than "!strcmp(a, b)".
  11. *
  12. * Example:
  13. * if (streq(str, ""))
  14. * printf("String is empty!\n");
  15. */
  16. #define streq(a,b) (strcmp((a),(b)) == 0)
  17. /**
  18. * strstarts - Does this string start with this prefix?
  19. * @str: string to test
  20. * @prefix: prefix to look for at start of str
  21. *
  22. * Example:
  23. * if (strstarts(str, "foo"))
  24. * printf("String %s begins with 'foo'!\n", str);
  25. */
  26. #define strstarts(str,prefix) (strncmp((str),(prefix),strlen(prefix)) == 0)
  27. /**
  28. * strends - Does this string end with this postfix?
  29. * @str: string to test
  30. * @postfix: postfix to look for at end of str
  31. *
  32. * Example:
  33. * if (strends(str, "foo"))
  34. * printf("String %s end with 'foo'!\n", str);
  35. */
  36. static inline bool strends(const char *str, const char *postfix)
  37. {
  38. if (strlen(str) < strlen(postfix))
  39. return false;
  40. return streq(str + strlen(str) - strlen(postfix), postfix);
  41. }
  42. #endif /* CCAN_STRING_H */