mirror of https://github.com/adamdruppe/arsd.git
stuff minigui can use
This commit is contained in:
parent
39be258048
commit
8153ef9330
65
conv.d
65
conv.d
|
|
@ -16,6 +16,17 @@ static import arsd.core;
|
||||||
Converts a string into the other given type. Throws on failure.
|
Converts a string into the other given type. Throws on failure.
|
||||||
+/
|
+/
|
||||||
T to(T)(scope const(char)[] str) {
|
T to(T)(scope const(char)[] str) {
|
||||||
|
static if(is(T == enum)) {
|
||||||
|
switch(str) {
|
||||||
|
default:
|
||||||
|
throw new EnumConvException(T.stringof, str.idup);
|
||||||
|
foreach(memberName; __traits(allMembers, T))
|
||||||
|
case memberName:
|
||||||
|
return __traits(getMember, T, memberName);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
static if(is(T : long)) {
|
static if(is(T : long)) {
|
||||||
// FIXME: unsigned? overflowing? radix? keep reading or stop on invalid char?
|
// FIXME: unsigned? overflowing? radix? keep reading or stop on invalid char?
|
||||||
StringToIntArgs args;
|
StringToIntArgs args;
|
||||||
|
|
@ -25,7 +36,9 @@ T to(T)(scope const(char)[] str) {
|
||||||
if(ret != v)
|
if(ret != v)
|
||||||
throw new StringToIntConvException("overflow", 0, str.idup, 0);
|
throw new StringToIntConvException("overflow", 0, str.idup, 0);
|
||||||
return ret;
|
return ret;
|
||||||
} else static if(is(T : double)) {
|
}
|
||||||
|
else
|
||||||
|
static if(is(T : double)) {
|
||||||
import core.stdc.stdlib;
|
import core.stdc.stdlib;
|
||||||
import core.stdc.errno;
|
import core.stdc.errno;
|
||||||
arsd.core.CharzBuffer z = str;
|
arsd.core.CharzBuffer z = str;
|
||||||
|
|
@ -38,7 +51,9 @@ T to(T)(scope const(char)[] str) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
static assert(0, "Unsupported type: " ~ T.stringof);
|
static assert(0, "Unsupported type: " ~ T.stringof);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -53,6 +68,16 @@ string to(T:string, From)(From value) {
|
||||||
return arsd.core.toStringInternal(value);
|
return arsd.core.toStringInternal(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/++
|
||||||
|
Converts ints to other types of ints or enums
|
||||||
|
+/
|
||||||
|
T to(T)(long value) {
|
||||||
|
static if(is(T == enum))
|
||||||
|
return cast(T) value; // FIXME check if the value is actually in range
|
||||||
|
else
|
||||||
|
return checkedConversion!T(value);
|
||||||
|
}
|
||||||
|
|
||||||
/+
|
/+
|
||||||
T to(T, F)(F value) if(!is(F : const(char)[])) {
|
T to(T, F)(F value) if(!is(F : const(char)[])) {
|
||||||
// if the language allows implicit conversion, let it do its thing
|
// if the language allows implicit conversion, let it do its thing
|
||||||
|
|
@ -120,7 +145,16 @@ unittest {
|
||||||
/++
|
/++
|
||||||
|
|
||||||
+/
|
+/
|
||||||
class ValueOutOfRangeException : arsd.core.ArsdExceptionBase {
|
class ConvException : arsd.core.ArsdExceptionBase {
|
||||||
|
this(string msg, string file, size_t line) {
|
||||||
|
super(msg, file, line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/++
|
||||||
|
|
||||||
|
+/
|
||||||
|
class ValueOutOfRangeException : ConvException {
|
||||||
this(string type, long userSuppliedValue, long minimumAcceptableValue, long maximumAcceptableValue, string file = __FILE__, size_t line = __LINE__) {
|
this(string type, long userSuppliedValue, long minimumAcceptableValue, long maximumAcceptableValue, string file = __FILE__, size_t line = __LINE__) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.userSuppliedValue = userSuppliedValue;
|
this.userSuppliedValue = userSuppliedValue;
|
||||||
|
|
@ -142,6 +176,31 @@ class ValueOutOfRangeException : arsd.core.ArsdExceptionBase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/++
|
||||||
|
|
||||||
|
+/
|
||||||
|
class EnumConvException : ConvException {
|
||||||
|
this(string type, string userSuppliedValue, string file = __FILE__, size_t line = __LINE__) {
|
||||||
|
this.type = type;
|
||||||
|
this.userSuppliedValue = userSuppliedValue;
|
||||||
|
|
||||||
|
super("No such enum value", file, line);
|
||||||
|
|
||||||
|
}
|
||||||
|
string type;
|
||||||
|
string userSuppliedValue;
|
||||||
|
|
||||||
|
override void getAdditionalPrintableInformation(scope void delegate(string name, in char[] value) sink) const {
|
||||||
|
sink("type", type);
|
||||||
|
sink("userSuppliedValue", userSuppliedValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
enum A { a, b, c }
|
||||||
|
// to!A("d");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/++
|
/++
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue