summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2017-11-12 11:57:47 +0100
committerEugen Wissner <belka@caraus.de>2017-11-12 11:57:47 +0100
commit7797f0a1fefdacc069e8f15e0f103e55b27f2dea (patch)
tree72abe2691098c5e4f1da83c23edfb88f653c099c
parent4bbc8b510afdce8daebc941e46af8f986100f688 (diff)
downloadtanya-7797f0a1fefdacc069e8f15e0f103e55b27f2dea.tar.gz
format.conv.number2String -> format.integral2String (intern)
-rw-r--r--source/tanya/format/conv.d78
-rw-r--r--source/tanya/format/package.d80
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");
+}