Skip to content

Commit

Permalink
Row_printNanoseconds() code shrink
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Explorer09 committed Jan 2, 2025
1 parent 16ff93d commit c7b6e4b
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions Row.c
Original file line number Diff line number Diff line change
Expand Up @@ -419,22 +419,17 @@ void Row_printNanoseconds(RichString* str, unsigned long long totalNanoseconds,
}

unsigned long long totalMicroseconds = totalNanoseconds / 1000;
if (totalMicroseconds < 1000000) {
len = xSnprintf(buffer, sizeof(buffer), ".%06lus ", (unsigned long)totalMicroseconds);
RichString_appendnAscii(str, baseColor, buffer, len);
return;
}

unsigned long long totalSeconds = totalMicroseconds / 1000000;
unsigned long microseconds = totalMicroseconds % 1000000;
if (totalSeconds < 60) {
int width = 5;
unsigned long fraction = microseconds / 10;
if (totalSeconds >= 10) {
int width = 6;
unsigned long fraction = microseconds;
for (unsigned long long limit = 1; totalSeconds >= limit; limit *= 10) {
width--;
fraction /= 10;
}
len = xSnprintf(buffer, sizeof(buffer), "%u.%0*lus ", (unsigned int)totalSeconds, width, fraction);
// "%.u" prints no digits if (totalSeconds == 0).
len = xSnprintf(buffer, sizeof(buffer), "%.u.%0*lus ", (unsigned int)totalSeconds, width, fraction);
RichString_appendnAscii(str, baseColor, buffer, len);
return;
}
Expand Down

0 comments on commit c7b6e4b

Please sign in to comment.