• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ascii-boxes / boxes / 25972901088

16 May 2026 09:03PM UTC coverage: 82.822% (-0.03%) from 82.852%
25972901088

push

github

tsjensen
Fix a Heisenbug in u32_insert_space_at() in unicode.c

2776 of 3695 branches covered (75.13%)

Branch coverage included in aggregate %.

5 of 7 new or added lines in 1 file covered. (71.43%)

4345 of 4903 relevant lines covered (88.62%)

98936.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.66
/src/unicode.c
1
/*
2
 * boxes - Command line filter to draw/remove ASCII boxes around text
3
 * Copyright (c) 1999-2024 Thomas Jensen and the boxes contributors
4
 *
5
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
6
 * License, version 3, as published by the Free Software Foundation.
7
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
8
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
9
 * details.
10
 * You should have received a copy of the GNU General Public License along with this program.
11
 * If not, see <https://www.gnu.org/licenses/>.
12
 *
13
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
14
 */
15

16
/*
17
 * Functions and constants for handling unicode strings with libunistring.
18
 */
19

20
#include "config.h"
21
#include <errno.h>
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25

26
#include <uniconv.h>
27
#include <unictype.h>
28
#include <unistr.h>
29

30
#include "boxes.h"
31
#include "tools.h"
32
#include "unicode.h"
33

34

35

36
/* effective character encoding of input and output text */
37
const char *encoding;
38

39
/* ucs4_t character '\t' (tab)  */
40
const ucs4_t char_tab = 0x00000009;
41

42
/* ucs4_t character ' '  (space) */
43
const ucs4_t char_space = 0x00000020;
44

45
/* ucs4_t character '\r' (carriage return) */
46
const ucs4_t char_cr = 0x0000000d;
47

48
/* ucs4_t character '\n' (newline) */
49
const ucs4_t char_newline = 0x0000000a;
50

51
/* ucs4_t character 0x1b (escape)  */
52
const ucs4_t char_esc = 0x0000001b;
53

54
/* ucs4_t character '\0' (zero) */
55
const ucs4_t char_nul = 0x00000000;
56

57

58

59
int is_char_at(const uint32_t *text, const size_t idx, const ucs4_t expected_char)
9,148,752✔
60
{
61
    return text != NULL && u32_cmp(text + idx, &expected_char, 1) == 0;
9,148,752!
62
}
63

64

65

66
void set_char_at(uint32_t *text, const size_t idx, const ucs4_t char_to_set)
191,676✔
67
{
68
    u32_set(text + idx, char_to_set, 1);
191,676✔
69
}
191,676✔
70

71

72

73
int is_empty(const uint32_t *text)
9,145,118✔
74
{
75
    return text == NULL || is_char_at(text, 0, char_nul);
9,145,118✔
76
}
77

78

79

80
int u32_is_blank(const uint32_t *text)
56✔
81
{
82
    if (is_empty(text)) {
56✔
83
        return 1;
1✔
84
    }
85

86
    for (const uint32_t *c = text; *c != char_nul; c++) {
297✔
87
        if (!is_blank(*c)) {
242!
88
            return 0;
×
89
        }
90
    }
91
    return 1;
55✔
92
}
93

94

95

96
int is_ascii_printable(const ucs4_t c)
9,060,020✔
97
{
98
    return c >= 0x20 && c < 0x7f;
9,060,020✔
99
}
100

101

102

103
int is_allowed_anywhere(const ucs4_t c)
9,522,609✔
104
{
105
    /* ESC, CR, LF, and TAB are control characters */
106
    return !uc_is_cntrl(c) || c == char_tab || c == char_cr || c == char_newline || c == char_esc;
9,522,609✔
107
}
108

109

110

111
int is_allowed_in_shape(const ucs4_t c)
109,913✔
112
{
113
    return is_allowed_anywhere(c) && c != char_cr && c != char_newline;
109,913✔
114
}
115

116

117

118
int is_allowed_in_sample(const ucs4_t c)
293,201✔
119
{
120
    return is_allowed_anywhere(c);
293,201✔
121
}
122

123

124

125
int is_allowed_in_filename(const ucs4_t c)
281✔
126
{
127
    return is_allowed_anywhere(c) && c != char_cr && c != char_newline && c != char_esc;
281✔
128
}
129

130

131

