Push string/push string copy in string library

master
Allen Webster 2018-11-27 20:36:09 -08:00
parent ceda879326
commit 89516d827b
15 changed files with 109 additions and 86 deletions

View File

@ -9,6 +9,8 @@
#include <stdint.h> #include <stdint.h>
#include "4coder_lib/4coder_arena.h"
#include "4coder_lib/4coder_heap.h"
#include "4coder_lib/4coder_string.h" #include "4coder_lib/4coder_string.h"
#include "4coder_lib/4cpp_lexer_types.h" #include "4coder_lib/4cpp_lexer_types.h"
#include "4coder_API/4coder_version.h" #include "4coder_API/4coder_version.h"

View File

@ -15,16 +15,15 @@
#include "4coder_generated/command_metadata.h" #include "4coder_generated/command_metadata.h"
#include "4coder_lib/4coder_arena.cpp"
#include "4coder_lib/4coder_heap.cpp"
#define FSTRING_IMPLEMENTATION #define FSTRING_IMPLEMENTATION
#include "4coder_lib/4coder_string.h" #include "4coder_lib/4coder_string.h"
#include "4coder_lib/4coder_table.h" #include "4coder_lib/4coder_table.h"
#include "4coder_lib/4coder_arena.h"
#include "4coder_lib/4coder_heap.h"
#include "4coder_lib/4coder_utf8.h" #include "4coder_lib/4coder_utf8.h"
#include "4coder_lib/4cpp_lexer.h" #include "4coder_lib/4cpp_lexer.h"
#include "4coder_lib/4coder_arena.cpp"
#include "4coder_lib/4coder_heap.cpp"
#include "4coder_ui_helper.h" #include "4coder_ui_helper.h"
#include "4coder_helper.h" #include "4coder_helper.h"

View File

@ -176,7 +176,7 @@ bind(context, key_up, MDFR_NONE, lister__move_up);
bind(context, 'k', MDFR_ALT, lister__move_up); bind(context, 'k', MDFR_ALT, lister__move_up);
bind(context, key_page_up, MDFR_NONE, lister__move_up); bind(context, key_page_up, MDFR_NONE, lister__move_up);
bind(context, key_down, MDFR_NONE, lister__move_down); bind(context, key_down, MDFR_NONE, lister__move_down);
bind(context, 'j', MDFR_ALT, lister__move_up); bind(context, 'j', MDFR_ALT, lister__move_down);
bind(context, key_page_down, MDFR_NONE, lister__move_down); bind(context, key_page_down, MDFR_NONE, lister__move_down);
bind(context, key_mouse_wheel, MDFR_NONE, lister__wheel_scroll); bind(context, key_mouse_wheel, MDFR_NONE, lister__wheel_scroll);
bind(context, key_mouse_left, MDFR_NONE, lister__mouse_press); bind(context, key_mouse_left, MDFR_NONE, lister__mouse_press);
@ -576,7 +576,7 @@ static Meta_Key_Bind fcoder_binds_for_default_default_lister_ui_map[16] = {
{0, 107, 2, "lister__move_up", 15, LINK_PROCS(lister__move_up)}, {0, 107, 2, "lister__move_up", 15, LINK_PROCS(lister__move_up)},
{0, 55305, 0, "lister__move_up", 15, LINK_PROCS(lister__move_up)}, {0, 55305, 0, "lister__move_up", 15, LINK_PROCS(lister__move_up)},
{0, 55298, 0, "lister__move_down", 17, LINK_PROCS(lister__move_down)}, {0, 55298, 0, "lister__move_down", 17, LINK_PROCS(lister__move_down)},
{0, 106, 2, "lister__move_up", 15, LINK_PROCS(lister__move_up)}, {0, 106, 2, "lister__move_down", 17, LINK_PROCS(lister__move_down)},
{0, 55306, 0, "lister__move_down", 17, LINK_PROCS(lister__move_down)}, {0, 55306, 0, "lister__move_down", 17, LINK_PROCS(lister__move_down)},
{0, 55312, 0, "lister__wheel_scroll", 20, LINK_PROCS(lister__wheel_scroll)}, {0, 55312, 0, "lister__wheel_scroll", 20, LINK_PROCS(lister__wheel_scroll)},
{0, 55308, 0, "lister__mouse_press", 19, LINK_PROCS(lister__mouse_press)}, {0, 55308, 0, "lister__mouse_press", 19, LINK_PROCS(lister__mouse_press)},

View File

@ -81,26 +81,6 @@ end_temp_memory(Temp_Memory temp){
((Partition*)temp.handle)->pos = temp.pos; ((Partition*)temp.handle)->pos = temp.pos;
} }
inline Tail_Temp_Partition
begin_tail_part(Partition *data, i32_4tech size){
Tail_Temp_Partition result = {};
if (data->pos + size <= data->max){
result.handle = data;
result.old_max = data->max;
data->max -= size;
result.part = make_part(data->base + data->max, size);
}
return(result);
}
inline void
end_tail_part(Tail_Temp_Partition temp){
if (temp.handle){
Partition *part = (Partition*)temp.handle;
part->max = temp.old_max;
}
}
#define reset_temp_memory end_temp_memory #define reset_temp_memory end_temp_memory
// BOTTOM // BOTTOM

