small util fixes

This commit is contained in:
Matthew 2025-12-29 18:23:32 +11:00
parent e77c1790f7
commit 5f35fba904
2 changed files with 43 additions and 19 deletions

View File

@ -41,6 +41,12 @@ struct TempArena
ArenaPool* start_pool;
}
extern(C)
{
void gc_setProxy(void* p);
void gc_clrProxy();
}
T*
MAlloc(T)()
{

56
util.d
View File

@ -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");
}
}