src/string.c

branch
constsstr
changeset 275
96f643d30ff1
parent 272
2def28b65328
child 276
f1b2146d4805
equal deleted inserted replaced
274:0923c036b913 275:96f643d30ff1
47 sstr_t string; 47 sstr_t string;
48 string.ptr = cstring; 48 string.ptr = cstring;
49 string.length = length; 49 string.length = length;
50 return string; 50 return string;
51 } 51 }
52
53 scstr_t scstr(const char *cstring) {
54 scstr_t string;
55 string.ptr = cstring;
56 string.length = strlen(cstring);
57 return string;
58 }
59
60 scstr_t scstrn(const char *cstring, size_t length) {
61 scstr_t string;
62 string.ptr = cstring;
63 string.length = length;
64 return string;
65 }
66
52 67
53 size_t sstrnlen(size_t n, sstr_t s, ...) { 68 size_t sstrnlen(size_t n, sstr_t s, ...) {
54 va_list ap; 69 va_list ap;
55 size_t size = s.length; 70 size_t size = s.length;
56 va_start(ap, s); 71 va_start(ap, s);
389 } else { 404 } else {
390 return -1; 405 return -1;
391 } 406 }
392 } 407 }
393 408
394 sstr_t sstrdup(sstr_t s) { 409 sstr_t scstrdup(scstr_t s) {
395 return sstrdup_a(ucx_default_allocator(), s); 410 return sstrdup_a(ucx_default_allocator(), s);
396 } 411 }
397 412
398 sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t s) { 413 sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t s) {
399 sstr_t newstring; 414 sstr_t newstring;
400 newstring.ptr = (char*)almalloc(allocator, s.length + 1); 415 newstring.ptr = (char*)almalloc(allocator, s.length + 1);
401 if (newstring.ptr) { 416 if (newstring.ptr) {
402 newstring.length = s.length; 417 newstring.length = s.length;
403 newstring.ptr[newstring.length] = 0; 418 newstring.ptr[newstring.length] = 0;
422 } 437 }
423 438
424 return newstr; 439 return newstr;
425 } 440 }
426 441
427 int sstrprefix(sstr_t string, sstr_t prefix) { 442 int ucx_strprefix(scstr_t string, scstr_t prefix) {
428 if (string.length == 0) { 443 if (string.length == 0) {
429 return prefix.length == 0; 444 return prefix.length == 0;
430 } 445 }
431 if (prefix.length == 0) { 446 if (prefix.length == 0) {
432 return 1; 447 return 1;
437 } else { 452 } else {
438 return memcmp(string.ptr, prefix.ptr, prefix.length) == 0; 453 return memcmp(string.ptr, prefix.ptr, prefix.length) == 0;
439 } 454 }
440 } 455 }
441 456
442 int sstrsuffix(sstr_t string, sstr_t suffix) { 457 int ucx_strsuffix(scstr_t string, scstr_t suffix) {
443 if (string.length == 0) { 458 if (string.length == 0) {
444 return suffix.length == 0; 459 return suffix.length == 0;
445 } 460 }
446 if (suffix.length == 0) { 461 if (suffix.length == 0) {
447 return 1; 462 return 1;
453 return memcmp(string.ptr+string.length-suffix.length, 468 return memcmp(string.ptr+string.length-suffix.length,
454 suffix.ptr, suffix.length) == 0; 469 suffix.ptr, suffix.length) == 0;
455 } 470 }
456 } 471 }
457 472
458 sstr_t sstrlower(sstr_t string) { 473 sstr_t ucx_strlower(scstr_t string) {
459 sstr_t ret = sstrdup(string); 474 sstr_t ret = sstrdup(string);
460 for (size_t i = 0; i < ret.length ; i++) { 475 for (size_t i = 0; i < ret.length ; i++) {
461 ret.ptr[i] = tolower(ret.ptr[i]); 476 ret.ptr[i] = tolower(ret.ptr[i]);
462 } 477 }
463 return ret; 478 return ret;
464 } 479 }
465 480
466 sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string) { 481 sstr_t ucx_strlower_a(UcxAllocator *allocator, scstr_t string) {
467 sstr_t ret = sstrdup_a(allocator, string); 482 sstr_t ret = sstrdup_a(allocator, string);
468 for (size_t i = 0; i < ret.length ; i++) { 483 for (size_t i = 0; i < ret.length ; i++) {
469 ret.ptr[i] = tolower(ret.ptr[i]); 484 ret.ptr[i] = tolower(ret.ptr[i]);
470 } 485 }
471 return ret; 486 return ret;
472 } 487 }
473 488
474 sstr_t sstrupper(sstr_t string) { 489 sstr_t ucx_strupper(scstr_t string) {
475 sstr_t ret = sstrdup(string); 490 sstr_t ret = sstrdup(string);
476 for (size_t i = 0; i < ret.length ; i++) { 491 for (size_t i = 0; i < ret.length ; i++) {
477 ret.ptr[i] = toupper(ret.ptr[i]); 492 ret.ptr[i] = toupper(ret.ptr[i]);
478 } 493 }
479 return ret; 494 return ret;
480 } 495 }
481 496
482 sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string) { 497 sstr_t ucx_strupper_a(UcxAllocator *allocator, scstr_t string) {
483 sstr_t ret = sstrdup_a(allocator, string); 498 sstr_t ret = sstrdup_a(allocator, string);
484 for (size_t i = 0; i < ret.length ; i++) { 499 for (size_t i = 0; i < ret.length ; i++) {
485 ret.ptr[i] = toupper(ret.ptr[i]); 500 ret.ptr[i] = toupper(ret.ptr[i]);
486 } 501 }
487 return ret; 502 return ret;
488 } 503 }
504
505 // private string conversion functions
506 scstr_t ucx_sc2sc(scstr_t c) {
507 return c;
508 }
509 scstr_t ucx_ss2sc(sstr_t str) {
510 scstr_t cs;
511 cs.ptr = str.ptr;
512 cs.length = str.length;
513 return cs;
514 }
515 scstr_t ucx_ss2c_s(scstr_t c) {
516 return c;
517 }

mercurial