diff options
| author | 3gg <3gg@shellblade.net> | 2024-06-15 11:42:29 -0700 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2024-06-15 11:42:29 -0700 |
| commit | 993424547df0d253d546dbe7adee9b2448294b08 (patch) | |
| tree | b56e4b7e54228d073d80bf9580846a3295295b92 /cstring/src | |
| parent | f5127be2865c90b26de896c1adbc5a19ea3a0bd6 (diff) | |
Add dynamically-sized strings.
Diffstat (limited to 'cstring/src')
| -rw-r--r-- | cstring/src/cstring.c | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/cstring/src/cstring.c b/cstring/src/cstring.c new file mode 100644 index 0000000..832cb85 --- /dev/null +++ b/cstring/src/cstring.c | |||
| @@ -0,0 +1,103 @@ | |||
| 1 | #include <cstring.h> | ||
| 2 | |||
| 3 | #include <assert.h> | ||
| 4 | #include <stdio.h> | ||
| 5 | #include <stdlib.h> | ||
| 6 | |||
| 7 | string string_new(const char* cstr) { | ||
| 8 | const size_t length = strlen(cstr); | ||
| 9 | const size_t size = length + 1; | ||
| 10 | |||
| 11 | char* data = calloc(size, sizeof(char)); | ||
| 12 | ASSERT(data); | ||
| 13 | if (length > 0) { | ||
| 14 | memcpy(data, cstr, length); | ||
| 15 | } | ||
| 16 | |||
| 17 | return (string){ | ||
| 18 | .data = data, | ||
| 19 | .length = length, | ||
| 20 | }; | ||
| 21 | } | ||
| 22 | |||
| 23 | void string_del(string* str) { | ||
| 24 | if (str->data) { | ||
| 25 | free((void*)str->data); | ||
| 26 | str->data = 0; | ||
| 27 | str->length = 0; | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | string string_concat(string left, string right) { | ||
| 32 | const size_t length = left.length + right.length; | ||
| 33 | const size_t size = length + 1; | ||
| 34 | |||
| 35 | char* data = calloc(size, sizeof(char)); | ||
| 36 | ASSERT(data); | ||
| 37 | if (length > 0) { | ||
| 38 | memcpy(data, left.data, left.length); | ||
| 39 | memcpy(data + left.length, right.data, right.length); | ||
| 40 | } | ||
| 41 | |||
| 42 | return (string){ | ||
| 43 | .data = data, | ||
| 44 | .length = length, | ||
| 45 | }; | ||
| 46 | } | ||
| 47 | |||
| 48 | string string_from_size(size_t size) { | ||
| 49 | const size_t length = snprintf(NULL, 0, "%zu", size) + 1; | ||
| 50 | char* data = calloc(length, sizeof(char)); | ||
| 51 | ASSERT(data); | ||
| 52 | snprintf(data, length, "%zu", size); | ||
| 53 | return (string){ | ||
| 54 | .data = data, | ||
| 55 | .length = length, | ||
| 56 | }; | ||
| 57 | } | ||
| 58 | |||
| 59 | string string_format_size(size_t size) { | ||
| 60 | const size_t multiples[] = {1073741824, 1048576, 1024, 1}; | ||
| 61 | const char* units[] = {"G", "M", "K", "B"}; | ||
| 62 | |||
| 63 | size_t integer = 0; | ||
| 64 | size_t fractional = 0; | ||
| 65 | |||
| 66 | int i; | ||
| 67 | for (i = 0; i < 4; ++i) { | ||
| 68 | const size_t m = multiples[i]; | ||
| 69 | if (size >= m) { | ||
| 70 | integer = size / m; | ||
| 71 | fractional = size % m; | ||
| 72 | break; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | double s; | ||
| 77 | const char* unit; | ||
| 78 | const char* format; | ||
| 79 | if (i == 4) { // 0 | ||
| 80 | s = (double)size; | ||
| 81 | unit = ""; | ||
| 82 | format = "%f%s"; | ||
| 83 | } else if (i == 3) { // Bytes | ||
| 84 | s = (double)integer + (double)fractional / (double)multiples[i]; | ||
| 85 | unit = units[i]; | ||
| 86 | format = "%.0f%s"; | ||
| 87 | } else { // KB, MB, GB | ||
| 88 | assert(i >= 0); | ||
| 89 | assert(i < 3); | ||
| 90 | s = (double)integer + (double)fractional / (double)multiples[i]; | ||
| 91 | unit = units[i]; | ||
| 92 | format = "%.2f%s"; | ||
| 93 | } | ||
| 94 | |||
| 95 | const size_t length = snprintf(NULL, 0, format, s, unit) + 1; | ||
| 96 | char* data = calloc(length, sizeof(char)); | ||
| 97 | ASSERT(data); | ||
| 98 | snprintf(data, length, format, s, unit); | ||
| 99 | return (string){ | ||
| 100 | .data = data, | ||
| 101 | .length = length, | ||
| 102 | }; | ||
| 103 | } | ||
