Compare commits
8
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
075b049894 | ||
|
|
e34cadf6e7 | ||
|
|
47082f3de2 | ||
|
|
d8a5e176e2 | ||
|
|
10186202ad | ||
|
|
7639298b9b | ||
|
|
02638b20b9 | ||
|
|
a7635226ac |
@@ -2,4 +2,4 @@
|
||||
CFLAGS=${CFLAGS:-"-fsanitize=address -ggdb"}
|
||||
CC=${CC:-gcc}
|
||||
set -x
|
||||
${CC} -DDEBUG -pipe -Wall -Wextra -Wno-unused-parameter -Wno-unused-variable -std=gnu2x $CFLAGS src/*c
|
||||
${CC} -DDEBUG -pipe -Wall -Wextra -Wno-unused-parameter -Wno-unused-variable -std=gnu2x $CFLAGS src/*c src/base/*c
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "arena.h"
|
||||
#include "base_core.h"
|
||||
#include "core.h"
|
||||
#include "platform.h"
|
||||
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
#pragma once
|
||||
#include "base_core.h"
|
||||
#include "core.h"
|
||||
|
||||
|
||||
typedef U32 ArenaFlags;
|
||||
typedef struct ArenaOptions ArenaOptions;
|
||||
enum {
|
||||
ArenaFlag_NoDecommit, // arena_ temp_end/reset/reset_to doesn't decommit memory
|
||||
ArenaFlag_NoDecommit = 1, // operations which shrinks the arena does not decommit memory.
|
||||
// (arena_trim() can still decommit when called explicitly)
|
||||
};
|
||||
struct ArenaOptions {
|
||||
U32 size;
|
||||
U32 commit_size;
|
||||
ArenaFlags flags; // No flags defined yet
|
||||
ArenaFlags flags;
|
||||
};
|
||||
|
||||
typedef struct MemoryBlock MemoryBlock;
|
||||
@@ -410,3 +410,171 @@ static INLINE void allocator_free_all(Allocator *allocator)
|
||||
#define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
|
||||
((void)(addr), (void)(size))
|
||||
#endif
|
||||
|
||||
|
||||
// linked lists
|
||||
|
||||
/*
|
||||
SList - singly linked lists
|
||||
"list" struct must have a "*first" and "**last" member to entrys
|
||||
"first"/"last" structs must have a "*next" member
|
||||
*/
|
||||
|
||||
/**
|
||||
* Initialize a list
|
||||
*
|
||||
* @param list struct pointer with a *first and a **last member to an entry struct
|
||||
*/
|
||||
#define SListInit(list) \
|
||||
do { \
|
||||
(list)->first = NULL; \
|
||||
(list)->last = &(list)->first; \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* Adds entry to the head of a list
|
||||
*
|
||||
* @param list struct pointer with a *first and a **last member to an entry struct
|
||||
* @param entry struct pointer with a *next member to an entry struct
|
||||
*/
|
||||
#define SListAddFirst(list, entry) \
|
||||
do { \
|
||||
(entry)->next = (list)->first; \
|
||||
if ((entry)->next == NULL) { \
|
||||
(list)->last = &(entry)->next; \
|
||||
} \
|
||||
(list)->first = (entry); \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* Adds entry to the tail of a list
|
||||
*
|
||||
* @param list struct pointer with a *first and a **last member to an entry struct
|
||||
* @param entry struct pointer with a *next member to an entry struct
|
||||
*/
|
||||
#define SListAddLast(list, entry) \
|
||||
do { \
|
||||
(entry)->next = NULL; \
|
||||
*(list)->last = (entry); \
|
||||
(list)->last = &(entry)->next; \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* Remove head from list, list MUST not be empty
|
||||
*
|
||||
* @param entry struct pointer with a *next member to an entry struct
|
||||
*/
|
||||
#define SListRemoveFirst(list) \
|
||||
do { \
|
||||
(list)->first = (list)->first->next; \
|
||||
if ((list)->first == NULL) { \
|
||||
(list)->last = &(list)->first; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define SListIsEmpty(list) ((list)->first == NULL)
|
||||
#define SListResetNode(node) ((node)->next = NULL)
|
||||
|
||||
/*
|
||||
DList - doubly linked lists
|
||||
"list" struct must have a "*first" and "*last" member to entrys
|
||||
"first"/"last" structs must have a "*next" and a "*prev" member
|
||||
*/
|
||||
|
||||
/**
|
||||
* Initialize a list
|
||||
*
|
||||
* @param list struct pointer with a *first and a *last member to an entry struct
|
||||
*/
|
||||
#define DListInit(list) \
|
||||
do { \
|
||||
(list)->first = NULL; \
|
||||
(list)->last = NULL; \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* Adds entry to the head of a list
|
||||
*
|
||||
* @param list struct pointer with a *first and a *last member to an entry struct
|
||||
* @param entry struct pointer with a *next and *prev member to an entry struct
|
||||
*/
|
||||
#define DListAddFirst(list, entry) \
|
||||
do { \
|
||||
(entry)->next = (list)->first; \
|
||||
(entry)->prev = NULL; \
|
||||
if ((list)->first != NULL) { \
|
||||
(list)->first->prev = (entry); \
|
||||
} else { \
|
||||
(list)->last = (entry); \
|
||||
} \
|
||||
(list)->first = (entry); \
|
||||
} while (0)
|
||||
/**
|
||||
* Adds entry to the tail of a list
|
||||
*
|
||||
* @param list struct pointer with a *first and a *last member to an entry struct
|
||||
* @param entry struct pointer with a *next and *prev member to an entry struct
|
||||
*/
|
||||
#define DListAddLast(list, entry) \
|
||||
do { \
|
||||
(entry)->prev = (list)->last; \
|
||||
(entry)->next = NULL; \
|
||||
if ((list)->last != NULL) { \
|
||||
(list)->last->next = (entry); \
|
||||
} else { \
|
||||
(list)->first = (entry); \
|
||||
} \
|
||||
(list)->last = (entry); \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* Remove first entry from list, list MUST not be empty
|
||||
*
|
||||
* @param entry struct pointer with a *next and *prev member to an entry struct
|
||||
*/
|
||||
#define DListRemoveFirst(list) \
|
||||
do { \
|
||||
(list)->first = (list)->first->next; \
|
||||
if ((list)->first != NULL) { \
|
||||
(list)->first->prev = NULL; \
|
||||
} else { \
|
||||
(list)->last = NULL; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* Remove last entry from list, list MUST not be empty
|
||||
*
|
||||
* @param entry struct pointer with a *next and *prev member to an entry struct
|
||||
*/
|
||||
#define DListRemoveLast(list) \
|
||||
do { \
|
||||
(list)->last = (list)->last->prev; \
|
||||
if ((list)->last != NULL) { \
|
||||
(list)->last->next = NULL; \
|
||||
} else { \
|
||||
(list)->first = NULL; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* Remove last entry from list, entry must be in the list
|
||||
*
|
||||
* @param entry struct pointer with a *next and *prev member to an entry struct
|
||||
*/
|
||||
#define DListRemove(list, entry) \
|
||||
do { \
|
||||
if ((entry)->next != NULL) { \
|
||||
(entry)->next->prev = (entry)->prev; \
|
||||
} else { \
|
||||
(list)->last = (entry)->prev; \
|
||||
} \
|
||||
if ((entry)->prev != NULL) { \
|
||||
(entry)->prev->next = (entry)->next; \
|
||||
} else { \
|
||||
(list)->first = (entry)->next; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define DListIsEmpty(list) ((list)->first == NULL)
|
||||
#define DListResetNode(node) ((node)->next = (node)->prev = NULL)
|
||||
@@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
#include "base_core.h"
|
||||
#include "core.h"
|
||||
|
||||
// Mini Arena for arena allocations from a supplied buffer
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "base_core.h"
|
||||
#include "core.h"
|
||||
#include "platform.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
#include <stdlib.h>
|
||||
#include "base_core.h"
|
||||
#include "core.h"
|
||||
|
||||
void *platform_malloc(size_t size);
|
||||
void platform_free(void *mem);
|
||||
@@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
#include "base_core.h"
|
||||
#include "core.h"
|
||||
|
||||
typedef union APoolElement APoolElement;
|
||||
union APoolElement {
|
||||
@@ -93,36 +93,50 @@ S64 str_index(Str haystack, Str needle)
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static INLINE S16 char_hex_to_binary(U8 c)
|
||||
{
|
||||
if (c >='0' && c <='9')
|
||||
return c - '0';
|
||||
else if (c >= 'A' && c <= 'F')
|
||||
return c - 'A' + 10;
|
||||
else if (c >= 'a' && c <= 'f')
|
||||
return c - 'a' + 10;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static const char hex_chars[] = "0123456789ABCDEF";
|
||||
|
||||
Str str_hex_encode(Str source, Str destination)
|
||||
Str str_hex_encode(Str binary_data, Str hex_result)
|
||||
{
|
||||
Assert(destination.len >= source.len * 2);
|
||||
U32 hex_len = source.len * 2;
|
||||
for (U32 i = 0, t = 0; i < source.len; ++i,t+=2) {
|
||||
Assert(hex_result.len >= binary_data.len * 2);
|
||||
U32 hex_len = binary_data.len * 2;
|
||||
for (U32 i = 0, t = 0; i < binary_data.len; ++i,t+=2) {
|
||||
U8 high_nibble;
|
||||
U8 low_nibble;
|
||||
|
||||
high_nibble = (source.str[i] & 0xF0) >> 4 ;
|
||||
low_nibble = source.str[i] & 0x0F ;
|
||||
high_nibble = (binary_data.str[i] & 0xF0) >> 4 ;
|
||||
low_nibble = binary_data.str[i] & 0x0F ;
|
||||
|
||||
destination.str[t] = hex_chars[high_nibble];
|
||||
destination.str[t+1] = hex_chars[low_nibble];
|
||||
hex_result.str[t] = hex_chars[high_nibble];
|
||||
hex_result.str[t+1] = hex_chars[low_nibble];
|
||||
}
|
||||
|
||||
destination.len = hex_len;
|
||||
return destination;
|
||||
hex_result.len = hex_len;
|
||||
return hex_result;
|
||||
}
|
||||
|
||||
Str str_hex_decode(Str source, Str destination)
|
||||
Str str_hex_decode(Str hex_str, Str result)
|
||||
{
|
||||
Assert(destination.len >= source.len/2);
|
||||
Assert(result.len >= hex_str.len/2);
|
||||
U32 dest_pos = 0;
|
||||
U32 source_pos = 0;
|
||||
|
||||
while (source_pos + 1 < source.len) {
|
||||
while (source_pos + 1 < hex_str.len) {
|
||||
|
||||
if (char_is_ascii_whitespace(source.str[source_pos])) {
|
||||
if (char_is_ascii_whitespace(hex_str.str[source_pos])) {
|
||||
source_pos++;
|
||||
continue;
|
||||
}
|
||||
@@ -130,20 +144,20 @@ Str str_hex_decode(Str source, Str destination)
|
||||
S16 high_nibble;
|
||||
S16 low_nibble;
|
||||
|
||||
high_nibble = char_hex_to_binary(source.str[source_pos]);
|
||||
low_nibble = char_hex_to_binary(source.str[source_pos + 1]);
|
||||
high_nibble = char_hex_to_binary(hex_str.str[source_pos]);
|
||||
low_nibble = char_hex_to_binary(hex_str.str[source_pos + 1]);
|
||||
if (high_nibble == -1 || low_nibble == -1) {
|
||||
destination.len = 0;
|
||||
return destination;
|
||||
result.len = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
destination.str[dest_pos] = (high_nibble << 4 ) | low_nibble;
|
||||
result.str[dest_pos] = (high_nibble << 4 ) | low_nibble;
|
||||
source_pos += 2;
|
||||
dest_pos++;
|
||||
}
|
||||
|
||||
destination.len = dest_pos;
|
||||
return destination;
|
||||
result.len = dest_pos;
|
||||
return result;
|
||||
}
|
||||
// split @str on any byte in @separators, place split strings in @vec
|
||||
void str_split(Str str, Str separators, StrVec *vec)
|
||||
@@ -210,6 +224,105 @@ Str str_join(Allocator *allocator, StrVec strings, Str separator)
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Each entry in utf8_lead[] is an 8-bit descriptor:
|
||||
*
|
||||
* 7 6 5 4 3 2 1 0
|
||||
* +---------+-----+
|
||||
* | range | len |
|
||||
* +---------+-----+
|
||||
*
|
||||
* len:
|
||||
* Number of bytes in the UTF-8 sequence.
|
||||
*
|
||||
* 0 = byte cannot start a valid UTF-8 sequence
|
||||
* 1 = ASCII
|
||||
* 2 = two-byte UTF-8 sequence
|
||||
* 3 = three-byte UTF-8 sequence
|
||||
* 4 = four-byte UTF-8 sequence
|
||||
*
|
||||
* range:
|
||||
* Selects the legal range for byte 1, the first continuation byte.
|
||||
*
|
||||
* This range is enough to encode all of UTF-8's special boundary rules:
|
||||
*
|
||||
* E0 A0..BF prevents overlong 3-byte encodings
|
||||
* ED 80..9F excludes UTF-16 surrogate code points
|
||||
* F0 90..BF prevents overlong 4-byte encodings
|
||||
* F4 80..8F limits Unicode to U+10FFFF
|
||||
*
|
||||
* All continuation bytes after byte 1 simply use the normal 80..BF range.
|
||||
*/
|
||||
|
||||
#define UTF8_LENGTH_BITS 3u
|
||||
#define UTF8_LENGTH_MASK ((1u << UTF8_LENGTH_BITS) - 1u)
|
||||
|
||||
#define UTF8_DESC(length, range) \
|
||||
((U8)((length) | ((U8)(range) << UTF8_LENGTH_BITS)))
|
||||
|
||||
#define UTF8_DESC_LENGTH(desc) \
|
||||
((U8)((desc) & UTF8_LENGTH_MASK))
|
||||
|
||||
#define UTF8_DESC_RANGE(desc) \
|
||||
((U8)((desc) >> UTF8_LENGTH_BITS))
|
||||
|
||||
|
||||
/*
|
||||
* Classes of allowed values for the first continuation byte.
|
||||
*/
|
||||
enum Utf8RangeClass {
|
||||
/*
|
||||
* No continuation byte exists.
|
||||
*
|
||||
* Used only by ASCII descriptors.
|
||||
*/
|
||||
UTF8_RANGE_NONE = 0,
|
||||
|
||||
/*
|
||||
* Normal continuation-byte range.
|
||||
*/
|
||||
UTF8_RANGE_80_BF,
|
||||
|
||||
/*
|
||||
* E0 xx xx:
|
||||
*
|
||||
* E0 80..9F would encode values below U+0800 and therefore be
|
||||
* non-canonical overlong UTF-8.
|
||||
*/
|
||||
UTF8_RANGE_A0_BF,
|
||||
|
||||
/*
|
||||
* ED xx xx:
|
||||
*
|
||||
* ED A0..BF would encode U+D800..U+DFFF, which are UTF-16 surrogate
|
||||
* code points and are not Unicode scalar values.
|
||||
*/
|
||||
UTF8_RANGE_80_9F,
|
||||
|
||||
/*
|
||||
* F0 xx xx xx:
|
||||
*
|
||||
* F0 80..8F would encode values below U+10000 and therefore be
|
||||
* non-canonical overlong UTF-8.
|
||||
*/
|
||||
UTF8_RANGE_90_BF,
|
||||
|
||||
/*
|
||||
* F4 xx xx xx:
|
||||
*
|
||||
* F4 90..BF would encode values above Unicode's maximum scalar value,
|
||||
* U+10FFFF.
|
||||
*/
|
||||
UTF8_RANGE_80_8F,
|
||||
};
|
||||
|
||||
|
||||
typedef struct {
|
||||
U8 lo;
|
||||
U8 hi;
|
||||
} Utf8Range;
|
||||
|
||||
/*
|
||||
* Allowed range of the first continuation byte for each range class.
|
||||
*
|
||||
@@ -225,6 +338,36 @@ static const Utf8Range utf8_ranges[] = {
|
||||
};
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Each entry in utf8_lead[] is an 8-bit descriptor:
|
||||
*
|
||||
* 7 6 5 4 3 2 1 0
|
||||
* +---------+-----+
|
||||
* | range | len |
|
||||
* +---------+-----+
|
||||
*
|
||||
* len:
|
||||
* Number of bytes in the UTF-8 sequence.
|
||||
*
|
||||
* 0 = byte cannot start a valid UTF-8 sequence
|
||||
* 1 = ASCII
|
||||
* 2 = two-byte UTF-8 sequence
|
||||
* 3 = three-byte UTF-8 sequence
|
||||
* 4 = four-byte UTF-8 sequence
|
||||
*
|
||||
* range:
|
||||
* Selects the legal range for byte 1, the first continuation byte.
|
||||
*
|
||||
* This range is enough to encode all of UTF-8's special boundary rules:
|
||||
*
|
||||
* E0 A0..BF prevents overlong 3-byte encodings
|
||||
* ED 80..9F excludes UTF-16 surrogate code points
|
||||
* F0 90..BF prevents overlong 4-byte encodings
|
||||
* F4 80..8F limits Unicode to U+10FFFF
|
||||
*
|
||||
* All continuation bytes after byte 1 simply use the normal 80..BF range.
|
||||
*/
|
||||
/*
|
||||
* Classification of every possible first byte.
|
||||
*
|
||||
@@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
#include "base_core.h"
|
||||
#include "core.h"
|
||||
|
||||
typedef struct Str Str;
|
||||
struct Str {
|
||||
@@ -9,6 +9,7 @@ struct Str {
|
||||
|
||||
Str str_empty(void);
|
||||
Str str_slice(Str str, U32 start, U32 len);
|
||||
// returns -1 if not found, or index start of first needle
|
||||
S64 str_index(Str haystack, Str needle);
|
||||
|
||||
Str str_concat(Allocator *alloc, Str first, Str second);
|
||||
@@ -65,6 +66,17 @@ static INLINE S64 str_find_byte(Str str, U8 byte)
|
||||
return -1;
|
||||
}
|
||||
|
||||
static INLINE Str str_replace_byte(Str str, U8 orig_byte, U8 new_byte)
|
||||
{
|
||||
for (U32 i = 0; i < str.len; i++) {
|
||||
if (str.str[i] == orig_byte) {
|
||||
str.str[i] = new_byte;
|
||||
}
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
static INLINE bool str_starts_with(Str str, Str prefix)
|
||||
{
|
||||
if (prefix.len > str.len) {
|
||||
@@ -91,28 +103,28 @@ static INLINE Str str_skip(Str str, U32 len)
|
||||
return new_str;
|
||||
}
|
||||
|
||||
// Destination must be at least source.len * 2. Returns destination.
|
||||
Str str_hex_encode(Str source, Str destination);
|
||||
// Destination must be at least source.len / 2. Returns destination.
|
||||
Str str_hex_decode(Str source, Str destination);
|
||||
// hex_result must be at least source.len * 2. Returns hex_result.
|
||||
Str str_hex_encode(Str binary_data, Str hex_result);
|
||||
// result must be at least source.len / 2. Returns result.
|
||||
Str str_hex_decode(Str hex_str, Str result);
|
||||
|
||||
static INLINE Str str_hex_encode_alloc(Allocator *allocator, Str str)
|
||||
static INLINE Str str_hex_encode_alloc(Allocator *allocator, Str binary_data)
|
||||
{
|
||||
Assert(str.len <= U32_Max / 2);
|
||||
U8 hex_len = str.len * 2;
|
||||
Assert(binary_data.len <= U32_Max / 2);
|
||||
U8 hex_len = binary_data.len * 2;
|
||||
U8 *result = allocator_push_array(allocator, U8, hex_len);
|
||||
Str destination = str_create(result, hex_len);
|
||||
|
||||
return str_hex_encode(str, destination);
|
||||
return str_hex_encode(binary_data, destination);
|
||||
}
|
||||
|
||||
static INLINE Str str_hex_decode_alloc(Allocator *allocator, Str str)
|
||||
static INLINE Str str_hex_decode_alloc(Allocator *allocator, Str hex_str)
|
||||
{
|
||||
U8 binary_len = str.len / 2;
|
||||
U8 binary_len = hex_str.len / 2;
|
||||
U8 *result = allocator_push_array(allocator, U8, binary_len);
|
||||
Str destination = str_create(result, binary_len);
|
||||
|
||||
return str_hex_decode(str, destination);
|
||||
return str_hex_decode(hex_str, destination);
|
||||
}
|
||||
// String Vector
|
||||
typedef struct StrVec StrVec;
|
||||
@@ -147,17 +159,6 @@ static INLINE bool char_is_digit(U8 byte)
|
||||
return byte >= '0' && byte <= '9';
|
||||
}
|
||||
|
||||
static INLINE S16 char_hex_to_binary(U8 c)
|
||||
{
|
||||
if (c >='0' && c <='9')
|
||||
return c - '0';
|
||||
else if (c >= 'A' && c <= 'F')
|
||||
return c - 'A' + 10;
|
||||
else if (c >= 'a' && c <= 'f')
|
||||
return c - 'a' + 10;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
// UTF-8/Unicode
|
||||
|
||||
@@ -197,111 +198,12 @@ typedef struct {
|
||||
/*
|
||||
* True only when rune was decoded from valid UTF-8.
|
||||
*
|
||||
* This is required because U+FFFD itself may legitimately occur in
|
||||
* valid input, so rune == RUNE_ERROR does not imply an error.
|
||||
* U+FFFD itself may legitimately occur in valid
|
||||
* input, so rune == RUNE_ERROR does not imply an error.
|
||||
*/
|
||||
bool valid;
|
||||
} RuneDecode;
|
||||
|
||||
|
||||
/*
|
||||
* Each entry in utf8_lead[] is an 8-bit descriptor:
|
||||
*
|
||||
* 7 6 5 4 3 2 1 0
|
||||
* +---------+-----+
|
||||
* | range | len |
|
||||
* +---------+-----+
|
||||
*
|
||||
* len:
|
||||
* Number of bytes in the UTF-8 sequence.
|
||||
*
|
||||
* 0 = byte cannot start a valid UTF-8 sequence
|
||||
* 1 = ASCII
|
||||
* 2 = two-byte UTF-8 sequence
|
||||
* 3 = three-byte UTF-8 sequence
|
||||
* 4 = four-byte UTF-8 sequence
|
||||
*
|
||||
* range:
|
||||
* Selects the legal range for byte 1, the first continuation byte.
|
||||
*
|
||||
* This range is enough to encode all of UTF-8's special boundary rules:
|
||||
*
|
||||
* E0 A0..BF prevents overlong 3-byte encodings
|
||||
* ED 80..9F excludes UTF-16 surrogate code points
|
||||
* F0 90..BF prevents overlong 4-byte encodings
|
||||
* F4 80..8F limits Unicode to U+10FFFF
|
||||
*
|
||||
* All continuation bytes after byte 1 simply use the normal 80..BF range.
|
||||
*/
|
||||
|
||||
#define UTF8_LENGTH_BITS 3u
|
||||
#define UTF8_LENGTH_MASK ((1u << UTF8_LENGTH_BITS) - 1u)
|
||||
|
||||
#define UTF8_DESC(length, range) \
|
||||
((U8)((length) | ((U8)(range) << UTF8_LENGTH_BITS)))
|
||||
|
||||
#define UTF8_DESC_LENGTH(desc) \
|
||||
((U8)((desc) & UTF8_LENGTH_MASK))
|
||||
|
||||
#define UTF8_DESC_RANGE(desc) \
|
||||
((U8)((desc) >> UTF8_LENGTH_BITS))
|
||||
|
||||
|
||||
/*
|
||||
* Classes of allowed values for the first continuation byte.
|
||||
*/
|
||||
enum Utf8RangeClass {
|
||||
/*
|
||||
* No continuation byte exists.
|
||||
*
|
||||
* Used only by ASCII descriptors.
|
||||
*/
|
||||
UTF8_RANGE_NONE = 0,
|
||||
|
||||
/*
|
||||
* Normal continuation-byte range.
|
||||
*/
|
||||
UTF8_RANGE_80_BF,
|
||||
|
||||
/*
|
||||
* E0 xx xx:
|
||||
*
|
||||
* E0 80..9F would encode values below U+0800 and therefore be
|
||||
* non-canonical overlong UTF-8.
|
||||
*/
|
||||
UTF8_RANGE_A0_BF,
|
||||
|
||||
/*
|
||||
* ED xx xx:
|
||||
*
|
||||
* ED A0..BF would encode U+D800..U+DFFF, which are UTF-16 surrogate
|
||||
* code points and are not Unicode scalar values.
|
||||
*/
|
||||
UTF8_RANGE_80_9F,
|
||||
|
||||
/*
|
||||
* F0 xx xx xx:
|
||||
*
|
||||
* F0 80..8F would encode values below U+10000 and therefore be
|
||||
* non-canonical overlong UTF-8.
|
||||
*/
|
||||
UTF8_RANGE_90_BF,
|
||||
|
||||
/*
|
||||
* F4 xx xx xx:
|
||||
*
|
||||
* F4 90..BF would encode values above Unicode's maximum scalar value,
|
||||
* U+10FFFF.
|
||||
*/
|
||||
UTF8_RANGE_80_8F,
|
||||
};
|
||||
|
||||
|
||||
typedef struct {
|
||||
U8 lo;
|
||||
U8 hi;
|
||||
} Utf8Range;
|
||||
|
||||
// Helper:
|
||||
RuneDecode _str_utf8_decode_full(Str str, U32 offset);
|
||||
/*
|
||||
+119
-6
@@ -1,9 +1,9 @@
|
||||
#include "base_core.h"
|
||||
#include "arena.h"
|
||||
#include "mini_arena.h"
|
||||
#include "platform.h"
|
||||
#include "pool.h"
|
||||
#include "string.h"
|
||||
#include "base/core.h"
|
||||
#include "base/arena.h"
|
||||
#include "base/marena.h"
|
||||
#include "base/platform.h"
|
||||
#include "base/pool.h"
|
||||
#include "base/string.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -515,6 +515,8 @@ void print_runes(Str str)
|
||||
|
||||
void string_test_utf8(void)
|
||||
{
|
||||
TRACEF("\n");
|
||||
|
||||
Str str1 = S("Test 1ø2 ☺");
|
||||
|
||||
TRACEF("str1:\n");
|
||||
@@ -537,6 +539,8 @@ void string_test_utf8(void)
|
||||
|
||||
void string_test_hex(void)
|
||||
{
|
||||
TRACEF("\n");
|
||||
|
||||
MArena arena;
|
||||
U8 data[4096];
|
||||
marena_init(&arena, data, sizeof data);
|
||||
@@ -557,8 +561,115 @@ void string_test_hex(void)
|
||||
|
||||
Str str2_decoded = str_hex_decode_alloc(&allocator, str2_encoded);
|
||||
printf("str2_decoded: '%.*s'\n", str2_decoded.len, str2_decoded.str);
|
||||
}
|
||||
|
||||
typedef struct StrSLNode StrSLNode;
|
||||
struct StrSLNode {
|
||||
Str str;
|
||||
StrSLNode *next;
|
||||
};
|
||||
|
||||
typedef struct StrSList StrSList;
|
||||
struct StrSList {
|
||||
StrSLNode *first;
|
||||
StrSLNode **last;
|
||||
};
|
||||
|
||||
void string_test_slist(void)
|
||||
{
|
||||
TRACEF("\n");
|
||||
|
||||
Str str1 = S("one");
|
||||
Str str2 = S("two");
|
||||
Str str3 = S("three");
|
||||
StrSList strlist = {};
|
||||
StrSLNode n1 = {str1, NULL};
|
||||
StrSLNode n2 = {str2, NULL};
|
||||
StrSLNode n3 = {str3, NULL};
|
||||
|
||||
SListInit(&strlist);
|
||||
SListAddFirst(&strlist, &n1);
|
||||
SListAddFirst(&strlist, &n2);
|
||||
SListAddFirst(&strlist, &n3);
|
||||
TRACEF("List test first\n");
|
||||
|
||||
for (StrSLNode *n = strlist.first; n != NULL; n = n->next) {
|
||||
printf("Node: %.*s\n", n->str.len, n->str.str);
|
||||
}
|
||||
TRACEF("List test first done\n");
|
||||
SListInit(&strlist);
|
||||
SListAddLast(&strlist, &n1);
|
||||
SListAddLast(&strlist, &n2);
|
||||
SListAddLast(&strlist, &n3);
|
||||
SListRemoveFirst(&strlist);
|
||||
TRACEF("List test last\n");
|
||||
|
||||
for (StrSLNode *n = strlist.first; n != NULL; n = n->next) {
|
||||
printf("Node: %.*s\n", n->str.len, n->str.str);
|
||||
}
|
||||
TRACEF("List test last done\n");
|
||||
|
||||
}
|
||||
typedef struct StrDLNode StrDLNode;
|
||||
struct StrDLNode {
|
||||
Str str;
|
||||
StrDLNode *next;
|
||||
StrDLNode *prev;
|
||||
};
|
||||
|
||||
typedef struct StrDList StrDList;
|
||||
struct StrDList {
|
||||
StrDLNode *first;
|
||||
StrDLNode *last;
|
||||
};
|
||||
|
||||
void string_test_dlist(void)
|
||||
{
|
||||
TRACEF("\n");
|
||||
|
||||
Str str1 = S("one");
|
||||
Str str2 = S("two");
|
||||
Str str3 = S("three");
|
||||
Str str4 = S("four");
|
||||
StrDList strlist = {};
|
||||
StrDLNode n1 = {str1, NULL, NULL};
|
||||
StrDLNode n2 = {str2, NULL, NULL};
|
||||
StrDLNode n3 = {str3, NULL, NULL};
|
||||
StrDLNode n4 = {str4, NULL, NULL};
|
||||
|
||||
DListInit(&strlist);
|
||||
DListAddFirst(&strlist, &n1);
|
||||
DListAddFirst(&strlist, &n2);
|
||||
DListAddFirst(&strlist, &n3);
|
||||
TRACEF("List test first\n");
|
||||
|
||||
for (StrDLNode *n = strlist.first; n != NULL; n = n->next) {
|
||||
printf("Node: %.*s\n", n->str.len, n->str.str);
|
||||
}
|
||||
TRACEF("List test first done\n");
|
||||
|
||||
DListInit(&strlist);
|
||||
DListAddLast(&strlist, &n2);
|
||||
DListAddLast(&strlist, &n3);
|
||||
DListAddLast(&strlist, &n4);
|
||||
DListAddFirst(&strlist, &n1);
|
||||
|
||||
TRACEF("List test full\n");
|
||||
|
||||
for (StrDLNode *n = strlist.first; n != NULL; n = n->next) {
|
||||
printf("Node: %.*s\n", n->str.len, n->str.str);
|
||||
}
|
||||
TRACEF("List test full done\n");
|
||||
|
||||
DListRemove(&strlist, &n1);
|
||||
DListRemoveFirst(&strlist);
|
||||
DListRemoveLast(&strlist);
|
||||
TRACEF("List test tail removed\n");
|
||||
|
||||
for (StrDLNode *n = strlist.first; n != NULL; n = n->next) {
|
||||
printf("Node: %.*s\n", n->str.len, n->str.str);
|
||||
}
|
||||
TRACEF("List test tail removed done\n");
|
||||
|
||||
}
|
||||
|
||||
@@ -580,6 +691,8 @@ int main(int argc, char *argv[])
|
||||
string_test_join();
|
||||
string_test_utf8();
|
||||
string_test_hex();
|
||||
string_test_slist();
|
||||
string_test_dlist();
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user