strcasestr.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* Case-insensitive searching in a string.
  2. Copyright (C) 2005-2015 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2005.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include <string.h>
  17. #include <ctype.h>
  18. #include <stdbool.h>
  19. #include <strings.h>
  20. #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
  21. /* Two-Way algorithm. */
  22. #define RETURN_TYPE char *
  23. #define AVAILABLE(h, h_l, j, n_l) \
  24. (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l)) \
  25. && ((h_l) = (j) + (n_l)))
  26. #define CANON_ELEMENT(c) TOLOWER (c)
  27. #define CMP_FUNC(p1, p2, l) \
  28. strncasecmp ((const char *) (p1), (const char *) (p2), l)
  29. #include "str-two-way.h"
  30. /* Find the first occurrence of NEEDLE in HAYSTACK, using
  31. case-insensitive comparison. This function gives unspecified
  32. results in multibyte locales. */
  33. char *
  34. strcasestr (const char *haystack_start, const char *needle_start)
  35. {
  36. const char *haystack = haystack_start;
  37. const char *needle = needle_start;
  38. size_t needle_len; /* Length of NEEDLE. */
  39. size_t haystack_len; /* Known minimum length of HAYSTACK. */
  40. bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */
  41. /* Determine length of NEEDLE, and in the process, make sure
  42. HAYSTACK is at least as long (no point processing all of a long
  43. NEEDLE if HAYSTACK is too short). */
  44. while (*haystack && *needle)
  45. {
  46. ok &= (TOLOWER ((unsigned char) *haystack)
  47. == TOLOWER ((unsigned char) *needle));
  48. haystack++;
  49. needle++;
  50. }
  51. if (*needle)
  52. return NULL;
  53. if (ok)
  54. return (char *) haystack_start;
  55. needle_len = needle - needle_start;
  56. haystack = haystack_start + 1;
  57. haystack_len = needle_len - 1;
  58. /* Perform the search. Abstract memory is considered to be an array
  59. of 'unsigned char' values, not an array of 'char' values. See
  60. ISO C 99 section 6.2.6.1. */
  61. if (needle_len < LONG_NEEDLE_THRESHOLD)
  62. return two_way_short_needle ((const unsigned char *) haystack,
  63. haystack_len,
  64. (const unsigned char *) needle_start,
  65. needle_len);
  66. return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
  67. (const unsigned char *) needle_start,
  68. needle_len);
  69. }
  70. #undef LONG_NEEDLE_THRESHOLD