strstr.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 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. #include <string.h>
  25. typedef unsigned chartype;
  26. char *strstr (const char *phaystack, const char *pneedle)
  27. {
  28. const unsigned char *haystack, *needle;
  29. chartype b;
  30. const unsigned char *rneedle;
  31. haystack = (const unsigned char *) phaystack;
  32. if ((b = *(needle = (const unsigned char *) pneedle)))
  33. {
  34. chartype c;
  35. haystack--; /* possible ANSI violation */
  36. {
  37. chartype a;
  38. do
  39. if (!(a = *++haystack))
  40. goto ret0;
  41. while (a != b);
  42. }
  43. if (!(c = *++needle))
  44. goto foundneedle;
  45. ++needle;
  46. goto jin;
  47. for (;;)
  48. {
  49. {
  50. chartype a;
  51. if (0)
  52. jin:{
  53. if ((a = *++haystack) == c)
  54. goto crest;
  55. }
  56. else
  57. a = *++haystack;
  58. do
  59. {
  60. for (; a != b; a = *++haystack)
  61. {
  62. if (!a)
  63. goto ret0;
  64. if ((a = *++haystack) == b)
  65. break;
  66. if (!a)
  67. goto ret0;
  68. }
  69. }
  70. while ((a = *++haystack) != c);
  71. }
  72. crest:
  73. {
  74. chartype a;
  75. {
  76. const unsigned char *rhaystack;
  77. if (*(rhaystack = haystack-- + 1) == (a = *(rneedle = needle)))
  78. do
  79. {
  80. if (!a)
  81. goto foundneedle;
  82. if (*++rhaystack != (a = *++needle))
  83. break;
  84. if (!a)
  85. goto foundneedle;
  86. }
  87. while (*++rhaystack == (a = *++needle));
  88. needle = rneedle; /* took the register-poor aproach */
  89. }
  90. if (!a)
  91. break;
  92. }
  93. }
  94. }
  95. foundneedle:
  96. return (char *) haystack;
  97. ret0:
  98. return 0;
  99. }
  100. libc_hidden_def(strstr)