- shorten str8_lit macro to STR8
- use strlen() in STR8 and remove str8_from_cstring(). Usage with string literal seem to always be optimized by compile-time constant, so there's little need for a confusing macro that only works with literals and breaks with pointers.
- replaced str8_unbox() with str8_lp/str8_ip
This commit is contained in:
Martin Fouilleul 2023-03-08 11:40:40 +01:00
parent f07f56948d
commit bfc19665ce
9 changed files with 29 additions and 40 deletions

View File

@ -116,9 +116,9 @@ int main()
f32 lineHeight = fontScale*(extents.ascent + extents.descent + extents.leading);
int codePointCount = utf8_codepoint_count_for_string(str8_from_cstring((char*)TEST_STRING));
int codePointCount = utf8_codepoint_count_for_string(STR8((char*)TEST_STRING));
u32* codePoints = malloc_array(utf32, codePointCount);
utf8_to_codepoints(codePointCount, codePoints, str8_from_cstring((char*)TEST_STRING));
utf8_to_codepoints(codePointCount, codePoints, STR8((char*)TEST_STRING));
u32 glyphCount = 0;
for(int i=0; i<codePointCount; i++)

View File

@ -31,7 +31,7 @@ int main()
mg_surface surface = mg_surface_create_for_window(window, MG_BACKEND_METAL);
//*/
mg_canvas canvas = mg_canvas_create(surface, (mp_rect){0, 0, 800, 600});
mg_image image = mg_image_create_from_file(canvas, str8_lit("Top512.png"), true);
mg_image image = mg_image_create_from_file(canvas, STR8("Top512.png"), true);
u8 colors[64];
for(int i=0; i<64; i+=4)
@ -43,7 +43,7 @@ int main()
}
mg_image image3 = mg_image_create_from_rgba8(canvas, 4, 4, colors);
mg_image image2 = mg_image_create_from_file(canvas, str8_lit("triceratops.png"), true);
mg_image image2 = mg_image_create_from_file(canvas, STR8("triceratops.png"), true);
//NOTE(martin): create font
char* fontPath = 0;
@ -132,7 +132,7 @@ int main()
mg_set_font(canvas, font);
mg_set_font_size(canvas, 32);
mg_move_to(canvas, 500, 500);
mg_text_outlines(canvas, str8_lit("Hello, world!"));
mg_text_outlines(canvas, STR8("Hello, world!"));
mg_set_color_rgba(canvas, 0, 0, 0, 1);
mg_fill(canvas);

View File