132
int is_allowed_in_kv_string(const ucs4_t c)
29,443✔
133
{
134
    return is_allowed_anywhere(c) && c != char_cr && c != char_newline && c != char_esc;
29,443✔
135
}
136

137

138

139
int is_blank(const ucs4_t c)
18,148,948✔
140
{
141
    return c == char_tab || uc_is_blank(c);
18,148,948✔
142
}
143

144

145

146
uint32_t *new_empty_string32()
327✔
147
{
148
    return (uint32_t *) calloc(1, sizeof(uint32_t));
327✔
149
}
150

151

152

153
ucs4_t to_utf32(char ascii)
2,296✔
154
{
155
    ucs4_t c = char_nul;
2,296✔
156
    if (ascii >= 0x20 && ascii < 0x7f) {
2,296!
157
        c = (ucs4_t)ascii;  // Store the ASCII value directly in c
2,295✔
158
    }
159
    return c;
2,296✔
160
}
161

162

163

164
uint32_t *advance_next32(const uint32_t *s, size_t *invis)
9,125,255✔
165
{
166
    if (is_empty(s)) {
9,125,255!
167
        return (uint32_t *) s;
×
168
    }
169

170
    int ansipos = 0;
9,125,255✔
171
    (*invis) = 0;
9,125,255✔
172
    ucs4_t c;
173
    const uint32_t *rest = s;
9,125,255✔
174
    while ((rest = u32_next(&c, rest))) {
9,294,976✔
175
        if (ansipos == 0 && c == char_esc) {
9,294,975✔
176
            /* Found an ESC char, count it as invisible and move 1 forward in the detection of CSI sequences */
177
            (*invis)++;
31,702✔
178
            ansipos++;
31,702✔
179
        } else if (ansipos == 1 && (c == '[' || c == '(')) {
9,263,273!
180
            /* Found '[' char after ESC. A CSI sequence has started. */
181
            (*invis)++;
31,702✔
182
            ansipos++;
31,702✔
183
        } else if (ansipos == 1 && c >= 0x40 && c <= 0x5f) { /* between '@' and '_' (mostly uppercase letters) */
9,231,571!
184
            /* Found a byte designating the end of a two-byte escape sequence */
185
            (*invis)++;
×
186
            ansipos = 0;
×
187
            break;
×
188
        } else if (ansipos == 2) {
9,231,571✔
189
            /* Inside CSI sequence - Keep counting chars as invisible */
190
            (*invis)++;
138,018✔
191

192
            /* A char between 0x40 and 0x7e signals the end of an CSI or escape sequence */
193
            if (c >= 0x40 && c <= 0x7e) {
138,018!
194
                ansipos = 0;
31,701✔
195
                break;
31,701✔
196
            }
197
        } else {
198
            break;
9,093,553✔
199
        }
200
    }
201
    if (rest == NULL) {
9,125,255✔
202
        rest = s + u32_strlen(s);
1✔
203
    }
204
    return (uint32_t *) rest;
9,125,255✔
205
}
206

207

208

209
uint32_t *u32_strconv_from_input(const char *src)
2,635✔
210
{
211
    return u32_strconv_from_arg(src, encoding);
2,635✔
212
}
213

214

215

216
uint32_t *u32_strconv_from_arg(const char *src, const char *sourceEncoding)
287,998✔
217
{
218
    if (src == NULL) {
287,998✔
219
        return NULL;
2✔
220
    }
221
    if (src[0] == '\0') {
287,996✔
222
        return new_empty_string32();
63✔
223
    }
224

225
    uint32_t *result = u32_strconv_from_encoding(
287,933✔
226
            src,                    /* the source string to convert */
227
            sourceEncoding,         /* the character encoding from which to convert */
228
            iconveh_question_mark); /* produce one question mark '?' per unconvertible character */
229

230
    if (result == NULL) {
287,933!
231
        fprintf(stderr, "%s: failed to convert from '%s' to UTF-32: %s\n", PROJECT, sourceEncoding, strerror(errno));
×
232
    }
233
    return result;
287,933✔
234
}
235

236

237

238
char *u32_strconv_to_output(const uint32_t *src)
3,367✔
239
{
240
    return u32_strconv_to_arg(src, encoding);
3,367✔
241
}
242

243

244

