test #include #include #include #include #include "str.h" #include "util.h" #define INIT_SZ 20 void _str_ensure(str *s, int newsz) { int tmp; newsz += 1; if (newsz < s->capacity) return; tmp = s->capacity * 2; if (tmp > newsz) newsz = tmp; s->ptr = xrealloc(s->ptr, newsz); s->capacity = newsz; } void str_init(str *s) { assert(s); s->len = 0; s->capacity = INIT_SZ; s->ptr = xmalloc(s->capacity); s->ptr[0] = '\0'; printf("str_init: len=%d, capacity=%d\n", s->len, s->capacity); } void str_deinit(str *s) { if (s->ptr) free(s->ptr); s->ptr = NULL; s->len = 0; s->capacity = 0; printf("str_deinit: memory freed\n"); } void str_init_data(str *s, const char *initial) { assert(s && initial); int len = strlen(initial); s->len = len; s->capacity = len + 1; if (s->capacity < INIT_SZ) s->capacity = INIT_SZ; s->ptr = xmalloc(s->capacity); strcpy(s->ptr, initial); printf("str_init_data: '%s' (%d)\n", s->ptr, s->len); } char * str_data(str *s) { assert(s); return s->ptr; } void str_set(str *s, char *cstr) { assert(s && cstr); int len = strlen(cstr); _str_ensure(s, len); strcpy(s->ptr, cstr); s->len = len; printf("str_set: '%s' (%d)\n", s->ptr, s->len); } str str_copy(str *s) { assert(s); str new_str; str_init(&new_str); _str_ensure(&new_str, s->len); strcpy(new_str.ptr, s->ptr); new_str.len = s->len; printf("str_copy: '%s' -> '%s'\n", s->ptr, new_str.ptr); return new_str; } int str_count(str *s, char ch) { assert(s); int count = 0; for (int i = 0; i < s->len; i++) { if (s->ptr[i] == ch) count++; } printf("str_count: '%c' = %d in '%s'\n", ch, count, s->ptr); return count; } void str_append(str *s, char *cstr) { assert(s && cstr); str_append_n(s, cstr, strlen(cstr)); } void str_append_n(str *s, char *cstr, int len) { assert(s && cstr); _str_ensure(s, s->len + len); memcpy(s->ptr + s->len, cstr, len); s->len += len; s->ptr[s->len] = '\0'; printf("str_append_n: '%s' (%d)\n", s->ptr, s->len); } void str_shrink(str *s, int len) { assert(s && len >= 0); if (len > s->len) len = s->len; s->len = len; s->ptr[s->len] = '\0'; printf("str_shrink: '%s' (%d)\n", s->ptr, s->len); } int str_find(str *s, char *substr) { assert(s && substr); char *p = strstr(s->ptr, substr); int result = (p == NULL) ? -1 : p - s->ptr; printf("str_find: '%s' in '%s' = %d\n", substr, s->ptr, result); return result; } str str_sub(str *s, int start_idx, int length) { assert(s && start_idx >= 0 && length >= 0); str s2; str_init(&s2); if (start_idx >= s->len) return s2; if (start_idx + length > s->len) length = s->len - start_idx; _str_ensure(&s2, length); memcpy(s2.ptr, s->ptr + start_idx, length); s2.ptr[length] = '\0'; s2.len = length; printf("str_sub: '%s'[%d:%d] = '%s'\n", s->ptr, start_idx, start_idx+length, s2.ptr); return s2; } int str_replace(str *s, char *substr, char *replacement) { assert(s && substr && replacement); int count = 0; str result; str_init(&result); int substr_len = strlen(substr); int replacement_len = strlen(replacement); char *current = s->ptr; char *found; while ((found = strstr(current, substr)) != NULL) { int prefix_len = found - current; if (prefix_len > 0) str_append_n(&result, current, prefix_len); str_append_n(&result, replacement, replacement_len); current = found + substr_len; count++; } if (*current != '\0') str_append(&result, current); free(s->ptr); s->ptr = result.ptr; s->len = result.len; s->capacity = result.capacity; printf("str_replace: '%s' (%d)\n", s->ptr, count); return count; }