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

ascii-boxes / boxes / 7072668221

02 Dec 2023 09:26PM UTC coverage: 88.23% (+4.3%) from 83.954%
7072668221

push

github

tsjensen
.gitignore cmake

2743 of 3298 branches covered (0.0%)

Branch coverage included in aggregate %.

4476 of 4884 relevant lines covered (91.65%)

23903.41 hits per line

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

86.72
/src/tools.c
1
/*
2
 * boxes - Command line filter to draw/remove ASCII boxes around text
3
 * Copyright (c) 1999-2023 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
 * Provide tool functions for error reporting and some string handling
18
 */
19

20
#include "config.h"
21

22
#include <errno.h>
23
#include <stdarg.h>
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <string.h>
27
#include <strings.h>
28
#include <uniconv.h>
29
#include <unictype.h>
30
#include <unistr.h>
31
#include <unitypes.h>
32
#include <uniwidth.h>
33

34
#include "boxes.h"
35
#include "regulex.h"
36
#include "shape.h"
37
#include "tools.h"
38
#include "unicode.h"
39

40

41

42
static pcre2_code *pattern_ascii_id = NULL;
43
static pcre2_code *pattern_ascii_id_strict = NULL;
44

45
/**
46
 * Initialize the `bx_fprintf` function pointer to point to the original
47
 * `bx_fprintf` function, now renamed `bx_fprintf_original`. During unit
48
 * tests, this will be replaced with `__wrap_bx_fprintf`, which stores
49
 * the result that would have been printed so the output can be validated.
50
 * This is necessary for unit testing and CI to work with MacOS.
51
 */
52
bx_fprintf_t bx_fprintf = bx_fprintf_original;
53

54

55
static pcre2_code *get_pattern_ascii_id(int strict)
2,320✔
56
{
57
    pcre2_code *result = NULL;
2,320✔
58
    if (strict) {
2,320✔
59
        if (pattern_ascii_id_strict == NULL) {
2,300✔
60
            pattern_ascii_id_strict = compile_pattern("^(?!.*?--|none)[a-z][a-z0-9-]*(?<!-)$");
268✔
61
        }
134✔
62
        result = pattern_ascii_id_strict;
2,300✔
63
    }
1,150✔
64
    else {
65
        if (pattern_ascii_id == NULL) {
20✔
66
            pattern_ascii_id = compile_pattern("^(?!.*?[-_]{2,}|none)[a-zA-Z][a-zA-Z0-9_-]*(?<![-_])$");
2✔
67
        }
1✔
68
        result = pattern_ascii_id;
20✔
69
    }
70
    return result;
2,320✔
71
}
72

73

74

75
int strisyes(const char *s)
50✔
76
/*
77
 *  Determine if the string s has a contents indicating "yes".
78
 *
79
 *     s   string to examine
80
 *
81
 *  RETURNS:  == 0  string does NOT indicate yes (including errors)
82
 *            != 0  string indicates yes
83
 *
84
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
85
 */
86
{
87
    if (s == NULL) {
50✔
88
        return 0;
2✔
89
    }
90

91
    if (!strncasecmp("on", s, 3)) {
48✔
92
        return 1;
4✔
93
    }
94
    else if (!strncasecmp("yes", s, 4)) {
44✔
95
        return 1;
4✔
96
    }
97
    else if (!strncasecmp("true", s, 5)) {
40✔
98
        return 1;
8✔
99
    }
100
    else if (!strncmp("1", s, 2)) {
32✔
101
        return 1;
2✔
102
    }
103
    else if (!strncasecmp("t", s, 2)) {
30✔
104
        return 1;
4✔
105
    }
106
    else {
107
        return 0;
26✔
108
    }
109
}
25✔
110

111

112

113
int strisno(const char *s)
46✔
114
/*
115
 *  Determine if the string s has a contents indicating "no".
116
 *
117
 *     s   string to examine
118
 *
119
 *  RETURNS:  == 0  string does NOT indicate no (including errors)
120
 *            != 0  string indicates no
121
 *
122
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
123
 */
