vn/code/core/core_string.h

190 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;
};
struct string16
{
s64 Count;
u16 *Data;
};
2023-06-21 16:59:36 +00:00
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);
2023-08-22 03:19:51 +00:00
inline string MakeString(u8 *Start, u8 *End);
2023-06-21 16:59:36 +00:00
inline string MakeString(char *CString);
2023-08-22 03:19:51 +00:00
#define StrLit(String) MakeString((u8 *)String, (s64)(ArrayCount(String) - 1))
inline string16 MakeString16(u16 *Data, s64 Count);
2023-06-21 16:59:36 +00:00
//- 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(arena *Arena, string String);
static string PushFormatVariadic(arena *Arena, char *Format, va_list Arguments);
static string PushFormat(arena *Arena, char *Format, ...);
static string PushCString(arena *Arena, char *String);
2023-06-21 16:59:36 +00:00
//- sixten: Conversion
static s64 ConvertStringToS64(string String);
static string ConvertS64ToString(arena *Arena, s64 Value);
static string StringFromCodepoint(arena *Arena, u32 Codepoint);
static r64 DoubleFromString(string String);
2023-06-21 16:59:36 +00:00
2023-07-19 15:09:41 +00:00
//- sixten: Replacing
static string RemoveAll(arena *Arena, string Text, char ToRemove);
2023-07-19 15:09:41 +00:00
2023-06-21 16:59:36 +00:00
//- sixten: "C Style" strings
static s64 StringLength(char *String);
2023-08-22 03:19:51 +00:00
static s64 StringLength16(u16 *String);
2023-06-21 16:59:36 +00:00
//~ sixten: String list
static void AppendString(string_list *List, string String, arena *Arena);
static string JoinStringList(string_list *List, arena *Arena);
2023-06-21 16:59:36 +00:00
//~ 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(u16 *Data, s64 Count);
2023-07-24 13:50:57 +00:00
static u32 EncodeUTF16Codepoint(u16 *Dest, u32 Codepoint);
2023-06-21 16:59:36 +00:00
static s64 UTF8FromCodepoint(u8 *Out, u32 Codepoint);
static string String8FromString16(arena *Arena, string16 String);
static string16 String16FromString8(arena *Arena, string String);
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(arena *Arena, range1_s64_list *List, range1_s64 Range);
static range1_s64_array Range1S64ArrayFromList(arena *Arena, range1_s64_list *List);
2023-07-19 15:09:41 +00:00
static s64 OffsetFromTextPoint(string Text, range1_s64_array Lines, text_point Point);
2023-06-21 16:59:36 +00:00
#endif //CORE_STRING_H