Mercurial > libedl
comparison src/str.c @ 12:0cc2555db371
*: make our string functions not stupid
strncat() made everything slow!
new addition internally: an EDL_header structure to make things
not stupid. if we were using C++ it wouldn't really be necessary,
but alas :)
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Wed, 27 Mar 2024 13:38:30 -0400 |
parents | 0c98b46eaf73 |
children | 2d7c810a1ac2 |
comparison
equal
deleted
inserted
replaced
11:a0bd92c4c0e8 | 12:0cc2555db371 |
---|---|
23 | 23 |
24 str->data = realloc(str->data, new_capacity * sizeof(char)); | 24 str->data = realloc(str->data, new_capacity * sizeof(char)); |
25 if (!str->data) | 25 if (!str->data) |
26 return 0; | 26 return 0; |
27 | 27 |
28 //if (new_capacity > str->capacity) | 28 if (new_capacity > str->capacity) |
29 // memset(&str->data[str->capacity], 0, (new_capacity - str->capacity) * sizeof(char)); | 29 memset(&str->data[str->capacity], 0, (new_capacity - str->capacity) * sizeof(char)); |
30 | 30 |
31 str->capacity = new_capacity; | 31 str->capacity = new_capacity; |
32 | 32 |
33 return 1; | 33 return 1; |
34 } | 34 } |
36 int EDL_internal_string_append(EDL_internal_string* str, const char* data, const size_t length) { | 36 int EDL_internal_string_append(EDL_internal_string* str, const char* data, const size_t length) { |
37 if (str->capacity == 0) | 37 if (str->capacity == 0) |
38 if (!EDL_internal_string_allocate(str, 1)) | 38 if (!EDL_internal_string_allocate(str, 1)) |
39 return 0; | 39 return 0; |
40 | 40 |
41 /* increase capacity if needed */ | |
41 if (str->size + length + 1 >= str->capacity) { | 42 if (str->size + length + 1 >= str->capacity) { |
42 size_t capacity = 1; | 43 size_t capacity = 1; |
43 while (capacity < (str->size + length + 1)) | 44 while (capacity < (str->size + length + 1)) |
44 capacity *= 2; | 45 capacity *= 2; |
45 | 46 |
46 if (!EDL_internal_string_allocate(str, capacity)) | 47 if (!EDL_internal_string_allocate(str, capacity)) |
47 return 0; | 48 return 0; |
48 } | 49 } |
49 | 50 |
50 strncat(str->data, data, length); | 51 memcpy(&str->data[str->size], data, length); |
51 str->size += length; | 52 str->size += length; |
52 | 53 |
53 return length; | 54 return length; |
54 } | 55 } |
55 | 56 |