124
{
125
    if (s == NULL) {
46✔
126
        return 0;
2✔
127
    }
128

129
    if (!strncasecmp("off", s, 4)) {
44✔
130
        return 1;
4✔
131
    }
132
    else if (!strncasecmp("no", s, 3)) {
40✔
133
        return 1;
4✔
134
    }
135
    else if (!strncasecmp("false", s, 6)) {
36✔
136
        return 1;
6✔
137
    }
138
    else if (!strncmp("0", s, 2)) {
30✔
139
        return 1;
2✔
140
    }
141
    else if (!strncasecmp("f", s, 2)) {
28✔
142
        return 1;
4✔
143
    }
144
    else {
145
        return 0;
24✔
146
    }
147
}
23✔
148

149

150

151
char *concat_strings_alloc(size_t count, ...)
59✔
152
{
153
    if (count < 1) {
59!
154
        return strdup("");
×
155
    }
156

157
    size_t total_len = 0;
59✔
158
    const char *src;
159
    va_list va;
160

161
    va_start(va, count);
59✔
162
    for (size_t i = 0; i < count; i++) {
219✔
163
        src = va_arg(va, const char *);
160!
164
        if (src != NULL) {
160✔
165
            total_len += strlen(src);
160✔
166
        }
79✔
167
    }
79✔
168
    va_end(va);
59✔
169

170
    char *result = malloc(total_len + 1);
59✔
171
    char *p = result;
59✔
172

173
    va_start(va, count);
59✔
174
    for (size_t i = 0; i < count; i++) {
219✔
175
        src = va_arg(va, const char *);
160!
176
        if (src != NULL && src[0] != '\0') {
160!
177
            strcpy(p, src);
154✔
178
            p += strlen(src);
154✔
179
        }
76✔
180
    }
79✔
181
    va_end(va);
59✔
182

183
    *p = '\0';
59✔
184
    return result;
59✔
185
}
29✔
186

187

188

189
char *repeat(char *s, size_t count)
2,108✔
190
{
191
    if (s == NULL) {
2,108✔
192
        return NULL;
2✔
193
    }
194

195
    size_t len = strlen(s);
2,106✔
196
    char *result = (char *) malloc(count * len + 1);
2,106✔
197
    if (result != NULL) {
2,106✔
198
        char *dest = result;
2,106✔
199
        for (size_t i = 0; i < count; i++) {
15,368✔
200
            strcpy(dest, s);
13,262✔
201
            dest += len;
13,262✔
202
        }
6,631✔
203
        *dest = '\0';
2,106✔
204
    }
1,053✔
205
    return result;
2,106✔
206
}
1,054✔
207

208

209

210
int empty_line(const line_t *line)
1,290✔
211
{
212
    if (!line) {
1,290!
213
        return 1;
×
214
    }
215
    return bxs_is_blank(line->text);
1,290✔
216
}
645✔
217

218

219

220
size_t expand_tabs_into(const uint32_t *input_buffer, const int tabstop, uint32_t **text, size_t **tabpos,
1,450✔
221
        size_t *tabpos_len)
