deduplicate string escaping paths in eval/cfg/etc

This commit is contained in:
Ryan Fleury
2024-09-13 12:08:22 -07:00
parent 306f8d84c5
commit 7cd1d175de
9 changed files with 122 additions and 210 deletions
+97
View File
@@ -1607,6 +1607,103 @@ indented_from_string(Arena *arena, String8 string)
return result;
}
////////////////////////////////
//~ rjf: Text Escaping
internal String8
escaped_from_raw_str8(Arena *arena, String8 string)
{
Temp scratch = scratch_begin(&arena, 1);
String8List parts = {0};
U64 start_split_idx = 0;
for(U64 idx = 0; idx <= string.size; idx += 1)
{
U8 byte = (idx < string.size) ? string.str[idx] : 0;
B32 split = 1;
String8 separator_replace = {0};
switch(byte)
{
default:{split = 0;}break;
case 0: {}break;
case '\a': {separator_replace = str8_lit("\\a");}break;
case '\b': {separator_replace = str8_lit("\\b");}break;
case '\f': {separator_replace = str8_lit("\\f");}break;
case '\n': {separator_replace = str8_lit("\\n");}break;
case '\r': {separator_replace = str8_lit("\\r");}break;
case '\t': {separator_replace = str8_lit("\\t");}break;
case '\v': {separator_replace = str8_lit("\\v");}break;
case '\\': {separator_replace = str8_lit("\\\\");}break;
case '"': {separator_replace = str8_lit("\\\"");}break;
case '?': {separator_replace = str8_lit("\\?");}break;
}
if(split)
{
String8 substr = str8_substr(string, r1u64(start_split_idx, idx));
start_split_idx = idx+1;
str8_list_push(scratch.arena, &parts, substr);
if(separator_replace.size != 0)
{
str8_list_push(scratch.arena, &parts, separator_replace);
}
}
}
StringJoin join = {0};
String8 result = str8_list_join(arena, &parts, &join);
scratch_end(scratch);
return result;
}
internal String8
raw_from_escaped_str8(Arena *arena, String8 string)
{
Temp scratch = scratch_begin(&arena, 1);
String8List strs = {0};
U64 start = 0;
for(U64 idx = 0; idx <= string.size; idx += 1)
{
if(idx == string.size || string.str[idx] == '\\' || string.str[idx] == '\r')
{
String8 str = str8_substr(string, r1u64(start, idx));
if(str.size != 0)
{
str8_list_push(scratch.arena, &strs, str);
}
start = idx+1;
}
if(idx < string.size && string.str[idx] == '\\')
{
U8 next_char = string.str[idx+1];
U8 replace_byte = 0;
switch(next_char)
{
default:{}break;
case 'a': replace_byte = 0x07; break;
case 'b': replace_byte = 0x08; break;
case 'e': replace_byte = 0x1b; break;
case 'f': replace_byte = 0x0c; break;
case 'n': replace_byte = 0x0a; break;
case 'r': replace_byte = 0x0d; break;
case 't': replace_byte = 0x09; break;
case 'v': replace_byte = 0x0b; break;
case '\\':replace_byte = '\\'; break;
case '\'':replace_byte = '\''; break;
case '"': replace_byte = '"'; break;
case '?': replace_byte = '?'; break;
}
String8 replace_string = push_str8_copy(scratch.arena, str8(&replace_byte, 1));
str8_list_push(scratch.arena, &strs, replace_string);
if(replace_byte == '\\' || replace_byte == '"' || replace_byte == '\'')
{
idx += 1;
start += 1;
}
}
}
String8 result = str8_list_join(arena, &strs, 0);
scratch_end(scratch);
return result;
}
////////////////////////////////
//~ rjf: Text Wrapping