View File

@ -55,12 +55,6 @@ struct Temp_Memory{
i32_4tech pos; i32_4tech pos;
}; };
struct Tail_Temp_Partition{
Partition part;
void *handle;
i32_4tech old_max;
};
#endif #endif
// BOTTOM // BOTTOM

View File

@ -1,5 +1,5 @@
/* /*
4coder_string.h - Version 1.0.114 4coder_string.h - Version 1.0.116
no warranty implied; use at your own risk no warranty implied; use at your own risk
This software is in the public domain. Where that dedication is not This software is in the public domain. Where that dedication is not
@ -217,6 +217,8 @@ FSTRING_LINK String get_first_double_line(String source);
FSTRING_LINK String get_next_double_line(String source, String line); FSTRING_LINK String get_next_double_line(String source, String line);
FSTRING_LINK String get_next_word(String source, String prev_word); FSTRING_LINK String get_next_word(String source, String prev_word);
FSTRING_LINK String get_first_word(String source); FSTRING_LINK String get_first_word(String source);
FSTRING_LINK String string_push(Partition *part, i32_4tech size);
FSTRING_LINK String string_push_copy(Partition *part, String str);
#endif #endif
@ -487,11 +489,7 @@ make_string_cap(void *str, i32_4tech size, i32_4tech mem_size){
#if !defined(FSTRING_GUARD) #if !defined(FSTRING_GUARD)
FSTRING_INLINE String FSTRING_INLINE String
make_string(void *str, i32_4tech size){ make_string(void *str, i32_4tech size){
String result; return(make_string(str, size, size));
result.str = (char*)str;
result.size = size;
result.memory_size = size;
return(result);
} }
#endif #endif
@ -976,6 +974,7 @@ compare_ss(String a, String b){
// Finding Characters and Substrings // Finding Characters and Substrings
// //
#if defined(FSTRING_IMPLEMENTATION) #if defined(FSTRING_IMPLEMENTATION)
FSTRING_LINK i32_4tech FSTRING_LINK i32_4tech
find_c_char(char *str, i32_4tech start, char character){ find_c_char(char *str, i32_4tech start, char character){
@ -1476,7 +1475,8 @@ append_padding(String *dest, char c, i32_4tech target_size){
#if defined(FSTRING_IMPLEMENTATION) #if defined(FSTRING_IMPLEMENTATION)
FSTRING_LINK void FSTRING_LINK void
string_interpret_escapes(String src, char *dst){ string_interpret_escapes(String src, char *dst)
{
i32_4tech mode = 0; i32_4tech mode = 0;
i32_4tech j = 0; i32_4tech j = 0;
for (i32_4tech i = 0; i < src.size; ++i){ for (i32_4tech i = 0; i < src.size; ++i){
@ -2295,6 +2295,31 @@ get_first_word(String source){
} }
#endif #endif
#if defined(FSTRING_IMPLEMENTATION)
FSTRING_LINK String
string_push(Partition *part, i32_4tech size){
String result = {};
result.str = push_array(part, char, size);
if (result.str != 0){
result.memory_size = size;
}
return(result);
}
#endif
#if defined(FSTRING_IMPLEMENTATION)
FSTRING_LINK String
string_push_copy(Partition *part, String str){
String result = {};
result.str = push_array(part, char, str.size + 1);
if (result.str != 0){
result.memory_size = str.size + 1;
copy(&result, str);
}
return(result);
}
#endif
// TODO(allen): eliminate this. // TODO(allen): eliminate this.
#ifndef FSTRING_EXPERIMENTAL #ifndef FSTRING_EXPERIMENTAL
#define FSTRING_EXPERIMENTAL #define FSTRING_EXPERIMENTAL

View File

@ -12,21 +12,19 @@
// TODO(allen): get away from string.h // TODO(allen): get away from string.h
#include <string.h> #include <string.h>
#include "4ed_defines.h"
#include "4coder_API/4coder_custom.h" #include "4coder_API/4coder_custom.h"
#include "4ed_defines.h"
#include "4ed_math.h" #include "4ed_math.h"
#include "4ed_font.h" #include "4ed_font.h"
#include "4ed_system.h" #include "4ed_system.h"
#define PREFERRED_ALIGNMENT 8 #define PREFERRED_ALIGNMENT 8
#include "4coder_lib/4coder_arena.cpp"
#include "4coder_lib/4coder_heap.cpp"
#define FSTRING_IMPLEMENTATION #define FSTRING_IMPLEMENTATION
#define FSTRING_C
#include "4coder_lib/4coder_string.h" #include "4coder_lib/4coder_string.h"
#include "4coder_lib/4coder_arena.h"
#include "4coder_lib/4coder_heap.h"
#include "4coder_lib/4coder_table.h" #include "4coder_lib/4coder_table.h"
#include "4coder_lib/4coder_utf8.h" #include "4coder_lib/4coder_utf8.h"
@ -35,9 +33,6 @@ struct Mem_Options{
Heap heap; Heap heap;
}; };
#include "4coder_lib/4coder_arena.cpp"
#include "4coder_lib/4coder_heap.cpp"
#include "4ed_render_target.h" #include "4ed_render_target.h"
#include "4ed_render_format.h" #include "4ed_render_format.h"
#include "4ed.h" #include "4ed.h"

View File

@ -629,7 +629,7 @@ generate_remapping_code_and_data(Partition *part){
bind(part, mappings, 'k' , MDFR_ALT , lister__move_up); bind(part, mappings, 'k' , MDFR_ALT , lister__move_up);
bind(part, mappings, key_page_up , MDFR_NONE, lister__move_up); bind(part, mappings, key_page_up , MDFR_NONE, lister__move_up);
bind(part, mappings, key_down , MDFR_NONE, lister__move_down); bind(part, mappings, key_down , MDFR_NONE, lister__move_down);
bind(part, mappings, 'j' , MDFR_ALT , lister__move_up); bind(part, mappings, 'j' , MDFR_ALT , lister__move_down);
bind(part, mappings, key_page_down, MDFR_NONE, lister__move_down); bind(part, mappings, key_page_down, MDFR_NONE, lister__move_down);
bind(part, mappings, key_mouse_wheel , MDFR_NONE, lister__wheel_scroll); bind(part, mappings, key_mouse_wheel , MDFR_NONE, lister__wheel_scroll);
bind(part, mappings, key_mouse_left , MDFR_NONE, lister__mouse_press); bind(part, mappings, key_mouse_left , MDFR_NONE, lister__mouse_press);

View File

@ -23,10 +23,10 @@
# include "4coder_API/4coder_keycodes.h" # include "4coder_API/4coder_keycodes.h"
# include "4coder_API/4coder_style.h" # include "4coder_API/4coder_style.h"
# define FSTRING_IMPLEMENTATION
# include "4coder_lib/4coder_string.h"
# include "4coder_lib/4coder_arena.h" # include "4coder_lib/4coder_arena.h"
# include "4coder_lib/4coder_arena.cpp" # include "4coder_lib/4coder_arena.cpp"
# define FSTRING_IMPLEMENTATION
# include "4coder_lib/4coder_string.h"
# include "4coder_API/4coder_types.h" # include "4coder_API/4coder_types.h"

View File

@ -17,14 +17,13 @@
#include "4coder_lib/4coder_utf8.h" #include "4coder_lib/4coder_utf8.h"
#if defined(FRED_SUPER) #if defined(FRED_SUPER)
# include "4coder_API/4coder_keycodes.h"
# include "4coder_API/4coder_style.h"
# define FSTRING_IMPLEMENTATION
# include "4coder_lib/4coder_string.h"
# include "4coder_lib/4coder_arena.h" # include "4coder_lib/4coder_arena.h"
# include "4coder_lib/4coder_arena.cpp" # include "4coder_lib/4coder_arena.cpp"
# define FSTRING_IMPLEMENTATION
# include "4coder_lib/4coder_string.h"
# include "4coder_API/4coder_keycodes.h"
# include "4coder_API/4coder_style.h"
# include "4coder_API/4coder_types.h" # include "4coder_API/4coder_types.h"
#else #else

View File

@ -28,16 +28,14 @@
#include "4coder_lib/4coder_utf8.h" #include "4coder_lib/4coder_utf8.h"
#if defined(FRED_SUPER) #if defined(FRED_SUPER)
# include "4coder_API/4coder_keycodes.h"
# include "4coder_API/4coder_style.h"
# define FSTRING_IMPLEMENTATION
# include "4coder_lib/4coder_string.h"
# include "4coder_lib/4coder_arena.h" # include "4coder_lib/4coder_arena.h"
# include "4coder_lib/4coder_arena.cpp" # include "4coder_lib/4coder_arena.cpp"
# define FSTRING_IMPLEMENTATION
# include "4coder_lib/4coder_string.h"
# include "4coder_API/4coder_keycodes.h"
# include "4coder_API/4coder_style.h"
# include "4coder_API/4coder_types.h" # include "4coder_API/4coder_types.h"
#else #else
# include "4coder_default_bindings.cpp" # include "4coder_default_bindings.cpp"
#endif #endif
@ -59,8 +57,13 @@
////////////////////////////// //////////////////////////////
enum{
ErrorString_UseLog = 0,
ErrorString_UseErrorBox = 1,
};
internal void internal void
win32_output_error_string(b32 use_error_box = true); win32_output_error_string(i32 error_string_type);
////////////////////////////// //////////////////////////////
@ -293,7 +296,7 @@ internal void
win32_post_clipboard(char *text, i32 len){ win32_post_clipboard(char *text, i32 len){
if (OpenClipboard(win32vars.window_handle)){ if (OpenClipboard(win32vars.window_handle)){
if (!EmptyClipboard()){ if (!EmptyClipboard()){
win32_output_error_string(false); win32_output_error_string(ErrorString_UseLog);
} }
HANDLE memory_handle = GlobalAlloc(GMEM_MOVEABLE, len + 1); HANDLE memory_handle = GlobalAlloc(GMEM_MOVEABLE, len + 1);
if (memory_handle){ if (memory_handle){
@ -789,24 +792,24 @@ win32_init_gl(HDC hdc){
format.iLayerType = PFD_MAIN_PLANE; format.iLayerType = PFD_MAIN_PLANE;
i32 suggested_format_index = ChoosePixelFormat(hwgldc, &format); i32 suggested_format_index = ChoosePixelFormat(hwgldc, &format);
if (suggested_format_index == 0){ if (suggested_format_index == 0){
win32_output_error_string(); win32_output_error_string(ErrorString_UseErrorBox);
GLInitFail("ChoosePixelFormat"); GLInitFail("ChoosePixelFormat");
} }
DescribePixelFormat(hwgldc, suggested_format_index, sizeof(format), &format); DescribePixelFormat(hwgldc, suggested_format_index, sizeof(format), &format);
if (!SetPixelFormat(hwgldc, suggested_format_index, &format)){ if (!SetPixelFormat(hwgldc, suggested_format_index, &format)){
win32_output_error_string(); win32_output_error_string(ErrorString_UseErrorBox);
GLInitFail("SetPixelFormat"); GLInitFail("SetPixelFormat");
} }
HGLRC wglcontext = wglCreateContext(hwgldc); HGLRC wglcontext = wglCreateContext(hwgldc);
if (wglcontext == 0){ if (wglcontext == 0){
win32_output_error_string(); win32_output_error_string(ErrorString_UseErrorBox);
GLInitFail("wglCreateContext"); GLInitFail("wglCreateContext");
} }
if (!wglMakeCurrent(hwgldc, wglcontext)){ if (!wglMakeCurrent(hwgldc, wglcontext)){
win32_output_error_string(); win32_output_error_string(ErrorString_UseErrorBox);
GLInitFail("wglMakeCurrent"); GLInitFail("wglMakeCurrent");
} }
@ -1326,7 +1329,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdS
LOG("Initializing clipboard\n"); LOG("Initializing clipboard\n");
if (!AddClipboardFormatListener(win32vars.window_handle)){ if (!AddClipboardFormatListener(win32vars.window_handle)){
win32_output_error_string(false); win32_output_error_string(ErrorString_UseLog);
} }
win32vars.clip_max = KB(16); win32vars.clip_max = KB(16);

View File

@ -20,7 +20,7 @@ system_load_library_direct(Library *library, char *name){
library->lib = LoadLibraryA(name); library->lib = LoadLibraryA(name);
b32 success = (library->lib != 0); b32 success = (library->lib != 0);
if (!success){ if (!success){
win32_output_error_string(false); win32_output_error_string(ErrorString_UseLog);
} }
return(success); return(success);
} }

View File

@ -1,5 +1,5 @@
1 1
0 0
116 117

View File

@ -18,9 +18,11 @@
#define BACKUP_FOLDER ".." SLASH ".." SLASH "string_backup" #define BACKUP_FOLDER ".." SLASH ".." SLASH "string_backup"
#include "../4coder_lib/4cpp_lexer.h" #include "../4coder_lib/4coder_arena.h"
#include "../4coder_lib/4coder_arena.cpp"
#define FSTRING_IMPLEMENTATION #define FSTRING_IMPLEMENTATION
#include "../4coder_lib/4coder_string.h" #include "../4coder_lib/4coder_string.h"
#include "../4coder_lib/4cpp_lexer.h"
#include "../4ed_defines.h" #include "../4ed_defines.h"
#include "../meta/4ed_meta_defines.h" #include "../meta/4ed_meta_defines.h"
@ -149,17 +151,18 @@ print_function_body_code(String *out, Parse_Context *context, i32 start){
} }
internal void internal void
file_move(char *path, char *file_name){ file_move(Partition *part, char *path, char *file_name){
fm_copy_file(fm_str(file_name), fm_str(path, "/", file_name)); fm_copy_file(fm_str(part, file_name), fm_str(part, path, "/", file_name));
} }
int main(){ int main(){
META_BEGIN(); META_BEGIN();
fm_init_system(); Partition part_ = fm_init_system();
Partition *part = &part_;
// NOTE(allen): Parse the internal string file. // NOTE(allen): Parse the internal string file.
char *string_files[] = { INTERNAL_STRING, 0 }; char *string_files[] = { INTERNAL_STRING, 0 };
Meta_Unit string_unit = compile_meta_unit(".", string_files, ExpandArray(meta_keywords)); Meta_Unit string_unit = compile_meta_unit(part, ".", string_files, ExpandArray(meta_keywords));
if (string_unit.parse == 0){ if (string_unit.parse == 0){
Assert(!"Missing one or more input files!"); Assert(!"Missing one or more input files!");
@ -191,7 +194,7 @@ int main(){
} }
// NOTE(allen): String Library // NOTE(allen): String Library
String out = str_alloc(10 << 20); String out = str_alloc(part, 10 << 20);
Cpp_Token *token = 0; Cpp_Token *token = 0;
i32 start = 0; i32 start = 0;
@ -212,7 +215,7 @@ int main(){
append(&out, "/*\n"); append(&out, "/*\n");
append(&out, GENERATED_FILE " - Version "V_MAJ"."V_MIN"."); append(&out, GENERATED_FILE " - Version " V_MAJ "." V_MIN ".");
append_int_to_str(&out, build_number); append_int_to_str(&out, build_number);
append(&out, "\n"); append(&out, "\n");
@ -436,11 +439,11 @@ int main(){
// NOTE(allen): Publish the new file. (Would like to be able to automatically test the result before publishing). // NOTE(allen): Publish the new file. (Would like to be able to automatically test the result before publishing).
{ {
fm_make_folder_if_missing(BACKUP_FOLDER SLASH V_MAJ SLASH V_MIN); fm_make_folder_if_missing(part, BACKUP_FOLDER SLASH V_MAJ SLASH V_MIN);
file_move(BACKUP_FOLDER SLASH V_MAJ SLASH V_MIN, INTERNAL_STRING); file_move(part, BACKUP_FOLDER SLASH V_MAJ SLASH V_MIN, INTERNAL_STRING);
file_move(BACKUP_FOLDER SLASH V_MAJ SLASH V_MIN, GENERATED_FILE); file_move(part, BACKUP_FOLDER SLASH V_MAJ SLASH V_MIN, GENERATED_FILE);
fm_delete_file(GENERATED_FILE); fm_delete_file(GENERATED_FILE);
printf("published "GENERATED_FILE": v%d.%d.%d\n", major_number, minor_number, build_number); printf("published " GENERATED_FILE ": v%d.%d.%d\n", major_number, minor_number, build_number);
save_build_number(BUILD_NUMBER_FILE, major_number, minor_number, build_number + 1); save_build_number(BUILD_NUMBER_FILE, major_number, minor_number, build_number + 1);
} }

View File

@ -201,11 +201,7 @@ does not specify the size of the memory it is also assumed that this size is the
of the memory.) of the memory.)
DOC(This call returns the String created from the parameters.) DOC(This call returns the String created from the parameters.)
*/{ */{
String result; return(make_string(str, size, size));
result.str = (char*)str;
result.size = size;
result.memory_size = size;
return(result);
} }
API_EXPORT_MACRO API_EXPORT_MACRO
@ -2126,6 +2122,33 @@ DOC_SEE(get_next_word)
return(word); return(word);
} }
API_EXPORT FSTRING_LINK String
string_push(Partition *part, i32_4tech size)/*
DOC_PARAM(part, A partition on which the string will be allocated.)
DOC_PARAM(size, The number of bytes to allocated for the new string.)
DOC_RETURN(If successfull returns an empty string with capacity equal to the size parameter, otherwise returns a null string.)*/{
String result = {};
result.str = push_array(part, char, size);
if (result.str != 0){
result.memory_size = size;
}
return(result);
}
API_EXPORT FSTRING_LINK String
string_push_copy(Partition *part, String str)/*
DOC_PARAM(part, A partition on which the string will be allocated.)
DOC_PARAM(str, The source string to copy into the new string. The copy includes a null terminator whther or not the source does.)
DOC_RETURN(If successfull returns a string copy of str, otherwise returns a null string.)*/{
String result = {};
result.str = push_array(part, char, str.size + 1);
if (result.str != 0){
result.memory_size = str.size + 1;
copy(&result, str);
}
return(result);
}
// TODO(allen): eliminate this. // TODO(allen): eliminate this.
#ifndef FSTRING_EXPERIMENTAL #ifndef FSTRING_EXPERIMENTAL
#define FSTRING_EXPERIMENTAL #define FSTRING_EXPERIMENTAL