Host C Specs


Directory: src/c/
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 16 / 0 / 16
Functions: 100.0% 2 / 0 / 2
Branches: 100.0% 12 / 0 / 12

core/text/cstring_fit.h
Line Branch Exec Source
1 /**
2 * @file cstring_fit.h
3 * @brief Copying text into a fixed buffer, and checking what a buffer holds before it is used.
4 *
5 * @ingroup lib_core
6 */
7 #pragma once
8 #include <stdbool.h>
9 #include <stddef.h>
10 #include <stdint.h>
11 #include <string.h>
12
13 /**
14 * @addtogroup lib_core
15 * @{
16 */
17
18 /**
19 * @brief Copy a string into a fixed buffer, keeping only what fits and always terminating it.
20 *
21 * The cut counts bytes, so a character that takes more than one byte can be split at it. The fonts
22 * carry only the characters a face shows, which a character past ASCII is not, so it draws as a box
23 * whether it is cut or whole, and walking back to a character boundary would cost bytes for nothing.
24 *
25 * @param dst The buffer to write into.
26 * @param src The string to copy, or NULL to empty the buffer.
27 * @param size The buffer's size, terminator included.
28 */
29 54 static inline void cstring_fit(char *dst, const char *src, size_t size)
30 {
31
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 53 times.
54 if (size == 0)
32 {
33 1 return;
34 }
35
36
2/2
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 52 times.
53 if (!src)
37 {
38 1 dst[0] = '\0';
39 1 return;
40 }
41
42 52 strncpy(dst, src, size - 1);
43 52 dst[size - 1] = '\0';
44 }
45
46 /**
47 * @brief Whether copying @p value into a buffer would leave it reading exactly as it does now.
48 *
49 * The copy keeps only what fits, so the incoming string is measured the same way. Comparing it in
50 * full instead would call a value longer than the buffer a change every single time, which for a
51 * settings field means re-reacting to a setting nobody touched.
52 *
53 * @param current What the buffer holds now.
54 * @param value The string that would be copied in. Must not be NULL.
55 * @param size The buffer's size, terminator included.
56 * @return True when the copy would leave the buffer unchanged.
57 */
58 14 static inline bool cstring_fit_same(const char *current, const char *value, size_t size)
59 {
60
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 13 times.
14 if (size == 0)
61 {
62 1 return true; // nowhere to put it, so nothing about the buffer can move
63 }
64
65 13 size_t room = size - 1;
66 13 size_t incoming = strlen(value);
67
2/2
✓ Branch 4 → 5 taken 3 times.
✓ Branch 4 → 6 taken 10 times.
13 if (incoming > room)
68 {
69 3 incoming = room;
70 }
71
72
4/4
✓ Branch 6 → 7 taken 10 times.
✓ Branch 6 → 9 taken 3 times.
✓ Branch 7 → 8 taken 8 times.
✓ Branch 7 → 9 taken 2 times.
13 return strlen(current) == incoming && strncmp(current, value, incoming) == 0;
73 }
74
75 /**
76 * @brief Copy bytes into a fixed buffer, taking what fits and terminating it.
77 *
78 * For the length-prefixed text the phone sends, where the length is the phone's word and the room
79 * is ours. What does not fit is dropped rather than written past the end.
80 *
81 * @param[out] dst Where to write. Ends terminated whenever cap is at least 1.
82 * @param cap How much room dst has, terminator included. A cap of 0 has no room even for the
83 * terminator, so dst is left untouched and not terminated.
84 * @param src The bytes to copy.
85 * @param len How many bytes there are.
86 */
87 void copy_bounded(char *dst, uint8_t cap, const uint8_t *src, uint8_t len);
88
89 /**
90 * @brief Whether a byte range holds a string worth keeping.
91 *
92 * Worth keeping means it ends inside its own room, it says something, and it carries no control
93 * bytes. Everything from 0x20 up counts as text, UTF-8 included, so a place name spelled with an
94 * umlaut is text and not damage. Getting that wrong is how a saved city turns back into the
95 * default one on the next boot.
96 *
97 * @param s The bytes to look at.
98 * @param size How much room they have.
99 * @return Whether it reads as a string.
100 */
101 bool cstring_is_clean(const char *s, uint16_t size);
102
103 /**
104 * @brief Whether a saved text setting reads as one, where an empty setting can be the real value.
105 *
106 * A setting whose default is empty, such as a picker whose "None" is "", holds nothing on
107 * purpose, so empty is a value for it and not damage. Any other setting keeps the cstring_is_clean
108 * rule, since an empty one there is what a wiped blob reads like.
109 *
110 * @param s The bytes to look at.
111 * @param size How much room they have.
112 * @param default_str The setting's default, or NULL for none.
113 * @return Whether the setting is worth keeping.
114 */
115 bool cstring_setting_is_clean(const char *s, uint16_t size, const char *default_str);
116
117 /** @} */
118