1
0

string_nocasestr.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * string_nocasestr.c
  3. *
  4. * Find a string within another string in a case insensitive manner
  5. */
  6. #include "private.h"
  7. #include "lub/ctype.h"
  8. /*--------------------------------------------------------- */
  9. const char *lub_string_nocasestr(const char *cs, const char *ct)
  10. {
  11. const char *p = NULL;
  12. const char *result = NULL;
  13. while (*cs) {
  14. const char *q = cs;
  15. p = ct;
  16. /*lint -e155 Ignoring { }'ed sequence within an expression, 0 assumed
  17. * MACRO implementation uses braces to prevent multiple increments
  18. * when called.
  19. */
  20. /*lint -e506 Constant value Boolean
  21. * not the case because of tolower() evaluating to 0 under lint
  22. * (see above)
  23. */
  24. while (*p && *q
  25. && (lub_ctype_tolower(*p) == lub_ctype_tolower(*q))) {
  26. p++, q++;
  27. }
  28. if (0 == *p) {
  29. break;
  30. }
  31. cs++;
  32. }
  33. if (p && !*p) {
  34. /* we've found the first match of ct within cs */
  35. result = cs;
  36. }
  37. return result;
  38. }
  39. /*--------------------------------------------------------- */
  40. unsigned int lub_string_equal_part(const char *str1, const char *str2,
  41. bool_t utf8)
  42. {
  43. unsigned int cnt = 0;
  44. if (!str1 || !str2)
  45. return cnt;
  46. while (*str1 && *str2) {
  47. if (*str1 != *str2)
  48. break;
  49. cnt++;
  50. str1++;
  51. str2++;
  52. }
  53. if (!utf8)
  54. return cnt;
  55. /* UTF8 features */
  56. if (cnt && (UTF8_11 == (*(str1 - 1) & UTF8_MASK)))
  57. cnt--;
  58. return cnt;
  59. }
  60. /*--------------------------------------------------------- */