|
| 1 | +#include <sys/types.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <stdio.h> |
| 4 | +#include <string.h> |
| 5 | +#include <ctype.h> |
| 6 | +#define KEEPALIVE __attribute__((used)) |
| 7 | + |
| 8 | +typedef struct { |
| 9 | + char *buffer; |
| 10 | + size_t size; |
| 11 | + size_t capacity; |
| 12 | +} ss_string; |
| 13 | + |
| 14 | +size_t KEEPALIVE ss_string_get_capacity_with_size(size_t size) { |
| 15 | + if (size == 0) { |
| 16 | + return 16; |
| 17 | + } else if (size < 16) { |
| 18 | + return 32; |
| 19 | + } else if (size < 32) { |
| 20 | + return 48; |
| 21 | + } else { |
| 22 | + return size * 3 / 2; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +ss_string* KEEPALIVE ss_string_create_with_capacity(size_t capacity) { |
| 27 | + ss_string* str = (ss_string*) malloc(sizeof(ss_string)); |
| 28 | + if (!str) { |
| 29 | + return NULL; |
| 30 | + } |
| 31 | + str->buffer = (char *) calloc(capacity, 1); |
| 32 | + str->capacity = capacity; |
| 33 | + str->size = 0; |
| 34 | + memset(str->buffer, 0, capacity); |
| 35 | + return str; |
| 36 | +} |
| 37 | + |
| 38 | +ss_string* KEEPALIVE ss_string_create(const char *literal) { |
| 39 | + size_t size = strlen(literal); |
| 40 | + size_t capacity = ss_string_get_capacity_with_size(size); |
| 41 | + ss_string* str = ss_string_create_with_capacity(capacity); |
| 42 | + if (!str) { |
| 43 | + return NULL; |
| 44 | + } |
| 45 | + memcpy(str->buffer, literal, size); |
| 46 | + str->size = size; |
| 47 | + return str; |
| 48 | +} |
| 49 | + |
| 50 | +void KEEPALIVE ss_string_delete(ss_string* str) { |
| 51 | + free(str->buffer); |
| 52 | + free(str); |
| 53 | +} |
| 54 | + |
| 55 | +size_t KEEPALIVE ss_string_get_size(ss_string* str) { |
| 56 | + return str->size; |
| 57 | +} |
| 58 | + |
| 59 | +int KEEPALIVE ss_string_grow_with_capacity(ss_string* str, size_t new_capacity) { |
| 60 | + char *new_buffer = (char *) calloc(new_capacity, 1); |
| 61 | + if (!new_buffer) { |
| 62 | + return -1; |
| 63 | + } |
| 64 | + memset(new_buffer, 0, new_capacity); |
| 65 | + memcpy(new_buffer, str->buffer, str->size); |
| 66 | + free(str->buffer); |
| 67 | + str->buffer = new_buffer; |
| 68 | + str->capacity = new_capacity; |
| 69 | + return 0; |
| 70 | +} |
| 71 | + |
| 72 | +int KEEPALIVE ss_string_grow(ss_string* str) { |
| 73 | + if (str->capacity < 32) { |
| 74 | + str->capacity += 16; |
| 75 | + } else { |
| 76 | + str->capacity = str->capacity * 3 / 2; |
| 77 | + } |
| 78 | + return ss_string_grow_with_capacity(str, str->capacity); |
| 79 | +} |
| 80 | + |
| 81 | +int KEEPALIVE ss_string_append_with_literal(ss_string* str, const char *literal) { |
| 82 | + size_t literal_size = strlen(literal); |
| 83 | + size_t needed_size = str->size + literal_size; |
| 84 | + size_t needed_capacity = ss_string_get_capacity_with_size(needed_size); |
| 85 | + if (str->capacity < needed_capacity) { |
| 86 | + if (ss_string_grow_with_capacity(str, needed_capacity) == -1) { |
| 87 | + return -1; |
| 88 | + } |
| 89 | + } |
| 90 | + strncat(str->buffer, literal, literal_size); |
| 91 | + str->size = needed_size; |
| 92 | + return 0; |
| 93 | +} |
| 94 | + |
| 95 | +int KEEPALIVE ss_string_append(ss_string* dest, ss_string* src) { |
| 96 | + return ss_string_append_with_literal(dest, src->buffer); |
| 97 | +} |
| 98 | + |
| 99 | +int KEEPALIVE ss_string_prepend_with_literal(ss_string* str, const char *literal) { |
| 100 | + size_t literal_size = strlen(literal); |
| 101 | + size_t needed_size = str->size + literal_size; |
| 102 | + size_t needed_capacity = ss_string_get_capacity_with_size(needed_size); |
| 103 | + if (str->capacity < needed_capacity) { |
| 104 | + if (ss_string_grow_with_capacity(str, needed_capacity) == -1) { |
| 105 | + return -1; |
| 106 | + } |
| 107 | + } |
| 108 | + memmove(str->buffer + literal_size, str->buffer, literal_size + 1); |
| 109 | + memcpy(str->buffer, literal, literal_size); |
| 110 | + str->size = needed_size; |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
| 114 | +int KEEPALIVE ss_string_prepend(ss_string* dest, ss_string* src) { |
| 115 | + return ss_string_prepend_with_literal(dest, src->buffer); |
| 116 | +} |
| 117 | + |
| 118 | +ss_string* KEEPALIVE ss_string_slice(ss_string* str, ssize_t from, ssize_t to) { |
| 119 | + if (from < 0) { |
| 120 | + from = str->size + from; |
| 121 | + } |
| 122 | + if (to < 0) { |
| 123 | + to = str->size + to; |
| 124 | + } |
| 125 | + if (to < from) { |
| 126 | + return NULL; |
| 127 | + } |
| 128 | + size_t slice_size = to - from; |
| 129 | + size_t new_capacity = ss_string_get_capacity_with_size(slice_size); |
| 130 | + ss_string* new_str = ss_string_create_with_capacity(new_capacity); |
| 131 | + memcpy(new_str->buffer, str->buffer + from, slice_size); |
| 132 | + new_str->size = slice_size; |
| 133 | + return new_str; |
| 134 | +} |
| 135 | + |
| 136 | +int KEEPALIVE ss_string_equals(ss_string* str1, ss_string* str2) { |
| 137 | + size_t max_size = str1->size > str2->size ? str1->size : str2->size; |
| 138 | + return strncmp(str1->buffer, str2->buffer, max_size) == 0; |
| 139 | +} |
| 140 | + |
| 141 | +ssize_t KEEPALIVE ss_string_index_of_with_literal(ss_string* str, const char *literal) { |
| 142 | + char *sub = strstr(str->buffer, literal); |
| 143 | + if (!sub) { |
| 144 | + return -1; |
| 145 | + } |
| 146 | + return sub - str->buffer; |
| 147 | +} |
| 148 | + |
| 149 | +ssize_t KEEPALIVE ss_string_index_of(ss_string* str, ss_string* substr) { |
| 150 | + return ss_string_index_of_with_literal(str, substr->buffer); |
| 151 | +} |
| 152 | + |
| 153 | +int KEEPALIVE ss_string_trim_left(ss_string* str) { |
| 154 | + size_t i = 0; |
| 155 | + while (isspace(str->buffer[i])) { |
| 156 | + i += 1; |
| 157 | + } |
| 158 | + char *new_buffer = (char *) calloc(str->capacity, 1); |
| 159 | + if (!new_buffer) { |
| 160 | + return -1; |
| 161 | + } |
| 162 | + memset(new_buffer, 0, str->capacity); |
| 163 | + memcpy(new_buffer, str->buffer + i, str->size - i); |
| 164 | + free(str->buffer); |
| 165 | + str->buffer = new_buffer; |
| 166 | + str->size -= i; |
| 167 | + return 0; |
| 168 | +} |
| 169 | + |
| 170 | +void KEEPALIVE ss_string_trim_right(ss_string* str) { |
| 171 | + ssize_t i = str->size - 1; |
| 172 | + while (i >= 0 && isspace(str->buffer[i])) { |
| 173 | + str->buffer[i] = 0; |
| 174 | + i -= 1; |
| 175 | + } |
| 176 | + str->size = i + 1; |
| 177 | +} |
| 178 | + |
| 179 | +int KEEPALIVE ss_string_trim(ss_string* str) { |
| 180 | + ss_string_trim_right(str); |
| 181 | + return ss_string_trim_left(str); |
| 182 | +} |
| 183 | + |
| 184 | +void KEEPALIVE ss_console_log(ss_string *str) { |
| 185 | + printf("%s\n", str->buffer); |
| 186 | +} |
0 commit comments