From 7797f0a1fefdacc069e8f15e0f103e55b27f2dea Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Sun, 12 Nov 2017 11:57:47 +0100 Subject: [PATCH] format.conv.number2String -> format.integral2String (intern) --- source/tanya/format/conv.d | 78 ---------------------------------- source/tanya/format/package.d | 80 ++++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 79 deletions(-) diff --git a/source/tanya/format/conv.d b/source/tanya/format/conv.d index a86a8cd..0209d80 100644 --- a/source/tanya/format/conv.d +++ b/source/tanya/format/conv.d @@ -355,81 +355,3 @@ if (isIntegral!From && is(To == enum)) throw make!ConvException(defaultAllocator, "Value not found in enum '" ~ To.stringof ~ "'"); } - -// Returns the last part of buffer with converted number. -package(tanya) char[] number2String(T)(const T number, - return ref char[21] buffer) -if (isIntegral!T) -{ - // abs the integer. - ulong n64 = number < 0 ? -cast(long) number : number; - - char* start = buffer[].ptr + buffer.sizeof - 1; - - while (true) - { - // Do in 32-bit chunks (avoid lots of 64-bit divides even with constant - // denominators). - char* o = start - 8; - uint n; - if (n64 >= 100000000) - { - n = n64 % 100000000; - n64 /= 100000000; - } - else - { - n = cast(uint) n64; - n64 = 0; - } - - while (n) - { - *--start = cast(char) (n % 10) + '0'; - n /= 10; - } - // Ignore the leading zero if it was the last part of the integer. - if (n64 == 0) - { - if ((start[0] == '0') - && (start != (buffer[].ptr + buffer.sizeof -1))) - { - ++start; - } - break; - } - // Copy leading zeros if it wasn't the most significant part of the - // integer. - while (start != o) - { - *--start = '0'; - } - } - - // Get the length that we have copied. - uint l = cast(uint) ((buffer[].ptr + buffer.sizeof - 1) - start); - if (l == 0) - { - *--start = '0'; - l = 1; - } - else if (number < 0) // Set the sign. - { - *--start = '-'; - ++l; - } - - return buffer[$ - l - 1 .. $ - 1]; -} - -// Converting an integer to string. -@nogc nothrow pure @system unittest -{ - char[21] buf; - - assert(number2String(80, buf) == "80"); - assert(number2String(-80, buf) == "-80"); - assert(number2String(0, buf) == "0"); - assert(number2String(uint.max, buf) == "4294967295"); - assert(number2String(int.min, buf) == "-2147483648"); -} diff --git a/source/tanya/format/package.d b/source/tanya/format/package.d index 152321b..9f226bd 100644 --- a/source/tanya/format/package.d +++ b/source/tanya/format/package.d @@ -14,4 +14,82 @@ */ module tanya.format; -public import tanya.format.conv; \ No newline at end of file +public import tanya.format.conv; +import tanya.meta.trait; + +// Returns the last part of buffer with converted number. +package(tanya) char[] integral2String(T)(T number, return ref char[21] buffer) +if (isIntegral!T) +{ + // abs the integer. + ulong n64 = number < 0 ? -cast(long) number : number; + + char* start = buffer[].ptr + buffer.sizeof - 1; + + while (true) + { + // Do in 32-bit chunks (avoid lots of 64-bit divides even with constant + // denominators). + char* o = start - 8; + uint n; + if (n64 >= 100000000) + { + n = n64 % 100000000; + n64 /= 100000000; + } + else + { + n = cast(uint) n64; + n64 = 0; + } + + while (n) + { + *--start = cast(char) (n % 10) + '0'; + n /= 10; + } + // Ignore the leading zero if it was the last part of the integer. + if (n64 == 0) + { + if ((start[0] == '0') + && (start != (buffer[].ptr + buffer.sizeof -1))) + { + ++start; + } + break; + } + // Copy leading zeros if it wasn't the most significant part of the + // integer. + while (start != o) + { + *--start = '0'; + } + } + + // Get the length that we have copied. + uint l = cast(uint) ((buffer[].ptr + buffer.sizeof - 1) - start); + if (l == 0) + { + *--start = '0'; + l = 1; + } + else if (number < 0) // Set the sign. + { + *--start = '-'; + ++l; + } + + return buffer[$ - l - 1 .. $ - 1]; +} + +// Converting an integer to string. +@nogc nothrow pure @system unittest +{ + char[21] buf; + + assert(integral2String(80, buf) == "80"); + assert(integral2String(-80, buf) == "-80"); + assert(integral2String(0, buf) == "0"); + assert(integral2String(uint.max, buf) == "4294967295"); + assert(integral2String(int.min, buf) == "-2147483648"); +}