Skip to content

Commit 3708bbf

Browse files
Transform control flow to be simpler. No additional branches.
1 parent edc2042 commit 3708bbf

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

stl/inc/xmemory

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2808,20 +2808,25 @@ _NODISCARD _Elem* _UIntegral_to_buff(_Elem* _RNext, _UTy _UVal) { // used by bot
28082808
auto _UVal_trunc = static_cast<unsigned long>(_UVal);
28092809
#endif // ^^^ !defined(_WIN64) ^^^
28102810

2811-
if (_UVal_trunc >= 10) {
2812-
do {
2813-
const unsigned long _UVal_trunc_part = _UVal_trunc % 100;
2814-
_UVal_trunc /= 100;
2815-
_RNext -= 2;
2816-
_CSTD memcpy(_RNext, _Digit_pairs<_Elem>._Data[_UVal_trunc_part], 2 * sizeof(_Elem));
2817-
} while (_UVal_trunc >= 10);
2818-
2819-
if (_UVal_trunc == 0) {
2820-
return _RNext;
2821-
}
2811+
// If we have a single digit, print [0, 9] and return. (This is necessary to correctly handle 0.)
2812+
if (_UVal_trunc < 10) {
2813+
*--_RNext = static_cast<_Elem>('0' + _UVal_trunc);
2814+
return _RNext;
2815+
}
2816+
2817+
// Print one or more pairs of digits.
2818+
do {
2819+
const unsigned long _UVal_trunc_part = _UVal_trunc % 100;
2820+
_UVal_trunc /= 100;
2821+
_RNext -= 2;
2822+
_CSTD memcpy(_RNext, _Digit_pairs<_Elem>._Data[_UVal_trunc_part], 2 * sizeof(_Elem));
2823+
} while (_UVal_trunc >= 10);
2824+
2825+
// If we have an unpaired digit, print it. For example, 1729 is printed as 17 29, and 19937 is printed as 1 99 37.
2826+
if (_UVal_trunc != 0) {
2827+
*--_RNext = static_cast<_Elem>('0' + _UVal_trunc);
28222828
}
28232829

2824-
*--_RNext = static_cast<_Elem>('0' + _UVal_trunc);
28252830
return _RNext;
28262831
}
28272832
_STD_END

0 commit comments

Comments
 (0)