mirror of
https://github.com/Kapendev/parin.git
synced 2025-04-26 04:59:54 +03:00
Bug fixes and helper functions.
This commit is contained in:
parent
3bec41c424
commit
0dc6afa4ea
3 changed files with 82 additions and 89 deletions
|
@ -45,6 +45,11 @@ struct Vec2 {
|
|||
this(xy[0], xy[1]);
|
||||
}
|
||||
|
||||
pragma(inline, true)
|
||||
this(IVec2 xy) {
|
||||
this(xy.x, xy.y);
|
||||
}
|
||||
|
||||
pragma(inline, true)
|
||||
Vec2 opUnary(const(char)[] op)() {
|
||||
return Vec2(
|
||||
|
@ -150,7 +155,7 @@ struct Vec2 {
|
|||
}
|
||||
|
||||
const(char)[] toStr() {
|
||||
return "{} {}".format(x, y);
|
||||
return "({} {})".format(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -186,6 +191,11 @@ struct Vec3 {
|
|||
this(xy.x, xy.y, z);
|
||||
}
|
||||
|
||||
pragma(inline, true)
|
||||
this(IVec3 xyz) {
|
||||
this(xyz.x, xyz.y, xyz.z);
|
||||
}
|
||||
|
||||
pragma(inline, true)
|
||||
Vec3 opUnary(const(char)[] op)() {
|
||||
return Vec3(
|
||||
|
@ -253,7 +263,7 @@ struct Vec3 {
|
|||
}
|
||||
|
||||
const(char)[] toStr() {
|
||||
return "{} {} {}".format(x, y, z);
|
||||
return "({} {} {})".format(x, y, z);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -286,6 +296,11 @@ struct Vec4 {
|
|||
this(xyzw[0], xyzw[1], xyzw[2], xyzw[3]);
|
||||
}
|
||||
|
||||
pragma(inline, true)
|
||||
this(IVec4 xyzw) {
|
||||
this(xyzw.x, xyzw.y, xyzw.z, xyzw.w);
|
||||
}
|
||||
|
||||
pragma(inline, true)
|
||||
Vec4 opUnary(const(char)[] op)() {
|
||||
return Vec4(
|
||||
|
@ -359,7 +374,7 @@ struct Vec4 {
|
|||
}
|
||||
|
||||
const(char)[] toStr() {
|
||||
return "{} {} {} {}".format(x, y, z, w);
|
||||
return "({} {} {} {})".format(x, y, z, w);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -683,7 +698,7 @@ struct Rect {
|
|||
}
|
||||
|
||||
const(char)[] toStr() {
|
||||
return "{} {} {} {}".format(position.x, position.y, size.x, size.y);
|
||||
return "({} {} {} {})".format(position.x, position.y, size.x, size.y);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -708,7 +723,7 @@ struct Circ {
|
|||
}
|
||||
|
||||
const(char)[] toStr() {
|
||||
return "{} {} {}".format(position.x, position.y, radius);
|
||||
return "({} {} {})".format(position.x, position.y, radius);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -743,7 +758,7 @@ struct Line {
|
|||
}
|
||||
|
||||
const(char)[] toStr() {
|
||||
return "{} {} {} {}".format(a.x, a.y, b.x, b.y);
|
||||
return "({} {} {} {})".format(a.x, a.y, b.x, b.y);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -774,11 +789,11 @@ struct IVec2 {
|
|||
|
||||
pragma(inline, true)
|
||||
this(Vec2 xy) {
|
||||
this(cast(int) floor(xy.x), cast(int) floor(xy.y));
|
||||
this(cast(int) xy.x, cast(int) xy.y);
|
||||
}
|
||||
|
||||
const(char)[] toStr() {
|
||||
return "{} {}".format(x, y);
|
||||
return "({} {})".format(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -816,11 +831,11 @@ struct IVec3 {
|
|||
|
||||
pragma(inline, true)
|
||||
this(Vec3 xyz) {
|
||||
this(cast(int) floor(xyz.x), cast(int) floor(xyz.y), cast(int) floor(xyz.z));
|
||||
this(cast(int) xyz.x, cast(int) xyz.y, cast(int) xyz.z);
|
||||
}
|
||||
|
||||
const(char)[] toStr() {
|
||||
return "{} {}".format(x, y);
|
||||
return "({} {})".format(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -855,11 +870,11 @@ struct IVec4 {
|
|||
|
||||
pragma(inline, true)
|
||||
this(Vec4 xyzw) {
|
||||
this(cast(int) floor(xyzw.x), cast(int) floor(xyzw.y), cast(int) floor(xyzw.z), cast(int) floor(xyzw.w));
|
||||
this(cast(int) xyzw.x, cast(int) xyzw.y, cast(int) xyzw.z, cast(int) xyzw.w);
|
||||
}
|
||||
|
||||
const(char)[] toStr() {
|
||||
return "{} {}".format(x, y);
|
||||
return "({} {})".format(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -964,3 +979,27 @@ bool equals(Vec3 a, Vec3 b) {
|
|||
bool equals(Vec4 a, Vec4 b) {
|
||||
return equals(a.x, b.x) && equals(a.y, b.y) && equals(a.z, b.z) && equals(a.w, b.w);
|
||||
}
|
||||
|
||||
Vec2 toVec(IVec2 vec) {
|
||||
return Vec2(vec);
|
||||
}
|
||||
|
||||
Vec3 toVec(IVec3 vec) {
|
||||
return Vec3(vec);
|
||||
}
|
||||
|
||||
Vec4 toVec(IVec4 vec) {
|
||||
return Vec4(vec);
|
||||
}
|
||||
|
||||
IVec2 toIVec(Vec2 vec) {
|
||||
return IVec2(vec);
|
||||
}
|
||||
|
||||
IVec3 toIVec(Vec3 vec) {
|
||||
return IVec3(vec);
|
||||
}
|
||||
|
||||
IVec4 toIVec(Vec4 vec) {
|
||||
return IVec4(vec);
|
||||
}
|
|
@ -32,7 +32,6 @@ enum ToValueResultError : ubyte {
|
|||
}
|
||||
|
||||
struct ToStrOptions {
|
||||
bool boolIsShort = false;
|
||||
ubyte floatPrecision = 2;
|
||||
}
|
||||
|
||||
|
@ -161,15 +160,6 @@ bool startsWith(const(char)[] str, char start) {
|
|||
return startsWith(str, charToStr(start));
|
||||
}
|
||||
|
||||
bool startsWithIgnoreCase(const(char)[] str, const(char)[] start) {
|
||||
if (str.length < start.length) return false;
|
||||
return str[0 .. start.length].equalsIgnoreCase(start);
|
||||
}
|
||||
|
||||
bool startsWithIgnoreCase(const(char)[] str, char start) {
|
||||
return startsWithIgnoreCase(str, charToStr(start));
|
||||
}
|
||||
|
||||
bool endsWith(const(char)[] str, const(char)[] end) {
|
||||
if (str.length < end.length) return false;
|
||||
return str[$ - end.length .. $] == end;
|
||||
|
@ -179,15 +169,6 @@ bool endsWith(const(char)[] str, char end) {
|
|||
return endsWith(str, charToStr(end));
|
||||
}
|
||||
|
||||
bool endsWithIgnoreCase(const(char)[] str, const(char)[] end) {
|
||||
if (str.length < end.length) return false;
|
||||
return str[$ - end.length .. $].equalsIgnoreCase(end);
|
||||
}
|
||||
|
||||
bool endsWithIgnoreCase(const(char)[] str, char end) {
|
||||
return endsWithIgnoreCase(str, charToStr(end));
|
||||
}
|
||||
|
||||
int count(const(char)[] str, const(char)[] item) {
|
||||
int result = 0;
|
||||
if (str.length < item.length || item.length == 0) return result;
|
||||
|
@ -204,25 +185,9 @@ int count(const(char)[] str, char item) {
|
|||
return count(str, charToStr(item));
|
||||
}
|
||||
|
||||
int countIgnoreCase(const(char)[] str, const(char)[] item) {
|
||||
int result = 0;
|
||||
if (str.length < item.length || item.length == 0) return result;
|
||||
foreach (i; 0 .. str.length - item.length) {
|
||||
if (str[i .. i + item.length].equalsIgnoreCase(item)) {
|
||||
result += 1;
|
||||
i += item.length - 1;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int countIgnoreCase(const(char)[] str, char item) {
|
||||
return countIgnoreCase(str, charToStr(item));
|
||||
}
|
||||
|
||||
int findStart(const(char)[] str, const(char)[] item) {
|
||||
if (str.length < item.length || item.length == 0) return -1;
|
||||
foreach (i; 0 .. str.length - item.length) {
|
||||
foreach (i; 0 .. str.length - item.length + 1) {
|
||||
if (str[i .. i + item.length] == item) return cast(int) i;
|
||||
}
|
||||
return -1;
|
||||
|
@ -232,21 +197,9 @@ int findStart(const(char)[] str, char item) {
|
|||
return findStart(str, charToStr(item));
|
||||
}
|
||||
|
||||
int findStartIgnoreCase(const(char)[] str, const(char)[] item) {
|
||||
if (str.length < item.length || item.length == 0) return -1;
|
||||
foreach (i; 0 .. str.length - item.length) {
|
||||
if (str[i .. i + item.length].equalsIgnoreCase(item)) return cast(int) i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int findStartIgnoreCase(const(char)[] str, char item) {
|
||||
return findStartIgnoreCase(str, charToStr(item));
|
||||
}
|
||||
|
||||
int findEnd(const(char)[] str, const(char)[] item) {
|
||||
if (str.length < item.length || item.length == 0) return -1;
|
||||
foreach_reverse (i; 0 .. str.length - item.length) {
|
||||
foreach_reverse (i; 0 .. str.length - item.length + 1) {
|
||||
if (str[i .. i + item.length] == item) return cast(int) i;
|
||||
}
|
||||
return -1;
|
||||
|
@ -256,18 +209,6 @@ int findEnd(const(char)[] str, char item) {
|
|||
return findEnd(str, charToStr(item));
|
||||
}
|
||||
|
||||
int findEndIgnoreCase(const(char)[] str, const(char)[] item) {
|
||||
if (str.length < item.length || item.length == 0) return -1;
|
||||
foreach_reverse (i; 0 .. str.length - item.length) {
|
||||
if (str[i .. i + item.length].equalsIgnoreCase(item)) return cast(int) i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int findEndIgnoreCase(const(char)[] str, char item) {
|
||||
return findEndIgnoreCase(str, charToStr(item));
|
||||
}
|
||||
|
||||
const(char)[] advance(const(char)[] str, size_t amount) {
|
||||
if (str.length < amount) {
|
||||
return str[$ .. $];
|
||||
|
@ -387,12 +328,8 @@ const(char)[] skipLine(ref inout(char)[] str) {
|
|||
return skipValue(str, '\n');
|
||||
}
|
||||
|
||||
const(char)[] boolToStr(bool value, bool isShort = false) {
|
||||
if (isShort) {
|
||||
return value ? "t" : "f";
|
||||
} else {
|
||||
return value ? "true" : "false";
|
||||
}
|
||||
const(char)[] boolToStr(bool value) {
|
||||
return value ? "true" : "false";
|
||||
}
|
||||
|
||||
const(char)[] charToStr(char value) {
|
||||
|
@ -519,7 +456,7 @@ const(char)[] toStr(T)(T value, ToStrOptions options = ToStrOptions()) {
|
|||
static if (isCharType!T) {
|
||||
return charToStr(value);
|
||||
} else static if (isBoolType!T) {
|
||||
return boolToStr(value, options.boolIsShort);
|
||||
return boolToStr(value);
|
||||
} else static if (isUnsignedType!T) {
|
||||
return unsignedToStr(value);
|
||||
} else static if (isSignedType!T) {
|
||||
|
@ -541,9 +478,9 @@ const(char)[] toStr(T)(T value, ToStrOptions options = ToStrOptions()) {
|
|||
|
||||
ToValueResult!bool toBool(const(char)[] str) {
|
||||
auto result = ToValueResult!bool();
|
||||
if (str == "t" || str == "true") {
|
||||
if (str == "true") {
|
||||
result.value = true;
|
||||
} else if (str == "f" || str == "false") {
|
||||
} else if (str == "false") {
|
||||
result.value = false;
|
||||
} else {
|
||||
result.error = ToValueResultError.invalid;
|
||||
|
|
|
@ -15,10 +15,12 @@ union SumTypeData(A...) {
|
|||
}
|
||||
|
||||
enum kindCount = A.length;
|
||||
|
||||
alias BaseType = A[0];
|
||||
alias Types = A;
|
||||
}
|
||||
|
||||
alias SumTypeKind = ubyte;
|
||||
alias SumTypeKind = int;
|
||||
|
||||
struct SumType(A...) {
|
||||
SumTypeData!A data;
|
||||
|
@ -134,7 +136,7 @@ T toSumType(T)(SumTypeKind kind) {
|
|||
T result;
|
||||
static foreach (i, Type; T.Types) {
|
||||
if (i == kind) {
|
||||
result = Type();
|
||||
result = Type.init;
|
||||
goto loopExit;
|
||||
}
|
||||
}
|
||||
|
@ -142,23 +144,38 @@ T toSumType(T)(SumTypeKind kind) {
|
|||
return result;
|
||||
}
|
||||
|
||||
T toSumType(T)(const(char)[] kindName) {
|
||||
static assert(isSumType!T, "Type `" ~ T.stringof ~ "` is not a sum type.");
|
||||
|
||||
T result;
|
||||
static foreach (i, Type; T.Types) {
|
||||
if (Type.stringof == kindName) {
|
||||
result = Type.init;
|
||||
goto loopExit;
|
||||
}
|
||||
}
|
||||
import popka.core.io;
|
||||
loopExit:
|
||||
return result;
|
||||
}
|
||||
|
||||
bool isSumType(T)() {
|
||||
return is(T : SumType!A, A...);
|
||||
}
|
||||
|
||||
bool hasCommonBase(T)() {
|
||||
int checkCommonBase(T)() {
|
||||
static assert(isSumType!T, "Type `" ~ T.stringof ~ "` is not a sum type.");
|
||||
|
||||
static foreach (member; T.init.data.tupleof[1 .. $]) {
|
||||
static foreach (i, member; T.init.data.tupleof[1 .. $]) {
|
||||
static if (isPrimaryType!(typeof(member)) || member.tupleof.length == 0) {
|
||||
static if (!is(T.BaseType == typeof(member))) {
|
||||
return false;
|
||||
return i + 1;
|
||||
}
|
||||
} else static if (!is(T.BaseType == typeof(member.tupleof[0]))) {
|
||||
return false;
|
||||
return i + 1;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return -1;
|
||||
}
|
||||
|
||||
mixin template addBase(T) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue