From ce1da546318a7aac9877b7be5d6fc83735835b9c Mon Sep 17 00:00:00 2001 From: Danil Sidoruk Date: Wed, 20 Dec 2023 17:52:25 +0300 Subject: [PATCH 1/4] Fix issue 24075 - Can't use toChars with ushort or ubyte --- std/conv.d | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/std/conv.d b/std/conv.d index 3a533814894..7096c973665 100644 --- a/std/conv.d +++ b/std/conv.d @@ -5712,8 +5712,8 @@ 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 */ @@ -5721,8 +5721,10 @@ private auto hexStrLiteral(String)(scope String hexData) 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)))) + (is(immutable T == immutable ubyte) || is(immutable T == immutable ushort) || is( + immutable T == immutable uint) || is(immutable T == immutable ulong) || + radix == 10 && (is(immutable T == immutable byte) || is(immutable T == immutable short) || is( + immutable T == immutable int) || is(immutable T == immutable long)))) { alias UT = Unqual!T; From db91b6ccbc23732dba9f36d679a8012791a2c908 Mon Sep 17 00:00:00 2001 From: Danil Sidoruk Date: Wed, 20 Dec 2023 17:52:59 +0300 Subject: [PATCH 2/4] Add unittests for ubyte, ushort, byte, short overloads of conv.toChars --- std/conv.d | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/std/conv.d b/std/conv.d index 7096c973665..3d337417570 100644 --- a/std/conv.d +++ b/std/conv.d @@ -5872,8 +5872,12 @@ if ((radix == 2 || radix == 8 || radix == 10 || radix == 16) && assert(toChars(123) == toChars(123)); { + assert(toChars!2(cast(ubyte) 0).array == "0"); + assert(toChars!2(cast(ushort) 0).array == "0"); assert(toChars!2(0u).array == "0"); assert(toChars!2(0Lu).array == "0"); + assert(toChars!2(cast(ubyte) 1).array == "1"); + assert(toChars!2(cast(ushort) 1).array == "1"); assert(toChars!2(1u).array == "1"); assert(toChars!2(1Lu).array == "1"); @@ -5886,10 +5890,14 @@ if ((radix == 2 || radix == 8 || radix == 10 || radix == 16) && assert(s.retro.array == "01"); } { + assert(toChars!8(cast(ubyte) 0).array == "0"); + assert(toChars!8(cast(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); @@ -5900,10 +5908,14 @@ if ((radix == 2 || radix == 8 || radix == 10 || radix == 16) && assert(s.retro.array == "01"); } { + assert(toChars!10(cast(ubyte) 0).array == "0"); + assert(toChars!10(cast(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"); @@ -5920,10 +5932,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"); @@ -5940,6 +5958,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)(cast(ubyte) 0).array == "0"); + assert(toChars!(16)(cast (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); From 7d012ab076d643c56f3f75f543456ab6229996d7 Mon Sep 17 00:00:00 2001 From: Danil Sidoruk Date: Sun, 24 Dec 2023 20:57:45 +0300 Subject: [PATCH 3/4] Replace case (type) num -> type(num) --- std/conv.d | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/std/conv.d b/std/conv.d index 3d337417570..310598998e4 100644 --- a/std/conv.d +++ b/std/conv.d @@ -5872,12 +5872,12 @@ if ((radix == 2 || radix == 8 || radix == 10 || radix == 16) && assert(toChars(123) == toChars(123)); { - assert(toChars!2(cast(ubyte) 0).array == "0"); - assert(toChars!2(cast(ushort) 0).array == "0"); + 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(cast(ubyte) 1).array == "1"); - assert(toChars!2(cast(ushort) 1).array == "1"); + 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"); @@ -5890,8 +5890,8 @@ if ((radix == 2 || radix == 8 || radix == 10 || radix == 16) && assert(s.retro.array == "01"); } { - assert(toChars!8(cast(ubyte) 0).array == "0"); - assert(toChars!8(cast(ushort) 0).array == "0"); + 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"); @@ -5908,8 +5908,8 @@ if ((radix == 2 || radix == 8 || radix == 10 || radix == 16) && assert(s.retro.array == "01"); } { - assert(toChars!10(cast(ubyte) 0).array == "0"); - assert(toChars!10(cast(ushort) 0).array == "0"); + 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"); @@ -5958,8 +5958,8 @@ 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)(cast(ubyte) 0).array == "0"); - assert(toChars!(16)(cast (ushort) 0).array == "0"); + 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"); From b29f127886de80351eedf4331f07e10384c823ca Mon Sep 17 00:00:00 2001 From: Dennis Date: Sat, 30 Dec 2023 15:22:21 +0100 Subject: [PATCH 4/4] Update std/conv.d --- std/conv.d | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/std/conv.d b/std/conv.d index 310598998e4..23b33c4d78c 100644 --- a/std/conv.d +++ b/std/conv.d @@ -5721,10 +5721,7 @@ private auto hexStrLiteral(String)(scope String hexData) 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 ubyte) || is(immutable T == immutable ushort) || is( - immutable T == immutable uint) || is(immutable T == immutable ulong) || - radix == 10 && (is(immutable T == immutable byte) || is(immutable T == immutable short) || is( - immutable T == immutable int) || is(immutable T == immutable long)))) + isIntegral!T && (radix == 10 || isUnsigned!T)) { alias UT = Unqual!T;