blob: 835c7dbfce04ffbe8bfe7ec7050cc24895f18da3 [file] [log] [blame]
Guido van Rossume270b431992-09-03 20:21:07 +00001/***********************************************************
2Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
25/* strop module */
26
27#include "allobjects.h"
28#include "modsupport.h"
29
30
31static object *
32strop_split(self, args)
33 object *self; /* Not used */
34 object *args;
35{
36 int len, i, j;
37 char *s;
38 char c;
39 object *list, *item;
40
41 if (!getargs(args, "s#", &s, &len))
42 return NULL;
43 list = newlistobject(0);
44 if (list == NULL)
45 return NULL;
46
47 i = 0;
48 while (i < len) {
49 while (i < len &&
50 ((c = s[i]) == ' ' || c == '\t' || c == '\n')) {
51 i = i+1;
52 }
53 j = i;
54 while (i < len &&
55 !((c = s[i]) == ' ' || c == '\t' || c == '\n')) {
56 i = i+1;
57 }
58 if (j < i) {
59 item = newsizedstringobject(s+j, (int)(i-j));
60 if (item == NULL || addlistitem(list, item) < 0) {
61 DECREF(list);
62 return NULL;
63 }
64 }
65 }
66
67 return list;
68}
69
70
71static object *
72strop_splitfields(self, args)
73 object *self; /* Not used */
74 object *args;
75{
76 int len, n, i, j;
77 char *s, *sub;
78 char c;
79 object *list, *item;
80
81 if (!getargs(args, "(s#s#)", &s, &len, &sub, &n))
82 return NULL;
83 if (n == 0) {
84 err_setstr(ValueError, "empty separator");
85 return NULL;
86 }
87
88 list = newlistobject(0);
89 if (list == NULL)
90 return NULL;
91
92 i = j = 0;
93 while (i+n <= len) {
94 if (s[i] == sub[0] && (n == 1 || strncmp(s+i, sub, n) == 0)) {
95 item = newsizedstringobject(s+j, (int)(i-j));
96 if (item == NULL || addlistitem(list, item) < 0) {
97 DECREF(list);
98 return NULL;
99 }
100 i = j = i + n;
101 }
102 else
103 i++;
104 }
105 item = newsizedstringobject(s+j, (int)(len-j));
106 if (item == NULL || addlistitem(list, item) < 0) {
107 DECREF(list);
108 return NULL;
109 }
110
111 return list;
112}
113
114
115static object *
116strop_index(self, args)
117 object *self; /* Not used */
118 object *args;
119{
120 char *s, *sub;
121 int len, n, i;
122
123 if (getargs(args, "(s#s#i)", &s, &len, &sub, &n, &i)) {
124 if (i < 0 || i+n > len) {
125 err_setstr(ValueError, "start offset out of range");
126 return NULL;
127 }
128 }
129 else {
130 err_clear();
131 if (!getargs(args, "(s#s#)", &s, &len, &sub, &n))
132 return NULL;
133 i = 0;
134 }
135
136 if (n == 0)
137 return newintobject((long)i);
138
139 len -= n;
140 for (; i <= len; i++) {
141 if (s[i] == sub[0]) {
142 if (n == 1 || strncmp(s+i, sub, n) == 0)
143 return newintobject((long)i);
144 }
145 }
146
147 err_setstr(ValueError, "substring not found");
148 return NULL;
149}
150
151
152static object *
153strop_strip(self, args)
154 object *self; /* Not used */
155 object *args;
156{
157 char *s;
158 int len, i, j;
159 char c;
160
161 if (!getargs(args, "s#", &s, &len))
162 return NULL;
163
164 i = 0;
165 while (i < len && ((c = s[i]) == ' ' || c == '\t' || c == '\n')) {
166 i++;
167 }
168
169 j = len;
170 do {
171 j--;
172 } while (j >= i && ((c = s[j]) == ' ' || c == '\t' || c == '\n'));
173 j++;
174
175 if (i == 0 && j == len) {
176 INCREF(args);
177 return args;
178 }
179 else
180 return newsizedstringobject(s+i, j-i);
181}
182
183
Guido van Rossum5c850621992-09-11 23:55:51 +0000184#include <ctype.h>
185
186static object *
187strop_lower(self, args)
188 object *self; /* Not used */
189 object *args;
190{
191 char *s;
192 int i, n;
193 object *new;
194 int changed;
195
196 if (!getargs(args, "s#", &s, &n))
197 return NULL;
198 new = newsizedstringobject(s, n);
199 if (new == NULL)
200 return NULL;
201 s = getstringvalue(new);
202 changed = 0;
203 for (i = 0; i < n; i++) {
204 char c = s[i];
205 if (isupper(c)) {
206 changed = 1;
207 s[i] = tolower(c);
208 }
209 }
210 if (!changed) {
211 DECREF(new);
212 INCREF(args);
213 return args;
214 }
215 return new;
216}
217
218
219static object *
220strop_upper(self, args)
221 object *self; /* Not used */
222 object *args;
223{
224 char *s;
225 int i, n;
226 object *new;
227 int changed;
228
229 if (!getargs(args, "s#", &s, &n))
230 return NULL;
231 new = newsizedstringobject(s, n);
232 if (new == NULL)
233 return NULL;
234 s = getstringvalue(new);
235 changed = 0;
236 for (i = 0; i < n; i++) {
237 char c = s[i];
238 if (islower(c)) {
239 changed = 1;
240 s[i] = toupper(c);
241 }
242 }
243 if (!changed) {
244 DECREF(new);
245 INCREF(args);
246 return args;
247 }
248 return new;
249}
250
251
252static object *
253strop_swapcase(self, args)
254 object *self; /* Not used */
255 object *args;
256{
257 char *s;
258 int i, n;
259 object *new;
260 int changed;
261
262 if (!getargs(args, "s#", &s, &n))
263 return NULL;
264 new = newsizedstringobject(s, n);
265 if (new == NULL)
266 return NULL;
267 s = getstringvalue(new);
268 changed = 0;
269 for (i = 0; i < n; i++) {
270 char c = s[i];
271 if (islower(c)) {
272 changed = 1;
273 s[i] = toupper(c);
274 }
275 else if (isupper(c)) {
276 changed = 1;
277 s[i] = tolower(c);
278 }
279 }
280 if (!changed) {
281 DECREF(new);
282 INCREF(args);
283 return args;
284 }
285 return new;
286}
287
288
Guido van Rossume270b431992-09-03 20:21:07 +0000289/* List of functions defined in the module */
290
291static struct methodlist strop_methods[] = {
292 {"index", strop_index},
Guido van Rossum5c850621992-09-11 23:55:51 +0000293 {"lower", strop_lower},
Guido van Rossume270b431992-09-03 20:21:07 +0000294 {"split", strop_split},
295 {"splitfields", strop_splitfields},
296 {"strip", strop_strip},
Guido van Rossum5c850621992-09-11 23:55:51 +0000297 {"swapcase", strop_swapcase},
298 {"upper", strop_upper},
Guido van Rossume270b431992-09-03 20:21:07 +0000299 {NULL, NULL} /* sentinel */
300};
301
302
303/* Initialization function for the module (*must* be called initstrop) */
304
305void
306initstrop()
307{
308 initmodule("strop", strop_methods);
309}