#include #include #include #include "str.h" #include "util.h" #define INIT_SZ 20 //Внутренняя функция, //позволяет убедиться что строка может вместить как минимум newsz символов 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); <<<<<<< HEAD // ======= s->len = 0; s->capacity = INIT_SZ; s->ptr = malloc(INIT_SZ); s->ptr[0] = '\0'; >>>>>>> f115cdc (all tasks done) } void str_deinit(str *s) { if (s->ptr) free(s->ptr); s->ptr = NULL; } void str_init_data(str *s, const char *initial) { assert(s && initial); <<<<<<< HEAD // ======= int len = strlen(initial); s->len = len; s->capacity = len + 1; s->ptr = malloc(s->capacity); strcpy(s->ptr, initial); >>>>>>> f115cdc (all tasks done) } char * str_data(str *s) { assert(s); <<<<<<< HEAD // return NULL; ======= return s->ptr; >>>>>>> f115cdc (all tasks done) } void str_set(str *s, char *cstr) { assert(s && cstr); <<<<<<< HEAD // ======= int len = strlen(cstr); _str_ensure(s, len); strcpy(s->ptr, cstr); s->len = len; >>>>>>> f115cdc (all tasks done) } str str_copy(str *s) { assert(s); <<<<<<< HEAD // ======= str s2; str_init(&s2); _str_ensure(&s2, s->len); strcpy(s2.ptr, s->ptr); s2.len = s->len; return s2; >>>>>>> f115cdc (all tasks done) } int str_count(str *s, char ch) { assert(s); <<<<<<< HEAD // return 0; ======= int count = 0; for (int i = 0; i < s->len; i++) { if (s->ptr[i] == ch) count++; } return count; >>>>>>> f115cdc (all tasks done) } void str_append(str *s, char *cstr) { assert(s && cstr); <<<<<<< HEAD // ======= int len = strlen(cstr); _str_ensure(s, s->len + len); strcpy(s->ptr + s->len, cstr); s->len += len; >>>>>>> f115cdc (all tasks done) } void str_append_n(str *s, char *cstr, int len) { assert(s && cstr); <<<<<<< HEAD // ======= _str_ensure(s, s->len + len); memcpy(s->ptr + s->len, cstr, len); s->len += len; s->ptr[s->len] = '\0'; >>>>>>> f115cdc (all tasks done) } void str_shrink(str *s, int len) { assert(s && len >= 0); <<<<<<< HEAD // ======= if (len < s->len) { s->len= len; s->ptr[len] = '\0'; } >>>>>>> f115cdc (all tasks done) } int str_find(str *s, char *substr) { assert(s && substr); char *p = strstr(s->ptr, substr); if (p == NULL) return -1; return p - s->ptr; } str str_sub(str *s, int start_idx, int length) { assert(s && start_idx >= 0 && length >= 0); str s2; str_init(&s2); <<<<<<< HEAD // ======= if (start_idx >= s-> len) { return s2; } int actual_len = length; if (start_idx + length > s->len) { actual_len = s->len - start_idx; } _str_ensure(&s2, actual_len); memcpy(s2.ptr, s->ptr + start_idx, actual_len); s2.ptr[actual_len] = '\0'; s2.len = actual_len; >>>>>>> f115cdc (all tasks done) return s2; } // Hint: you can create temporary string object! int str_replace(str *s, char *substr, char *replacement) { assert(s && substr && replacement); <<<<<<< HEAD // return 0; ======= int count = 0; int substr_len = strlen(substr); int repl_len = strlen(replacement); str tmp; str_init(&tmp); int i = 0; while (i < s->len) { int pos = str_find(s, substr); if (pos == -1) { str_append_n(&tmp, s->ptr + i, s->len - i); break; } if (pos > 0) { str_append_n(&tmp, s->ptr, pos); } str_append(&tmp, replacement); count++; int skip = pos + substr_len; memmove(s->ptr, s->ptr + skip, s->len - skip + 1); s->len -= skip; i = 0; } str_set(s, tmp.ptr); str_deinit(&tmp); return count; >>>>>>> f115cdc (all tasks done) }