@ -263,7 +263,7 @@ int main()
root = ui_box_top();
ui_pattern pattern = {0};
ui_pattern_push(mem_scratch(), &pattern, (ui_selector){.kind = UI_SEL_TEXT, .text = str8_lit("b")});
ui_pattern_push(mem_scratch(), &pattern, (ui_selector){.kind = UI_SEL_TEXT, .text = STR8("b")});
ui_pattern_push(mem_scratch(), &pattern, (ui_selector){.kind = UI_SEL_TAG, .tag = ui_tag_make("foo")});
ui_style_match_before(pattern, &(ui_style){.fontSize = 36}, UI_STYLE_FONT_SIZE);
@ -280,14 +280,14 @@ int main()
pattern = (ui_pattern){0};
ui_pattern_push(mem_scratch(), &pattern, (ui_selector){.kind = UI_SEL_TEXT, .text = str8_lit("c")});
ui_pattern_push(mem_scratch(), &pattern, (ui_selector){.kind = UI_SEL_TEXT, .text = STR8("c")});
ui_pattern_push(mem_scratch(), &pattern, (ui_selector){.kind = UI_SEL_TAG, .tag = ui_tag_make("button")});
ui_style_match_after(pattern,
&(ui_style){.bgColor = {1, 0.5, 0.5, 1}},
UI_STYLE_BG_COLOR);
pattern = (ui_pattern){0};
ui_pattern_push(mem_scratch(), &pattern, (ui_selector){.kind = UI_SEL_TEXT, .text = str8_lit("c")});
ui_pattern_push(mem_scratch(), &pattern, (ui_selector){.kind = UI_SEL_TEXT, .text = STR8("c")});
ui_pattern_push(mem_scratch(), &pattern, (ui_selector){.kind = UI_SEL_TAG, .tag = ui_tag_make("button")});
ui_pattern_push(mem_scratch(), &pattern, (ui_selector){.kind = UI_SEL_STATUS, .op = UI_SEL_AND, .status = UI_ACTIVE|UI_HOVER});
ui_style_match_after(pattern,
@ -297,7 +297,7 @@ int main()
ui_container("a", defaultFlags)
{
ui_pattern pattern = {0};
ui_pattern_push(mem_scratch(), &pattern, (ui_selector){.kind = UI_SEL_TEXT, .text = str8_lit("b")});
ui_pattern_push(mem_scratch(), &pattern, (ui_selector){.kind = UI_SEL_TEXT, .text = STR8("b")});
ui_style_match_before(pattern, &(ui_style){.fontSize = 22}, UI_STYLE_FONT_SIZE);
ui_container("b", defaultFlags)
@ -369,7 +369,7 @@ int main()
mg_set_color_rgba(0, 0, 0, 1);
mg_move_to(0, 38);
mg_text_outlines(str8_lit("hello, world"));
mg_text_outlines(STR8("hello, world"));
mg_fill();
mg_matrix_pop();

View File

@ -2068,7 +2068,7 @@ str8 mp_app_get_resource_path(mem_arena* arena, const char* name)
str8 dirPath = mp_path_directory(executablePath);
str8_list_push(scratch, &list, dirPath);
str8_list_push(scratch, &list, str8_lit("/"));
str8_list_push(scratch, &list, STR8("/"));
str8_list_push(scratch, &list, str8_push_cstring(scratch, name));
str8 path = str8_list_join(scratch, list);
char* pathCString = str8_to_cstring(scratch, path);

View File

@ -1414,7 +1414,7 @@ ui_sig ui_label_str8(str8 label)
ui_sig ui_label(const char* label)
{
return(ui_label_str8(str8_from_cstring((char*)label)));
return(ui_label_str8(STR8((char*)label)));
}
ui_sig ui_button_str8(str8 label)
@ -1485,7 +1485,7 @@ ui_sig ui_button_str8(str8 label)
ui_sig ui_button(const char* label)
{
return(ui_button_str8(str8_from_cstring((char*)label)));
return(ui_button_str8(STR8((char*)label)));
}

View File

@ -332,8 +332,8 @@ ui_box* ui_box_lookup_key(ui_key key);
ui_box* ui_box_lookup_str8(str8 string);
// C-string helper
#define ui_key_make(s) ui_key_make_str8(str8_lit(s))
#define ui_box_lookup(s) ui_box_lookup_str8(str8_lit(s))
#define ui_key_make(s) ui_key_make_str8(STR8(s))
#define ui_box_lookup(s) ui_box_lookup_str8(STR8(s))
//-------------------------------------------------------------------------------------
// Box hierarchy building
@ -351,9 +351,9 @@ ui_box* ui_box_top(void);
void ui_box_set_render_proc(ui_box* box, ui_box_render_proc proc, void* data);
// C-string helpers
#define ui_box_lookup(s) ui_box_lookup_str8(str8_lit(s))
#define ui_box_make(s, flags) ui_box_make_str8(str8_lit(s), flags)
#define ui_box_begin(s, flags) ui_box_begin_str8(str8_lit(s), flags)
#define ui_box_lookup(s) ui_box_lookup_str8(STR8(s))
#define ui_box_make(s, flags) ui_box_make_str8(STR8(s), flags)
#define ui_box_begin(s, flags) ui_box_begin_str8(STR8(s), flags)
//-------------------------------------------------------------------------------------
// Box status and signals
@ -378,9 +378,9 @@ void ui_tag_box_str8(ui_box* box, str8 string);
void ui_tag_next_str8(str8 string);
// C-string helpers
#define ui_tag_make(s) ui_tag_make_str8(str8_lit(s))
#define ui_tag_box(b, s) ui_tag_box_str8(b, str8_lit(s))
#define ui_tag_next(s) ui_tag_next_str8(str8_lit(s))
#define ui_tag_make(s) ui_tag_make_str8(STR8(s))
#define ui_tag_box(b, s) ui_tag_box_str8(b, STR8(s))
#define ui_tag_next(s) ui_tag_next_str8(STR8(s))
//-------------------------------------------------------------------------------------
// Styling

View File

@ -22,18 +22,6 @@ str8 str8_from_buffer(u64 len, char* buffer)
return((str8){.len = len, .ptr = buffer});
}
str8 str8_from_cstring(char* str)
{
if(!str)
{
return((str8){0});
}
else
{
return(str8_from_buffer(strlen(str), (char*)str));
}
}
str8 str8_slice(str8 s, u64 start, u64 end)
{
ASSERT(start <= end && start <= s.len && end <= s.len);
@ -63,7 +51,7 @@ str8 str8_push_cstring(mem_arena* arena, const char* str)
str8 str8_push_copy(mem_arena* arena, str8 s)
{
return(str8_push_buffer(arena, str8_unbox(s)));
return(str8_push_buffer(arena, str8_lp(s)));
}
str8 str8_push_slice(mem_arena* arena, str8 s, u64 start, u64 end)

View File

@ -26,11 +26,12 @@ typedef struct str8
char* ptr;
} str8;
#define str8_lit(s) ((str8){.len = sizeof(s)-1, .ptr = (char*)(s)})
#define str8_unbox(s) (int)((s).len), ((s).ptr)
#define STR8(s) ((str8){.len = (s) ? strlen(s) : 0, .ptr = (char*)s})
#define str8_lp(s) ((s).len), ((s).ptr)
#define str8_ip(s) (int)str8_lp(s)
MP_API str8 str8_from_buffer(u64 len, char* buffer);
MP_API str8 str8_from_cstring(char* str);
MP_API str8 str8_slice(str8 s, u64 start, u64 end);
MP_API str8 str8_push_buffer(mem_arena* arena, u64 len, char* buffer);

View File

@ -990,12 +990,12 @@ str8 mp_app_get_resource_path(mem_arena* arena, const char* name)
_splitpath_s(executablePathCString, driveBuffer, MAX_PATH, dirBuffer, MAX_PATH, 0, 0, 0, 0);
str8 drive = str8_from_cstring(driveBuffer);
str8 dirPath = str8_from_cstring(dirBuffer);
str8 drive = STR8(driveBuffer);
str8 dirPath = STR8(dirBuffer);
str8_list_push(scratch, &list, drive);
str8_list_push(scratch, &list, dirPath);
str8_list_push(scratch, &list, str8_lit("\\"));
str8_list_push(scratch, &list, STR8("\\"));
str8_list_push(scratch, &list, str8_push_cstring(scratch, name));
str8 path = str8_list_join(scratch, list);
char* pathCString = str8_to_cstring(scratch, path);