1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
|
/***********************************************************
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
******************************************************************/
/* strop module */
#include "allobjects.h"
#include "modsupport.h"
#include <ctype.h>
/* XXX This file assumes that the <ctype.h> is*() functions
XXX are defined for all 8-bit characters! */
#include <errno.h>
/* The lstrip(), rstrip() and strip() functions are implemented
in do_strip(), which uses an additional parameter to indicate what
type of strip should occur. */
#define LEFTSTRIP 0
#define RIGHTSTRIP 1
#define BOTHSTRIP 2
static object *
split_whitespace(s, len, maxsplit)
char *s;
int len;
int maxsplit;
{
int i, j, err;
int countsplit;
object *list, *item;
list = newlistobject(0);
if (list == NULL)
return NULL;
i = 0;
countsplit = 0;
while (i < len) {
while (i < len && isspace(Py_CHARMASK(s[i]))) {
i = i+1;
}
j = i;
while (i < len && !isspace(Py_CHARMASK(s[i]))) {
i = i+1;
}
if (j < i) {
item = newsizedstringobject(s+j, (int)(i-j));
if (item == NULL) {
DECREF(list);
return NULL;
}
err = addlistitem(list, item);
DECREF(item);
if (err < 0) {
DECREF(list);
return NULL;
}
countsplit++;
if (maxsplit && (countsplit >= maxsplit)) {
item = newsizedstringobject(s+i, (int)(len - i));
if (item == NULL) {
DECREF(list);
return NULL;
}
err = addlistitem(list, item);
DECREF(item);
if (err < 0) {
DECREF(list);
return NULL;
}
i = len;
}
}
}
return list;
}
static object *
strop_splitfields(self, args)
object *self; /* Not used */
object *args;
{
int len, n, i, j, err;
int splitcount, maxsplit;
char *s, *sub;
object *list, *item;
sub = NULL;
n = 0;
splitcount = 0;
maxsplit = 0;
if (!newgetargs(args, "s#|z#i", &s, &len, &sub, &n, &maxsplit))
return NULL;
if (sub == NULL)
return split_whitespace(s, len, maxsplit);
if (n == 0) {
err_setstr(ValueError, "empty separator");
return NULL;
}
list = newlistobject(0);
if (list == NULL)
return NULL;
i = j = 0;
while (i+n <= len) {
if (s[i] == sub[0] && (n == 1 || strncmp(s+i, sub, n) == 0)) {
item = newsizedstringobject(s+j, (int)(i-j));
if (item == NULL)
goto fail;
err = addlistitem(list, item);
DECREF(item);
if (err < 0)
goto fail;
i = j = i + n;
splitcount++;
if (maxsplit && (splitcount >= maxsplit))
break;
}
else
i++;
}
item = newsizedstringobject(s+j, (int)(len-j));
if (item == NULL)
goto fail;
err = addlistitem(list, item);
DECREF(item);
if (err < 0)
goto fail;
return list;
fail:
DECREF(list);
return NULL;
}
static object *
strop_joinfields(self, args)
object *self; /* Not used */
object *args;
{
object *seq, *item, *res;
object * (*getitem) FPROTO((object *, int));
char *sep, *p;
int seplen, seqlen, reslen, itemlen, i;
sep = NULL;
seplen = 0;
if (!newgetargs(args, "O|s#", &seq, &sep, &seplen))
return NULL;
if (sep == NULL) {
sep = " ";
seplen = 1;
}
if (is_listobject(seq)) {
getitem = getlistitem;
seqlen = getlistsize(seq);
}
else if (is_tupleobject(seq)) {
getitem = gettupleitem;
seqlen = gettuplesize(seq);
}
else {
err_setstr(TypeError, "first argument must be list/tuple");
return NULL;
}
reslen = 0;
for (i = 0; i < seqlen; i++) {
item = getitem(seq, i);
if (!is_stringobject(item)) {
err_setstr(TypeError,
"first argument must be list/tuple of strings");
return NULL;
}
if (i > 0)
reslen = reslen + seplen;
reslen = reslen + getstringsize(item);
}
if (seqlen == 1) {
/* Optimization if there's only one item */
item = getitem(seq, 0);
INCREF(item);
return item;
}
res = newsizedstringobject((char *)NULL, reslen);
if (res == NULL)
return NULL;
p = getstringvalue(res);
for (i = 0; i < seqlen; i++) {
item = getitem(seq, i);
if (i > 0) {
memcpy(p, sep, seplen);
p += seplen;
}
itemlen = getstringsize(item);
memcpy(p, getstringvalue(item), itemlen);
p += itemlen;
}
if (p != getstringvalue(res) + reslen) {
err_setstr(SystemError, "strop.joinfields: assertion failed");
return NULL;
}
return res;
}
static object *
strop_find(self, args)
object *self; /* Not used */
object *args;
{
char *s, *sub;
int len, n, i;
if (getargs(args, "(s#s#i)", &s, &len, &sub, &n, &i)) {
if (i < 0)
i += len;
if (i < 0)
i = 0;
}
else {
err_clear();
if (!getargs(args, "(s#s#)", &s, &len, &sub, &n))
return NULL;
i = 0;
}
if (n == 0)
return newintobject((long)i);
len -= n;
for (; i <= len; ++i)
if (s[i] == sub[0] &&
(n == 1 || strncmp(&s[i+1], &sub[1], n-1) == 0))
return newintobject((long)i);
return newintobject(-1L);
}
static object *
strop_rfind(self, args)
object *self; /* Not used */
object *args;
{
char *s, *sub;
int len, n, i, j;
if (getargs(args, "(s#s#i)", &s, &len, &sub, &n, &i)) {
if (i < 0)
i += len;
if (i < 0)
i = 0;
}
else {
err_clear();
if (!getargs(args, "(s#s#)", &s, &len, &sub, &n))
return NULL;
i = 0;
}
if (n == 0)
return newintobject((long)len);
for (j = len-n; j >= i; --j)
if (s[j] == sub[0] &&
(n == 1 || strncmp(&s[j+1], &sub[1], n-1) == 0))
return newintobject((long)j);
return newintobject(-1L);
}
static object *
do_strip(args, striptype)
object *args;
int striptype;
{
char *s;
int len, i, j;
if (!getargs(args, "s#", &s, &len))
return NULL;
i = 0;
if (striptype != RIGHTSTRIP) {
while (i < len && isspace(Py_CHARMASK(s[i]))) {
i++;
}
}
j = len;
if (striptype != LEFTSTRIP) {
do {
j--;
} while (j >= i && isspace(Py_CHARMASK(s[j])));
j++;
}
if (i == 0 && j == len) {
INCREF(args);
return args;
}
else
return newsizedstringobject(s+i, j-i);
}
static object *
strop_strip(self, args)
object *self; /* Not used */
object *args;
{
return do_strip(args, BOTHSTRIP);
}
static object *
strop_lstrip(self, args)
object *self; /* Not used */
object *args;
{
return do_strip(args, LEFTSTRIP);
}
static object *
strop_rstrip(self, args)
object *self; /* Not used */
object *args;
{
return do_strip(args, RIGHTSTRIP);
}
static object *
strop_lower(self, args)
object *self; /* Not used */
object *args;
{
char *s, *s_new;
int i, n;
object *new;
int changed;
if (!getargs(args, "s#", &s, &n))
return NULL;
new = newsizedstringobject(NULL, n);
if (new == NULL)
return NULL;
s_new = getstringvalue(new);
changed = 0;
for (i = 0; i < n; i++) {
int c = Py_CHARMASK(*s++);
if (isupper(c)) {
changed = 1;
*s_new = tolower(c);
} else
*s_new = c;
s_new++;
}
if (!changed) {
DECREF(new);
INCREF(args);
return args;
}
return new;
}
static object *
strop_upper(self, args)
object *self; /* Not used */
object *args;
{
char *s, *s_new;
int i, n;
object *new;
int changed;
if (!getargs(args, "s#", &s, &n))
return NULL;
new = newsizedstringobject(NULL, n);
if (new == NULL)
return NULL;
s_new = getstringvalue(new);
changed = 0;
for (i = 0; i < n; i++) {
int c = Py_CHARMASK(*s++);
if (islower(c)) {
changed = 1;
*s_new = toupper(c);
} else
*s_new = c;
s_new++;
}
if (!changed) {
DECREF(new);
INCREF(args);
return args;
}
return new;
}
static object *
strop_capitalize(self, args)
object *self; /* Not used */
object *args;
{
char *s, *s_new;
int i, n;
object *new;
int changed;
if (!getargs(args, "s#", &s, &n))
return NULL;
new = newsizedstringobject(NULL, n);
if (new == NULL)
return NULL;
s_new = getstringvalue(new);
changed = 0;
if (0 < n) {
int c = Py_CHARMASK(*s++);
if (islower(c)) {
changed = 1;
*s_new = toupper(c);
} else
*s_new = c;
s_new++;
}
for (i = 1; i < n; i++) {
int c = Py_CHARMASK(*s++);
if (isupper(c)) {
changed = 1;
*s_new = tolower(c);
} else
*s_new = c;
s_new++;
}
if (!changed) {
DECREF(new);
INCREF(args);
return args;
}
return new;
}
static object *
strop_swapcase(self, args)
object *self; /* Not used */
object *args;
{
char *s, *s_new;
int i, n;
object *new;
int changed;
if (!getargs(args, "s#", &s, &n))
return NULL;
new = newsizedstringobject(NULL, n);
if (new == NULL)
return NULL;
s_new = getstringvalue(new);
changed = 0;
for (i = 0; i < n; i++) {
int c = Py_CHARMASK(*s++);
if (islower(c)) {
changed = 1;
*s_new = toupper(c);
}
else if (isupper(c)) {
changed = 1;
*s_new = tolower(c);
}
else
*s_new = c;
s_new++;
}
if (!changed) {
DECREF(new);
INCREF(args);
return args;
}
return new;
}
static object *
strop_atoi(self, args)
object *self; /* Not used */
object *args;
{
extern long mystrtol PROTO((const char *, char **, int));
extern unsigned long mystrtoul PROTO((const char *, char **, int));
char *s, *end;
int base = 10;
long x;
if (args != NULL && is_tupleobject(args)) {
if (!getargs(args, "(si)", &s, &base))
return NULL;
if (base != 0 && base < 2 || base > 36) {
err_setstr(ValueError, "invalid base for atoi()");
return NULL;
}
}
else if (!getargs(args, "s", &s))
return NULL;
if (s[0] == '\0') {
err_setstr(ValueError, "empty string for atoi()");
return NULL;
}
errno = 0;
if (base == 0 && s[0] == '0')
x = (long) mystrtoul(s, &end, base);
else
x = mystrtol(s, &end, base);
if (*end != '\0') {
err_setstr(ValueError, "invalid literal for atoi()");
return NULL;
}
else if (errno != 0) {
err_setstr(OverflowError, "atoi() literal too large");
return NULL;
}
return newintobject(x);
}
static object *
strop_atol(self, args)
object *self; /* Not used */
object *args;
{
char *s, *end;
int base = 10;
object *x;
if (args != NULL && is_tupleobject(args)) {
if (!getargs(args, "(si)", &s, &base))
return NULL;
if (base != 0 && base < 2 || base > 36) {
err_setstr(ValueError, "invalid base for atol()");
return NULL;
}
}
else if (!getargs(args, "s", &s))
return NULL;
if (s[0] == '\0') {
err_setstr(ValueError, "empty string for atol()");
return NULL;
}
x = long_escan(s, &end, base);
if (x == NULL)
return NULL;
if (base == 0 && (*end == 'l' || *end == 'L'))
end++;
if (*end != '\0') {
err_setstr(ValueError, "invalid literal for atol()");
DECREF(x);
return NULL;
}
return x;
}
static object *
strop_atof(self, args)
object *self; /* Not used */
object *args;
{
extern double strtod PROTO((const char *, char **));
char *s, *end;
double x;
if (!getargs(args, "s", &s))
return NULL;
if (s[0] == '\0') {
err_setstr(ValueError, "empty string for atof()");
return NULL;
}
errno = 0;
x = strtod(s, &end);
if (*end != '\0') {
err_setstr(ValueError, "invalid literal for atof()");
return NULL;
}
else if (errno != 0) {
err_setstr(OverflowError, "atof() literal too large");
return NULL;
}
return newfloatobject(x);
}
static PyObject *
strop_maketrans(self, args)
PyObject *self; /* Not used */
PyObject *args;
{
unsigned char c[256], *from=NULL, *to=NULL;
int i, fromlen=0, tolen=0;
if (PyTuple_Size(args)!=0) {
if (!PyArg_ParseTuple(args, "s#s#", &from, &fromlen,
&to, &tolen))
return NULL;
}
if (fromlen!=tolen) {
PyErr_SetString(ValueError,
"maketrans arguments must have same length");
return NULL;
}
for(i=0; i<256; i++)
c[i]=(unsigned char)i;
for(i=0; i<fromlen; i++) {
c[from[i]]=to[i];
}
return PyString_FromStringAndSize((char *)c, 256);
}
static object *
strop_translate(self, args)
object *self;
object *args;
{
char *input, *table, *output, *output_start, *delete=NULL;
int inlen, tablen, dellen;
PyObject *result;
int i, trans_table[256];
if (!PyArg_ParseTuple(args, "s#s#|s#", &input, &inlen,
&table, &tablen, &delete, &dellen))
return NULL;
if (tablen != 256) {
PyErr_SetString(ValueError,
"translation table must be 256 characters long");
return NULL;
}
for(i=0; i<256; i++)
trans_table[i]=Py_CHARMASK(table[i]);
if (delete!=NULL) {
for(i=0; i<dellen; i++)
trans_table[delete[i]]=-1;
}
result = PyString_FromStringAndSize((char *)NULL, inlen);
if (result == NULL)
return NULL;
output_start = output = PyString_AsString(result);
if (delete!=NULL && dellen!=0) {
for (i = 0; i < inlen; i++) {
int c = Py_CHARMASK(*input++);
if (trans_table[c]!=-1)
*output++ = (char)trans_table[c];
}
/* Fix the size of the resulting string */
if (inlen > 0 &&_PyString_Resize(&result, output-output_start))
return NULL;
} else {
/* If no deletions are required, use a faster loop */
for (i = 0; i < inlen; i++) {
int c = Py_CHARMASK(*input++);
*output++ = (char)trans_table[c];
}
}
return result;
}
/* List of functions defined in the module */
static struct methodlist strop_methods[] = {
{"atof", strop_atof},
{"atoi", strop_atoi},
{"atol", strop_atol},
{"capitalize", strop_capitalize},
{"find", strop_find},
{"join", strop_joinfields, 1},
{"joinfields", strop_joinfields, 1},
{"lstrip", strop_lstrip},
{"lower", strop_lower},
{"rfind", strop_rfind},
{"rstrip", strop_rstrip},
{"split", strop_splitfields, 1},
{"splitfields", strop_splitfields, 1},
{"strip", strop_strip},
{"swapcase", strop_swapcase},
{"maketrans", strop_maketrans, 1},
{"translate", strop_translate, 1},
{"upper", strop_upper},
{NULL, NULL} /* sentinel */
};
void
initstrop()
{
object *m, *d, *s;
char buf[256];
int c, n;
m = initmodule("strop", strop_methods);
d = getmoduledict(m);
/* Create 'whitespace' object */
n = 0;
for (c = 0; c < 256; c++) {
if (isspace(c))
buf[n++] = c;
}
s = newsizedstringobject(buf, n);
if (s) {
dictinsert(d, "whitespace", s);
DECREF(s);
}
/* Create 'lowercase' object */
n = 0;
for (c = 0; c < 256; c++) {
if (islower(c))
buf[n++] = c;
}
s = newsizedstringobject(buf, n);
if (s) {
dictinsert(d, "lowercase", s);
DECREF(s);
}
/* Create 'uppercase' object */
n = 0;
for (c = 0; c < 256; c++) {
if (isupper(c))
buf[n++] = c;
}
s = newsizedstringobject(buf, n);
if (s) {
dictinsert(d, "uppercase", s);
DECREF(s);
}
if (err_occurred())
fatal("can't initialize module strop");
}
|