strstr.c 2.7 KB

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