strstr.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Return the offset of one string within another.
  2. Copyright (C) 1994,1996,1997,2000,2001,2003 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 Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the 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. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. /*
  16. * My personal strstr() implementation that beats most other algorithms.
  17. * Until someone tells me otherwise, I assume that this is the
  18. * fastest implementation of strstr() in C.
  19. * I deliberately chose not to comment it. You should have at least
  20. * as much fun trying to understand it, as I had to write it :-).
  21. *
  22. * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */
  23. #include <string.h>
  24. typedef unsigned chartype;
  25. char *strstr (const char *phaystack, const char *pneedle)
  26. {
  27. const unsigned char *haystack, *needle;
  28. chartype b;
  29. const unsigned char *rneedle;
  30. haystack = (const unsigned char *) phaystack;
  31. if ((b = *(needle = (const unsigned char *) pneedle)))
  32. {
  33. chartype c;
  34. haystack--; /* possible ANSI violation */
  35. {
  36. chartype a;
  37. do
  38. if (!(a = *++haystack))
  39. goto ret0;
  40. while (a != b);
  41. }
  42. if (!(c = *++needle))
  43. goto foundneedle;
  44. ++needle;
  45. goto jin;
  46. for (;;)
  47. {
  48. {
  49. chartype a;
  50. if (0)
  51. jin:{
  52. if ((a = *++haystack) == c)
  53. goto crest;
  54. }
  55. else
  56. a = *++haystack;
  57. do
  58. {
  59. for (; a != b; a = *++haystack)
  60. {
  61. if (!a)
  62. goto ret0;
  63. if ((a = *++haystack) == b)
  64. break;
  65. if (!a)
  66. goto ret0;
  67. }
  68. }
  69. while ((a = *++haystack) != c);
  70. }
  71. crest:
  72. {
  73. chartype a;
  74. {
  75. const unsigned char *rhaystack;
  76. if (*(rhaystack = haystack-- + 1) == (a = *(rneedle = needle)))
  77. do
  78. {
  79. if (!a)
  80. goto foundneedle;
  81. if (*++rhaystack != (a = *++needle))
  82. break;
  83. if (!a)
  84. goto foundneedle;
  85. }
  86. while (*++rhaystack == (a = *++needle));
  87. needle = rneedle; /* took the register-poor aproach */
  88. }
  89. if (!a)
  90. break;
  91. }
  92. }
  93. }
  94. foundneedle:
  95. return (char *) haystack;
  96. ret0:
  97. return 0;
  98. }
  99. libc_hidden_def(strstr)