-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString.h
191 lines (173 loc) · 4.7 KB
/
String.h
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
#if 1
/*
* Create a new empty string of a certain size.
* Does not put it in for GC tracking, since contents should be
* filled after returning.
*/
inline tp_obj tp_string_t(tp_vm *tp, int n) {
tp_obj r = tp_string_n(0, n);
r.string.info = (_tp_string*)calloc(sizeof(_tp_string) + n, 1);
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.
*/
inline tp_obj tp_string_copy(tp_vm *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).
*/
inline tp_obj tp_string_sub(tp_vm *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;
}
inline tp_obj tp_printf(tp_vm *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);
}
inline 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;
}
inline tp_obj tp_join(tp_vm *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);
}
inline tp_obj tp_split(tp_vm *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_sub(tp, v, 0, i));
v.string.val += i + d.string.len; v.string.len -= i + d.string.len;
}
_tp_list_append(tp, r.list.val, tp_string_sub(tp, v, 0, v.string.len));
return r;
}
inline tp_obj tp_find(tp_vm *tp) {
tp_obj s = TP_OBJ();
tp_obj v = TP_OBJ();
return tp_number(_tp_str_index(s, v));
}
inline tp_obj tp_str_index(tp_vm *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"));
}
inline tp_obj tp_str2(tp_vm *tp) {
tp_obj v = TP_OBJ();
return tp_str(tp, v);
}
inline tp_obj tp_chr(tp_vm *tp) {
int v = TP_NUM();
return tp_string_n(tp->chars[(unsigned char)v], 1);
}
inline tp_obj tp_ord(tp_vm *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]);
}
inline tp_obj tp_strip(tp_vm *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);
}
inline tp_obj tp_replace(tp_vm *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);
}
#endif