2024-04-18 12:59:11 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
#include "dqn.h"
|
|
|
|
|
|
2024-03-25 05:11:57 +00:00
|
|
|
|
/*
|
2024-01-31 12:49:23 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// $$$$$$\ $$$$$$$$\ $$$$$$$\ $$$$$$\ $$\ $$\ $$$$$$\
|
|
|
|
|
// $$ __$$\\__$$ __|$$ __$$\ \_$$ _|$$$\ $$ |$$ __$$\
|
|
|
|
|
// $$ / \__| $$ | $$ | $$ | $$ | $$$$\ $$ |$$ / \__|
|
|
|
|
|
// \$$$$$$\ $$ | $$$$$$$ | $$ | $$ $$\$$ |$$ |$$$$\
|
|
|
|
|
// \____$$\ $$ | $$ __$$< $$ | $$ \$$$$ |$$ |\_$$ |
|
|
|
|
|
// $$\ $$ | $$ | $$ | $$ | $$ | $$ |\$$$ |$$ | $$ |
|
|
|
|
|
// \$$$$$$ | $$ | $$ | $$ |$$$$$$\ $$ | \$$ |\$$$$$$ |
|
|
|
|
|
// \______/ \__| \__| \__|\______|\__| \__| \______/
|
|
|
|
|
//
|
|
|
|
|
// dqn_string.cpp
|
|
|
|
|
//
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2024-03-25 05:11:57 +00:00
|
|
|
|
*/
|
2024-01-31 12:49:23 +00:00
|
|
|
|
|
|
|
|
|
// NOTE: [$CSTR] Dqn_CStr8 /////////////////////////////////////////////////////////////////////////
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API Dqn_usize Dqn_CStr8_FSize(DQN_FMT_ATTRIB char const *fmt, ...)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
|
|
|
|
va_list args;
|
|
|
|
|
va_start(args, fmt);
|
2024-01-31 12:49:23 +00:00
|
|
|
|
Dqn_usize result = DQN_VSNPRINTF(nullptr, 0, fmt, args);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
va_end(args);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API Dqn_usize Dqn_CStr8_FVSize(DQN_FMT_ATTRIB char const *fmt, va_list args)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
|
|
|
|
va_list args_copy;
|
|
|
|
|
va_copy(args_copy, args);
|
2024-01-31 12:49:23 +00:00
|
|
|
|
Dqn_usize result = DQN_VSNPRINTF(nullptr, 0, fmt, args_copy);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
va_end(args_copy);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API Dqn_usize Dqn_CStr8_Size(char const *src)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
|
|
|
|
Dqn_usize result = 0;
|
|
|
|
|
while (src && src[0] != 0) {
|
|
|
|
|
src++;
|
|
|
|
|
result++;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API Dqn_usize Dqn_CStr16_Size(wchar_t const *src)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
|
|
|
|
Dqn_usize result = 0;
|
|
|
|
|
while (src && src[0] != 0) {
|
|
|
|
|
src++;
|
|
|
|
|
result++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-31 12:49:23 +00:00
|
|
|
|
// NOTE: [$STR6] Dqn_Str16 /////////////////////////////////////////////////////////////////////////
|
|
|
|
|
DQN_API bool operator==(Dqn_Str16 const &lhs, Dqn_Str16 const &rhs)
|
|
|
|
|
{
|
|
|
|
|
bool result = false;
|
|
|
|
|
if (lhs.size == rhs.size)
|
|
|
|
|
result = DQN_MEMCMP(lhs.data, rhs.data, lhs.size * sizeof(*lhs.data)) == 0;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DQN_API bool operator!=(Dqn_Str16 const &lhs, Dqn_Str16 const &rhs)
|
|
|
|
|
{
|
|
|
|
|
bool result = !(lhs == rhs);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NOTE: [$STR8] Dqn_Str8 //////////////////////////////////////////////////////////////////////////
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_InitCStr8(char const *src)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2024-01-31 12:49:23 +00:00
|
|
|
|
Dqn_usize size = Dqn_CStr8_Size(src);
|
|
|
|
|
Dqn_Str8 result = Dqn_Str8_Init(src, size);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API bool Dqn_Str8_IsAll(Dqn_Str8 string, Dqn_Str8IsAll is_all)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2024-01-31 12:49:23 +00:00
|
|
|
|
bool result = Dqn_Str8_HasData(string);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
if (!result)
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
switch (is_all) {
|
2023-10-24 13:11:48 +00:00
|
|
|
|
case Dqn_Str8IsAll_Digits: {
|
2023-07-04 14:04:53 +00:00
|
|
|
|
for (Dqn_usize index = 0; result && index < string.size; index++)
|
|
|
|
|
result = string.data[index] >= '0' && string.data[index] <= '9';
|
|
|
|
|
} break;
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
case Dqn_Str8IsAll_Hex: {
|
|
|
|
|
Dqn_Str8 trimmed = Dqn_Str8_TrimPrefix(string, DQN_STR8("0x"), Dqn_Str8EqCase_Insensitive);
|
2024-08-01 03:34:36 +00:00
|
|
|
|
for (Dqn_usize index = 0; result && index < trimmed.size; index++) {
|
2023-07-04 14:04:53 +00:00
|
|
|
|
char ch = trimmed.data[index];
|
|
|
|
|
result = (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
|
|
|
|
|
}
|
|
|
|
|
} break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_Slice(Dqn_Str8 string, Dqn_usize offset, Dqn_usize size)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 result = Dqn_Str8_Init(string.data, 0);
|
2024-01-31 12:49:23 +00:00
|
|
|
|
if (!Dqn_Str8_HasData(string))
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
Dqn_usize capped_offset = DQN_MIN(offset, string.size);
|
|
|
|
|
Dqn_usize max_size = string.size - capped_offset;
|
|
|
|
|
Dqn_usize capped_size = DQN_MIN(size, max_size);
|
2023-10-24 13:11:48 +00:00
|
|
|
|
result = Dqn_Str8_Init(string.data + capped_offset, capped_size);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_Advance(Dqn_Str8 string, Dqn_usize amount)
|
2023-08-25 10:35:04 +00:00
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 result = Dqn_Str8_Slice(string, amount, DQN_USIZE_MAX);
|
2023-08-25 10:35:04 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-19 12:11:00 +00:00
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_NextLine(Dqn_Str8 string)
|
|
|
|
|
{
|
|
|
|
|
Dqn_Str8 result = Dqn_Str8_BinarySplit(string, DQN_STR8("\n")).rhs;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API Dqn_Str8BinarySplitResult Dqn_Str8_BinarySplitArray(Dqn_Str8 string, Dqn_Str8 const *find, Dqn_usize find_size)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8BinarySplitResult result = {};
|
2024-01-31 12:49:23 +00:00
|
|
|
|
if (!Dqn_Str8_HasData(string) || !find || find_size == 0)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
result.lhs = string;
|
2023-10-24 13:11:48 +00:00
|
|
|
|
for (size_t index = 0; !result.rhs.data && index < string.size; index++) {
|
2023-07-04 14:04:53 +00:00
|
|
|
|
for (Dqn_usize find_index = 0; find_index < find_size; find_index++) {
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 find_item = find[find_index];
|
|
|
|
|
Dqn_Str8 string_slice = Dqn_Str8_Slice(string, index, find_item.size);
|
|
|
|
|
if (Dqn_Str8_Eq(string_slice, find_item)) {
|
2023-07-04 14:04:53 +00:00
|
|
|
|
result.lhs.size = index;
|
|
|
|
|
result.rhs.data = string_slice.data + find_item.size;
|
|
|
|
|
result.rhs.size = string.size - (index + find_item.size);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API Dqn_Str8BinarySplitResult Dqn_Str8_BinarySplit(Dqn_Str8 string, Dqn_Str8 find)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8BinarySplitResult result = Dqn_Str8_BinarySplitArray(string, &find, 1);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-18 12:59:11 +00:00
|
|
|
|
DQN_API Dqn_Str8BinarySplitResult Dqn_Str8_BinarySplitLastArray(Dqn_Str8 string, Dqn_Str8 const *find, Dqn_usize find_size)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8BinarySplitResult result = {};
|
2024-01-31 12:49:23 +00:00
|
|
|
|
if (!Dqn_Str8_HasData(string) || !find || find_size == 0)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
result.lhs = string;
|
|
|
|
|
for (size_t index = string.size - 1; !result.rhs.data && index < string.size; index--) {
|
|
|
|
|
for (Dqn_usize find_index = 0; find_index < find_size; find_index++) {
|
|
|
|
|
Dqn_Str8 find_item = find[find_index];
|
|
|
|
|
Dqn_Str8 string_slice = Dqn_Str8_Slice(string, index, find_item.size);
|
|
|
|
|
if (Dqn_Str8_Eq(string_slice, find_item)) {
|
|
|
|
|
result.lhs.size = index;
|
|
|
|
|
result.rhs.data = string_slice.data + find_item.size;
|
|
|
|
|
result.rhs.size = string.size - (index + find_item.size);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-07-04 14:04:53 +00:00
|
|
|
|
}
|
2023-10-24 13:11:48 +00:00
|
|
|
|
}
|
2023-07-04 14:04:53 +00:00
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2023-07-04 14:04:53 +00:00
|
|
|
|
|
2024-04-18 12:59:11 +00:00
|
|
|
|
DQN_API Dqn_Str8BinarySplitResult Dqn_Str8_BinarySplitLast(Dqn_Str8 string, Dqn_Str8 find)
|
2023-10-24 13:11:48 +00:00
|
|
|
|
{
|
2024-04-18 12:59:11 +00:00
|
|
|
|
Dqn_Str8BinarySplitResult result = Dqn_Str8_BinarySplitLastArray(string, &find, 1);
|
2023-10-24 13:11:48 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-31 12:49:23 +00:00
|
|
|
|
DQN_API Dqn_usize Dqn_Str8_Split(Dqn_Str8 string, Dqn_Str8 delimiter, Dqn_Str8 *splits, Dqn_usize splits_count, Dqn_Str8SplitIncludeEmptyStrings mode)
|
2023-10-24 13:11:48 +00:00
|
|
|
|
{
|
|
|
|
|
Dqn_usize result = 0; // The number of splits in the actual string.
|
2024-01-31 12:49:23 +00:00
|
|
|
|
if (!Dqn_Str8_HasData(string) || !Dqn_Str8_HasData(delimiter) || delimiter.size <= 0)
|
2023-10-24 13:11:48 +00:00
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
Dqn_Str8BinarySplitResult split = {};
|
|
|
|
|
Dqn_Str8 first = string;
|
|
|
|
|
do {
|
|
|
|
|
split = Dqn_Str8_BinarySplit(first, delimiter);
|
2024-01-31 12:49:23 +00:00
|
|
|
|
if (split.lhs.size || mode == Dqn_Str8SplitIncludeEmptyStrings_Yes) {
|
2023-10-24 13:11:48 +00:00
|
|
|
|
if (splits && result < splits_count)
|
|
|
|
|
splits[result] = split.lhs;
|
|
|
|
|
result++;
|
|
|
|
|
}
|
|
|
|
|
first = split.rhs;
|
|
|
|
|
} while (first.size);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-31 12:49:23 +00:00
|
|
|
|
DQN_API Dqn_Slice<Dqn_Str8> Dqn_Str8_SplitAlloc(Dqn_Arena *arena, Dqn_Str8 string, Dqn_Str8 delimiter, Dqn_Str8SplitIncludeEmptyStrings mode)
|
2023-08-16 12:06:48 +00:00
|
|
|
|
{
|
2024-01-31 12:49:23 +00:00
|
|
|
|
Dqn_Slice<Dqn_Str8> result = {};
|
|
|
|
|
Dqn_usize splits_required = Dqn_Str8_Split(string, delimiter, /*splits*/ nullptr, /*count*/ 0, mode);
|
|
|
|
|
result.data = Dqn_Arena_NewArray(arena, Dqn_Str8, splits_required, Dqn_ZeroMem_No);
|
2023-08-16 12:06:48 +00:00
|
|
|
|
if (result.data) {
|
2024-01-31 12:49:23 +00:00
|
|
|
|
result.size = Dqn_Str8_Split(string, delimiter, result.data, splits_required, mode);
|
2023-08-16 12:06:48 +00:00
|
|
|
|
DQN_ASSERT(splits_required == result.size);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-01 03:34:36 +00:00
|
|
|
|
DQN_API Dqn_Str8FindResult Dqn_Str8_FindStr8Array(Dqn_Str8 string, Dqn_Str8 const *find, Dqn_usize find_size, Dqn_Str8EqCase eq_case)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8FindResult result = {};
|
2024-01-31 12:49:23 +00:00
|
|
|
|
if (!Dqn_Str8_HasData(string) || !find || find_size == 0)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
for (Dqn_usize index = 0; !result.found && index < string.size; index++) {
|
|
|
|
|
for (Dqn_usize find_index = 0; find_index < find_size; find_index++) {
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 find_item = find[find_index];
|
|
|
|
|
Dqn_Str8 string_slice = Dqn_Str8_Slice(string, index, find_item.size);
|
2024-08-01 03:34:36 +00:00
|
|
|
|
if (Dqn_Str8_Eq(string_slice, find_item, eq_case)) {
|
2023-07-04 14:04:53 +00:00
|
|
|
|
result.found = true;
|
|
|
|
|
result.index = index;
|
2023-10-24 13:11:48 +00:00
|
|
|
|
result.start_to_before_match = Dqn_Str8_Init(string.data, index);
|
|
|
|
|
result.match = Dqn_Str8_Init(string.data + index, find_item.size);
|
|
|
|
|
result.match_to_end_of_buffer = Dqn_Str8_Init(result.match.data, string.size - index);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-01 03:34:36 +00:00
|
|
|
|
DQN_API Dqn_Str8FindResult Dqn_Str8_FindStr8(Dqn_Str8 string, Dqn_Str8 find, Dqn_Str8EqCase eq_case)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2024-08-01 03:34:36 +00:00
|
|
|
|
Dqn_Str8FindResult result = Dqn_Str8_FindStr8Array(string, &find, 1, eq_case);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-18 12:59:11 +00:00
|
|
|
|
DQN_API Dqn_Str8FindResult Dqn_Str8_Find(Dqn_Str8 string, uint32_t flags)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8FindResult result = {};
|
2023-07-04 14:04:53 +00:00
|
|
|
|
for (size_t index = 0; !result.found && index < string.size; index++) {
|
2023-10-24 13:11:48 +00:00
|
|
|
|
result.found |= ((flags & Dqn_Str8FindFlag_Digit) && Dqn_Char_IsDigit(string.data[index]));
|
|
|
|
|
result.found |= ((flags & Dqn_Str8FindFlag_Alphabet) && Dqn_Char_IsAlphabet(string.data[index]));
|
|
|
|
|
result.found |= ((flags & Dqn_Str8FindFlag_Whitespace) && Dqn_Char_IsWhitespace(string.data[index]));
|
|
|
|
|
result.found |= ((flags & Dqn_Str8FindFlag_Plus) && string.data[index] == '+');
|
|
|
|
|
result.found |= ((flags & Dqn_Str8FindFlag_Minus) && string.data[index] == '-');
|
2023-07-04 14:04:53 +00:00
|
|
|
|
if (result.found) {
|
|
|
|
|
result.index = index;
|
2023-10-24 13:11:48 +00:00
|
|
|
|
result.match = Dqn_Str8_Init(string.data + index, 1);
|
|
|
|
|
result.match_to_end_of_buffer = Dqn_Str8_Init(result.match.data, string.size - index);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-31 12:49:23 +00:00
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_Segment(Dqn_Arena *arena, Dqn_Str8 src, Dqn_usize segment_size, char segment_char)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2024-01-31 12:49:23 +00:00
|
|
|
|
if (!segment_size || !Dqn_Str8_HasData(src)) {
|
|
|
|
|
Dqn_Str8 result = Dqn_Str8_Copy(arena, src);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Dqn_usize segments = src.size / segment_size;
|
|
|
|
|
if (src.size % segment_size == 0)
|
|
|
|
|
segments--;
|
2023-07-04 14:04:53 +00:00
|
|
|
|
|
2024-01-31 12:49:23 +00:00
|
|
|
|
Dqn_usize segment_counter = 0;
|
|
|
|
|
Dqn_Str8 result = Dqn_Str8_Alloc(arena, src.size + segments, Dqn_ZeroMem_Yes);
|
|
|
|
|
Dqn_usize write_index = 0;
|
2023-07-04 14:04:53 +00:00
|
|
|
|
DQN_FOR_UINDEX(src_index, src.size) {
|
|
|
|
|
result.data[write_index++] = src.data[src_index];
|
2024-01-31 12:49:23 +00:00
|
|
|
|
if ((src_index + 1) % segment_size == 0 && segment_counter < segments) {
|
2023-07-04 14:04:53 +00:00
|
|
|
|
result.data[write_index++] = segment_char;
|
2024-01-31 12:49:23 +00:00
|
|
|
|
segment_counter++;
|
|
|
|
|
}
|
2023-07-04 14:04:53 +00:00
|
|
|
|
DQN_ASSERTF(write_index <= result.size, "result.size=%zu, write_index=%zu", result.size, write_index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DQN_ASSERTF(write_index == result.size, "result.size=%zu, write_index=%zu", result.size, write_index);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-31 12:49:23 +00:00
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_ReverseSegment(Dqn_Arena *arena, Dqn_Str8 src, Dqn_usize segment_size, char segment_char)
|
|
|
|
|
{
|
|
|
|
|
if (!segment_size || !Dqn_Str8_HasData(src)) {
|
|
|
|
|
Dqn_Str8 result = Dqn_Str8_Copy(arena, src);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Dqn_usize segments = src.size / segment_size;
|
|
|
|
|
if (src.size % segment_size == 0)
|
|
|
|
|
segments--;
|
|
|
|
|
|
|
|
|
|
Dqn_usize write_counter = 0;
|
|
|
|
|
Dqn_usize segment_counter = 0;
|
|
|
|
|
Dqn_Str8 result = Dqn_Str8_Alloc(arena, src.size + segments, Dqn_ZeroMem_Yes);
|
|
|
|
|
Dqn_usize write_index = result.size - 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DQN_MSVC_WARNING_PUSH
|
|
|
|
|
DQN_MSVC_WARNING_DISABLE(6293) // NOTE: Ill-defined loop
|
|
|
|
|
for (size_t src_index = src.size - 1; src_index < src.size; src_index--) {
|
|
|
|
|
DQN_MSVC_WARNING_POP
|
|
|
|
|
result.data[write_index--] = src.data[src_index];
|
|
|
|
|
if (++write_counter % segment_size == 0 && segment_counter < segments) {
|
|
|
|
|
result.data[write_index--] = segment_char;
|
|
|
|
|
segment_counter++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DQN_ASSERT(write_index == SIZE_MAX);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-04 14:04:53 +00:00
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API bool Dqn_Str8_Eq(Dqn_Str8 lhs, Dqn_Str8 rhs, Dqn_Str8EqCase eq_case)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
|
|
|
|
if (lhs.size != rhs.size)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (lhs.size == 0)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
if (!lhs.data || !rhs.data)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
bool result = true;
|
|
|
|
|
switch (eq_case) {
|
2023-10-24 13:11:48 +00:00
|
|
|
|
case Dqn_Str8EqCase_Sensitive: {
|
2023-07-04 14:04:53 +00:00
|
|
|
|
result = (DQN_MEMCMP(lhs.data, rhs.data, lhs.size) == 0);
|
|
|
|
|
} break;
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
case Dqn_Str8EqCase_Insensitive: {
|
2023-07-04 14:04:53 +00:00
|
|
|
|
for (Dqn_usize index = 0; index < lhs.size && result; index++)
|
|
|
|
|
result = (Dqn_Char_ToLower(lhs.data[index]) == Dqn_Char_ToLower(rhs.data[index]));
|
|
|
|
|
} break;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API bool Dqn_Str8_EqInsensitive(Dqn_Str8 lhs, Dqn_Str8 rhs)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
|
bool result = Dqn_Str8_Eq(lhs, rhs, Dqn_Str8EqCase_Insensitive);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API bool Dqn_Str8_StartsWith(Dqn_Str8 string, Dqn_Str8 prefix, Dqn_Str8EqCase eq_case)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 substring = {string.data, DQN_MIN(prefix.size, string.size)};
|
|
|
|
|
bool result = Dqn_Str8_Eq(substring, prefix, eq_case);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API bool Dqn_Str8_StartsWithInsensitive(Dqn_Str8 string, Dqn_Str8 prefix)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
|
bool result = Dqn_Str8_StartsWith(string, prefix, Dqn_Str8EqCase_Insensitive);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API bool Dqn_Str8_EndsWith(Dqn_Str8 string, Dqn_Str8 suffix, Dqn_Str8EqCase eq_case)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 substring = {string.data + string.size - suffix.size, DQN_MIN(string.size, suffix.size)};
|
|
|
|
|
bool result = Dqn_Str8_Eq(substring, suffix, eq_case);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API bool Dqn_Str8_EndsWithInsensitive(Dqn_Str8 string, Dqn_Str8 suffix)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
|
bool result = Dqn_Str8_EndsWith(string, suffix, Dqn_Str8EqCase_Insensitive);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API bool Dqn_Str8_HasChar(Dqn_Str8 string, char ch)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
|
|
|
|
bool result = false;
|
|
|
|
|
for (Dqn_usize index = 0; !result && index < string.size; index++)
|
|
|
|
|
result = string.data[index] == ch;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_TrimPrefix(Dqn_Str8 string, Dqn_Str8 prefix, Dqn_Str8EqCase eq_case)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 result = string;
|
|
|
|
|
if (Dqn_Str8_StartsWith(string, prefix, eq_case)) {
|
2023-07-04 14:04:53 +00:00
|
|
|
|
result.data += prefix.size;
|
|
|
|
|
result.size -= prefix.size;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-01 03:34:36 +00:00
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_TrimHexPrefix(Dqn_Str8 string)
|
|
|
|
|
{
|
|
|
|
|
Dqn_Str8 result = Dqn_Str8_TrimPrefix(string, DQN_STR8("0x"), Dqn_Str8EqCase_Insensitive);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_TrimSuffix(Dqn_Str8 string, Dqn_Str8 suffix, Dqn_Str8EqCase eq_case)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 result = string;
|
|
|
|
|
if (Dqn_Str8_EndsWith(string, suffix, eq_case))
|
2023-07-04 14:04:53 +00:00
|
|
|
|
result.size -= suffix.size;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_TrimAround(Dqn_Str8 string, Dqn_Str8 trim_string)
|
|
|
|
|
{
|
|
|
|
|
Dqn_Str8 result = Dqn_Str8_TrimPrefix(string, trim_string);
|
|
|
|
|
result = Dqn_Str8_TrimSuffix(result, trim_string);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_TrimWhitespaceAround(Dqn_Str8 string)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 result = string;
|
2024-01-31 12:49:23 +00:00
|
|
|
|
if (!Dqn_Str8_HasData(string))
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
char const *start = string.data;
|
|
|
|
|
char const *end = string.data + string.size;
|
|
|
|
|
while (start < end && Dqn_Char_IsWhitespace(start[0]))
|
|
|
|
|
start++;
|
|
|
|
|
|
|
|
|
|
while (end > start && Dqn_Char_IsWhitespace(end[-1]))
|
|
|
|
|
end--;
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
result = Dqn_Str8_Init(start, end - start);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_TrimByteOrderMark(Dqn_Str8 string)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 result = string;
|
2024-01-31 12:49:23 +00:00
|
|
|
|
if (!Dqn_Str8_HasData(result))
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
// TODO(dqn): This is little endian
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 UTF8_BOM = DQN_STR8("\xEF\xBB\xBF");
|
|
|
|
|
Dqn_Str8 UTF16_BOM_BE = DQN_STR8("\xEF\xFF");
|
|
|
|
|
Dqn_Str8 UTF16_BOM_LE = DQN_STR8("\xFF\xEF");
|
|
|
|
|
Dqn_Str8 UTF32_BOM_BE = DQN_STR8("\x00\x00\xFE\xFF");
|
|
|
|
|
Dqn_Str8 UTF32_BOM_LE = DQN_STR8("\xFF\xFE\x00\x00");
|
2023-07-04 14:04:53 +00:00
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
result = Dqn_Str8_TrimPrefix(result, UTF8_BOM, Dqn_Str8EqCase_Sensitive);
|
|
|
|
|
result = Dqn_Str8_TrimPrefix(result, UTF16_BOM_BE, Dqn_Str8EqCase_Sensitive);
|
|
|
|
|
result = Dqn_Str8_TrimPrefix(result, UTF16_BOM_LE, Dqn_Str8EqCase_Sensitive);
|
|
|
|
|
result = Dqn_Str8_TrimPrefix(result, UTF32_BOM_BE, Dqn_Str8EqCase_Sensitive);
|
|
|
|
|
result = Dqn_Str8_TrimPrefix(result, UTF32_BOM_LE, Dqn_Str8EqCase_Sensitive);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_FileNameFromPath(Dqn_Str8 path)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 separators[] = {DQN_STR8("/"), DQN_STR8("\\")};
|
2024-04-18 12:59:11 +00:00
|
|
|
|
Dqn_Str8BinarySplitResult split = Dqn_Str8_BinarySplitLastArray(path, separators, DQN_ARRAY_UCOUNT(separators));
|
2024-01-31 12:49:23 +00:00
|
|
|
|
Dqn_Str8 result = Dqn_Str8_HasData(split.rhs) ? split.rhs : split.lhs;
|
2023-10-24 13:11:48 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2023-07-04 14:04:53 +00:00
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_FileNameNoExtension(Dqn_Str8 path)
|
|
|
|
|
{
|
|
|
|
|
Dqn_Str8 file_name = Dqn_Str8_FileNameFromPath(path);
|
|
|
|
|
Dqn_Str8 result = Dqn_Str8_FilePathNoExtension(file_name);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_FilePathNoExtension(Dqn_Str8 path)
|
|
|
|
|
{
|
2024-04-18 12:59:11 +00:00
|
|
|
|
Dqn_Str8BinarySplitResult split = Dqn_Str8_BinarySplitLast(path, DQN_STR8("."));
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 result = split.lhs;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-31 12:49:23 +00:00
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_FileExtension(Dqn_Str8 path)
|
|
|
|
|
{
|
2024-04-18 12:59:11 +00:00
|
|
|
|
Dqn_Str8BinarySplitResult split = Dqn_Str8_BinarySplitLast(path, DQN_STR8("."));
|
2024-01-31 12:49:23 +00:00
|
|
|
|
Dqn_Str8 result = split.rhs;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API Dqn_Str8ToU64Result Dqn_Str8_ToU64(Dqn_Str8 string, char separator)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
|
|
|
|
// NOTE: Argument check
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8ToU64Result result = {};
|
2024-01-31 12:49:23 +00:00
|
|
|
|
if (!Dqn_Str8_HasData(string)) {
|
|
|
|
|
result.success = true;
|
2023-08-16 11:59:38 +00:00
|
|
|
|
return result;
|
2024-01-31 12:49:23 +00:00
|
|
|
|
}
|
2023-07-04 14:04:53 +00:00
|
|
|
|
|
|
|
|
|
// NOTE: Sanitize input/output
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 trim_string = Dqn_Str8_TrimWhitespaceAround(string);
|
2023-08-16 11:59:38 +00:00
|
|
|
|
if (trim_string.size == 0) {
|
2024-01-31 12:49:23 +00:00
|
|
|
|
result.success = true;
|
2023-08-16 11:59:38 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2023-07-04 14:04:53 +00:00
|
|
|
|
|
|
|
|
|
// NOTE: Handle prefix '+'
|
|
|
|
|
Dqn_usize start_index = 0;
|
|
|
|
|
if (!Dqn_Char_IsDigit(trim_string.data[0])) {
|
|
|
|
|
if (trim_string.data[0] != '+')
|
2023-08-16 11:59:38 +00:00
|
|
|
|
return result;
|
2023-07-04 14:04:53 +00:00
|
|
|
|
start_index++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NOTE: Convert the string number to the binary number
|
|
|
|
|
for (Dqn_usize index = start_index; index < trim_string.size; index++) {
|
|
|
|
|
char ch = trim_string.data[index];
|
|
|
|
|
if (index) {
|
|
|
|
|
if (separator != 0 && ch == separator)
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Dqn_Char_IsDigit(ch))
|
2023-08-16 11:59:38 +00:00
|
|
|
|
return result;
|
2023-07-04 14:04:53 +00:00
|
|
|
|
|
2023-08-16 11:59:38 +00:00
|
|
|
|
result.value = Dqn_Safe_MulU64(result.value, 10);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
uint64_t digit = ch - '0';
|
2023-08-16 11:59:38 +00:00
|
|
|
|
result.value = Dqn_Safe_AddU64(result.value, digit);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-16 11:59:38 +00:00
|
|
|
|
result.success = true;
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API Dqn_Str8ToI64Result Dqn_Str8_ToI64(Dqn_Str8 string, char separator)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
|
|
|
|
// NOTE: Argument check
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8ToI64Result result = {};
|
2024-01-31 12:49:23 +00:00
|
|
|
|
if (!Dqn_Str8_HasData(string)) {
|
|
|
|
|
result.success = true;
|
2023-08-16 11:59:38 +00:00
|
|
|
|
return result;
|
2024-01-31 12:49:23 +00:00
|
|
|
|
}
|
2023-07-04 14:04:53 +00:00
|
|
|
|
|
|
|
|
|
// NOTE: Sanitize input/output
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 trim_string = Dqn_Str8_TrimWhitespaceAround(string);
|
2023-08-16 11:59:38 +00:00
|
|
|
|
if (trim_string.size == 0) {
|
2024-01-31 12:49:23 +00:00
|
|
|
|
result.success = true;
|
2023-08-16 11:59:38 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2023-07-04 14:04:53 +00:00
|
|
|
|
|
|
|
|
|
bool negative = false;
|
|
|
|
|
Dqn_usize start_index = 0;
|
|
|
|
|
if (!Dqn_Char_IsDigit(trim_string.data[0])) {
|
|
|
|
|
negative = (trim_string.data[start_index] == '-');
|
|
|
|
|
if (!negative && trim_string.data[0] != '+')
|
2023-08-16 11:59:38 +00:00
|
|
|
|
return result;
|
2023-07-04 14:04:53 +00:00
|
|
|
|
start_index++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NOTE: Convert the string number to the binary number
|
|
|
|
|
for (Dqn_usize index = start_index; index < trim_string.size; index++) {
|
|
|
|
|
char ch = trim_string.data[index];
|
|
|
|
|
if (index) {
|
|
|
|
|
if (separator != 0 && ch == separator)
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Dqn_Char_IsDigit(ch))
|
2023-08-16 11:59:38 +00:00
|
|
|
|
return result;
|
2023-07-04 14:04:53 +00:00
|
|
|
|
|
2023-08-16 11:59:38 +00:00
|
|
|
|
result.value = Dqn_Safe_MulU64(result.value, 10);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
uint64_t digit = ch - '0';
|
2023-08-16 11:59:38 +00:00
|
|
|
|
result.value = Dqn_Safe_AddU64(result.value, digit);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (negative)
|
2023-08-16 11:59:38 +00:00
|
|
|
|
result.value *= -1;
|
2023-07-04 14:04:53 +00:00
|
|
|
|
|
2023-08-16 11:59:38 +00:00
|
|
|
|
result.success = true;
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_Replace(Dqn_Str8 string,
|
|
|
|
|
Dqn_Str8 find,
|
|
|
|
|
Dqn_Str8 replace,
|
|
|
|
|
Dqn_usize start_index,
|
2024-01-31 12:49:23 +00:00
|
|
|
|
Dqn_Arena *arena,
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8EqCase eq_case)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 result = {};
|
2024-01-31 12:49:23 +00:00
|
|
|
|
if (!Dqn_Str8_HasData(string) || !Dqn_Str8_HasData(find) || find.size > string.size || find.size == 0 || string.size == 0) {
|
|
|
|
|
result = Dqn_Str8_Copy(arena, string);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-01 03:34:36 +00:00
|
|
|
|
Dqn_TLSTMem tmem = Dqn_TLS_TMem(arena);
|
|
|
|
|
Dqn_Str8Builder string_builder = Dqn_Str8Builder_Init(tmem.arena);
|
|
|
|
|
Dqn_usize max = string.size - find.size;
|
|
|
|
|
Dqn_usize head = start_index;
|
2023-07-04 14:04:53 +00:00
|
|
|
|
|
|
|
|
|
for (Dqn_usize tail = head; tail <= max; tail++) {
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 check = Dqn_Str8_Slice(string, tail, find.size);
|
|
|
|
|
if (!Dqn_Str8_Eq(check, find, eq_case))
|
2023-07-04 14:04:53 +00:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (start_index > 0 && string_builder.string_size == 0) {
|
|
|
|
|
// User provided a hint in the string to start searching from, we
|
|
|
|
|
// need to add the string up to the hint. We only do this if there's
|
|
|
|
|
// a replacement action, otherwise we have a special case for no
|
|
|
|
|
// replacements, where the entire string gets copied.
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 slice = Dqn_Str8_Init(string.data, head);
|
2024-08-01 03:34:36 +00:00
|
|
|
|
Dqn_Str8Builder_AddRef(&string_builder, slice);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 range = Dqn_Str8_Slice(string, head, (tail - head));
|
2024-08-01 03:34:36 +00:00
|
|
|
|
Dqn_Str8Builder_AddRef(&string_builder, range);
|
|
|
|
|
Dqn_Str8Builder_AddRef(&string_builder, replace);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
head = tail + find.size;
|
|
|
|
|
tail += find.size - 1; // NOTE: -1 since the for loop will post increment us past the end of the find string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string_builder.string_size == 0) {
|
|
|
|
|
// NOTE: No replacement possible, so we just do a full-copy
|
2024-01-31 12:49:23 +00:00
|
|
|
|
result = Dqn_Str8_Copy(arena, string);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
} else {
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 remainder = Dqn_Str8_Init(string.data + head, string.size - head);
|
2024-08-01 03:34:36 +00:00
|
|
|
|
Dqn_Str8Builder_AddRef(&string_builder, remainder);
|
2024-01-31 12:49:23 +00:00
|
|
|
|
result = Dqn_Str8Builder_Build(&string_builder, arena);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-31 12:49:23 +00:00
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_ReplaceInsensitive(Dqn_Str8 string, Dqn_Str8 find, Dqn_Str8 replace, Dqn_usize start_index, Dqn_Arena *arena)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2024-01-31 12:49:23 +00:00
|
|
|
|
Dqn_Str8 result = Dqn_Str8_Replace(string, find, replace, start_index, arena, Dqn_Str8EqCase_Insensitive);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API void Dqn_Str8_Remove(Dqn_Str8 *string, Dqn_usize offset, Dqn_usize size)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2024-01-31 12:49:23 +00:00
|
|
|
|
if (!string || !Dqn_Str8_HasData(*string))
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
char *end = string->data + string->size;
|
|
|
|
|
char *dest = DQN_MIN(string->data + offset, end);
|
|
|
|
|
char *src = DQN_MIN(string->data + offset + size, end);
|
|
|
|
|
Dqn_usize bytes_to_move = end - src;
|
|
|
|
|
DQN_MEMMOVE(dest, src, bytes_to_move);
|
|
|
|
|
string->size -= bytes_to_move;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(__cplusplus)
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API bool operator==(Dqn_Str8 const &lhs, Dqn_Str8 const &rhs)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
|
bool result = Dqn_Str8_Eq(lhs, rhs, Dqn_Str8EqCase_Sensitive);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
DQN_API bool operator!=(Dqn_Str8 const &lhs, Dqn_Str8 const &rhs)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
|
|
|
|
bool result = !(lhs == rhs);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-01-31 12:49:23 +00:00
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_InitF(Dqn_Arena *arena, DQN_FMT_ATTRIB char const *fmt, ...)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
|
|
|
|
va_list va;
|
|
|
|
|
va_start(va, fmt);
|
2024-01-31 12:49:23 +00:00
|
|
|
|
Dqn_Str8 result = Dqn_Str8_InitFV(arena, fmt, va);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
va_end(va);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-31 12:49:23 +00:00
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_InitFV(Dqn_Arena *arena, DQN_FMT_ATTRIB char const *fmt, va_list args)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 result = {};
|
2023-07-04 14:04:53 +00:00
|
|
|
|
if (!fmt)
|
|
|
|
|
return result;
|
|
|
|
|
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_usize size = Dqn_CStr8_FVSize(fmt, args);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
if (size) {
|
2024-01-31 12:49:23 +00:00
|
|
|
|
result = Dqn_Str8_Alloc(arena, size, Dqn_ZeroMem_No);
|
|
|
|
|
if (Dqn_Str8_HasData(result))
|
|
|
|
|
DQN_VSNPRINTF(result.data, Dqn_Safe_SaturateCastISizeToInt(size + 1 /*null-terminator*/), fmt, args);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-31 12:49:23 +00:00
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_Alloc(Dqn_Arena *arena, Dqn_usize size, Dqn_ZeroMem zero_mem)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 result = {};
|
2024-01-31 12:49:23 +00:00
|
|
|
|
result.data = Dqn_Arena_NewArray(arena, char, size + 1, zero_mem);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
if (result.data)
|
|
|
|
|
result.size = size;
|
2024-08-01 03:34:36 +00:00
|
|
|
|
if (zero_mem == Dqn_ZeroMem_No)
|
|
|
|
|
result.data[result.size] = 0;
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-31 12:49:23 +00:00
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_CopyCString(Dqn_Arena *arena, char const *string, Dqn_usize size)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2023-10-24 13:11:48 +00:00
|
|
|
|
Dqn_Str8 result = {};
|
2023-07-04 14:04:53 +00:00
|
|
|
|
if (!string)
|
|
|
|
|
return result;
|
|
|
|
|
|
2024-01-31 12:49:23 +00:00
|
|
|
|
result = Dqn_Str8_Alloc(arena, size, Dqn_ZeroMem_No);
|
|
|
|
|
if (Dqn_Str8_HasData(result)) {
|
2023-07-04 14:04:53 +00:00
|
|
|
|
DQN_MEMCPY(result.data, string, size);
|
|
|
|
|
result.data[size] = 0;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-31 12:49:23 +00:00
|
|
|
|
DQN_API Dqn_Str8 Dqn_Str8_Copy(Dqn_Arena *arena, Dqn_Str8 string)
|
2023-07-04 14:04:53 +00:00
|
|
|
|
{
|
2024-01-31 12:49:23 +00:00
|
|
|
|
Dqn_Str8 result = Dqn_Str8_CopyCString(arena, string.data, string.size);
|
2023-07-04 14:04:53 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|