@@ -17,6 +17,10 @@ alias UnsignedStringBuf = char[20];
17
17
18
18
char [] unsignedToTempString (ulong value, return char [] buf, uint radix = 10 ) @safe
19
19
{
20
+ if (radix < 2 )
21
+ // not a valid radix, just return an empty string
22
+ return buf[$ .. $];
23
+
20
24
size_t i = buf.length;
21
25
do
22
26
{
@@ -74,6 +78,10 @@ unittest
74
78
assert (long .sizeof.unsignedToTempString == " 8" );
75
79
assert (uint .max.unsignedToTempString == " 4294967295" );
76
80
assert (ulong .max.unsignedToTempString == " 18446744073709551615" );
81
+
82
+ // test bad radices
83
+ assert (100. unsignedToTempString(buf, 1 ) == " " );
84
+ assert (100. unsignedToTempString(buf, 0 ) == " " );
77
85
}
78
86
79
87
alias SignedStringBuf = char [20 ];
@@ -151,7 +159,7 @@ unittest
151
159
* Returns:
152
160
* number of digits
153
161
*/
154
- int numDigits (uint radix = 10 )(ulong value) @safe
162
+ int numDigits (uint radix = 10 )(ulong value) @safe if (radix >= 2 && radix <= 36 )
155
163
{
156
164
int n = 1 ;
157
165
while (1 )
@@ -197,6 +205,11 @@ unittest
197
205
assert (1. numDigits! 2 == 1 );
198
206
assert (2. numDigits! 2 == 2 );
199
207
assert (3. numDigits! 2 == 2 );
208
+
209
+ // test bad radices
210
+ static assert (! __traits(compiles, 100. numDigits! 1 ()));
211
+ static assert (! __traits(compiles, 100. numDigits! 0 ()));
212
+ static assert (! __traits(compiles, 100. numDigits! 37 ()));
200
213
}
201
214
202
215
int dstrcmp ( scope const char [] s1, scope const char [] s2 ) @trusted
0 commit comments