| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | #include | 
| 2 |  |  |  |  |  |  | #include | 
| 3 |  |  |  |  |  |  | #include | 
| 4 |  |  |  |  |  |  | #include "b_string.h" | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 69302 |  |  |  |  |  | b_string *b_string_new_len(char *str, size_t len) { | 
| 7 |  |  |  |  |  |  | b_string *ret; | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 69302 | 50 |  |  |  |  | if ((ret = malloc(sizeof(*ret))) == NULL) { | 
| 10 | 0 |  |  |  |  |  | goto error_malloc_ret; | 
| 11 |  |  |  |  |  |  | } | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 69302 | 50 |  |  |  |  | if ((ret->str = malloc(len + 1)) == NULL) { | 
| 14 | 0 |  |  |  |  |  | goto error_malloc_str; | 
| 15 |  |  |  |  |  |  | } | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 69302 |  |  |  |  |  | strncpy(ret->str, str, len); | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 69302 |  |  |  |  |  | ret->len      = len; | 
| 20 | 69302 |  |  |  |  |  | ret->str[len] = '\0'; | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 69302 |  |  |  |  |  | return ret; | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | error_malloc_str: | 
| 25 | 0 |  |  |  |  |  | free(ret); | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | error_malloc_ret: | 
| 28 | 0 |  |  |  |  |  | return NULL; | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  |  | 
| 31 | 49685 |  |  |  |  |  | b_string *b_string_new(char *str) { | 
| 32 | 49685 |  |  |  |  |  | return b_string_new_len(str, strlen(str)); | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 6274 |  |  |  |  |  | b_string *b_string_dup(b_string *string) { | 
| 36 | 6274 |  |  |  |  |  | return b_string_new_len(string->str, string->len); | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 0 |  |  |  |  |  | b_string *b_string_append(b_string *string, b_string *add) { | 
| 40 |  |  |  |  |  |  | size_t newlen; | 
| 41 |  |  |  |  |  |  | char *tmp; | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 0 | 0 |  |  |  |  | if (add->len == 0) { | 
| 44 | 0 |  |  |  |  |  | return string; | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 0 |  |  |  |  |  | newlen = string->len + add->len; | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 0 | 0 |  |  |  |  | if ((tmp = realloc(string->str, newlen + 1)) == NULL) { | 
| 50 | 0 |  |  |  |  |  | goto error_realloc; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 0 |  |  |  |  |  | strncpy(tmp + string->len, add->str, add->len); | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 0 |  |  |  |  |  | string->str         = tmp; | 
| 56 | 0 |  |  |  |  |  | string->str[newlen] = '\0'; | 
| 57 | 0 |  |  |  |  |  | string->len         = newlen; | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 0 |  |  |  |  |  | return string; | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | error_realloc: | 
| 62 | 0 |  |  |  |  |  | return NULL; | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 727 |  |  |  |  |  | b_string *b_string_append_str(b_string *string, char *add_str) { | 
| 66 |  |  |  |  |  |  | size_t add_len, newlen; | 
| 67 |  |  |  |  |  |  | char *tmp; | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 727 | 50 |  |  |  |  | if ((add_len = strlen(add_str)) == 0) { | 
| 70 | 0 |  |  |  |  |  | return string; | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 727 |  |  |  |  |  | newlen = string->len + add_len; | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 727 | 50 |  |  |  |  | if ((tmp = realloc(string->str, newlen + 1)) == NULL) { | 
| 76 | 0 |  |  |  |  |  | goto error_realloc; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 727 |  |  |  |  |  | strncpy(tmp + string->len, add_str, add_len); | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 727 |  |  |  |  |  | string->str         = tmp; | 
| 82 | 727 |  |  |  |  |  | string->str[newlen] = '\0'; | 
| 83 | 727 |  |  |  |  |  | string->len         = newlen; | 
| 84 |  |  |  |  |  |  |  | 
| 85 | 727 |  |  |  |  |  | return string; | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | error_realloc: | 
| 88 | 0 |  |  |  |  |  | return NULL; | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 90775 |  |  |  |  |  | size_t b_string_len(b_string *string) { | 
| 92 | 90775 |  |  |  |  |  | return string->len; | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 77325 |  |  |  |  |  | void b_string_free(b_string *string) { | 
| 96 | 77325 | 100 |  |  |  |  | if (string == NULL) return; | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 63708 |  |  |  |  |  | free(string->str); | 
| 99 | 63708 |  |  |  |  |  | string->str = NULL; | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 63708 |  |  |  |  |  | free(string); | 
| 102 |  |  |  |  |  |  | } |