~ubuntu-branches/ubuntu/breezy/gettext/breezy

« back to all changes in this revision

Viewing changes to gettext-tools/lib/strstr.c

  • Committer: Bazaar Package Importer
  • Author(s): Santiago Vila
  • Date: 2004-03-14 17:40:02 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040314174002-p1ad5ldve1hqzhye
Tags: 0.14.1-2
* Added libexpat1-dev to Build-Depends, for glade support.
* Added libc0.1-dev to Build-Depends, for GNU/kFreeBSD.
* Removed special-casing of knetbsd-gnu in debian/rules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Return the offset of one string within another.
 
2
   Copyright (C) 1994, 1996, 1997, 2000, 2002 Free Software Foundation, Inc.
 
3
   This file is part of the GNU C Library.
 
4
 
 
5
   The GNU C Library is free software; you can redistribute it and/or
 
6
   modify it under the terms of the GNU Library General Public License as
 
7
   published by the Free Software Foundation; either version 2 of the
 
8
   License, or (at your option) any later version.
 
9
 
 
10
   The GNU C Library is distributed in the hope that it will be useful,
 
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
   Library General Public License for more details.
 
14
 
 
15
   You should have received a copy of the GNU Library General Public
 
16
   License along with the GNU C Library; see the file COPYING.LIB.  If not,
 
17
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
18
   Boston, MA 02111-1307, USA.  */
 
19
 
 
20
/*
 
21
 * My personal strstr() implementation that beats most other algorithms.
 
22
 * Until someone tells me otherwise, I assume that this is the
 
23
 * fastest implementation of strstr() in C.
 
24
 * I deliberately chose not to comment it.  You should have at least
 
25
 * as much fun trying to understand it, as I had to write it :-).
 
26
 *
 
27
 * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */
 
28
 
 
29
#if HAVE_CONFIG_H
 
30
# include <config.h>
 
31
#endif
 
32
 
 
33
#if defined _LIBC || defined HAVE_STRING_H
 
34
# include <string.h>
 
35
#endif
 
36
 
 
37
typedef unsigned chartype;
 
38
 
 
39
#undef strstr
 
40
 
 
41
char *
 
42
strstr (const char *phaystack, const char *pneedle)
 
43
{
 
44
  register const unsigned char *haystack, *needle;
 
45
  register chartype b, c;
 
46
 
 
47
  haystack = (const unsigned char *) phaystack;
 
48
  needle = (const unsigned char *) pneedle;
 
49
 
 
50
  b = *needle;
 
51
  if (b != '\0')
 
52
    {
 
53
      haystack--;                               /* possible ANSI violation */
 
54
      do
 
55
        {
 
56
          c = *++haystack;
 
57
          if (c == '\0')
 
58
            goto ret0;
 
59
        }
 
60
      while (c != b);
 
61
 
 
62
      c = *++needle;
 
63
      if (c == '\0')
 
64
        goto foundneedle;
 
65
      ++needle;
 
66
      goto jin;
 
67
 
 
68
      for (;;)
 
69
        {
 
70
          register chartype a;
 
71
          register const unsigned char *rhaystack, *rneedle;
 
72
 
 
73
          do
 
74
            {
 
75
              a = *++haystack;
 
76
              if (a == '\0')
 
77
                goto ret0;
 
78
              if (a == b)
 
79
                break;
 
80
              a = *++haystack;
 
81
              if (a == '\0')
 
82
                goto ret0;
 
83
shloop:;    }
 
84
          while (a != b);
 
85
 
 
86
jin:      a = *++haystack;
 
87
          if (a == '\0')
 
88
            goto ret0;
 
89
 
 
90
          if (a != c)
 
91
            goto shloop;
 
92
 
 
93
          rhaystack = haystack-- + 1;
 
94
          rneedle = needle;
 
95
          a = *rneedle;
 
96
 
 
97
          if (*rhaystack == a)
 
98
            do
 
99
              {
 
100
                if (a == '\0')
 
101
                  goto foundneedle;
 
102
                ++rhaystack;
 
103
                a = *++needle;
 
104
                if (*rhaystack != a)
 
105
                  break;
 
106
                if (a == '\0')
 
107
                  goto foundneedle;
 
108
                ++rhaystack;
 
109
                a = *++needle;
 
110
              }
 
111
            while (*rhaystack == a);
 
112
 
 
113
          needle = rneedle;             /* took the register-poor approach */
 
114
 
 
115
          if (a == '\0')
 
116
            break;
 
117
        }
 
118
    }
 
119
foundneedle:
 
120
  return (char*) haystack;
 
121
ret0:
 
122
  return 0;
 
123
}