aboutsummaryrefslogtreecommitdiff
path: root/cstring/include/cstring.h
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2024-06-15 11:42:29 -0700
committer3gg <3gg@shellblade.net>2024-06-15 11:42:29 -0700
commit993424547df0d253d546dbe7adee9b2448294b08 (patch)
treeb56e4b7e54228d073d80bf9580846a3295295b92 /cstring/include/cstring.h
parentf5127be2865c90b26de896c1adbc5a19ea3a0bd6 (diff)
Add dynamically-sized strings.
Diffstat (limited to 'cstring/include/cstring.h')
-rw-r--r--cstring/include/cstring.h37
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.
118DEF_STRING(mstring, 256) // Medium. 122DEF_STRING(mstring, 256) // Medium.
119DEF_STRING(lstring, 1024) // Large. 123DEF_STRING(lstring, 1024) // Large.
120DEF_STRING(xlstring, 4096) // Extra large. 124DEF_STRING(xlstring, 4096) // Extra large.
125
126// -----------------------------------------------------------------------------
127// Dynamically-sized strings.
128
129typedef struct string {
130 const char* data;
131 size_t length;
132} string;
133
134/// Create a new string.
135string string_new(const char*);
136
137/// Delete the string.
138void string_del(string*);
139
140/// Get the string's length.
141static inline size_t string_length(const string str) { return str.length; }
142
143/// Get the string's data.
144static inline const char* string_data(const string str) { return str.data; }
145
146/// Concatenate two strings.
147string string_concat(string left, string right);
148
149/// Convert a size to string.
150string 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.
155string string_format_size(size_t);