222
{
223
    static uint32_t temp[LINE_MAX_BYTES + 100]; /* work string */
224
    size_t io;                                  /* character position in work string */
225
    size_t tabnum = 0;                          /* index of the current tab */
1,450✔
226

227
    *text = NULL;
1,450✔
228
    *tabpos = NULL;
1,450✔
229
    *tabpos_len = 0;
1,450✔
230

231
    if (opt.tabexp == 'k') {
1,450✔
232
        /* We need to know the exact tab positions only if expansion type 'k' is requested (keep tabs as much as they
233
         * were as possible). Else we'll just convert spaces and tabs without having to know where exactly the tabs
234
         * were in the first place. */
235
        ucs4_t puc;
236
        const uint32_t *rest = input_buffer;
18✔
237
        while ((rest = u32_next(&puc, rest))) {
200✔
238
            if (puc == char_tab) {
182✔
239
                (*tabpos_len)++;
10✔
240
            }
5✔
241
        }
242
    }
9✔
243

244
    if (*tabpos_len > 0) {
1,450✔
245
        *tabpos = (size_t *) calloc((*tabpos_len) + 1, sizeof(size_t));
6✔
246
        if (*tabpos == NULL) {
6!
247
            return 0; /* out of memory */
×
248
        }
249
    }
3✔
250

251
    ucs4_t puc;
252
    const uint32_t *rest = input_buffer;
1,450✔
253
    io = 0;
1,450✔
254
    while ((rest = u32_next(&puc, rest)) && io < (LINE_MAX_BYTES - 12)) {
93,316✔
255
        if (puc == char_tab) {
91,866✔
256
            if (*tabpos_len > 0) {
1,050✔
257
                (*tabpos)[tabnum++] = io;
10✔
258
            }
5✔
259
            size_t num_spc = tabstop - (io % tabstop);
1,050✔
260
            u32_set(temp + io, char_space, num_spc);
1,050✔
261
            io += num_spc;
1,050✔
262
        }
525✔
263
        else {
264
            set_char_at(temp, io, puc);
90,816✔
265
            ++io;
90,816✔
266
        }
267
    }
268
    temp[io] = 0;
1,450✔
269

270
    *text = u32_strdup(temp);
1,450✔
271
    if (*text == NULL) {
1,450✔
272
        return 0;
×
273
    }
274
    return io;
1,450✔
275
}
725✔
276

277

278

279
void btrim(char *text, size_t *len)
1,870✔
280
/*
281
 *  Remove trailing whitespace from line.
282
 *
283
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
284
 */
285
{
286
    long idx = (long) (*len) - 1;
1,870✔
287

288
    while (idx >= 0 && (text[idx] == '\n' || text[idx] == '\r' || text[idx] == '\t' || text[idx] == ' ')) {
4,695!
289
        text[idx--] = '\0';
1,890✔
290
    }
291

292
    *len = idx + 1;
1,870✔
293
}
1,870✔
294

295

296

297
void btrim32(uint32_t *text, size_t *len)
1,196✔
298
/*
299
 *  Remove trailing whitespace from line (unicode and escape sequence enabled version).
300
 *  CHECK replace by bxs_rtrim() from bxstring module
301
 *
302
 *      text     string to trim
303
 *      len      pointer to the length of the string in characters
304
 *
305
 *  Both the string and the length will be modified as trailing whitespace is removed.
306
 *
307
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
308
 */
309
{
310
    if (text == NULL || len == 0) {
1,196!
311
        return;
×
312
    }
313

314
    const uint32_t *rest = text;
1,196✔
315
    int last_char_pos = -1;
1,196✔
316
    size_t step_invis;
317

318
    for (ucs4_t c = text[0]; c != char_nul; c = rest[0]) {
36,236✔
319
        if (c != char_esc) {
35,040✔
320
            if (!uc_is_c_whitespace(c) && !uc_is_property_white_space(c) && !uc_is_property_bidi_whitespace(c)) {
31,304!
321
                last_char_pos = (int) (rest - text);
23,986✔
322
            }
11,993✔
323
        }
15,652✔
324
        rest = advance_next32(rest, &step_invis);
35,040✔
325
    }
17,520✔
326

327
    /* If the last character is followed by an escape sequence, keep it (but only one). */
328
    if (last_char_pos >= 0) {
1,196✔
329
        rest = text + last_char_pos + 1;
1,164✔
330
        if (rest[0] == char_esc) {
1,164✔
331
            advance_next32(rest, &step_invis);
204✔
332
            last_char_pos += step_invis;
204✔
333
        }
102✔
334
    }
582✔
335

336
    set_char_at(text, (size_t) (last_char_pos + 1), char_nul);
1,196✔
337
    *len = (size_t) (last_char_pos + 1);
1,196✔
338
}
598✔
339

