00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 module mango.format.Long;
00040
00041 private import mango.format.Int,
00042 mango.format.Number;
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 class Long : Number
00054 {
00055
00056
00057
00058
00059 final static tChar[] format (long i)
00060 {
00061 return format (new tChar[32], i);
00062 }
00063
00064
00065
00066
00067
00068
00069 final static tChar[] format (ulong i, Radix radix, tChar fill = tChar.init)
00070 {
00071 return format (new tChar[64], i, radix, fill);
00072 }
00073
00074
00075
00076
00077
00078
00079 final static tChar[] format (tChar[] dst, long i)
00080 in {
00081 assert (dst.length);
00082 }
00083 body
00084 {
00085 if (i >= 0)
00086 return format (dst, i, Radix.Decimal);
00087
00088 uint len = dst.length - format (dst[1..length], -i, Radix.Decimal).length;
00089 dst [--len] = '-';
00090 return dst [len..length];
00091 }
00092
00093
00094
00095
00096
00097
00098 final static tChar[] format (tChar[] dst, ulong i, Radix radix, tChar fill = tChar.init)
00099 {
00100 if (i <= uint.max)
00101 return Int.format (dst, i, radix, fill);
00102
00103 uint len = dst.length;
00104 char* p = dst.ptr + len;
00105
00106 if (len)
00107 switch (radix)
00108 {
00109 case Radix.Binary:
00110 do {
00111 *--p = '0' + (i & 1);
00112 } while ((i >>>= 1) && --len);
00113 break;
00114
00115 case Radix.Octal:
00116 do {
00117 *--p = '0' + (i & 7);
00118 } while ((i >>>= 3) && --len);
00119 break;
00120
00121 case Radix.Hexadecimal:
00122 do {
00123 char c = i & 0x0f;
00124 *--p = c + ((c < 10) ? '0' : ('a' - 10));
00125 } while ((i >>= 4) && --len);
00126 break;
00127
00128 default:
00129 do {
00130 *--p = '0' + (i % 10);
00131 } while ((i /= 10) && --len);
00132 break;
00133 }
00134
00135 if (! len)
00136 throw error;
00137
00138 if (fill != tChar.init)
00139 while (p > dst.ptr)
00140 *--p = fill;
00141
00142 return dst[p-dst.ptr..length];
00143 }
00144
00145
00146
00147
00148
00149
00150 final static ulong parse (tChar[] src, Radix radix=Radix.Decimal, uint* ate=null)
00151 {
00152 bool sign;
00153 uint count = trim (src, sign, &radix);
00154
00155 ulong value;
00156 foreach (tChar c; src[count..length])
00157 {
00158 if (c >= '0' || c <= '9')
00159 {}
00160 else
00161 if (c >= 'a' && c <= 'f')
00162 c -= 39;
00163 else
00164 if (c >= 'A' && c <= 'F')
00165 c -= 7;
00166 else
00167 break;
00168
00169 value = value * radix + (c - '0');
00170 ++count;
00171 }
00172
00173 if (ate)
00174 *ate = count;
00175 return sign ? -value : value;
00176 }
00177 }
00178
00179