Host C Specs


Directory: src/c/
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 96.9% 31 / 0 / 32
Functions: 100.0% 5 / 0 / 5
Branches: 84.6% 22 / 0 / 26

core/text/number_format.c
Line Branch Exec Source
1 /**
2 * @file number_format.c
3 * @brief Pure number formatting, no SDK behind it.
4 *
5 * @ingroup lib_core
6 */
7 #include "text/number_format.h"
8 #include "text/cstring_fit.h"
9
10 #include <stdio.h>
11 #include <string.h>
12
13 /**
14 * @brief The size of a number without its sign, as an unsigned value.
15 *
16 * abs() on INT_MIN has no answer that fits an int, and the stock price comes straight off the wire.
17 * Doing the negation in unsigned maths gives the right size for every int, INT_MIN included.
18 *
19 * @param value The number to take the size of.
20 * @return The value with its sign dropped.
21 */
22 15 static unsigned int magnitude_of(int value)
23 {
24
2/2
✓ Branch 2 → 3 taken 6 times.
✓ Branch 2 → 4 taken 9 times.
15 return value < 0 ? 0u - (unsigned int)value : (unsigned int)value;
25 }
26
27 7 void number_group(char *buffer, size_t size, int value)
28 {
29
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 7 times.
7 if (size == 0)
30 {
31 ✗ return;
32 }
33
34 // strip the sign into an unsigned magnitude so INT_MIN stays well-defined
35 // then walk the digits inserting separators
36 char digits[16];
37 7 snprintf(digits, sizeof(digits), "%u", magnitude_of(value));
38
39 7 size_t len = strlen(digits);
40 char out[24];
41 7 size_t w = 0;
42
43
3/4
✓ Branch 5 → 6 taken 2 times.
✓ Branch 5 → 8 taken 5 times.
✓ Branch 6 → 7 taken 2 times.
✗ Branch 6 → 8 not taken.
7 if (value < 0 && w < sizeof(out) - 1)
44 {
45 2 out[w++] = '-';
46 }
47
48
3/4
✓ Branch 15 → 16 taken 38 times.
✓ Branch 15 → 17 taken 7 times.
✓ Branch 16 → 9 taken 38 times.
✗ Branch 16 → 17 not taken.
45 for (size_t i = 0; i < len && w < sizeof(out) - 1; i++)
49 {
50 // a separator precedes every group of three counted from the right
51
4/4
✓ Branch 9 → 10 taken 31 times.
✓ Branch 9 → 12 taken 7 times.
✓ Branch 10 → 11 taken 9 times.
✓ Branch 10 → 12 taken 22 times.
38 if (i > 0 && (len - i) % 3 == 0)
52 {
53 9 out[w++] = '\'';
54 }
55
56
1/2
✓ Branch 12 → 13 taken 38 times.
✗ Branch 12 → 14 not taken.
38 if (w < sizeof(out) - 1)
57 {
58 38 out[w++] = digits[i];
59 }
60 }
61
62 7 out[w] = '\0';
63
64 7 cstring_fit(buffer, out, size);
65 }
66
67 4 void fmt_int_or_dash(char *buffer, size_t size, int value, const char *fmt)
68 {
69
2/2
✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 4 taken 2 times.
4 if (value < 0)
70 {
71 2 snprintf(buffer, size, "--");
72 }
73 else
74 {
75 2 snprintf(buffer, size, fmt, value);
76 }
77 4 }
78
79 4 void fmt_hundredths(char *buffer, size_t size, int value)
80 {
81 // pull the sign out front so a value between -1 and 0 still shows the minus, since the whole
82 // part would be 0 and would otherwise drop it
83
2/2
✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 4 taken 2 times.
4 const char *sign = value < 0 ? "-" : "";
84 4 unsigned int magnitude = magnitude_of(value);
85
86 4 snprintf(buffer, size, "%s%u.%02u", sign, magnitude / 100, magnitude % 100);
87 4 }
88
89 4 void fmt_pct_signed(char *buffer, size_t size, int value)
90 {
91 // same reason as above, plus a gain wears its plus so the direction reads without the colour
92
4/4
✓ Branch 2 → 3 taken 3 times.
✓ Branch 2 → 6 taken 1 time.
✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 5 taken 1 time.
4 const char *sign = value > 0 ? "+" : (value < 0 ? "-" : "");
93 4 unsigned int magnitude = magnitude_of(value);
94
95 4 snprintf(buffer, size, "%s%u.%02u%%", sign, magnitude / 100, magnitude % 100);
96 4 }
97