From 993424547df0d253d546dbe7adee9b2448294b08 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 15 Jun 2024 11:42:29 -0700 Subject: Add dynamically-sized strings. --- cstring/include/cstring.h | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'cstring/include/cstring.h') 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 @@ /// Fixed-size strings with value semantics. #pragma once -#include #include + +#include #include #include #include +// ----------------------------------------------------------------------------- +// Fix-sized strings. + /// A fixed-size string. /// The string is null-terminated so that it can be used with the usual C APIs. #define DEF_STRING(STRING, SIZE) \ @@ -118,3 +122,34 @@ DEF_STRING(sstring, 32) // Small. DEF_STRING(mstring, 256) // Medium. DEF_STRING(lstring, 1024) // Large. DEF_STRING(xlstring, 4096) // Extra large. + +// ----------------------------------------------------------------------------- +// Dynamically-sized strings. + +typedef struct string { + const char* data; + size_t length; +} string; + +/// Create a new string. +string string_new(const char*); + +/// Delete the string. +void string_del(string*); + +/// Get the string's length. +static inline size_t string_length(const string str) { return str.length; } + +/// Get the string's data. +static inline const char* string_data(const string str) { return str.data; } + +/// Concatenate two strings. +string string_concat(string left, string right); + +/// Convert a size to string. +string string_from_size(size_t); + +/// Convert and format a size to string. +/// The result uses B for bytes, K for kilobytes (1024), M for megabytes +/// (2**20), and G for gigabytes (2**30), with two decimal digits. +string string_format_size(size_t); -- cgit v1.2.3