1 |
|
---|
2 | /***********************************************************************
|
---|
3 |
|
---|
4 | $Id: stristr.c 1348 2008-12-20 04:25:22Z gyoung $
|
---|
5 |
|
---|
6 | Copyright (c) 1993-98 M. Kimes
|
---|
7 | Copyright (c) 2003, 2008 Steven H.Levine
|
---|
8 |
|
---|
9 | Case insensitive strings
|
---|
10 |
|
---|
11 | 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
|
---|
12 |
|
---|
13 | ***********************************************************************/
|
---|
14 |
|
---|
15 | #include <string.h>
|
---|
16 | #include <ctype.h>
|
---|
17 |
|
---|
18 | #include <os2.h>
|
---|
19 |
|
---|
20 | #include "stristr.h"
|
---|
21 |
|
---|
22 | // static CHAR *strnistr(register CHAR * t, CHAR * s, LONG len);
|
---|
23 |
|
---|
24 | CHAR *stristr(const register CHAR * t, const CHAR * s)
|
---|
25 | // CHAR *stristr(register CHAR * t, CHAR * s)
|
---|
26 | {
|
---|
27 | /* case-insensitive strstr() */
|
---|
28 |
|
---|
29 | const register CHAR *t1, *s1;
|
---|
30 |
|
---|
31 | while (*t) {
|
---|
32 | t1 = t;
|
---|
33 | s1 = s;
|
---|
34 | while (*s1) {
|
---|
35 | if (toupper(*s1) != toupper(*t))
|
---|
36 | break;
|
---|
37 | else {
|
---|
38 | s1++;
|
---|
39 | t++;
|
---|
40 | }
|
---|
41 | }
|
---|
42 | if (!*s1)
|
---|
43 | return (CHAR *)t1;
|
---|
44 | t = t1 + 1;
|
---|
45 | }
|
---|
46 | return NULL;
|
---|
47 | }
|
---|
48 |
|
---|
49 | #if 0 // JBS 11 Sep 08
|
---|
50 | CHAR *strnistr(register CHAR * t, CHAR * s, LONG len)
|
---|
51 | {
|
---|
52 | /* case-insensitive strnstr() */
|
---|
53 |
|
---|
54 | register CHAR *s1;
|
---|
55 | register LONG len2;
|
---|
56 |
|
---|
57 | len2 = 0;
|
---|
58 | while (len > len2) {
|
---|
59 | s1 = s;
|
---|
60 | while (len2 < len) {
|
---|
61 | if (toupper(*s1) != toupper(t[len2]))
|
---|
62 | break;
|
---|
63 | else {
|
---|
64 | len2++;
|
---|
65 | s1++;
|
---|
66 | }
|
---|
67 | }
|
---|
68 | if (!*s1)
|
---|
69 | return t + (len2 - strlen(s));
|
---|
70 | len2++;
|
---|
71 | }
|
---|
72 | return NULL;
|
---|
73 | }
|
---|
74 | #endif
|
---|
75 |
|
---|
76 | CHAR *strnstr(register CHAR * t, CHAR * s, LONG len)
|
---|
77 | {
|
---|
78 | /* strnstr() */
|
---|
79 |
|
---|
80 | register CHAR *s1;
|
---|
81 | register LONG len2;
|
---|
82 |
|
---|
83 | len2 = 0;
|
---|
84 | while (len > len2) {
|
---|
85 | s1 = s;
|
---|
86 | while (len2 < len) {
|
---|
87 | if (*s1 != t[len2])
|
---|
88 | break;
|
---|
89 | else {
|
---|
90 | len2++;
|
---|
91 | s1++;
|
---|
92 | }
|
---|
93 | }
|
---|
94 | if (!*s1)
|
---|
95 | return t + (len2 - strlen(s));
|
---|
96 | len2++;
|
---|
97 | }
|
---|
98 | return NULL;
|
---|
99 | }
|
---|
100 |
|
---|
101 | CHAR *findstring(CHAR * findthis, ULONG lenthis, CHAR * findin,
|
---|
102 | ULONG lenin, BOOL sensitive)
|
---|
103 | {
|
---|
104 | register CHAR *this, *in;
|
---|
105 | register ULONG lenthis2, lenin2;
|
---|
106 |
|
---|
107 | if (!findthis || !findin || !lenthis || !lenin)
|
---|
108 | return NULL;
|
---|
109 | do {
|
---|
110 | this = findthis;
|
---|
111 | lenthis2 = lenthis;
|
---|
112 | in = findin;
|
---|
113 | lenin2 = lenin;
|
---|
114 | while (lenthis2 && lenin2) {
|
---|
115 | if (!sensitive) {
|
---|
116 | if (toupper(*this) != toupper(*in))
|
---|
117 | break;
|
---|
118 | else {
|
---|
119 | this++;
|
---|
120 | in++;
|
---|
121 | lenthis2--;
|
---|
122 | lenin2--;
|
---|
123 | }
|
---|
124 | }
|
---|
125 | else {
|
---|
126 | if (*this != *in)
|
---|
127 | break;
|
---|
128 | else {
|
---|
129 | this++;
|
---|
130 | in++;
|
---|
131 | lenthis2--;
|
---|
132 | lenin2--;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 | if (lenthis2) {
|
---|
137 | lenin--;
|
---|
138 | findin++;
|
---|
139 | }
|
---|
140 | } while (lenin && lenthis2);
|
---|
141 | return (lenthis2) ? NULL : in - lenthis;
|
---|
142 | }
|
---|
143 |
|
---|
144 | #pragma alloc_text(MISC8,stristr,strnstr,strnistr,findstring)
|
---|