Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,8 @@
|
|||
import std.stdio, std.conv, std.string, std.ascii;
|
||||
|
||||
void main() {
|
||||
"1abcd".to!int(16).writeln;
|
||||
|
||||
writeln(60_272_032_366.to!string(36, LetterCase.lower), ' ',
|
||||
591_458.to!string(36, LetterCase.lower));
|
||||
}
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
import std.stdio, std.array, std.ascii;
|
||||
|
||||
immutable string mDigits = digits ~ lowercase;
|
||||
|
||||
ulong atoiRadix(in string str, in uint radix=10, int* consumed=null)
|
||||
nothrow {
|
||||
static int dtoi(in char dc, in uint radix) nothrow {
|
||||
static int[immutable char] digit;
|
||||
immutable char d = dc.toLower;
|
||||
if (digit.length == 0) // Not init yet.
|
||||
foreach (i, c; mDigits)
|
||||
digit[c] = i;
|
||||
if (radix > 1 && radix <= digit.length &&
|
||||
d in digit && digit[d] < radix)
|
||||
return digit[d];
|
||||
return int.min; // A negative for error.
|
||||
}
|
||||
|
||||
ulong result;
|
||||
int sp;
|
||||
for (; sp < str.length; sp++) {
|
||||
immutable int d = dtoi(str[sp], radix);
|
||||
if (d >= 0) // Valid digit char.
|
||||
result = radix * result + d;
|
||||
else
|
||||
break;
|
||||
}
|
||||
if (sp != str.length) // Some char in str not converted.
|
||||
sp = -sp;
|
||||
if (consumed !is null) // Signal error if not positive.
|
||||
*consumed = sp;
|
||||
return result;
|
||||
}
|
||||
|
||||
string itoaRadix(ulong num, in uint radix=10) pure nothrow
|
||||
in {
|
||||
assert(radix > 1 && radix <= mDigits.length);
|
||||
} body {
|
||||
string result;
|
||||
while (num > 0) {
|
||||
immutable uint d = num % radix;
|
||||
result = mDigits[d] ~ result;
|
||||
num = (num - d) / radix;
|
||||
}
|
||||
return result.empty ? "0" : result;
|
||||
}
|
||||
|
||||
void main() {
|
||||
immutable string numStr = "1ABcdxyz???";
|
||||
|
||||
int ate;
|
||||
writef("'%s' (base %d) = %d", numStr, 16,
|
||||
atoiRadix(numStr, 16, &ate));
|
||||
|
||||
if (ate <= 0)
|
||||
writefln("\tConverted only: '%s'", numStr[0 .. -ate]);
|
||||
else
|
||||
writeln();
|
||||
|
||||
writeln(itoaRadix(60_272_032_366, 36), " ",
|
||||
itoaRadix(591_458, 36));
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
import std.stdio, std.algorithm, std.ascii, std.array, std.string;
|
||||
|
||||
alias Digits = ubyte[];
|
||||
|
||||
Digits toBase(ulong number, in ubyte base) pure nothrow @safe {
|
||||
Digits result;
|
||||
while (number) {
|
||||
result = number % base ~ result;
|
||||
number /= base;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
enum fromBase = (in Digits digits, in ubyte base) pure nothrow @safe @nogc =>
|
||||
reduce!((n, k) => n * base + k)(0UL, digits);
|
||||
|
||||
immutable myDigits = digits ~ lowercase;
|
||||
|
||||
enum fromDigits = (in Digits digits) pure nothrow /*@safe*/ =>
|
||||
digits.map!(d => myDigits[d]).array;
|
||||
|
||||
enum convert = (in dchar d) pure nothrow @safe @nogc =>
|
||||
cast(ubyte)(d.isDigit ? d - '0' : std.ascii.toLower(d) - 'a' + 10);
|
||||
|
||||
enum toDigits = (in string number) pure nothrow @safe =>
|
||||
number.representation.map!convert.array;
|
||||
|
||||
void main() {
|
||||
"1ABcd".toDigits.fromBase(16).writeln;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue