Commit cbb4854
authored
Fix
Currently `Int64.toRadixStringUnsigned` adds an extra '0' when the result is
single digit, e.g. '02' instead of '2'.
To be able to use the standard library `toRadixString`, we split the number
into two halves, then calculate the lowest digit and the rest of the digits
separately. With the lowest digit removed, the rest of the digits can be passed
as a positive integer to `toRadixString`. The resulting strings are then
concatenated to get the final result.
The problem occurs when the number can be printed as a single digit. In this
case the rest of the number is printed as '0', giving us e.g. '02' instead of
'2'.
To fix, we return early by calling the standard library `toRadixString` when
the number is positive.
This works because
- The problem is only when printing single digits (as the lowest digit and the
rest are printed separately, and the problem is when the "rest" part is 0)
- Single-digit numbers need to have high 32-bits as zero (otherwise they'll be
negative and printed as many digits, or positive but larger than single
digit).
- When the high 32-bits are all zero, the number is positive, and can be
converted to string directly with the standard library signed
`toRadixString`.Int64.toRadixStringUnsigned extra leading digits (#926)1 parent 41a9e01 commit cbb4854
2 files changed
+20
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
364 | 364 | | |
365 | 365 | | |
366 | 366 | | |
367 | | - | |
368 | | - | |
| 367 | + | |
| 368 | + | |
369 | 369 | | |
370 | 370 | | |
371 | 371 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1140 | 1140 | | |
1141 | 1141 | | |
1142 | 1142 | | |
1143 | | - | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
1144 | 1161 | | |
1145 | 1162 | | |
1146 | 1163 | | |
| |||
0 commit comments