Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e270b43

Browse files
committed
Re-implement some operations from string.py in C, for speed.
1 parent be0cba4 commit e270b43

1 file changed

Lines changed: 201 additions & 0 deletions

File tree

Modules/stropmodule.c

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/***********************************************************
2+
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
3+
Netherlands.
4+
5+
All Rights Reserved
6+
7+
Permission to use, copy, modify, and distribute this software and its
8+
documentation for any purpose and without fee is hereby granted,
9+
provided that the above copyright notice appear in all copies and that
10+
both that copyright notice and this permission notice appear in
11+
supporting documentation, and that the names of Stichting Mathematisch
12+
Centrum or CWI not be used in advertising or publicity pertaining to
13+
distribution of the software without specific, written prior permission.
14+
15+
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16+
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17+
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18+
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21+
OF 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+
31+
static object *
32+
strop_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+
71+
static object *
72+
strop_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+
115+
static object *
116+
strop_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+
152+
static object *
153+
strop_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+
184+
/* List of functions defined in the module */
185+
186+
static struct methodlist strop_methods[] = {
187+
{"index", strop_index},
188+
{"split", strop_split},
189+
{"splitfields", strop_splitfields},
190+
{"strip", strop_strip},
191+
{NULL, NULL} /* sentinel */
192+
};
193+
194+
195+
/* Initialization function for the module (*must* be called initstrop) */
196+
197+
void
198+
initstrop()
199+
{
200+
initmodule("strop", strop_methods);
201+
}

0 commit comments

Comments
 (0)