strstr.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #if HAVE_CONFIG_H
  25. # include <config.h>
  26. #endif
  27. #if defined _LIBC || defined HAVE_STRING_H
  28. # include <string.h>
  29. #endif
  30. typedef unsigned chartype;
  31. #undef strstr
  32. char *strstr( const char *phaystack, const char *pneedle)
  33. {
  34. register const unsigned char *haystack, *needle;
  35. register chartype b, c;
  36. haystack = (const unsigned char *) phaystack;
  37. needle = (const unsigned char *) pneedle;
  38. b = *needle;
  39. if (b != '\0') {
  40. haystack--; /* possible ANSI violation */
  41. do {
  42. c = *++haystack;
  43. if (c == '\0')
  44. goto ret0;
  45. }
  46. while (c != b);
  47. c = *++needle;
  48. if (c == '\0')
  49. goto foundneedle;
  50. ++needle;
  51. goto jin;
  52. for (;;) {
  53. register chartype a;
  54. register const unsigned char *rhaystack, *rneedle;
  55. do {
  56. a = *++haystack;
  57. if (a == '\0')
  58. goto ret0;
  59. if (a == b)
  60. break;
  61. a = *++haystack;
  62. if (a == '\0')
  63. goto ret0;
  64. shloop:}
  65. while (a != b);
  66. jin:a = *++haystack;
  67. if (a == '\0')
  68. goto ret0;
  69. if (a != c)
  70. goto shloop;
  71. rhaystack = haystack-- + 1;
  72. rneedle = needle;
  73. a = *rneedle;
  74. if (*rhaystack == a)
  75. do {
  76. if (a == '\0')
  77. goto foundneedle;
  78. ++rhaystack;
  79. a = *++needle;
  80. if (*rhaystack != a)
  81. break;
  82. if (a == '\0')
  83. goto foundneedle;
  84. ++rhaystack;
  85. a = *++needle;
  86. }
  87. while (*rhaystack == a);
  88. needle = rneedle; /* took the register-poor approach */
  89. if (a == '\0')
  90. break;
  91. }
  92. }
  93. foundneedle:
  94. return (char *) haystack;
  95. ret0:
  96. return 0;
  97. }