340

341

342
size_t my_strrspn(const char *s, const char *accept)
2,978✔
343
{
344
    if (!s || *s == '\0') {
2,978✔
345
        return 0;
122✔
346
    }
347
    if (!accept || *accept == '\0') {
2,856✔
348
        return 0;
4✔
349
    }
350

351
    for (int i = strlen(s) - 1; i >= 0; i--) {
6,120✔
352
        size_t idx = (size_t) i;
4,126✔
353
        if (strchr(accept, s[idx]) == NULL) {
4,126✔
354
            return strlen(s) - (idx + 1);
858✔
355
        }
356
    }
1,634✔
357
    return strlen(s);
1,994✔
358
}
1,489✔
359

360

361

362
uint32_t *tabbify_indent(const size_t lineno, uint32_t *indentspc, const size_t indentspc_len)
2,096✔
363
{
364
    size_t i;
365
    uint32_t *result;
366
    size_t result_len;
367

368
    if (opt.tabexp != 'k') {
2,096✔
369
        return indentspc;
2,066✔
370
    }
371
    if (lineno >= input.num_lines) {
30!
372
        return NULL;
×
373
    }
374
    if (indentspc_len == 0) {
30✔
375
        return new_empty_string32();
×
376
    }
377

378
    result = (uint32_t *) malloc((indentspc_len + 1) * sizeof(uint32_t));
30✔
379
    if (result == NULL) {
30!
380
        perror(PROJECT);
×
381
        return NULL;
×
382
    }
383
    u32_set(result, char_space, indentspc_len);
30✔
384
    set_char_at(result, indentspc_len, char_nul);
30✔
385
    result_len = indentspc_len;
30✔
386

387
    for (i = 0; i < input.lines[lineno].tabpos_len && input.lines[lineno].tabpos[i] < indentspc_len; ++i) {
36✔
388
        size_t tpos = input.lines[lineno].tabpos[i];
6✔
389
        size_t nspc = opt.tabstop - (tpos % opt.tabstop); /* no of spcs covered by tab */
6✔
390
        if (tpos + nspc > input.indent) {
6✔
391
            break;
×
392
        }
393
        set_char_at(result, tpos, char_tab);
6✔
394
        result_len -= nspc - 1;
6✔
395
        set_char_at(result, result_len, char_nul);
6✔
396
    }
3✔
397

398
    return result;
30✔
399
}
1,048✔
400

401

402

403
char *nspaces(const size_t n)
1,954✔
404
{
405
    char *spaces = (char *) memset(malloc(n + 1), (int) ' ', n);
1,954✔
406
    spaces[n] = '\0';
1,954✔
407
    return spaces;
1,954✔
408
}
409

410

411

412
#if defined(DEBUG) || 0
413

414
/**
415
 *  Debugging Code: Display contents of input structure
416
 */
