From 5f35fba904479cfbaed6c543940caaf278e5e644 Mon Sep 17 00:00:00 2001 From: Matthew Date: Mon, 29 Dec 2025 18:23:32 +1100 Subject: [PATCH] small util fixes --- alloc.d | 6 ++++++ util.d | 56 +++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/alloc.d b/alloc.d index fbfca27..b04f3b1 100644 --- a/alloc.d +++ b/alloc.d @@ -41,6 +41,12 @@ struct TempArena ArenaPool* start_pool; } +extern(C) +{ + void gc_setProxy(void* p); + void gc_clrProxy(); +} + T* MAlloc(T)() { diff --git a/util.d b/util.d index 307c902..ee28c49 100644 --- a/util.d +++ b/util.d @@ -43,29 +43,22 @@ Assert(T)(T cond, string msg) } string -ConvToStr(T)(T[] arr) +Str(T)(T[] arr) { return (cast(immutable(char)*)arr.ptr)[0 .. arr.length]; } -pragma(inline) bool -CondIncr(i64 step, T)(bool cond, T* val) -{ - if(cond) - { - *val += step; - } - - return cond; -} +alias ConvToStr = Str; T[] -CastStr(T)(string str) +Arr(T)(string str) { T[] arr = (cast(T*)str.ptr)[0 .. str.length]; return arr; } +alias CastStr = Arr; + T[] CastArr(T, U)(U[] input_array) { @@ -81,12 +74,11 @@ Debugf(Args...)(string fmt, Args args) } void -Logf(Args...)(string fmt, Args args, string prefix = "INFO", string func = __FUNCTION__) +Logf(string prefix = "INFO ", Args...)(string fmt, Args args) { try { - debug writef("[%s] FUNC: [%s]: ", prefix, func); - else writef("[%s]: ", prefix); + writef("[%s]: ", prefix); writefln(fmt, args); } catch (Exception e) @@ -96,19 +88,39 @@ Logf(Args...)(string fmt, Args args, string prefix = "INFO", string func = __FUN } void -Errf(Args...)(string fmt, Args args) +Logif(Args...)(string fmt, Args args, string func = __FUNCTION__) { try { - stderr.write("[ERROR]: "); - stderr.writef(fmt, args, '\n'); + writef("FN: [%s] ", func); + Logf(fmt, args); } - catch(Exception e) + catch (Exception e) { assert(false, "Incompatible format type"); } } +void +Warnf(Args...)(string fmt, Args args) +{ + Logf!("WARN ", Args)(fmt, args); +} + +void +Errf(Args...)(string fmt, Args args) +{ + Logf!("ERROR", Args)(fmt, args); +} + +string +Scratchf(Args...)(string fmt, Args args) +{ + assert(g_scratch.init); + char[] buf = ScratchAlloc!(char)(fmt.length < 16 ? 32 : 128); + return Str(sformat(buf, fmt, args)); +} + void Log(string str) { @@ -1173,4 +1185,10 @@ version(DLIB_TEST) unittest char[] char_arr = ['a', 'b']; arr = CastArr!(u8)(char_arr); } + + { + ResetScratch(MB(4)); + string str = Scratchf("%s testing %s", 55, "testing"); + assert(str == "55 testing testing"); + } }