add str_replace_byte
This commit is contained in:
+35
-21
@@ -93,36 +93,50 @@ S64 str_index(Str haystack, Str needle)
|
|||||||
return -1;
|
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";
|
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);
|
Assert(hex_result.len >= binary_data.len * 2);
|
||||||
U32 hex_len = source.len * 2;
|
U32 hex_len = binary_data.len * 2;
|
||||||
for (U32 i = 0, t = 0; i < source.len; ++i,t+=2) {
|
for (U32 i = 0, t = 0; i < binary_data.len; ++i,t+=2) {
|
||||||
U8 high_nibble;
|
U8 high_nibble;
|
||||||
U8 low_nibble;
|
U8 low_nibble;
|
||||||
|
|
||||||
high_nibble = (source.str[i] & 0xF0) >> 4 ;
|
high_nibble = (binary_data.str[i] & 0xF0) >> 4 ;
|
||||||
low_nibble = source.str[i] & 0x0F ;
|
low_nibble = binary_data.str[i] & 0x0F ;
|
||||||
|
|
||||||
destination.str[t] = hex_chars[high_nibble];
|
hex_result.str[t] = hex_chars[high_nibble];
|
||||||
destination.str[t+1] = hex_chars[low_nibble];
|
hex_result.str[t+1] = hex_chars[low_nibble];
|
||||||
}
|
}
|
||||||
|
|
||||||
destination.len = hex_len;
|
hex_result.len = hex_len;
|
||||||
return destination;
|
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 dest_pos = 0;
|
||||||
U32 source_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++;
|
source_pos++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -130,20 +144,20 @@ Str str_hex_decode(Str source, Str destination)
|
|||||||
S16 high_nibble;
|
S16 high_nibble;
|
||||||
S16 low_nibble;
|
S16 low_nibble;
|
||||||
|
|
||||||
high_nibble = char_hex_to_binary(source.str[source_pos]);
|
high_nibble = char_hex_to_binary(hex_str.str[source_pos]);
|
||||||
low_nibble = char_hex_to_binary(source.str[source_pos + 1]);
|
low_nibble = char_hex_to_binary(hex_str.str[source_pos + 1]);
|
||||||
if (high_nibble == -1 || low_nibble == -1) {
|
if (high_nibble == -1 || low_nibble == -1) {
|
||||||
destination.len = 0;
|
result.len = 0;
|
||||||
return destination;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
destination.str[dest_pos] = (high_nibble << 4 ) | low_nibble;
|
result.str[dest_pos] = (high_nibble << 4 ) | low_nibble;
|
||||||
source_pos += 2;
|
source_pos += 2;
|
||||||
dest_pos++;
|
dest_pos++;
|
||||||
}
|
}
|
||||||
|
|
||||||
destination.len = dest_pos;
|
result.len = dest_pos;
|
||||||
return destination;
|
return result;
|
||||||
}
|
}
|
||||||
// split @str on any byte in @separators, place split strings in @vec
|
// split @str on any byte in @separators, place split strings in @vec
|
||||||
void str_split(Str str, Str separators, StrVec *vec)
|
void str_split(Str str, Str separators, StrVec *vec)
|
||||||
|
|||||||
+23
-22
@@ -9,6 +9,7 @@ struct Str {
|
|||||||
|
|
||||||
Str str_empty(void);
|
Str str_empty(void);
|
||||||
Str str_slice(Str str, U32 start, U32 len);
|
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);
|
S64 str_index(Str haystack, Str needle);
|
||||||
|
|
||||||
Str str_concat(Allocator *alloc, Str first, Str second);
|
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;
|
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)
|
static INLINE bool str_starts_with(Str str, Str prefix)
|
||||||
{
|
{
|
||||||
if (prefix.len > str.len) {
|
if (prefix.len > str.len) {
|
||||||
@@ -91,28 +103,28 @@ static INLINE Str str_skip(Str str, U32 len)
|
|||||||
return new_str;
|
return new_str;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destination must be at least source.len * 2. Returns destination.
|
// Destination must be at least source.len * 2. Returns hex_result.
|
||||||
Str str_hex_encode(Str source, Str destination);
|
Str str_hex_encode(Str binary_data, Str hex_result);
|
||||||
// Destination must be at least source.len / 2. Returns destination.
|
// Destination must be at least source.len / 2. Returns result.
|
||||||
Str str_hex_decode(Str source, Str destination);
|
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);
|
Assert(binary_data.len <= U32_Max / 2);
|
||||||
U8 hex_len = str.len * 2;
|
U8 hex_len = binary_data.len * 2;
|
||||||
U8 *result = allocator_push_array(allocator, U8, hex_len);
|
U8 *result = allocator_push_array(allocator, U8, hex_len);
|
||||||
Str destination = str_create(result, 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);
|
U8 *result = allocator_push_array(allocator, U8, binary_len);
|
||||||
Str destination = str_create(result, binary_len);
|
Str destination = str_create(result, binary_len);
|
||||||
|
|
||||||
return str_hex_decode(str, destination);
|
return str_hex_decode(hex_str, destination);
|
||||||
}
|
}
|
||||||
// String Vector
|
// String Vector
|
||||||
typedef struct StrVec StrVec;
|
typedef struct StrVec StrVec;
|
||||||
@@ -147,17 +159,6 @@ static INLINE bool char_is_digit(U8 byte)
|
|||||||
return byte >= '0' && byte <= '9';
|
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
|
// UTF-8/Unicode
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user