This repository was archived by the owner on Oct 12, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +36
-4
lines changed Expand file tree Collapse file tree 2 files changed +36
-4
lines changed Original file line number Diff line number Diff line change @@ -95,10 +95,33 @@ private string miniFormat(V)(const scope ref V v)
95
95
}
96
96
else static if (__traits(isIntegral, V))
97
97
{
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
+ }
102
125
}
103
126
else static if (__traits(isFloating, V))
104
127
{
Original file line number Diff line number Diff line change @@ -63,6 +63,15 @@ void testStrings()
63
63
// https://issues.dlang.org/show_bug.cgi?id=20322
64
64
test(" left" w, " right" w, ` "left" != "right"` );
65
65
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 != '∑'" );
66
75
}
67
76
68
77
void testToString ()()
You can’t perform that action at this time.
0 commit comments