small util fixes
This commit is contained in:
parent
e77c1790f7
commit
5f35fba904
6
alloc.d
6
alloc.d
@ -41,6 +41,12 @@ struct TempArena
|
||||
ArenaPool* start_pool;
|
||||
}
|
||||
|
||||
extern(C)
|
||||
{
|
||||
void gc_setProxy(void* p);
|
||||
void gc_clrProxy();
|
||||
}
|
||||
|
||||
T*
|
||||
MAlloc(T)()
|
||||
{
|
||||
|
||||
54
util.d
54
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,12 +88,12 @@ 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)
|
||||
{
|
||||
@ -109,6 +101,26 @@ Errf(Args...)(string fmt, Args args)
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user