strstr.c 2.8 KB

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