417
void print_input_lines(const char *heading)
418
{
419
    fprintf(stderr, "%d Input Lines%s:\n", (int) input.num_lines, heading != NULL ? heading : "");
420
    fprintf(stderr, "     [num_chars] \"real text\" [num_cols] \"ascii_text\"\n");
421
    for (size_t i = 0; i < input.num_lines; ++i) {
422
        char *outtext = bxs_to_output(input.lines[i].text);
423
        fprintf(stderr, "%4d [%02d] \"%s\"  [%02d] \"%s\"", (int) i,
424
                (int) input.lines[i].text->num_chars, outtext,
425
                (int) input.lines[i].text->num_columns, input.lines[i].text->ascii);
426
        BFREE(outtext);
427
        fprintf(stderr, "\tTabs: [");
428
        if (input.lines[i].tabpos != NULL) {
429
            for (size_t j = 0; j < input.lines[i].tabpos_len; ++j) {
430
                fprintf(stderr, "%d", (int) input.lines[i].tabpos[j]);
431
                if (j < input.lines[i].tabpos_len - 1) {
432
                    fprintf(stderr, ", ");
433
                }
434
            }
435
        }
436
        fprintf(stderr, "] (%d)", (int) input.lines[i].tabpos_len);
437
        fprintf(stderr, "\tinvisible=%d\n", (int) input.lines[i].text->num_chars_invisible);
438

439
        fprintf(stderr, "    visible_char=");
440
        if (input.lines[i].text->visible_char != NULL) {
441
            fprintf(stderr, "[");
442
            for (size_t j = 0; j < input.lines[i].text->num_chars_visible; j++) {
443
                fprintf(stderr, "%d%s", (int) input.lines[i].text->visible_char[j],
444
                    j == (input.lines[i].text->num_chars_visible - 1) ? "" : ", ");
445
            }
446
            fprintf(stderr, "]\n");
447
        }
448
        else {
449
            fprintf(stderr, "null\n");
450
        }
451

452
        fprintf(stderr, "      first_char=");
453
        if (input.lines[i].text->first_char != NULL) {
454
            fprintf(stderr, "[");
455
            for (size_t j = 0; j < input.lines[i].text->num_chars_visible; j++) {
456
                fprintf(stderr, "%d%s", (int) input.lines[i].text->first_char[j],
457
                    j == (input.lines[i].text->num_chars_visible - 1) ? "" : ", ");
458
            }
459
            fprintf(stderr, "]\n");
460
        }
461
        else {
462
            fprintf(stderr, "null\n");
463
        }
464
    }
465
    fprintf(stderr, " Longest line: %d columns\n", (int) input.maxline);
466
    fprintf(stderr, "  Indentation: %2d spaces\n", (int) input.indent);
467
    fprintf(stderr, "Final newline: %s\n", input.final_newline ? "yes" : "no");
468
}
469

470
#endif
471

472

473

474
/**
475
 * Analyze the multi-byte string in order to determine its metrics:
476
 * - number of visible columns it occupies
477
 * - number of escape characters (== number of escape sequences)
478
 * - the ASCII equivalent of the string
479
 * - the number of invisible characters in the string
480
 *
481
 * @param <s> the multi-byte string to analyze
482
 * @param <num_esc> pointer to where the number of escape sequences should be stored
483
 * @param <ascii> pointer to where the ASCII equivalent of the string should be stored
484
 * @param <posmap> pointer to the position map, which maps each position in <ascii> to a position in <s>
485
 * @returns the number of invisible characters in <s>
486
 */
487
size_t count_invisible_chars(const uint32_t *s, size_t *num_esc, char **ascii, size_t **posmap)
2,954✔
488
{
489
    size_t invis = 0; /* counts invisible column positions */
2,954✔
490
    *num_esc = 0;     /* counts the number of escape sequences found */
2,954✔
491

492
    if (is_empty(s)) {
2,954✔
493
        (*ascii) = (char *) strdup("");
116✔
494
        (*posmap) = NULL;
116✔
495
        return 0;
116✔
496
    }
497

498
    size_t buflen = (size_t) u32_strwidth(s, encoding) + 1;
2,838✔
499
    size_t map_size = BMAX((size_t) 5, buflen);
2,838✔
500
    size_t map_idx = 0;
2,838✔
501
    size_t *map = (size_t *) calloc(map_size, sizeof(size_t)); /* might not be enough if many double-wide chars */
2,838✔
502
    (*ascii) = (char *) calloc(buflen, sizeof(char));          /* maybe a little too much, but certainly enough */
2,838✔
503
    char *p = *ascii;
2,838✔
504

505
    size_t mb_idx = 0;
2,838✔
506
    size_t step_invis;
507
    const uint32_t *rest = s;
2,838✔
508

509
    for (ucs4_t c = s[0]; c != char_nul; c = rest[0]) {
11,756✔
510
        if (map_idx >= map_size - 4) {
8,918✔
511
            map_size = map_size * 2 + 1;
1,290✔
512
            map = (size_t *) realloc(map, map_size * sizeof(size_t));
1,290✔
513
        }
645✔
514

515
        if (c == char_esc) {
8,918!
516
            (*num_esc)++;
×
517
        }
518
        else if (is_ascii_printable(c)) {
8,918!
519
            *p = c & 0xff;
8,918✔
520
            map[map_idx++] = mb_idx;
8,918✔
521
            ++p;
8,918✔
522
        }
4,459✔
523
        else {
524
            int cols = uc_width(c, encoding);
×
525
            if (cols > 0) {
×
526
                memset(p, (int) 'x', cols);
×
527
                for (int i = 0; i < cols; i++) {
×
528
                    map[map_idx++] = mb_idx;
×
529
                }
530
                p += cols;
×
531
            }
532
        }
533

534
        rest = advance_next32(rest, &step_invis);
8,918✔
535

536
        mb_idx += BMAX((size_t) 1, step_invis);
8,918!
537
        invis += step_invis;
8,918✔
538
    }
4,459✔
539

540
    *p = '\0';
2,838✔
541
    (*posmap) = map;
2,838✔
542
    return invis;
2,838✔
543
}
1,477✔
544

