Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 83e75aa

Browse files
authored
Merge pull request #3057 from MoonlightSentinel/assert-chars
Fix Issue 20757 - checkaction=context prints characters as integers merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
2 parents 2b0afa3 + 70741bc commit 83e75aa

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/core/internal/dassert.d

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,33 @@ private string miniFormat(V)(const scope ref V v)
9595
}
9696
else static if (__traits(isIntegral, V))
9797
{
98-
enum printfFormat = getPrintfFormat!V;
99-
char[20] val;
100-
const len = sprintf(&val[0], printfFormat, v);
101-
return val.idup[0 .. len];
98+
static if (is(V == char))
99+
{
100+
// Avoid invalid code points
101+
if (v < 0x7F)
102+
return ['\'', v, '\''];
103+
104+
uint tmp = v;
105+
return "cast(char) " ~ miniFormat(tmp);
106+
}
107+
else static if (is(V == wchar) || is(V == dchar))
108+
{
109+
import core.internal.utf: isValidDchar, toUTF8;
110+
111+
// Avoid invalid code points
112+
if (isValidDchar(v))
113+
return toUTF8(['\'', v, '\'']);
114+
115+
uint tmp = v;
116+
return "cast(" ~ V.stringof ~ ") " ~ miniFormat(tmp);
117+
}
118+
else
119+
{
120+
enum printfFormat = getPrintfFormat!V;
121+
char[20] val;
122+
const len = sprintf(&val[0], printfFormat, v);
123+
return val.idup[0 .. len];
124+
}
102125
}
103126
else static if (__traits(isFloating, V))
104127
{

test/exceptions/src/assert_fail.d

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ void testStrings()
6363
// https://issues.dlang.org/show_bug.cgi?id=20322
6464
test("left"w, "right"w, `"left" != "right"`);
6565
test("left"d, "right"d, `"left" != "right"`);
66+
67+
test('A', 'B', "'A' != 'B'");
68+
test(wchar(''), wchar(''), "'❤' != '∑'");
69+
test(dchar(''), dchar(''), "'❤' != '∑'");
70+
71+
// Detect invalid code points
72+
test(char(255), 'B', "cast(char) 255 != 'B'");
73+
test(wchar(0xD888), wchar(''), "cast(wchar) 55432 != '∑'");
74+
test(dchar(0xDDDD), dchar(''), "cast(dchar) 56797 != '∑'");
6675
}
6776

6877
void testToString()()

0 commit comments

Comments
 (0)