245
char *u32_strconv_to_arg(const uint32_t *src, const char *targetEncoding)
3,764✔
246
{
247
    if (src == NULL) {
3,764!
248
        return NULL;
×
249
    }
250
    if (is_empty(src)) {
3,764✔
251
        return strdup("");
14✔
252
    }
253

254
    char *result = u32_strconv_to_encoding(
3,750✔
255
            src,                    /* the source string to convert */
256
            targetEncoding,         /* the character encoding to which to convert */
257
            iconveh_question_mark); /* produce one question mark '?' per unconvertible character */
258

259
    if (result == NULL) {
3,750!
260
        fprintf(stderr, "%s: failed to convert from UTF-32 to '%s': %s\n", PROJECT, targetEncoding, strerror(errno));
×
261
    }
262
    return result;
3,750✔
263
}
264

265

266

267
const char *check_encoding(const char *manual_encoding, const char *system_encoding)
391✔
268
{
269
    if (manual_encoding != NULL) {
391✔
270
        uint32_t *unicode = u32_strconv_from_encoding(" ", manual_encoding, iconveh_error);
13✔
271
        if (unicode != NULL) {
13✔
272
            BFREE(unicode);
12!
273
            return manual_encoding;
12✔
274
        }
275
        fprintf(stderr, "%s: Invalid character encoding: %s - falling back to %s\n",
1✔
276
                PROJECT, manual_encoding, system_encoding);
277
        fflush(stderr);
1✔
278
    }
279
    return system_encoding;
379✔
280
}
281

282

283

284
char *to_utf8(uint32_t *src)
412✔
285
{
286
    if (src == NULL) {
412!
287
        return NULL;
×
288
    }
289
    if (is_empty(src)) {
412!
290
        return (char *) strdup("");
×
291
    }
292
    char *result = u32_strconv_to_encoding(src, "UTF-8", iconveh_error);
412✔
293
    if (result == NULL) {
412!
294
        bx_fprintf(stderr, "%s: failed to convert a string to UTF-8: %s\n", PROJECT, strerror(errno));
×
295
        return NULL;
×
296
    }
297
    return result;
412✔
298
}
299

300

301

302
uint32_t *u32_nspaces(const size_t n)
1,760✔
303
{
304
    uint32_t *result = (uint32_t *) malloc((n + 1) * sizeof(uint32_t));
1,760✔
305
    if (result == NULL) {
1,760!
306
        perror(PROJECT);
×
307
        return NULL;
×
308
    }
309
    if (n > 0) {
1,760✔
310
        u32_set(result, char_space, n);
1,339✔
311
    }
312
    set_char_at(result, n, char_nul);
1,760✔
313
    return result;
1,760✔
314
}
315

316

317

318
uint32_t *u32_strnrstr(const uint32_t *haystack, const uint32_t *needle, const size_t needle_len)
5,553✔
319
{
320
    if (is_empty(needle)) {
5,553✔
321
        return (uint32_t *) haystack;
49✔
322
    }
323
    if (is_empty(haystack)) {
5,504✔
324
        return NULL;
1✔
325
    }
326

327
    const uint32_t *p = u32_strrchr(haystack, needle[0]);
5,503✔
328
    if (!p) {
5,503✔
329
        return NULL;
536✔
330
    }
331

332
    while (p >= haystack) {
227,797✔
333
        if (u32_strncmp(p, needle, needle_len) == 0) {
224,873✔
334
            return (uint32_t *) p;
2,043✔
335
        }
336
        --p;
222,830✔
337
    }
338

339
    return NULL;
2,924✔
340
}
341

342

343

344
void u32_insert_space_at(uint32_t **s, const size_t idx, const size_t n)
4✔
345
{
346
    if (s == NULL || *s == NULL || n == 0) {
4!
347
        return;
2✔
348
    }
349

350
    size_t len = u32_strlen(*s);
2✔
351
    size_t x = idx;
2✔
352
    if (idx > len) {
2✔
353
        x = len;
1✔
354
    }
355

356
    uint32_t *tmp = (uint32_t *) realloc(*s, (len + 1 + n) * sizeof(uint32_t));
2✔
357
    if (tmp == NULL) {
2!
NEW
358
        perror(PROJECT);
×
NEW
359
        return;
×
360
    }
361

362
    *s = tmp;
2✔
363
    u32_move(*s + x + n, *s + x, len - x + 1);
2✔
364
    u32_set(*s + x, char_space, n);
2✔
365
}
366

367

368
/* vim: set cindent sw=4: */
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc