Skip to content

Commit

Permalink
Merge pull request #8866 from eoan-ermine/issue#24075
Browse files Browse the repository at this point in the history
Fix issue 24075 - Can't use toChars with `ushort` or `ubyte`
  • Loading branch information
dkorpel authored Dec 30, 2023
2 parents 216e572 + b29f127 commit 713ac80
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions std/conv.d
Original file line number Diff line number Diff line change
Expand Up @@ -5712,17 +5712,16 @@ private auto hexStrLiteral(String)(scope String hexData)
* radix = 2, 8, 10, 16
* Char = character type for output
* letterCase = lower for deadbeef, upper for DEADBEEF
* value = integer to convert. Can be uint or ulong. If radix is 10, can also be
* int or long.
* value = integer to convert. Can be ubyte, ushort, uint or ulong. If radix
* is 10, can also be byte, short, int or long.
* Returns:
* Random access range with slicing and everything
*/

auto toChars(ubyte radix = 10, Char = char, LetterCase letterCase = LetterCase.lower, T)(T value)
pure nothrow @nogc @safe
if ((radix == 2 || radix == 8 || radix == 10 || radix == 16) &&
(is(immutable T == immutable uint) || is(immutable T == immutable ulong) ||
radix == 10 && (is(immutable T == immutable int) || is(immutable T == immutable long))))
isIntegral!T && (radix == 10 || isUnsigned!T))
{
alias UT = Unqual!T;

Expand Down Expand Up @@ -5870,8 +5869,12 @@ if ((radix == 2 || radix == 8 || radix == 10 || radix == 16) &&
assert(toChars(123) == toChars(123));

{
assert(toChars!2(ubyte(0)).array == "0");
assert(toChars!2(ushort(0)).array == "0");
assert(toChars!2(0u).array == "0");
assert(toChars!2(0Lu).array == "0");
assert(toChars!2(ubyte(1)).array == "1");
assert(toChars!2(ushort(1)).array == "1");
assert(toChars!2(1u).array == "1");
assert(toChars!2(1Lu).array == "1");

Expand All @@ -5884,10 +5887,14 @@ if ((radix == 2 || radix == 8 || radix == 10 || radix == 16) &&
assert(s.retro.array == "01");
}
{
assert(toChars!8(ubyte(0)).array == "0");
assert(toChars!8(ushort(0)).array == "0");
assert(toChars!8(0u).array == "0");
assert(toChars!8(0Lu).array == "0");
assert(toChars!8(1u).array == "1");
assert(toChars!8(1234567Lu).array == "4553207");
assert(toChars!8(ubyte.max).array == "377");
assert(toChars!8(ushort.max).array == "177777");

auto r = toChars!8(8u);
assert(r.length == 2);
Expand All @@ -5898,10 +5905,14 @@ if ((radix == 2 || radix == 8 || radix == 10 || radix == 16) &&
assert(s.retro.array == "01");
}
{
assert(toChars!10(ubyte(0)).array == "0");
assert(toChars!10(ushort(0)).array == "0");
assert(toChars!10(0u).array == "0");
assert(toChars!10(0Lu).array == "0");
assert(toChars!10(1u).array == "1");
assert(toChars!10(1234567Lu).array == "1234567");
assert(toChars!10(ubyte.max).array == "255");
assert(toChars!10(ushort.max).array == "65535");
assert(toChars!10(uint.max).array == "4294967295");
assert(toChars!10(ulong.max).array == "18446744073709551615");

Expand All @@ -5918,10 +5929,16 @@ if ((radix == 2 || radix == 8 || radix == 10 || radix == 16) &&
assert(toChars!10(0L).array == "0");
assert(toChars!10(1).array == "1");
assert(toChars!10(1234567L).array == "1234567");
assert(toChars!10(byte.max).array == "127");
assert(toChars!10(short.max).array == "32767");
assert(toChars!10(int.max).array == "2147483647");
assert(toChars!10(long.max).array == "9223372036854775807");
assert(toChars!10(-byte.max).array == "-127");
assert(toChars!10(-short.max).array == "-32767");
assert(toChars!10(-int.max).array == "-2147483647");
assert(toChars!10(-long.max).array == "-9223372036854775807");
assert(toChars!10(byte.min).array == "-128");
assert(toChars!10(short.min).array == "-32768");
assert(toChars!10(int.min).array == "-2147483648");
assert(toChars!10(long.min).array == "-9223372036854775808");

Expand All @@ -5938,6 +5955,10 @@ if ((radix == 2 || radix == 8 || radix == 10 || radix == 16) &&
assert(toChars!(16)(0Lu).array == "0");
assert(toChars!(16)(10u).array == "a");
assert(toChars!(16, char, LetterCase.upper)(0x12AF34567Lu).array == "12AF34567");
assert(toChars!(16)(ubyte(0)).array == "0");
assert(toChars!(16)(ushort(0)).array == "0");
assert(toChars!(16)(ubyte.max).array == "ff");
assert(toChars!(16)(ushort.max).array == "ffff");

auto r = toChars!(16)(16u);
assert(r.length == 2);
Expand Down

0 comments on commit 713ac80

Please sign in to comment.