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

Commit

Permalink
Merge pull request #3057 from MoonlightSentinel/assert-chars
Browse files Browse the repository at this point in the history
Fix Issue 20757 - checkaction=context prints characters as integers
merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
  • Loading branch information
dlang-bot committed Apr 22, 2020
2 parents 2b0afa3 + 70741bc commit 83e75aa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/core/internal/dassert.d
Expand Up @@ -95,10 +95,33 @@ private string miniFormat(V)(const scope ref V v)
}
else static if (__traits(isIntegral, V))
{
enum printfFormat = getPrintfFormat!V;
char[20] val;
const len = sprintf(&val[0], printfFormat, v);
return val.idup[0 .. len];
static if (is(V == char))
{
// Avoid invalid code points
if (v < 0x7F)
return ['\'', v, '\''];

uint tmp = v;
return "cast(char) " ~ miniFormat(tmp);
}
else static if (is(V == wchar) || is(V == dchar))
{
import core.internal.utf: isValidDchar, toUTF8;

// Avoid invalid code points
if (isValidDchar(v))
return toUTF8(['\'', v, '\'']);

uint tmp = v;
return "cast(" ~ V.stringof ~ ") " ~ miniFormat(tmp);
}
else
{
enum printfFormat = getPrintfFormat!V;
char[20] val;
const len = sprintf(&val[0], printfFormat, v);
return val.idup[0 .. len];
}
}
else static if (__traits(isFloating, V))
{
Expand Down
9 changes: 9 additions & 0 deletions test/exceptions/src/assert_fail.d
Expand Up @@ -63,6 +63,15 @@ void testStrings()
// https://issues.dlang.org/show_bug.cgi?id=20322
test("left"w, "right"w, `"left" != "right"`);
test("left"d, "right"d, `"left" != "right"`);

test('A', 'B', "'A' != 'B'");
test(wchar(''), wchar(''), "'❤' != '∑'");
test(dchar(''), dchar(''), "'❤' != '∑'");

// Detect invalid code points
test(char(255), 'B', "cast(char) 255 != 'B'");
test(wchar(0xD888), wchar(''), "cast(wchar) 55432 != '∑'");
test(dchar(0xDDDD), dchar(''), "cast(dchar) 56797 != '∑'");
}

void testToString()()
Expand Down

0 comments on commit 83e75aa

Please sign in to comment.