Wed, 19 Dec 2012 03:27:15 +0200
Good bunch of changes
0 | 1 | /* |
2 | * botc source code | |
3 | * Copyright (C) 2012 Santeri `Dusk` Piippo | |
4 | * All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright notice, | |
10 | * this list of conditions and the following disclaimer. | |
11 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
12 | * this list of conditions and the following disclaimer in the documentation | |
13 | * and/or other materials provided with the distribution. | |
3 | 14 | * 3. Neither the name of the developer nor the names of its contributors may |
15 | * be used to endorse or promote products derived from this software without | |
16 | * specific prior written permission. | |
0 | 17 | * 4. Redistributions in any form must be accompanied by information on how to |
18 | * obtain complete source code for the software and any accompanying | |
19 | * software that uses the software. The source code must either be included | |
20 | * in the distribution or be available for no more than the cost of | |
21 | * distribution plus a nominal fee, and must be freely redistributable | |
22 | * under reasonable conditions. For an executable file, complete source | |
23 | * code means the source code for all modules it contains. It does not | |
24 | * include source code for modules or files that typically accompany the | |
25 | * major components of the operating system on which the executable file | |
26 | * runs. | |
27 | * | |
28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
29 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
34 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
35 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
36 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
37 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
38 | * POSSIBILITY OF SUCH DAMAGE. | |
39 | */ | |
40 | ||
41 | #include <stdio.h> | |
42 | #include <stdlib.h> | |
43 | #include <string.h> | |
44 | #include <stdarg.h> | |
66
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
45 | #include "array.h" |
0 | 46 | #include "str.h" |
47 | #include "common.h" | |
48 | ||
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
32
diff
changeset
|
49 | #define ITERATE_STRING(u) \ |
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
32
diff
changeset
|
50 | for (unsigned int u = 0; u < strlen (text); u++) |
0 | 51 | |
52 | // ============================================================================ | |
53 | // vdynformat: Try to write to a formatted string with size bytes first, if | |
54 | // that fails, double the size and keep recursing until it works. | |
55 | char* vdynformat (const char* c, va_list v, unsigned int size) { | |
56 | char* buffer = new char[size]; | |
57 | int r = vsnprintf (buffer, size-1, c, v); | |
58 | if (r > (int)size-1 || r < 0) { | |
59 | delete buffer; | |
60 | buffer = vdynformat (c, v, size*2); | |
61 | } | |
62 | return buffer; | |
63 | } | |
64 | ||
65 | // ============================================================================ | |
66 | str::str () { | |
67 | text = new char[1]; | |
68 | clear(); | |
69 | alloclen = strlen (text); | |
70 | } | |
71 | ||
72 | str::str (const char* c) { | |
73 | text = new char[1]; | |
74 | clear (); | |
75 | alloclen = strlen (text); | |
76 | append (c); | |
77 | } | |
78 | ||
79 | str::str (char c) { | |
80 | text = new char[1]; | |
81 | clear (); | |
82 | alloclen = strlen (text); | |
83 | append (c); | |
84 | } | |
85 | ||
86 | // ============================================================================ | |
87 | void str::clear () { | |
88 | delete text; | |
89 | text = new char[1]; | |
90 | text[0] = '\0'; | |
91 | curs = 0; | |
92 | } | |
93 | ||
94 | unsigned int str::len () {return strlen(text);} | |
95 | ||
96 | // ============================================================================ | |
97 | void str::resize (unsigned int len) { | |
98 | unsigned int oldlen = strlen (text); | |
99 | char* oldtext = new char[oldlen]; | |
100 | strncpy (oldtext, text, oldlen); | |
101 | ||
102 | delete text; | |
103 | text = new char[len+1]; | |
2
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
104 | for (unsigned int u = 0; u < len+1; u++) |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
105 | text[u] = 0; |
0 | 106 | strncpy (text, oldtext, len); |
107 | ||
108 | alloclen = len; | |
109 | } | |
110 | ||
111 | // ============================================================================ | |
112 | void str::dump () { | |
113 | for (unsigned int u = 0; u <= alloclen; u++) | |
2
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
114 | printf ("\t%u. %u (%c)\n", u, text[u], text[u]); |
0 | 115 | } |
116 | ||
117 | // ============================================================================ | |
118 | // Adds a new character at the end of the string. | |
119 | void str::append (char c) { | |
120 | // Out of space, thus resize | |
121 | if (curs == alloclen) | |
122 | resize (alloclen+1); | |
123 | text[curs] = c; | |
124 | curs++; | |
125 | } | |
126 | ||
127 | void str::append (const char* c) { | |
128 | resize (alloclen + strlen (c)); | |
129 | ||
130 | for (unsigned int u = 0; u < strlen (c); u++) { | |
131 | if (c[u] != 0) | |
132 | append (c[u]); | |
133 | } | |
134 | } | |
135 | ||
136 | void str::append (str c) { | |
137 | append (c.chars()); | |
138 | } | |
139 | ||
140 | // ============================================================================ | |
141 | void str::appendformat (const char* c, ...) { | |
142 | va_list v; | |
143 | ||
144 | va_start (v, c); | |
145 | char* buf = vdynformat (c, v, 256); | |
146 | va_end (v); | |
147 | ||
148 | append (buf); | |
149 | } | |
150 | ||
151 | void str::appendformat (str c, ...) { | |
152 | va_list v; | |
153 | ||
154 | va_start (v, c); | |
155 | char* buf = vdynformat (c.chars(), v, 256); | |
156 | va_end (v); | |
157 | ||
158 | append (buf); | |
159 | } | |
160 | ||
161 | // ============================================================================ | |
162 | char* str::chars () { | |
163 | return text; | |
164 | } | |
165 | ||
166 | // ============================================================================ | |
167 | unsigned int str::first (const char* c, unsigned int a) { | |
168 | unsigned int r = 0; | |
169 | unsigned int index = 0; | |
170 | for (; a < alloclen; a++) { | |
171 | if (text[a] == c[r]) { | |
172 | if (r == 0) | |
173 | index = a; | |
174 | ||
175 | r++; | |
176 | if (r == strlen (c)) | |
177 | return index; | |
178 | } else { | |
179 | if (r != 0) { | |
180 | // If the string sequence broke at this point, we need to | |
181 | // check this character again, for a new sequence just | |
182 | // might start right here. | |
183 | a--; | |
184 | } | |
185 | ||
186 | r = 0; | |
187 | } | |
188 | } | |
189 | ||
190 | return len (); | |
191 | } | |
192 | ||
193 | // ============================================================================ | |
194 | unsigned int str::last (const char* c, int a) { | |
195 | if (a == -1) | |
196 | a = len(); | |
197 | ||
198 | int max = strlen (c)-1; | |
199 | ||
200 | int r = max; | |
201 | for (; a >= 0; a--) { | |
202 | if (text[a] == c[r]) { | |
203 | r--; | |
204 | if (r == -1) | |
205 | return a; | |
206 | } else { | |
207 | if (r != max) | |
208 | a++; | |
209 | ||
210 | r = max; | |
211 | } | |
212 | } | |
213 | ||
214 | return len (); | |
215 | } | |
216 | ||
217 | // ============================================================================ | |
218 | str str::substr (unsigned int a, unsigned int b) { | |
219 | if (a > len()) a = len(); | |
220 | if (b > len()) b = len(); | |
221 | ||
222 | if (b == a) | |
223 | return ""; | |
224 | ||
225 | if (b < a) { | |
226 | printf ("str::substr: indices %u and %u given, should be the other way around, swapping..\n", a, b); | |
227 | ||
228 | // Swap the variables | |
229 | unsigned int c = a; | |
230 | a = b; | |
231 | b = c; | |
232 | } | |
233 | ||
234 | char* s = new char[b-a]; | |
235 | strncpy (s, text+a, b-a); | |
236 | return str(s); | |
237 | } | |
238 | ||
239 | // ============================================================================ | |
240 | void str::remove (unsigned int idx, unsigned int dellen) { | |
241 | str s1 = substr (0, idx); | |
242 | str s2 = substr (idx + dellen, len()); | |
243 | ||
244 | clear(); | |
245 | ||
246 | append (s1); | |
247 | append (s2); | |
248 | } | |
249 | ||
250 | // ============================================================================ | |
251 | void str::trim (int dellen) { | |
252 | if (!dellen) | |
253 | return; | |
254 | ||
32
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
255 | unsigned int delpos; |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
256 | if (dellen > 0) { |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
257 | delpos = len()-dellen; |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
258 | text[delpos] = 0; |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
259 | curs -= dellen; |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
260 | } else { |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
261 | str s = substr (-dellen, len()); |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
262 | clear(); |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
263 | append (s); |
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
264 | } |
0 | 265 | } |
266 | ||
267 | // ============================================================================ | |
268 | void str::replace (const char* o, const char* n, unsigned int a) { | |
269 | unsigned int idx; | |
270 | ||
271 | while ((idx = first (o, a)) != len()) { | |
272 | str s1 = substr (0, idx); | |
273 | str s2 = substr (idx + strlen (o), len()); | |
274 | ||
275 | clear(); | |
276 | ||
277 | append (s1); | |
278 | append (n); | |
279 | append (s2); | |
280 | } | |
281 | } | |
282 | ||
283 | void str::insert (char* c, unsigned int pos) { | |
284 | str s1 = substr (0, pos); | |
285 | str s2 = substr (pos, len()); | |
286 | ||
287 | clear(); | |
288 | append (s1); | |
289 | append (c); | |
290 | append (s2); | |
291 | } | |
292 | ||
293 | void str::reverse () { | |
294 | char* tmp = new char[alloclen]; | |
295 | strcpy (tmp, text); | |
296 | ||
297 | clear(); | |
298 | curs = 0; | |
299 | resize (alloclen); | |
300 | for (int i = alloclen-1; i >= 0; i--) | |
301 | append (tmp[i]); | |
302 | } | |
303 | ||
304 | void str::repeat (unsigned int n) { | |
305 | char* tmp = new char[alloclen]; | |
306 | strcpy (tmp, text); | |
307 | ||
308 | for (; n > 0; n--) | |
309 | append (tmp); | |
310 | } | |
311 | ||
312 | // ============================================================================ | |
313 | bool str::isnumber () { | |
30
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
17
diff
changeset
|
314 | ITERATE_STRING (u) { |
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
17
diff
changeset
|
315 | // Minus sign as the first character is allowed for negatives |
32
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
30
diff
changeset
|
316 | if (!u && text[u] == '-') |
30
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
17
diff
changeset
|
317 | continue; |
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
17
diff
changeset
|
318 | |
8
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
319 | if (text[u] < '0' || text[u] > '9') |
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
320 | return false; |
30
6c4efed2dbdd
Negative numbers are now considered numbers too...
Teemu Piippo <crimsondusk64@gmail.com>
parents:
17
diff
changeset
|
321 | } |
8
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
322 | return true; |
0 | 323 | } |
324 | ||
325 | // ============================================================================ | |
326 | bool str::isword () { | |
327 | ITERATE_STRING (u) { | |
8
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
328 | // lowercase letters |
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
329 | if (text[u] >= 'a' || text[u] <= 'z') |
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
330 | continue; |
0 | 331 | |
8
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
332 | // uppercase letters |
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
333 | if (text[u] >= 'A' || text[u] <= 'Z') |
c8bfa7e6ae1b
Commands are now read properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
334 | continue; |
0 | 335 | |
336 | return false; | |
337 | } | |
338 | return true; | |
339 | } | |
340 | ||
341 | // ============================================================================ | |
342 | int str::compare (const char* c) { | |
343 | return strcmp (text, c); | |
344 | } | |
345 | ||
346 | int str::compare (str c) { | |
347 | return compare (c.chars()); | |
348 | } | |
349 | ||
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
350 | int str::icompare (const char* c) { |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
351 | return icompare (str ((char*)c)); |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
352 | } |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
353 | |
17
b4fcc69e426a
Events and commands are now treated properly case-insensitively.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
354 | int str::icompare (str b) { |
b4fcc69e426a
Events and commands are now treated properly case-insensitively.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
355 | return strcmp (tolower().chars(), b.tolower().chars()); |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
356 | } |
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
8
diff
changeset
|
357 | |
0 | 358 | // ============================================================================ |
2
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
359 | str str::tolower () { |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
360 | str n = text; |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
361 | |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
362 | for (uint u = 0; u < len(); u++) { |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
363 | if (n[u] > 'A' && n[u] < 'Z') |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
364 | n.text[u] += ('a' - 'A'); |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
365 | } |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
366 | |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
367 | return n; |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
368 | } |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
369 | |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
370 | // ============================================================================ |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
371 | str str::toupper () { |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
372 | str n = text; |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
373 | |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
374 | for (uint u = 0; u < len(); u++) { |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
375 | if (n[u] > 'a' && n[u] < 'z') |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
376 | n.text[u] -= ('A' - 'a'); |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
377 | } |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
378 | |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
379 | return n; |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
380 | } |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
381 | |
bb2c45522eb6
Added event definitions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
382 | // ============================================================================ |
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
383 | unsigned int str::count (char* c) { |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
384 | unsigned int r = 0; |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
385 | unsigned int tmp = 0; |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
386 | ITERATE_STRING (u) { |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
387 | if (text[u] == c[r]) { |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
388 | r++; |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
389 | if (r == strlen (c)) { |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
390 | r = 0; |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
391 | tmp++; |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
392 | } |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
393 | } else { |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
394 | if (r != 0) |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
395 | u--; |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
396 | r = 0; |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
397 | } |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
398 | } |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
399 | |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
400 | return tmp; |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
401 | } |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
402 | |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
403 | // ============================================================================ |
66
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
404 | array<str> str::split (str del) { |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
405 | array<str> res; |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
406 | unsigned int a = 0; |
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
407 | |
66
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
408 | // Find all separators and store the text left to them. |
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
409 | while (1) { |
66
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
410 | unsigned int b = first (del, a); |
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
411 | |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
412 | if (b == len()) |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
413 | break; |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
414 | |
66
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
415 | res.push (substr (a, b)); |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
416 | a = b + strlen (del); |
6
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
417 | } |
0005527cad62
Command definitions are now read into memory.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
418 | |
66
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
419 | // Add the string at the right of the last separator |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
420 | res.push (substr (a, len())); |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
421 | return res; |
0 | 422 | } |
423 | ||
66
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
424 | array<str> str::operator/ (str splitstring) {return split(splitstring);} |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
425 | array<str> str::operator/ (char* splitstring) {return split(splitstring);} |
4fc1ec88aa41
Good bunch of changes
Teemu Piippo <crimsondusk64@gmail.com>
parents:
34
diff
changeset
|
426 | array<str> str::operator/ (const char* splitstring) {return split(splitstring);} |