Reorganize fmt and strings; Replace fmt.String_Buffer with strings.Builder

This commit is contained in:
gingerBill
2019-01-06 14:41:42 +00:00
parent 5acea1bceb
commit 594238a86c
8 changed files with 1282 additions and 1229 deletions
+1139 -1210
View File
File diff suppressed because it is too large Load Diff
+8 -7
View File
@@ -1,6 +1,7 @@
package log package log
import "core:fmt"; import "core:fmt";
import "core:strings";
import "core:os"; import "core:os";
import "core:time"; import "core:time";
@@ -70,7 +71,7 @@ file_console_logger_proc :: proc(logger_data: rawptr, level: Level, text: string
if(data.file_handle != os.INVALID_HANDLE) do h = data.file_handle; if(data.file_handle != os.INVALID_HANDLE) do h = data.file_handle;
else do h = level <= Level.Error ? os.stdout : os.stderr; else do h = level <= Level.Error ? os.stdout : os.stderr;
backing: [1024]byte; //NOTE(Hoej): 1024 might be too much for a header backing, unless somebody has really long paths. backing: [1024]byte; //NOTE(Hoej): 1024 might be too much for a header backing, unless somebody has really long paths.
buf := fmt.string_buffer_from_slice(backing[:]); buf := strings.builder_from_slice(backing[:]);
do_level_header(options, level, &buf); do_level_header(options, level, &buf);
@@ -90,10 +91,10 @@ file_console_logger_proc :: proc(logger_data: rawptr, level: Level, text: string
if data.ident != "" do fmt.sbprintf(&buf, "[%s] ", data.ident); if data.ident != "" do fmt.sbprintf(&buf, "[%s] ", data.ident);
//TODO(Hoej): When we have better atomics and such, make this thread-safe //TODO(Hoej): When we have better atomics and such, make this thread-safe
fmt.fprintf(h, "%s %s\n", fmt.to_string(buf), text); fmt.fprintf(h, "%s %s\n", strings.to_string(buf), text);
} }
do_level_header :: proc(opts : Options, level : Level, buf : ^fmt.String_Buffer) { do_level_header :: proc(opts : Options, level : Level, str : ^strings.Builder) {
RESET :: "\x1b[0m"; RESET :: "\x1b[0m";
RED :: "\x1b[31m"; RED :: "\x1b[31m";
@@ -109,13 +110,13 @@ do_level_header :: proc(opts : Options, level : Level, buf : ^fmt.String_Buffer)
} }
if Option.Level in opts { if Option.Level in opts {
if Option.Terminal_Color in opts do fmt.sbprint(buf, col); if Option.Terminal_Color in opts do fmt.sbprint(str, col);
fmt.sbprint(buf, Level_Headers[level]); fmt.sbprint(str, Level_Headers[level]);
if Option.Terminal_Color in opts do fmt.sbprint(buf, RESET); if Option.Terminal_Color in opts do fmt.sbprint(str, RESET);
} }
} }
do_location_header :: proc(opts : Options, buf : ^fmt.String_Buffer, location := #caller_location) { do_location_header :: proc(opts : Options, buf : ^strings.Builder, location := #caller_location) {
if Location_Header_Opts & opts != nil do fmt.sbprint(buf, "["); else do return; if Location_Header_Opts & opts != nil do fmt.sbprint(buf, "["); else do return;
file := location.file_path; file := location.file_path;
+4
View File
@@ -71,6 +71,10 @@ stdin := Handle(-1); // Not implemented
stdout := Handle(0); stdout := Handle(0);
stderr := Handle(0); stderr := Handle(0);
is_path_separator :: proc(r: rune) -> bool {
return r == '/';
}
current_thread_id :: proc "contextless" () -> int { current_thread_id :: proc "contextless" () -> int {
return OSGetThreadID(Handle(0x1000)); return OSGetThreadID(Handle(0x1000));
} }
+4
View File
@@ -203,6 +203,10 @@ foreign dl {
@(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---; @(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---;
} }
is_path_separator :: proc(r: rune) -> bool {
return r == '/';
}
get_last_error :: proc() -> int { get_last_error :: proc() -> int {
return __errno_location()^; return __errno_location()^;
} }
+4
View File
@@ -215,6 +215,10 @@ last_write_time :: proc(fd: Handle) -> File_Time {}
last_write_time_by_name :: proc(name: string) -> File_Time {} last_write_time_by_name :: proc(name: string) -> File_Time {}
*/ */
is_path_separator :: proc(r: rune) -> bool {
return r == '/';
}
stat :: inline proc(path: string) -> (Stat, bool) { stat :: inline proc(path: string) -> (Stat, bool) {
s: Stat; s: Stat;
cstr := strings.new_cstring(path); cstr := strings.new_cstring(path);
+4
View File
@@ -61,6 +61,10 @@ ERROR_FILE_IS_PIPE: Errno : 1<<29 + 0;
args := _alloc_command_line_arguments(); args := _alloc_command_line_arguments();
is_path_separator :: proc(r: rune) -> bool {
return r == '/' || r == '\\';
}
open :: proc(path: string, mode: int = O_RDONLY, perm: u32 = 0) -> (Handle, Errno) { open :: proc(path: string, mode: int = O_RDONLY, perm: u32 = 0) -> (Handle, Errno) {
if len(path) == 0 do return INVALID_HANDLE, ERROR_FILE_NOT_FOUND; if len(path) == 0 do return INVALID_HANDLE, ERROR_FILE_NOT_FOUND;
+107
View File
@@ -0,0 +1,107 @@
package strings
import "core:mem"
import "core:unicode/utf8"
import "core:strconv"
Builder :: struct {
buf: [dynamic]byte,
}
make_builder :: proc(allocator := context.allocator) -> Builder {
return Builder{make([dynamic]byte)};
}
builder_from_slice :: proc(backing: []byte) -> Builder {
s := transmute(mem.Raw_Slice)backing;
d := mem.Raw_Dynamic_Array{
data = s.data,
len = 0,
cap = s.len,
allocator = mem.nil_allocator(),
};
return transmute(Builder)d;
}
to_string :: proc(b: Builder) -> string {
return string(b.buf[:]);
}
builder_len :: proc(b: Builder) -> int {
return len(b.buf);
}
builder_cap :: proc(b: Builder) -> int {
return cap(b.buf);
}
write_byte :: proc(b: ^Builder, x: byte) {
append(&b.buf, x);
}
write_rune :: proc(b: ^Builder, r: rune) -> int {
if r < utf8.RUNE_SELF {
write_byte(b, byte(r));
return 1;
}
s, n := utf8.encode_rune(r);
write_bytes(b, s[:n]);
return n;
}
write_string :: proc(b: ^Builder, s: string) {
write_bytes(b, cast([]byte)s);
}
write_bytes :: proc(b: ^Builder, x: []byte) {
append(&b.buf, ..x);
}
write_encoded_rune :: proc(b: ^Builder, r: rune) {
write_byte(b, '\'');
switch r {
case '\a': write_string(b, "\\a");
case '\b': write_string(b, "\\b");
case '\e': write_string(b, "\\e");
case '\f': write_string(b, "\\f");
case '\n': write_string(b, "\\n");
case '\r': write_string(b, "\\r");
case '\t': write_string(b, "\\t");
case '\v': write_string(b, "\\v");
case:
if r < 32 {
write_string(b, "\\x");
buf: [2]byte;
s := strconv.append_bits(buf[:], u64(r), 16, true, 64, strconv.digits, nil);
switch len(s) {
case 0: write_string(b, "00");
case 1: write_rune(b, '0');
case 2: write_string(b, s);
}
} else {
write_rune(b, r);
}
}
write_byte(b, '\'');
}
write_u64 :: proc(b: ^Builder, i: u64, base: int = 10) {
buf: [129]byte;
s := strconv.append_bits(buf[:], u64(i), base, false, 64, strconv.digits, nil);
write_string(b, s);
}
write_i64 :: proc(b: ^Builder, i: i64, base: int = 10) {
buf: [129]byte;
s := strconv.append_bits(buf[:], u64(i), base, true, 64, strconv.digits, nil);
write_string(b, s);
}
write_uint :: proc(b: ^Builder, i: uint, base: int = 10) {
write_u64(b, u64(i), base);
}
write_int :: proc(b: ^Builder, i: int, base: int = 10) {
write_i64(b, i64(i), base);
}
+1 -1
View File
@@ -841,7 +841,7 @@ bit_set_type :: proc() {
incl(&x, 'F'); incl(&x, 'F');
assert('F' in x); assert('F' in x);
excl(&x, 'F'); excl(&x, 'F');
assert(!('F' in x)); assert('F' notin x);
y |= {1, 4, 2}; y |= {1, 4, 2};
assert(2 in y); assert(2 in y);