diff options
-rw-r--r-- | cstring/CMakeLists.txt | 7 | ||||
-rw-r--r-- | cstring/include/cstring.h | 37 | ||||
-rw-r--r-- | cstring/src/cstring.c | 103 |
3 files changed, 143 insertions, 4 deletions
diff --git a/cstring/CMakeLists.txt b/cstring/CMakeLists.txt index df2fa45..bc385c3 100644 --- a/cstring/CMakeLists.txt +++ b/cstring/CMakeLists.txt | |||
@@ -2,11 +2,12 @@ cmake_minimum_required(VERSION 3.0) | |||
2 | 2 | ||
3 | project(cstring) | 3 | project(cstring) |
4 | 4 | ||
5 | add_library(cstring INTERFACE) | 5 | add_library(cstring |
6 | src/cstring.c) | ||
6 | 7 | ||
7 | target_include_directories(cstring INTERFACE | 8 | target_include_directories(cstring PUBLIC |
8 | include) | 9 | include) |
9 | 10 | ||
10 | target_link_libraries(cstring INTERFACE | 11 | target_link_libraries(cstring PUBLIC |
11 | cassert | 12 | cassert |
12 | -lbsd) | 13 | -lbsd) |
diff --git a/cstring/include/cstring.h b/cstring/include/cstring.h index 134e68e..a3f6b3f 100644 --- a/cstring/include/cstring.h +++ b/cstring/include/cstring.h | |||
@@ -1,12 +1,16 @@ | |||
1 | /// Fixed-size strings with value semantics. | 1 | /// Fixed-size strings with value semantics. |
2 | #pragma once | 2 | #pragma once |
3 | 3 | ||
4 | #include <bsd/string.h> | ||
5 | #include <cassert.h> | 4 | #include <cassert.h> |
5 | |||
6 | #include <bsd/string.h> | ||
6 | #include <stdbool.h> | 7 | #include <stdbool.h> |
7 | #include <stdint.h> | 8 | #include <stdint.h> |
8 | #include <stdio.h> | 9 | #include <stdio.h> |
9 | 10 | ||
11 | // ----------------------------------------------------------------------------- | ||
12 | // Fix-sized strings. | ||
13 | |||
10 | /// A fixed-size string. | 14 | /// A fixed-size string. |
11 | /// The string is null-terminated so that it can be used with the usual C APIs. | 15 | /// The string is null-terminated so that it can be used with the usual C APIs. |
12 | #define DEF_STRING(STRING, SIZE) \ | 16 | #define DEF_STRING(STRING, SIZE) \ |
@@ -118,3 +122,34 @@ DEF_STRING(sstring, 32) // Small. | |||
118 | DEF_STRING(mstring, 256) // Medium. | 122 | DEF_STRING(mstring, 256) // Medium. |
119 | DEF_STRING(lstring, 1024) // Large. | 123 | DEF_STRING(lstring, 1024) // Large. |
120 | DEF_STRING(xlstring, 4096) // Extra large. | 124 | DEF_STRING(xlstring, 4096) // Extra large. |
125 | |||
126 | // ----------------------------------------------------------------------------- | ||
127 | // Dynamically-sized strings. | ||
128 | |||
129 | typedef struct string { | ||
130 | const char* data; | ||
131 | size_t length; | ||
132 | } string; | ||
133 | |||
134 | /// Create a new string. | ||
135 | string string_new(const char*); | ||
136 | |||
137 | /// Delete the string. | ||
138 | void string_del(string*); | ||
139 | |||
140 | /// Get the string's length. | ||
141 | static inline size_t string_length(const string str) { return str.length; } | ||
142 | |||
143 | /// Get the string's data. | ||
144 | static inline const char* string_data(const string str) { return str.data; } | ||
145 | |||
146 | /// Concatenate two strings. | ||
147 | string string_concat(string left, string right); | ||
148 | |||
149 | /// Convert a size to string. | ||
150 | string string_from_size(size_t); | ||
151 | |||
152 | /// Convert and format a size to string. | ||
153 | /// The result uses B for bytes, K for kilobytes (1024), M for megabytes | ||
154 | /// (2**20), and G for gigabytes (2**30), with two decimal digits. | ||
155 | string string_format_size(size_t); | ||
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 | } | ||