ucx/string.c

changeset 189
f43bbd33fec0
parent 185
a48428642b4e
child 192
1e51558b9d09
equal deleted inserted replaced
188:63f87e2884c1 189:f43bbd33fec0
27 */ 27 */
28 28
29 #include <stdlib.h> 29 #include <stdlib.h>
30 #include <string.h> 30 #include <string.h>
31 #include <stdarg.h> 31 #include <stdarg.h>
32 #include <ctype.h>
32 33
33 #include "string.h" 34 #include "string.h"
34 #include "allocator.h" 35 #include "allocator.h"
35 36
36 sstr_t sstr(char *cstring) { 37 sstr_t sstr(char *cstring) {
294 return newstring; 295 return newstring;
295 } 296 }
296 297
297 sstr_t sstrtrim(sstr_t string) { 298 sstr_t sstrtrim(sstr_t string) {
298 sstr_t newstr = string; 299 sstr_t newstr = string;
299 if (string.length == 0) { 300
300 return newstr; 301 while (newstr.length > 0 && isspace(*newstr.ptr)) {
301 } 302 newstr.ptr++;
302 303 newstr.length--;
303 size_t i; 304 }
304 for(i=0;i<string.length;i++) { 305 while (newstr.length > 0 && isspace(newstr.ptr[newstr.length-1])) {
305 char c = string.ptr[i]; 306 newstr.length--;
306 if(c > 32) { 307 }
307 break;
308 }
309 }
310 newstr.ptr = &string.ptr[i];
311 newstr.length = string.length - i;
312
313 if(newstr.length == 0) {
314 return newstr;
315 }
316
317 i = newstr.length - 1;
318 for(;;) {
319 char c = newstr.ptr[i];
320 if(c > 32) {
321 break;
322 }
323 if(i > 0) {
324 i--;
325 } else {
326 break;
327 }
328 }
329 newstr.length = i + 1;
330 308
331 return newstr; 309 return newstr;
332 } 310 }
333 311
334 int sstrprefix(sstr_t string, sstr_t prefix) { 312 int sstrprefix(sstr_t string, sstr_t prefix) {

mercurial