| 1 |
/+ |
|---|
| 2 |
Copyright (c) 2006 Eric Anderton, Tomasz Stachowiak |
|---|
| 3 |
|
|---|
| 4 |
Permission is hereby granted, free of charge, to any person |
|---|
| 5 |
obtaining a copy of this software and associated documentation |
|---|
| 6 |
files (the "Software"), to deal in the Software without |
|---|
| 7 |
restriction, including without limitation the rights to use, |
|---|
| 8 |
copy, modify, merge, publish, distribute, sublicense, and/or |
|---|
| 9 |
sell copies of the Software, and to permit persons to whom the |
|---|
| 10 |
Software is furnished to do so, subject to the following |
|---|
| 11 |
conditions: |
|---|
| 12 |
|
|---|
| 13 |
The above copyright notice and this permission notice shall be |
|---|
| 14 |
included in all copies or substantial portions of the Software. |
|---|
| 15 |
|
|---|
| 16 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|---|
| 17 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|---|
| 18 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|---|
| 19 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|---|
| 20 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|---|
| 21 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|---|
| 22 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|---|
| 23 |
OTHER DEALINGS IN THE SOFTWARE. |
|---|
| 24 |
+/ |
|---|
| 25 |
module enki.types; |
|---|
| 26 |
|
|---|
| 27 |
/+private import std.string; |
|---|
| 28 |
private import std.utf; |
|---|
| 29 |
private import std.conv;+/ |
|---|
| 30 |
import tango.text.Util; |
|---|
| 31 |
import tango.text.Unicode; |
|---|
| 32 |
import tango.util.Convert; |
|---|
| 33 |
static import tango.text.convert.Utf; |
|---|
| 34 |
|
|---|
| 35 |
version(EnkiUTF32){ |
|---|
| 36 |
alias dchar[] String; |
|---|
| 37 |
alias dchar Char; |
|---|
| 38 |
alias to!(dchar[]) transcodeToString; |
|---|
| 39 |
alias to!(char[]) transcodeToUTF8; |
|---|
| 40 |
} |
|---|
| 41 |
else version(EnkiUTF16){ |
|---|
| 42 |
alias wchar[] String; |
|---|
| 43 |
alias wchar Char; |
|---|
| 44 |
alias to!(wchar[]) transcodeToString; |
|---|
| 45 |
alias to!(char[]) transcodeToUTF8; |
|---|
| 46 |
} |
|---|
| 47 |
else{ |
|---|
| 48 |
alias char[] String; |
|---|
| 49 |
alias char Char; |
|---|
| 50 |
alias to!(char[]) transcodeToString; |
|---|
| 51 |
char[] transcodeToUTF8(char[] value){ return value; } |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
alias to!(char[]) toString; |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
// implicitly converable |
|---|
| 58 |
template convert(V,U : V){ |
|---|
| 59 |
U convert(V value){ |
|---|
| 60 |
return value; |
|---|
| 61 |
} |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
// conversion to string |
|---|
| 65 |
template convert(V,U:String){ |
|---|
| 66 |
String convert(V value){ |
|---|
| 67 |
static if(is(V == struct) || is(V == class)){ |
|---|
| 68 |
return value.toString(); |
|---|
| 69 |
} |
|---|
| 70 |
else{ |
|---|
| 71 |
static if(is(V == char[]) || is(V == wchar[])){ |
|---|
| 72 |
return transcodeToString(value); |
|---|
| 73 |
} |
|---|
| 74 |
else{ |
|---|
| 75 |
static if(is(V == dchar) || is(V == wchar) || is(V == char)){ |
|---|
| 76 |
Char[] temp = new Char[1]; |
|---|
| 77 |
temp[0] = value; |
|---|
| 78 |
return transcodeToString(temp); |
|---|
| 79 |
} |
|---|
| 80 |
else{ |
|---|
| 81 |
return transcodeToString(to!(char[])(value)); |
|---|
| 82 |
} |
|---|
| 83 |
} |
|---|
| 84 |
} |
|---|
| 85 |
} |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
// conversion from string |
|---|
| 89 |
template convert(V:String,U){ |
|---|
| 90 |
U convert(V value){ |
|---|
| 91 |
static if(is(U == int)){ |
|---|
| 92 |
return to!(U)(value); |
|---|
| 93 |
} |
|---|
| 94 |
else static if(is(U == uint)){ |
|---|
| 95 |
return to!(U)(value); |
|---|
| 96 |
} |
|---|
| 97 |
else static if(is(U == long)){ |
|---|
| 98 |
return to!(U)(value); |
|---|
| 99 |
} |
|---|
| 100 |
else static if(is(U == ulong)){ |
|---|
| 101 |
return to!(U)(value); |
|---|
| 102 |
} |
|---|
| 103 |
else static if(is(U == short)){ |
|---|
| 104 |
return to!(U)(value); |
|---|
| 105 |
} |
|---|
| 106 |
else static if(is(U == ushort)){ |
|---|
| 107 |
return to!(U)(value); |
|---|
| 108 |
} |
|---|
| 109 |
else static if(is(U == byte)){ |
|---|
| 110 |
return to!(U)(value); |
|---|
| 111 |
} |
|---|
| 112 |
else static if(is(U == ubyte)){ |
|---|
| 113 |
return to!(U)(value); |
|---|
| 114 |
} |
|---|
| 115 |
else static if(is(U == float)){ |
|---|
| 116 |
return to!(U)(value); |
|---|
| 117 |
} |
|---|
| 118 |
else static if(is(U == double)){ |
|---|
| 119 |
return to!(U)(value); |
|---|
| 120 |
} |
|---|
| 121 |
else static if(is(U == real)){ |
|---|
| 122 |
return to!(U)(value); |
|---|
| 123 |
} |
|---|
| 124 |
else static if(is(U == bool)){ |
|---|
| 125 |
return (value != null && value.length > 0); |
|---|
| 126 |
} |
|---|
| 127 |
else static if(is(U == char)){ |
|---|
| 128 |
static assert(false); |
|---|
| 129 |
} |
|---|
| 130 |
else static if(is(U == wchar)){ |
|---|
| 131 |
static assert(false); |
|---|
| 132 |
} |
|---|
| 133 |
else static if(is(U == dchar)){ |
|---|
| 134 |
static assert(false); |
|---|
| 135 |
} |
|---|
| 136 |
else{ |
|---|
| 137 |
// last ditch effort |
|---|
| 138 |
return cast(U)value; |
|---|
| 139 |
} |
|---|
| 140 |
} |
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
template convert(V,U : U[]){ |
|---|
| 144 |
U convert(V value){ |
|---|
| 145 |
return cast(U)value; |
|---|
| 146 |
} |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
// else |
|---|
| 150 |
template convert(V,U){ |
|---|
| 151 |
U convert(V value){ |
|---|
| 152 |
static if(is(U : V)){ |
|---|
| 153 |
return value; |
|---|
| 154 |
} |
|---|
| 155 |
else{ |
|---|
| 156 |
return cast(U)value; |
|---|
| 157 |
} |
|---|
| 158 |
} |
|---|
| 159 |
} |
|---|
| 160 |
|
|---|
| 161 |
template smartAssignCat(U,V){ |
|---|
| 162 |
void smartAssignCat(inout U u,V v){ |
|---|
| 163 |
u = convert!(V,U)(v); |
|---|
| 164 |
} |
|---|
| 165 |
} |
|---|
| 166 |
|
|---|
| 167 |
template smartAssignCat(U : U[],V : U){ |
|---|
| 168 |
void smartAssignCat(inout U[] u,V v){ |
|---|
| 169 |
u ~= v; |
|---|
| 170 |
} |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
template smartAssignCat(U : U[],V){ |
|---|
| 174 |
void smartAssignCat(inout U[] u,V v){ |
|---|
| 175 |
u ~= convert!(V,U[])(v); |
|---|
| 176 |
} |
|---|
| 177 |
} |
|---|
| 178 |
|
|---|
| 179 |
template smartAssign(U,V){ |
|---|
| 180 |
void smartAssign(inout U u,V v){ |
|---|
| 181 |
u = convert!(V,U)(v); |
|---|
| 182 |
} |
|---|
| 183 |
} |
|---|
| 184 |
/* |
|---|
| 185 |
template smartAssign(U : U[],V){ |
|---|
| 186 |
void smartAssign(inout U[] u,V v){ |
|---|
| 187 |
static if(is(V : U[])){ |
|---|
| 188 |
u = convert!(V,U[])(v); |
|---|
| 189 |
} |
|---|
| 190 |
else{ |
|---|
| 191 |
u = convert!(V,U[])(v); |
|---|
| 192 |
//u[0] = convert!(V,U[])(v); |
|---|
| 193 |
} |
|---|
| 194 |
} |
|---|
| 195 |
}*/ |
|---|
| 196 |
|
|---|
| 197 |
// template tuple type to help with parsing |
|---|
| 198 |
struct ResultT(T){ |
|---|
| 199 |
T result; |
|---|
| 200 |
bool success; |
|---|
| 201 |
|
|---|
| 202 |
alias T Type; |
|---|
| 203 |
alias typeof(*this) ThisType; |
|---|
| 204 |
|
|---|
| 205 |
public static ThisType opCall(T result){ |
|---|
| 206 |
ThisType _this; |
|---|
| 207 |
_this.result = result; |
|---|
| 208 |
_this.success = true; |
|---|
| 209 |
return _this; |
|---|
| 210 |
} |
|---|
| 211 |
|
|---|
| 212 |
public static ThisType opCall(T result,bool success){ |
|---|
| 213 |
ThisType _this; |
|---|
| 214 |
_this.result = result; |
|---|
| 215 |
_this.success = success; |
|---|
| 216 |
return _this; |
|---|
| 217 |
} |
|---|
| 218 |
|
|---|
| 219 |
public static ThisType opCall(){ |
|---|
| 220 |
ThisType _this; |
|---|
| 221 |
_this.success = false; |
|---|
| 222 |
return _this; |
|---|
| 223 |
} |
|---|
| 224 |
|
|---|
| 225 |
public bool assignCat(V)(inout V value){ |
|---|
| 226 |
if(this.success) smartAssignCat!(V,Type)(value,this.result); |
|---|
| 227 |
return this.success; |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
public bool assign(V)(inout V value){ |
|---|
| 231 |
if(this.success) smartAssign!(V,Type)(value,this.result);; |
|---|
| 232 |
return this.success; |
|---|
| 233 |
} |
|---|
| 234 |
|
|---|
| 235 |
public bool opCast(){ |
|---|
| 236 |
return this.success; |
|---|
| 237 |
} |
|---|
| 238 |
} |
|---|