Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1913 | jaeger | 1 | #include |
2 | |||
3 | tp_obj tp_string_t(TP, int n) { |
||
4 | tp_obj r = tp_string_n(0,n); |
||
5 | r.string.info = (_tp_string*)tp_malloc(sizeof(_tp_string)+n); |
||
6 | r.string.val = r.string.info->s; |
||
7 | return r; |
||
8 | } |
||
9 | |||
10 | tp_obj tp_printf(TP, char const *fmt,...) { |
||
11 | int l; |
||
12 | tp_obj r; |
||
13 | char tmp[2000]; |
||
14 | char *s; |
||
15 | va_list arg; |
||
16 | va_start(arg, fmt); |
||
17 | l = vsnprintf(tmp, sizeof(tmp), fmt,arg); |
||
18 | r = tp_string_t(tp,l); |
||
19 | s = r.string.info->s; |
||
20 | va_end(arg); |
||
21 | va_start(arg, fmt); |
||
22 | vsprintf(s,fmt,arg); |
||
23 | va_end(arg); |
||
24 | return tp_track(tp,r); |
||
25 | } |
||
26 | |||
27 | int _tp_str_index(tp_obj s, tp_obj k) { |
||
28 | int i=0; |
||
29 | while ((s.string.len - i) >= k.string.len) { |
||
30 | if (memcmp(s.string.val+i,k.string.val,k.string.len) == 0) { |
||
31 | return i; |
||
32 | } |
||
33 | i += 1; |
||
34 | } |
||
35 | return -1; |
||
36 | } |
||
37 | |||
38 | tp_obj tp_join(TP) { |
||
39 | tp_obj delim = TP_OBJ(); |
||
40 | tp_obj val = TP_OBJ(); |
||
41 | int l=0,i; |
||
42 | tp_obj r; |
||
43 | char *s; |
||
44 | for (i=0; i |
||
45 | if (i!=0) { l += delim.string.len; } |
||
46 | l += tp_str(tp,val.list.val->items[i]).string.len; |
||
47 | } |
||
48 | r = tp_string_t(tp,l); |
||
49 | s = r.string.info->s; |
||
50 | l = 0; |
||
51 | for (i=0; i |
||
52 | tp_obj e; |
||
53 | if (i!=0) { |
||
54 | memcpy(s+l,delim.string.val,delim.string.len); l += delim.string.len; |
||
55 | } |
||
56 | e = tp_str(tp,val.list.val->items[i]); |
||
57 | memcpy(s+l,e.string.val,e.string.len); l += e.string.len; |
||
58 | } |
||
59 | return tp_track(tp,r); |
||
60 | } |
||
61 | |||
62 | tp_obj tp_string_slice(TP,tp_obj s, int a, int b) { |
||
63 | tp_obj r = tp_string_t(tp,b-a); |
||
64 | char *m = r.string.info->s; |
||
65 | memcpy(m,s.string.val+a,b-a); |
||
66 | return tp_track(tp,r); |
||
67 | } |
||
68 | |||
69 | tp_obj tp_split(TP) { |
||
70 | tp_obj v = TP_OBJ(); |
||
71 | tp_obj d = TP_OBJ(); |
||
72 | tp_obj r = tp_list(tp); |
||
73 | |||
74 | int i; |
||
75 | while ((i=_tp_str_index(v,d))!=-1) { |
||
76 | _tp_list_append(tp,r.list.val,tp_string_slice(tp,v,0,i)); |
||
77 | v.string.val += i + d.string.len; v.string.len -= i + d.string.len; |
||
78 | /* tp_grey(tp,r); // should stop gc or something instead*/ |
||
79 | } |
||
80 | _tp_list_append(tp,r.list.val,tp_string_slice(tp,v,0,v.string.len)); |
||
81 | /* tp_grey(tp,r); // should stop gc or something instead*/ |
||
82 | return r; |
||
83 | } |
||
84 | |||
85 | |||
86 | tp_obj tp_find(TP) { |
||
87 | tp_obj s = TP_OBJ(); |
||
88 | tp_obj v = TP_OBJ(); |
||
89 | return tp_number(_tp_str_index(s,v)); |
||
90 | } |
||
91 | |||
92 | tp_obj tp_str_index(TP) { |
||
93 | tp_obj s = TP_OBJ(); |
||
94 | tp_obj v = TP_OBJ(); |
||
95 | int n = _tp_str_index(s,v); |
||
96 | if (n >= 0) { return tp_number(n); } |
||
97 | tp_raise(tp_None,"tp_str_index(%s,%s)",s,v); |
||
98 | } |
||
99 | |||
100 | tp_obj tp_str2(TP) { |
||
101 | tp_obj v = TP_OBJ(); |
||
102 | return tp_str(tp,v); |
||
103 | } |
||
104 | |||
105 | tp_obj tp_chr(TP) { |
||
106 | int v = TP_NUM(); |
||
107 | return tp_string_n(tp->chars[(unsigned char)v],1); |
||
108 | } |
||
109 | tp_obj tp_ord(TP) { |
||
110 | char const *s = TP_STR(); |
||
111 | return tp_number((unsigned char)s[0]); |
||
112 | } |
||
113 | |||
114 | tp_obj tp_strip(TP) { |
||
115 | char const *v = TP_STR(); |
||
116 | int i, l = strlen(v); int a = l, b = 0; |
||
117 | tp_obj r; |
||
118 | char *s; |
||
119 | for (i=0; i |
||
120 | if (v[i] != ' ' && v[i] != '\n' && v[i] != '\t' && v[i] != '\r') { |
||
121 | a = _tp_min(a,i); b = _tp_max(b,i+1); |
||
122 | } |
||
123 | } |
||
124 | if ((b-a) < 0) { return tp_string(""); } |
||
125 | r = tp_string_t(tp,b-a); |
||
126 | s = r.string.info->s; |
||
127 | memcpy(s,v+a,b-a); |
||
128 | return tp_track(tp,r); |
||
129 | } |
||
130 | |||
131 | |||
132 | tp_obj tp_replace(TP) { |
||
133 | tp_obj s = TP_OBJ(); |
||
134 | tp_obj k = TP_OBJ(); |
||
135 | tp_obj v = TP_OBJ(); |
||
136 | tp_obj p = s; |
||
137 | int i,n = 0; |
||
138 | int c; |
||
139 | int l; |
||
140 | tp_obj rr; |
||
141 | char *r; |
||
142 | char *d; |
||
143 | tp_obj z; |
||
144 | while ((i = _tp_str_index(p,k)) != -1) { |
||
145 | n += 1; |
||
146 | p.string.val += i + k.string.len; p.string.len -= i + k.string.len; |
||
147 | } |
||
148 | /* fprintf(stderr,"ns: %d\n",n); */ |
||
149 | l = s.string.len + n * (v.string.len-k.string.len); |
||
150 | rr = tp_string_t(tp,l); |
||
151 | r = rr.string.info->s; |
||
152 | d = r; |
||
153 | z = p = s; |
||
154 | while ((i = _tp_str_index(p,k)) != -1) { |
||
155 | p.string.val += i; p.string.len -= i; |
||
156 | memcpy(d,z.string.val,c=(p.string.val-z.string.val)); d += c; |
||
157 | p.string.val += k.string.len; p.string.len -= k.string.len; |
||
158 | memcpy(d,v.string.val,v.string.len); d += v.string.len; |
||
159 | z = p; |
||
160 | } |
||
161 | memcpy(d,z.string.val,(s.string.val + s.string.len) - z.string.val); |
||
162 | |||
163 | return tp_track(tp,rr); |
||
164 | }> |
||
165 |