-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.c
More file actions
223 lines (205 loc) · 5.84 KB
/
string.c
File metadata and controls
223 lines (205 loc) · 5.84 KB
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
/* File: String
* String handling functions.
*/
/*
* Create a new empty string of a certain size.
* Does not put it in for GC tracking, since contents should be
* filled after returning.
*/
tp_obj tp_string_t(TP, int n) {
tp_obj r = tp_string_n(0,n);
r.string.info = (_tp_string*)tp_malloc(tp, sizeof(_tp_string)+n);
r.string.info->len = n;
r.string.val = r.string.info->s;
return r;
}
/*
* Create a new string which is a copy of some memory.
* This is put into GC tracking for you.
*/
tp_obj tp_string_copy(TP, const char *s, int n) {
tp_obj r = tp_string_t(tp,n);
memcpy(r.string.info->s,s,n);
return tp_track(tp,r);
}
/*
* Create a new string which is a substring slice of another STRING.
* Does not need to be put into GC tracking, as its parent is
* already being tracked (supposedly).
*/
tp_obj tp_string_sub(TP, tp_obj s, int a, int b) {
int l = s.string.len;
a = _tp_max(0,(a<0?l+a:a)); b = _tp_min(l,(b<0?l+b:b));
tp_obj r = s;
r.string.val += a;
r.string.len = b-a;
return r;
}
tp_obj tp_printf(TP, char const *fmt,...) {
int l;
tp_obj r;
char *s;
va_list arg;
va_start(arg, fmt);
l = vsnprintf(NULL, 0, fmt,arg);
r = tp_string_t(tp,l);
s = r.string.info->s;
va_end(arg);
va_start(arg, fmt);
vsprintf(s,fmt,arg);
va_end(arg);
return tp_track(tp,r);
}
int _tp_str_index(tp_obj s, tp_obj k) {
int i=0;
while ((s.string.len - i) >= k.string.len) {
if (memcmp(s.string.val+i,k.string.val,k.string.len) == 0) {
return i;
}
i += 1;
}
return -1;
}
tp_obj tp_join(TP) {
tp_obj delim = TP_OBJ();
tp_obj val = TP_OBJ();
int l=0,i;
tp_obj r;
char *s;
for (i=0; i<val.list.val->len; i++) {
if (i!=0) { l += delim.string.len; }
l += tp_str(tp,val.list.val->items[i]).string.len;
}
r = tp_string_t(tp,l);
s = r.string.info->s;
l = 0;
for (i=0; i<val.list.val->len; i++) {
tp_obj e;
if (i!=0) {
memcpy(s+l,delim.string.val,delim.string.len); l += delim.string.len;
}
e = tp_str(tp,val.list.val->items[i]);
memcpy(s+l,e.string.val,e.string.len); l += e.string.len;
}
return tp_track(tp,r);
}
/* Function: tp_string_slice
* Create a new string sliced from an existing string.
*
* Unlike <tp_string> and <tp_string_n>, a copy of the actual string is made,
* and storage for the new string will always be managed by tinypy, no matter
* what kind of string you pass.
*
* Example:
* > tp_obj foo(void)
* > {
* > char test[4] = "foo";
* > return tp_string_slice(tp_string(test), 0, 3);
* > }
*
* This will work correctly, even though the variable "test" will go out of
* scope when the function returns, because tp_string_splice makes its own
* copy of the string.
*
* Parameters:
* s - The string to create a substring of.
* a - Index of the first character to include in the slice.
* b - Index of the first character not to include.
*
* Returns:
* A new string object corresponding to s[a:b] in actual tinypy code.
*/
tp_obj tp_string_slice(TP,tp_obj s, int a, int b) {
tp_obj r = tp_string_copy(tp,s.string.val+a,b-a);
return r;
}
tp_obj tp_split(TP) {
tp_obj v = TP_OBJ();
tp_obj d = TP_OBJ();
tp_obj r = tp_list(tp);
int i;
while ((i=_tp_str_index(v,d))!=-1) {
_tp_list_append(tp,r.list.val,tp_string_slice(tp,v,0,i));
v.string.val += i + d.string.len; v.string.len -= i + d.string.len;
/* tp_grey(tp,r); // should stop gc or something instead*/
}
_tp_list_append(tp,r.list.val,tp_string_slice(tp,v,0,v.string.len));
/* tp_grey(tp,r); // should stop gc or something instead*/
return r;
}
tp_obj tp_find(TP) {
tp_obj s = TP_OBJ();
tp_obj v = TP_OBJ();
return tp_number(_tp_str_index(s,v));
}
tp_obj tp_str_index(TP) {
tp_obj s = TP_OBJ();
tp_obj v = TP_OBJ();
int n = _tp_str_index(s,v);
if (n >= 0) { return tp_number(n); }
tp_raise(tp_None,tp_string("(tp_str_index) ValueError: substring not found"));
}
tp_obj tp_str2(TP) {
tp_obj v = TP_OBJ();
return tp_str(tp,v);
}
tp_obj tp_chr(TP) {
int v = TP_NUM();
return tp_string_n(tp->chars[(unsigned char)v],1);
}
tp_obj tp_ord(TP) {
tp_obj s = TP_STR();
if (s.string.len != 1) {
tp_raise(tp_None,tp_string("(tp_ord) TypeError: ord() expected a character"));
}
return tp_number((unsigned char)s.string.val[0]);
}
tp_obj tp_strip(TP) {
tp_obj o = TP_TYPE(TP_STRING);
char const *v = o.string.val; int l = o.string.len;
int i; int a = l, b = 0;
tp_obj r;
char *s;
for (i=0; i<l; i++) {
if (v[i] != ' ' && v[i] != '\n' && v[i] != '\t' && v[i] != '\r') {
a = _tp_min(a,i); b = _tp_max(b,i+1);
}
}
if ((b-a) < 0) { return tp_string(""); }
r = tp_string_t(tp,b-a);
s = r.string.info->s;
memcpy(s,v+a,b-a);
return tp_track(tp,r);
}
tp_obj tp_replace(TP) {
tp_obj s = TP_OBJ();
tp_obj k = TP_OBJ();
tp_obj v = TP_OBJ();
tp_obj p = s;
int i,n = 0;
int c;
int l;
tp_obj rr;
char *r;
char *d;
tp_obj z;
while ((i = _tp_str_index(p,k)) != -1) {
n += 1;
p.string.val += i + k.string.len; p.string.len -= i + k.string.len;
}
/* fprintf(stderr,"ns: %d\n",n); */
l = s.string.len + n * (v.string.len-k.string.len);
rr = tp_string_t(tp,l);
r = rr.string.info->s;
d = r;
z = p = s;
while ((i = _tp_str_index(p,k)) != -1) {
p.string.val += i; p.string.len -= i;
memcpy(d,z.string.val,c=(p.string.val-z.string.val)); d += c;
p.string.val += k.string.len; p.string.len -= k.string.len;
memcpy(d,v.string.val,v.string.len); d += v.string.len;
z = p;
}
memcpy(d,z.string.val,(s.string.val + s.string.len) - z.string.val);
return tp_track(tp,rr);
}