diff options
Diffstat (limited to 'cstring/include')
-rw-r--r-- | cstring/include/cstring.h | 37 |
1 files changed, 36 insertions, 1 deletions
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); | ||