545

546

547
int is_csi_reset(const uint32_t *csi)
33,576✔
548
{
549
    ucs4_t puc = '\0';
33,576✔
550
    const uint32_t *rest = csi;
33,576✔
551
    size_t csi_pos = 0;
33,576✔
552
    while ((rest = u32_next(&puc, rest))) {
117,286✔
553
        switch(csi_pos) {
117,280✔
554
            case 0:
16,765✔
555
                if (puc != char_esc) {
33,530✔
556
                    return 0;
4✔
557
                }
558
                break;
33,526✔
559
            case 1:
16,763✔
560
                if (puc != '[' && puc != '(') {
33,526✔
561
                    return 0;
2✔
562
                }
563
                break;
33,524✔
564
            case 2:
16,760✔
565
                if (puc != '0') {
33,564✔
566
                    if (puc >= 0x40 && puc <= 0x7e) {
25,632!
567
                        return 1;
36✔
568
                    }
569
                    else if ((puc == '1' && *rest == '0')
25,596!
570
                          || (puc == '3' && *rest == '9')
25,596✔
571
                          || (puc == '4' && *rest == '9')
29,541!
572
                          || (puc == '5' && *rest == '9')
16,824!
573
                          || (puc == '7' && *rest == '5')) {
16,824!
574
                        rest = u32_next(&puc, rest);
25,434✔
575
                        break;
25,434✔
576
                    }
577
                    return 0;
16,824✔
578
                }
579
                break;
7,932✔
580
            default:
8,330✔
581
                return (puc >= 0x40 && puc <= 0x7e) ? 1 : 0;
16,660!
582
        }
583
        csi_pos++;
83,710✔
584
    }
585
    return 0;
6✔
586
}
16,766✔
587

588

589

590
void analyze_line_ascii(input_t *input_ptr, line_t *line)
2,662✔
591
{
592
    if (line->text->num_columns > input_ptr->maxline) {
2,662✔
593
        input_ptr->maxline = line->text->num_columns;
584✔
594
    }
292✔
595
}
2,662✔
596

597

598

599
int array_contains(char **array, const size_t array_len, const char *s)
×
600
{
601
    int result = 0;
×
602
    if (array != NULL && array_len > 0) {
×
603
        for (size_t i = 0; i < array_len; ++i) {
×
604
            if (strcmp(array[i], s) == 0) {
×
605
                result = 1;
×
606
                break;
×
607
            }
608
        }
609
    }
610
    return result;
×
611
}
612

613

614

615
int array_contains0(char **array, const char *s)
2,392✔
616
{
617
    int result = 0;
2,392✔
618
    if (array != NULL) {
2,392✔
619
        for (size_t i = 0; array[i] != NULL; ++i) {
4,672✔
620
            if (strcasecmp(array[i], s) == 0) {
2,344✔
621
                result = 1;
64✔
622
                break;
64✔
623
            }
624
        }
1,140✔
625
    }
1,196✔
626
    return result;
2,392✔
627
}
628

