strstr.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Return the offset of one string within another.
  2. Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8. The GNU C Library 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 GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If not,
  14. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. /*
  17. * My personal strstr() implementation that beats most other algorithms.
  18. * Until someone tells me otherwise, I assume that this is the
  19. * fastest implementation of strstr() in C.
  20. * I deliberately chose not to comment it. You should have at least
  21. * as much fun trying to understand it, as I had to write it :-).
  22. *
  23. * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */
  24. /* added strcasestr support, davidm@lineo.com */
  25. #if HAVE_CONFIG_H
  26. # include <config.h>
  27. #endif
  28. #if defined _LIBC || defined HAVE_STRING_H
  29. # include <string.h>
  30. #endif
  31. typedef unsigned chartype;
  32. #if defined(L_strstr)
  33. #define VAL(x) (x)
  34. #define FUNC strstr
  35. #undef strstr
  36. #elif defined(L_strcasestr)
  37. #include <ctype.h>
  38. #define VAL(x) tolower(x)
  39. #define FUNC strcasestr
  40. #undef strcasestr
  41. #else
  42. #error "No target function defined."
  43. #endif
  44. char * FUNC ( const char *phaystack, const char *pneedle)
  45. {
  46. register const unsigned char *haystack, *needle;
  47. register chartype b, c;
  48. haystack = (const unsigned char *) phaystack;
  49. needle = (const unsigned char *) pneedle;
  50. b = *needle;
  51. if (b != '\0') {
  52. haystack--; /* possible ANSI violation */
  53. do {
  54. c = *++haystack;
  55. if (c == '\0')
  56. goto ret0;
  57. }
  58. while (VAL(c) != VAL(b));
  59. c = *++needle;
  60. if (c == '\0')
  61. goto foundneedle;
  62. ++needle;
  63. goto jin;
  64. for (;;) {
  65. register chartype a;
  66. register const unsigned char *rhaystack, *rneedle;
  67. do {
  68. a = *++haystack;
  69. if (a == '\0')
  70. goto ret0;
  71. if (VAL(a) == VAL(b))
  72. break;
  73. a = *++haystack;
  74. if (a == '\0')
  75. goto ret0;
  76. shloop:}
  77. while (VAL(a) != VAL(b));
  78. jin:a = *++haystack;
  79. if (a == '\0')
  80. goto ret0;
  81. if (VAL(a) != VAL(c))
  82. goto shloop;
  83. rhaystack = haystack-- + 1;
  84. rneedle = needle;
  85. a = *rneedle;
  86. if (VAL(*rhaystack) == VAL(a))
  87. do {
  88. if (a == '\0')
  89. goto foundneedle;
  90. ++rhaystack;
  91. a = *++needle;
  92. if (VAL(*rhaystack) != VAL(a))
  93. break;
  94. if (a == '\0')
  95. goto foundneedle;
  96. ++rhaystack;
  97. a = *++needle;
  98. }
  99. while (VAL(*rhaystack) == VAL(a));
  100. needle = rneedle; /* took the register-poor approach */
  101. if (a == '\0')
  102. break;
  103. }
  104. }
  105. foundneedle:
  106. return (char *) haystack;
  107. ret0:
  108. return 0;
  109. }