Skip to content

Commit c7b6e4b

Browse files
committed
Row_printNanoseconds() code shrink
Merge two xSnprintf() calls within the function. When the time value to print is less than one second, the "%.u" format allows us to print no digits in the seconds field. Signed-off-by: Kang-Che Sung <[email protected]>
1 parent 16ff93d commit c7b6e4b

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

Row.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -419,22 +419,17 @@ void Row_printNanoseconds(RichString* str, unsigned long long totalNanoseconds,
419419
}
420420

421421
unsigned long long totalMicroseconds = totalNanoseconds / 1000;
422-
if (totalMicroseconds < 1000000) {
423-
len = xSnprintf(buffer, sizeof(buffer), ".%06lus ", (unsigned long)totalMicroseconds);
424-
RichString_appendnAscii(str, baseColor, buffer, len);
425-
return;
426-
}
427-
428422
unsigned long long totalSeconds = totalMicroseconds / 1000000;
429423
unsigned long microseconds = totalMicroseconds % 1000000;
430424
if (totalSeconds < 60) {
431-
int width = 5;
432-
unsigned long fraction = microseconds / 10;
433-
if (totalSeconds >= 10) {
425+
int width = 6;
426+
unsigned long fraction = microseconds;
427+
for (unsigned long long limit = 1; totalSeconds >= limit; limit *= 10) {
434428
width--;
435429
fraction /= 10;
436430
}
437-
len = xSnprintf(buffer, sizeof(buffer), "%u.%0*lus ", (unsigned int)totalSeconds, width, fraction);
431+
// "%.u" prints no digits if (totalSeconds == 0).
432+
len = xSnprintf(buffer, sizeof(buffer), "%.u.%0*lus ", (unsigned int)totalSeconds, width, fraction);
438433
RichString_appendnAscii(str, baseColor, buffer, len);
439434
return;
440435
}

0 commit comments

Comments
 (0)