commit c040defd5f5cf5beed264dd057097625d9c0d423
parent d7ff776552bef524e905d85c2a5e7651c8408658
Author: M Stoeckl <code@mstoeckl.com>
Date: Mon, 21 Jan 2019 12:46:45 -0500
Fix edge case bug in numlen, dropping use of math.h functions
(Specifically, numlen when called with INT_MIN gave an incorrect
result, because abs(INT_MIN) == INT_MIN < 0.)
Diffstat:
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/common/util.c b/common/util.c
@@ -12,11 +12,12 @@ int wrap(int i, int max) {
}
int numlen(int n) {
- if (n == 0) {
- return 1;
+ int j = n <= 0 ? 1 : 0;
+ while (n) {
+ j++;
+ n /= 10;
}
- // Account for the '-' in negative numbers.
- return log10(abs(n)) + (n > 0 ? 1 : 2);
+ return j;
}
uint32_t parse_color(const char *color) {