| 227 | // ============================================================================= |
| 228 | |
| 229 | zvec_string_t *zvec_string_create(const char *str) { |
| 230 | if (!str) { |
| 231 | SET_LAST_ERROR(ZVEC_ERROR_INVALID_ARGUMENT, |
| 232 | "String pointer cannot be null"); |
| 233 | return nullptr; |
| 234 | } |
| 235 | |
| 236 | size_t len = strlen(str); |
| 237 | zvec_string_t *zstr = static_cast<zvec_string_t *>(malloc(sizeof(zvec_string_t))); |
| 238 | if (!zstr) { |
| 239 | SET_LAST_ERROR(ZVEC_ERROR_RESOURCE_EXHAUSTED, |
| 240 | "Failed to allocate memory for zvec_string_t"); |
| 241 | return nullptr; |
| 242 | } |
| 243 | |
| 244 | char *data_buffer = static_cast<char *>(malloc(len + 1)); |
| 245 | if (!data_buffer) { |
| 246 | free(zstr); |
| 247 | SET_LAST_ERROR(ZVEC_ERROR_RESOURCE_EXHAUSTED, |
| 248 | "Failed to allocate memory for string data"); |
| 249 | return nullptr; |
| 250 | } |
| 251 | |
| 252 | memcpy(data_buffer, str, len + 1); |
| 253 | zstr->data = data_buffer; |
| 254 | zstr->length = len; |
| 255 | zstr->capacity = len + 1; |
| 256 | return zstr; |
| 257 | } |
| 258 | |
| 259 | zvec_string_t *zvec_string_create_from_view(const zvec_string_view_t *view) { |
| 260 | if (!view || !view->data) { |
no outgoing calls