cstring

A simple and lightweight string library for C
git clone git://git.christosmarg.xyz/cstring.git
Log | Files | Refs | README | LICENSE

test.c (2338B)


      1 #include "cstring.h"
      2 
      3 // Compilation: gcc test.c -lcstring
      4 
      5 int
      6 main(int argc, char **argv)
      7 {
      8     cstring s = cstring_create("Hello world");
      9     printf("cstring_create: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
     10 
     11     cstring_sort_chars(&s, CSTRING_SORT_ASCENDING, NULL);
     12     printf("cstring_sort_chars: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
     13 
     14     // BUG
     15     cstring_assign(&s, "New string");
     16     printf("cstring_assign: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
     17 
     18     cstring_append(&s, "Appended text");
     19     printf("cstring_append: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
     20 
     21     cstring_prepend(&s, "OK");
     22     printf("cstring_prepend: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
     23 
     24     cstring_push_back(&s, 'c');
     25     printf("cstring_push_back: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
     26 
     27     cstring_insert(&s, "Inserted text", 4);
     28     printf("cstring_insert: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
     29     
     30     cstring_pop_back(&s);
     31     printf("cstring_pop_back: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
     32 
     33     cstring_clear(&s);
     34     printf("cstring_clear: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
     35 
     36     cstring_assign(&s, "CSTRING");
     37     printf("cstring_assign: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
     38     printf("cstring_front: %c\n", cstring_front(&s));
     39     printf("cstring_back: %c\n", cstring_back(&s));
     40 
     41     cstring_replace_char(&s, 3, 'x');
     42     printf("cstring_replace_char: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
     43 
     44     cstring_replace_str(&s, "hell", 0, strlen("hell"));
     45     printf("cstring_replace_str: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
     46 
     47     // BUGS
     48     cstring_erase(&s, 1, 4);
     49     printf("cstring_erase: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
     50 
     51     cstring_assign(&s, "hello aaaa hello abbb helo hello");
     52     cstring_erase_all_matching(&s, "hello");
     53     printf("cstring_erase_all_matching: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
     54 
     55     cstring_trim(&s, " ");
     56     printf("cstring_trim: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
     57 
     58     cstring_delete(&s);
     59     if (cstring_empty(&s))
     60         puts("cstring_delete: Deleted string.");
     61 
     62     return 0;
     63 }