629

630

631
int array_contains_bxs(bxstr_t **array, const size_t array_len, bxstr_t *s)
52✔
632
{
633
    int result = 0;
52✔
634
    if (array != NULL && array_len > 0) {
52✔
635
        for (size_t i = 0; i < array_len; ++i) {
30✔
636
            if (bxs_strcmp(array[i], s) == 0) {
18✔
637
                result = 1;
2✔
638
                break;
2✔
639
            }
640
        }
8✔
641
    }
7✔
642
    return result;
52✔
643
}
644

645

646

647
size_t array_count0(char **array)
2,318✔
648
{
649
    size_t num_elems = 0;
2,318✔
650
    if (array != NULL) {
2,318!
651
        while (array[num_elems] != NULL) {
4,430✔
652
            ++num_elems;
2,112✔
653
        }
654
    }
1,159✔
655
    return num_elems;
2,318✔
656
}
657

658

659

660
char *trimdup(char *s, char *e)
36✔
661
{
662
    if (s > e || (s == e && *s == '\0')) {
36!
663
        return strdup("");
×
664
    }
665
    while (s <= e && (*s == ' ' || *s == '\t')) {
54!
666
        ++s;
×
667
    }
668
    while (e > s && (*e == ' ' || *e == '\t')) {
54!
669
        --e;
×
670
    }
671
    return bx_strndup(s, e - s + 1);
36✔
672
}
18✔
673

674

675

676
int tag_is_valid(char *tag)
34✔
677
{
678
    pcre2_code *pattern = get_pattern_ascii_id(1);
34✔
679
    return regex_match(pattern, tag);
34✔
680
}
681

682

683

684
int is_ascii_id(bxstr_t *s, int strict)
2,294✔
685
{
686
    if (s == NULL || s->num_chars == 0) {
2,294✔
687
        return 0;
8✔
688
    }
689
    pcre2_code *pattern = get_pattern_ascii_id(strict);
2,286✔
690
    return u32_regex_match(pattern, s->memory);
2,286✔
691
}
1,147✔
692

693

694

695
char *bx_strndup(const char *s, size_t n)
36✔
696
{
697
    if (s == NULL) {
36✔
698
        return NULL;
×
699
    }
700

701
    size_t len = strlen(s);
36✔
702
    if (n < len) {
36✔
703
        len = n;
×
704
    }
705

706
    char *result = (char *) malloc(len + 1);
36✔
707
    if (result == NULL) {
36!
708
        return NULL;
×
709
    }
710

711
    result[len] = '\0';
36✔
712
    return (char *) memcpy(result, s, len);
36✔
713
}
18✔
714

715

716

717
void bx_fprintf_original(FILE *stream, const char *format, ...)
16✔
718
{
719
    va_list va;
720
    va_start(va, format);
16✔
721
    vfprintf(stream, format, va);
16✔
722
    va_end(va);
16✔
723
}
16✔
724

725

726
void set_bx_fprintf(bx_fprintf_t bx_fprintf_function) {
2✔
727
    bx_fprintf = bx_fprintf_function;
2✔
728
}
2✔
729

730

731
FILE *bx_fopens(bxstr_t *pathname, char *mode)
372✔
732
{
733
    return bx_fopen(to_utf8(pathname->memory), mode);
372✔
734
}
735

736

737

738
FILE *bx_fopen(char *pathname, char *mode)
746✔
739
{
740
    /*
741
     * On Linux/UNIX and OS X (Mac), one can access files with non-ASCII file names by passing them to fopen() as UTF-8.
742
     * On Windows, a different function must be called. (Info: https://stackoverflow.com/a/35065142/1005481)
743
     */
744
    FILE *f = fopen(pathname, mode);
746✔
745
    // TODO Windows
746
    return f;
746✔
747
}
748

749

750
/* vim: set 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