1 module imports.std11863conv;
2
3 import imports.std11863format;
4
to(T)5 template to(T)
6 {
7 T to(A...)(A args)
8 {
9 return toImpl!T(args);
10 }
11 }
12
toImpl(T,S)13 T toImpl(T, S)(S value)
14 {
15 static if (is(S == int) && is(T == string))
16 {
17 // other integral-to-string conversions with default radix
18 return toImpl!(T, S)(value, 10);
19 }
20 else
21 static assert(0);
22 }
23
toImpl(T,S)24 @trusted pure T toImpl(T, S)(S value, uint radix/*, LetterCase letterCase = LetterCase.upper*/)
25 {
26 static assert(is(S == int) && is(T == string));
27
28 alias EEType = char/*Unqual!(typeof(T.init[0]))*/;
29
30 T toStringRadixConvert(size_t bufLen, uint radix = 0, bool neg = false)(uint runtimeRadix = 0)
31 {
32 static if (neg)
33 ulong div = void, mValue = cast(uint)(-value);
34 else
35 uint/*Unsigned!(Unqual!S)*/ div = void, mValue = cast(uint)(value);
36
37 size_t index = bufLen;
38 EEType[bufLen] buffer = void;
39 char baseChar = /*letterCase == LetterCase.lower ? 'a' : */'A';
40 char mod = void;
41
42 do
43 {
44 static if (radix == 0)
45 {
46 div = cast(S)(mValue / runtimeRadix );
47 mod = cast(ubyte)(mValue % runtimeRadix);
48 mod += mod < 10 ? '0' : baseChar - 10;
49 }
50 else static if (radix > 10)
51 {
52 div = cast(S)(mValue / radix );
53 mod = cast(ubyte)(mValue % radix);
54 mod += mod < 10 ? '0' : baseChar - 10;
55 }
56 else
57 {
58 div = cast(S)(mValue / radix);
59 mod = mValue % radix + '0';
60 }
61 buffer[--index] = cast(char)mod;
62 mValue = div;
63 } while (mValue);
64
65 static if (neg)
66 {
67 buffer[--index] = '-';
68 }
69 return cast(T)buffer[index .. $].dup;
70 }
71
72 //enforce(radix >= 2 && radix <= 36, new ConvException("Radix error"));
73
74 switch(radix)
75 {
76 case 10:
77 if (value < 0)
78 return toStringRadixConvert!(S.sizeof * 3 + 1, 10, true)();
79 else
80 return toStringRadixConvert!(S.sizeof * 3, 10)();
81 //case 16:
82 // return toStringRadixConvert!(S.sizeof * 2, 16)();
83 //case 2:
84 // return toStringRadixConvert!(S.sizeof * 8, 2)();
85 //case 8:
86 // return toStringRadixConvert!(S.sizeof * 3, 8)();
87 default:
88 assert(0);//return toStringRadixConvert!(S.sizeof * 6)(radix);
89 }
90 }
91