alloc copy string with terminator function

This commit is contained in:
Matthew 2026-06-27 14:15:06 +10:00
parent a12e8c21e2
commit cf8043468d
2 changed files with 30 additions and 21 deletions

View File

@ -142,6 +142,15 @@ Alloc(string target)
return ConvToStr(str);
}
string
AllocEscaped(char[] target)
{
u8[] str = Alloc!(u8)(target.length+1);
str[0 .. target.length] = str[];
str[str.length-1] = '\0';
return cast(string)str[0 .. str.length-1];
}
T[]
Alloc(T)(T[] target, usize start, usize len)
{
@ -937,3 +946,10 @@ bool MallocCheck()
return heap_max_blocks == MallocFreeCount() + MallocUsedCount() + MallocMallocFreshCount();
}
unittest
{
{
string zeroed = AllocZeroed("Test");
assert(zeroed.ptr[4] == '\0');
}
}

View File

@ -470,11 +470,11 @@ struct Vector(T, int N)
return this;
}
ref Vector opAssign(U)(U u) if(isInstanceOf!(Vector, U))
ref Vector opAssign(U)(U rhs) if(isInstanceOf!(Vector, U))
{
static foreach(i; 0 .. N)
{
v[i] = cast(T)u.v[i];
this.v[i] = cast(T)rhs.v[i];
}
return this;
}
@ -488,15 +488,17 @@ struct Vector(T, int N)
{
bool result = true;
foreach(i; 0 .. N)
static foreach(i; 0 .. N)
{
if(Abs(v[i] - other.v[i]) > 0.0000009)
{
result = false;
break;
goto OpEqualsEnd;
}
}
OpEqualsEnd:
return result;
}
@ -512,20 +514,14 @@ struct Vector(T, int N)
Vector opUnary(string op)() if(op == "+" || op == "-" || op == "~" || op == "!")
{
Vector res;
static foreach(i; 0 .. N)
{
mixin("res.v[", i, "] = " ~ op ~ "v[", i, "];\n");
}
return res;
Vector result;
mixin("result.v[] = "~op~"v[];");
return result;
}
ref Vector opOpAssign(string op, U)(U value) if(is(U: Vector))
ref Vector opOpAssign(string op, U)(U rhs) if(is(U: Vector))
{
static foreach(i; 0 .. N)
{
mixin("v[", i, "] " ~op~ "= value.v[", i, "];");
}
mixin("this.v[] "~op~"= rhs.v[];");
return this;
}
@ -559,12 +555,9 @@ struct Vector(T, int N)
Vector
opBinaryRight(string op, U)(U operand) if(IsConvertible!(U) && (op == "*" || op == "+" || op == "-" || op == "/"))
{
Vector res;
static foreach(i; 0 .. N)
{
mixin("res.v[", i, "] = v[", i, "] " ~op~ " operand;");
}
return res;
Vector result;
mixin("result.v = this.v[]"~op~"operand;");
return result;
}
static if(N == 4)