add substring

This commit is contained in:
2025-12-06 22:27:20 +01:00
parent 38e612c54a
commit 0875f206cb
2 changed files with 33 additions and 25 deletions
+1
View File
@@ -273,6 +273,7 @@ void sb_add_u64(string_builder_t *this, uint64_t u64);
void sb_add_u32(string_builder_t *this, uint32_t u32); void sb_add_u32(string_builder_t *this, uint32_t u32);
void sb_add_u16(string_builder_t *this, uint16_t u16); void sb_add_u16(string_builder_t *this, uint16_t u16);
void sb_add_u8(string_builder_t *this, uint8_t u8); void sb_add_u8(string_builder_t *this, uint8_t u8);
void sb_add_substring(string_builder_t *this, const char *string, size_t substring_length);
const char *sb_build(string_builder_t *this, allocator_t output_allocator); const char *sb_build(string_builder_t *this, allocator_t output_allocator);
#ifdef CIG_IMPL #ifdef CIG_IMPL
+32 -25
View File
@@ -27,60 +27,67 @@ void sb_add_string(string_builder_t *this, const char *string) {
} }
} }
// 21 bytes is the max needed ever for a 64 bit integer, even unsigned.
#define MAX_SIZE (21)
void sb_add_i64(string_builder_t *this, int64_t i64) { void sb_add_i64(string_builder_t *this, int64_t i64) {
// 21 bytes is the max needed ever for a 64 bit integer, even unsigned. char *buffer = allocator_alloc(this->allocator, MAX_SIZE);
const unsigned long max_size = 21; snprintf(buffer, MAX_SIZE, "%"PRIi64, i64);
char *buffer = allocator_alloc(this->allocator, max_size);
snprintf(buffer, max_size, "%"PRIi64, i64);
sb_add_string(this, buffer); sb_add_string(this, buffer);
} }
void sb_add_i32(string_builder_t *this, int32_t i32) { void sb_add_i32(string_builder_t *this, int32_t i32) {
const unsigned long max_size = 21; char *buffer = allocator_alloc(this->allocator, MAX_SIZE);
char *buffer = allocator_alloc(this->allocator, max_size); snprintf(buffer, MAX_SIZE, "%"PRIi32, i32);
snprintf(buffer, max_size, "%"PRIi32, i32);
sb_add_string(this, buffer); sb_add_string(this, buffer);
} }
void sb_add_i16(string_builder_t *this, int16_t i16) { void sb_add_i16(string_builder_t *this, int16_t i16) {
const unsigned long max_size = 21; char *buffer = allocator_alloc(this->allocator, MAX_SIZE);
char *buffer = allocator_alloc(this->allocator, max_size); snprintf(buffer, MAX_SIZE, "%"PRIi16, i16);
snprintf(buffer, max_size, "%"PRIi16, i16);
sb_add_string(this, buffer); sb_add_string(this, buffer);
} }
void sb_add_i8(string_builder_t *this, int8_t i8) { void sb_add_i8(string_builder_t *this, int8_t i8) {
const unsigned long max_size = 21; char *buffer = allocator_alloc(this->allocator, MAX_SIZE);
char *buffer = allocator_alloc(this->allocator, max_size); snprintf(buffer, MAX_SIZE, "%"PRIi8, i8);
snprintf(buffer, max_size, "%"PRIi8, i8);
sb_add_string(this, buffer); sb_add_string(this, buffer);
} }
void sb_add_u64(string_builder_t *this, uint64_t u64) { void sb_add_u64(string_builder_t *this, uint64_t u64) {
const unsigned long max_size = 21; char *buffer = allocator_alloc(this->allocator, MAX_SIZE);
char *buffer = allocator_alloc(this->allocator, max_size); snprintf(buffer, MAX_SIZE, "%"PRIu64, u64);
snprintf(buffer, max_size, "%"PRIu64, u64);
sb_add_string(this, buffer); sb_add_string(this, buffer);
} }
void sb_add_u32(string_builder_t *this, uint32_t u32) { void sb_add_u32(string_builder_t *this, uint32_t u32) {
const unsigned long max_size = 21; char *buffer = allocator_alloc(this->allocator, MAX_SIZE);
char *buffer = allocator_alloc(this->allocator, max_size); snprintf(buffer, MAX_SIZE, "%"PRIu32, u32);
snprintf(buffer, max_size, "%"PRIu32, u32);
sb_add_string(this, buffer); sb_add_string(this, buffer);
} }
void sb_add_u16(string_builder_t *this, uint16_t u16) { void sb_add_u16(string_builder_t *this, uint16_t u16) {
const unsigned long max_size = 21; char *buffer = allocator_alloc(this->allocator, MAX_SIZE);
char *buffer = allocator_alloc(this->allocator, max_size); snprintf(buffer, MAX_SIZE, "%"PRIu16, u16);
snprintf(buffer, max_size, "%"PRIu16, u16);
sb_add_string(this, buffer); sb_add_string(this, buffer);
} }
void sb_add_u8(string_builder_t *this, uint8_t u8) { void sb_add_u8(string_builder_t *this, uint8_t u8) {
const unsigned long max_size = 21; char *buffer = allocator_alloc(this->allocator, MAX_SIZE);
char *buffer = allocator_alloc(this->allocator, max_size); snprintf(buffer, MAX_SIZE, "%"PRIu8, u8);
snprintf(buffer, max_size, "%"PRIu8, u8); sb_add_string(this, buffer);
}
void sb_add_substring(string_builder_t *this, const char *string, size_t substring_length) {
size_t string_length = strlen(string);
size_t min_length = string_length < substring_length
? string_length
: substring_length;
char *buffer = allocator_alloc(this->allocator, min_length + 1);
for ( size_t i = 0; i < min_length; i++ ) {
buffer[i] = string[i];
}
buffer[min_length-1] = '\0';
sb_add_string(this, buffer); sb_add_string(this, buffer);
} }