vn/code/core/core_string.h

184 lines
4.1 KiB
C
Raw Normal View History

2023-06-21 16:59:36 +00:00
/* date = June 19th 2023 9:15 pm */
#ifndef CORE_STRING_H
#define CORE_STRING_H
/////////////////////////////////////
//~ sixten: String types
struct string
{
s64 Count;
u8 *Data;
};
typedef string buffer;
struct string_node
{
string String;
string_node *Next;
string_node *Prev;
};
struct string_list
{
string_node *First;
string_node *Last;
s64 TotalCount;
};
2023-07-19 15:09:41 +00:00
/////////////////////////////////////
//~ sixten: String Chunk Types
struct string_chunk_list
{
s64 ChunkSize;
s64 ChunkCount;
s64 TotalCount;
string_node *First;
string_node *Last;
string_node *FirstFree;
string_node *LastFree;
};
2023-06-21 16:59:36 +00:00
2023-07-19 15:09:41 +00:00
/////////////////////////////////////
2023-06-21 16:59:36 +00:00
//~ sixten: Char funcitons
inline b32 IsWhitespace(char C);
inline b32 IsDigit(char C);
inline b32 IsLetter(char C);
2023-07-19 15:09:41 +00:00
/////////////////////////////////////
2023-06-21 16:59:36 +00:00
//~ sixten: String functions
//- sixten: Basic constructors
inline string MakeString(u8 *Data, s64 Count);
inline string MakeString(char *CString);
#define StrLit(String) MakeString((u8 *)String, ArrayCount(String) - 1)
//- sixten: Equality
static b32 AreEqual(string A, string B);
//- sixten: Substring
static string Substring(string String, range1_s64 Range);
static string Prefix(string String, s64 Count);
static string Suffix(string String, s64 Count);
//- sixten: Hashing
static u64 HashString(string String);
//- sixten: Searching
static s64 FirstIndexOf(string String, char Char);
static s64 LastIndexOf(string String, char Char);
static s64 FirstIndexOf(string String, string Sub);
static s64 LastIndexOf(string String, string Sub);
//- sixten: Allocation
static string PushString(memory_arena *Arena, string String);
static string PushFormatVariadic(memory_arena *Arena, char *Format, va_list Arguments);
static string PushFormat(memory_arena *Arena, char *Format, ...);
static string PushCString(memory_arena *Arena, char *String);
//- sixten: Conversion
static s64 ConvertStringToS64(string String);
static string ConvertS64ToString(memory_arena *Arena, s64 Value);
static string StringFromCodepoint(memory_arena *Arena, u32 Codepoint);
2023-07-19 15:09:41 +00:00
//- sixten: Replacing
static string RemoveAll(memory_arena *Arena, string Text, char ToRemove);
2023-06-21 16:59:36 +00:00
//- sixten: "C Style" strings
static s64 StringLength(char *String);
2023-07-24 13:50:57 +00:00
#if 0
2023-07-19 15:09:41 +00:00
/////////////////////////////////////
//~ sixten: String Chunk Functions
static string_chunk_list MakeStringChunkList(s64 ChunkSize);
static string JoinStringChunkList(memory_arena *Arena, string_chunk_list *List);
static void ReplaceRange(memory_arena *Arena, string_chunk_list *List, string Text, range1_s64 Range);
2023-07-24 13:50:57 +00:00
#endif
2023-07-19 15:09:41 +00:00
2023-06-21 16:59:36 +00:00
//~ sixten: String list
static void AppendString(string_list *List, string String, memory_arena *Arena);
static string JoinStringList(string_list *List, memory_arena *Arena);
//~ sixten: Unicode
2023-07-24 13:50:57 +00:00
struct string_decode
2023-06-21 16:59:36 +00:00
{
u32 Codepoint;
2023-07-24 13:50:57 +00:00
s32 Size;
2023-06-21 16:59:36 +00:00
};
2023-07-24 13:50:57 +00:00
static string_decode DecodeUTF8Codepoint(u8 *Data, s64 Count);
static u32 EncodeUTF8Codepoint(u8 *Dest, u32 Codepoint);
static string_decode DecodeUTF16Codepoint(u8 *Data, s64 Count);
static u32 EncodeUTF16Codepoint(u16 *Dest, u32 Codepoint);
2023-06-21 16:59:36 +00:00
static s64 UTF8FromCodepoint(u8 *Out, u32 Codepoint);
2023-07-19 15:09:41 +00:00
//~ sixten: Text point
struct text_point
{
s64 Line;
s64 Column;
};
struct text_range
{
text_point Min;
text_point Max;
};
static text_point TextPointFromOffset(string String, s64 Offset);
static s64 OffsetFromTextPoint(string String, text_point Point);
static text_range TextRange(text_point A, text_point B);
////////////////////////////////
//~ sixten: 1D Interval List & Array Types
struct range1_s64_node
{
range1_s64 Range;
range1_s64_node *Next;
};
struct range1_s64_list
{
s64 Count;
range1_s64_node *First;
range1_s64_node *Last;
};
struct range1_s64_array
{
s64 Count;
range1_s64 *Ranges;
};
////////////////////////////////
//~ sixten: 1D Interval List & Array Functions
static void Range1S64ListPush(memory_arena *Arena, range1_s64_list *List, range1_s64 Range);
static range1_s64_array Range1S64ArrayFromList(memory_arena *Arena, range1_s64_list *List);
static s64 OffsetFromTextPoint(string Text, range1_s64_array Lines, text_point Point);
2023-06-21 16:59:36 +00:00
#endif //CORE_STRING_H