strstr.c 2.9 KB

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