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

ascii-boxes / boxes / 6518013212

14 Oct 2023 01:37PM UTC coverage: 81.211% (-0.4%) from 81.608%
6518013212

push

github

tsjensen
remove

2349 of 3210 branches covered (0.0%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

3767 of 4321 relevant lines covered (87.18%)

7801.63 hits per line

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

74.01
/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
static pcre2_code *get_pattern_ascii_id(int strict)
770✔
47
{
48
    pcre2_code *result = NULL;
770✔
49
    if (strict) {
770✔
50
        if (pattern_ascii_id_strict == NULL) {
760✔
51
            pattern_ascii_id_strict = compile_pattern("^(?!.*?--|none)[a-z][a-z0-9-]*(?<!-)$");
127✔
52
        }
53
        result = pattern_ascii_id_strict;
760✔
54
    }
55
    else {
56
        if (pattern_ascii_id == NULL) {
10✔
57
            pattern_ascii_id = compile_pattern("^(?!.*?[-_]{2,}|none)[a-zA-Z][a-zA-Z0-9_-]*(?<![-_])$");
1✔
58
        }
59
        result = pattern_ascii_id;
10✔
60
    }
61
    return result;
770✔
62
}
63

64

65

66
int strisyes(const char *s)
25✔
67
/*
68
 *  Determine if the string s has a contents indicating "yes".
69
 *
70
 *     s   string to examine
71
 *
72
 *  RETURNS:  == 0  string does NOT indicate yes (including errors)
73
 *            != 0  string indicates yes
74
 *
75
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
76
 */
77
{
78
    if (s == NULL) {
25✔
79
        return 0;
1✔
80
    }
81

82
    if (!strncasecmp("on", s, 3)) {
24✔
83
        return 1;
2✔
84
    }
85
    else if (!strncasecmp("yes", s, 4)) {
22✔
86
        return 1;
2✔
87
    }
88
    else if (!strncasecmp("true", s, 5)) {
20✔
89
        return 1;
4✔
90
    }
91
    else if (!strncmp("1", s, 2)) {
16✔
92
        return 1;
1✔
93
    }
94
    else if (!strncasecmp("t", s, 2)) {
15✔
95
        return 1;
2✔
96
    }
97
    else {
98
        return 0;
13✔
99
    }
100
}
101

102

103

104
int strisno(const char *s)
23✔
105
/*
106
 *  Determine if the string s has a contents indicating "no".
107
 *
108
 *     s   string to examine
109
 *
110
 *  RETURNS:  == 0  string does NOT indicate no (including errors)
111
 *            != 0  string indicates no
112
 *
113
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
114
 */
115
{
116
    if (s == NULL) {
23✔
117
        return 0;
1✔
118
    }
119

120
    if (!strncasecmp("off", s, 4)) {
22✔
121
        return 1;
2✔
122
    }
123
    else if (!strncasecmp("no", s, 3)) {
20✔
124
        return 1;
2✔
125
    }
126
    else if (!strncasecmp("false", s, 6)) {
18✔
127
        return 1;
3✔
128
    }
129
    else if (!strncmp("0", s, 2)) {
15✔
130
        return 1;
1✔
131
    }
132
    else if (!strncasecmp("f", s, 2)) {
14✔
133
        return 1;
2✔
134
    }
135
    else {
136
        return 0;
12✔
137
    }
138
}
139

140

141

142
void concat_strings(char *dst, int max_len, int count, ...)
×
143
/*
144
 *  Concatenate a variable number of strings into a fixed-length buffer.
145
 *
146
 *      dst     Destination array
147
 *      max_len Maximum resulting string length (including terminating NULL).
148
 *      count   Number of source strings.
149
 *
150
 *  The concatenation process terminates when either the destination
151
 *  buffer is full or all 'count' strings are processed.  Null string
152
 *  pointers are treated as empty strings.
153
 *
154
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
155
 */
156
{
157
    va_list va;
158
    const char *src;
159

160
    va_start(va, count);
×
161

162
    /*
163
     *  Sanity check.
164
     */
165
    if (max_len < 1) {
×
166
        return;
×
167
    }
168

169
    if (max_len == 1 || count < 1) {
×
170
        *dst = '\0';
×
171
        return;
×
172
    }
173

174
    /*
175
     *  Loop over all input strings.
176
     */
177
    while (count-- > 0 && max_len > 1) {
×
178
        /*
179
         * Grab an input string pointer.  If it's NULL, skip it (eg. treat
180
         * it as empty.
181
         */
182
        src = va_arg(va, const char *);
×
183

184
        if (src == NULL) {
×
185
            continue;
×
186
        }
187

188
        /*
189
         * Concatenate 'src' onto 'dst', as long as we have room.
190
         */
191
        while (*src && max_len > 1) {
×
192
            *dst++ = *src++;
×
193
            max_len--;
×
194
        }
195
    }
196

197
    va_end(va);
×
198

199
    /*
200
     * Terminate the string with an ASCII NUL.
201
     */
202
    *dst = '\0';
×
203
}
204

205

206

207
char *concat_strings_alloc(size_t count, ...)
30✔
208
{
209
    if (count < 1) {
30!
210
        return strdup("");
×
211
    }
212

213
    size_t total_len = 0;
30✔
214
    const char *src;
215
    va_list va;
216

217
    va_start(va, count);
30✔
218
    for (size_t i = 0; i < count; i++) {
111✔
219
        src = va_arg(va, const char *);
81✔
220
        if (src != NULL) {
81!
221
            total_len += strlen(src);
81✔
222
        }
223
    }
224
    va_end(va);
30✔
225

226
    char *result = malloc(total_len + 1);
30✔
227
    char *p = result;
30✔
228

229
    va_start(va, count);
30✔
230
    for (size_t i = 0; i < count; i++) {
111✔
231
        src = va_arg(va, const char *);
81✔
232
        if (src != NULL && src[0] != '\0') {
81!
233
            strcpy(p, src);
78✔
234
            p += strlen(src);
78✔
235
        }
236
    }
237
    va_end(va);
30✔
238

239
    *p = '\0';
30✔
240
    return result;
30✔
241
}
242

243

244

245
char *repeat(char *s, size_t count)
1,049✔
246
{
247
    if (s == NULL) {
1,049✔
248
        return NULL;
1✔
249
    }
250

251
    size_t len = strlen(s);
1,048✔
252
    char *result = (char *) malloc(count * len + 1);
1,048✔
253
    if (result != NULL) {
1,048!
254
        char *dest = result;
1,048✔
255
        for (size_t i = 0; i < count; i++) {
7,655✔
256
            strcpy(dest, s);
6,607✔
257
            dest += len;
6,607✔
258
        }
259
        *dest = '\0';
1,048✔
260
    }
261
    return result;
1,048✔
262
}
263

264

265

266
int empty_line(const line_t *line)
624✔
267
{
268
    if (!line) {
624!
269
        return 1;
×
270
    }
271
    return bxs_is_blank(line->text);
624✔
272
}
273

274

275

276
size_t expand_tabs_into(const uint32_t *input_buffer, const int tabstop, uint32_t **text, size_t **tabpos,
650✔
277
        size_t *tabpos_len)
278
{
279
    static uint32_t temp[LINE_MAX_BYTES + 100]; /* work string */
280
    size_t io;                                  /* character position in work string */
281
    size_t tabnum = 0;                          /* index of the current tab */
650✔
282

283
    *text = NULL;
650✔
284
    *tabpos = NULL;
650✔
285
    *tabpos_len = 0;
650✔
286

287
    if (opt.tabexp == 'k') {
650✔
288
        /* We need to know the exact tab positions only if expansion type 'k' is requested (keep tabs as much as they
289
         * were as possible). Else we'll just convert spaces and tabs without having to know where exactly the tabs
290
         * were in the first place. */
291
        ucs4_t puc;
292
        const uint32_t *rest = input_buffer;
9✔
293
        while ((rest = u32_next(&puc, rest))) {
100✔
294
            if (puc == char_tab) {
91✔
295
                (*tabpos_len)++;
5✔
296
            }
297
        }
298
    }
299

300
    if (*tabpos_len > 0) {
650✔
301
        *tabpos = (size_t *) calloc((*tabpos_len) + 1, sizeof(size_t));
3✔
302
        if (*tabpos == NULL) {
3!
303
            return 0; /* out of memory */
×
304
        }
305
    }
306

307
    ucs4_t puc;
308
    const uint32_t *rest = input_buffer;
650✔
309
    io = 0;
650✔
310
    while ((rest = u32_next(&puc, rest)) && io < (LINE_MAX_BYTES - 12)) {
35,250!
311
        if (puc == char_tab) {
34,600✔
312
            if (*tabpos_len > 0) {
525✔
313
                (*tabpos)[tabnum++] = io;
5✔
314
            }
315
            size_t num_spc = tabstop - (io % tabstop);
525✔
316
            u32_set(temp + io, char_space, num_spc);
525✔
317
            io += num_spc;
525✔
318
        }
319
        else {
320
            set_char_at(temp, io, puc);
34,075✔
321
            ++io;
34,075✔
322
        }
323
    }
324
    temp[io] = 0;
650✔
325

326
    *text = u32_strdup(temp);
650✔
327
    if (*text == NULL) {
650!
328
        return 0;
×
329
    }
330
    return io;
650✔
331
}
332

333

334

335
void btrim(char *text, size_t *len)
593✔
336
/*
337
 *  Remove trailing whitespace from line.
338
 *
339
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
340
 */
341
{
342
    long idx = (long) (*len) - 1;
593✔
343

344
    while (idx >= 0 && (text[idx] == '\n' || text[idx] == '\r' || text[idx] == '\t' || text[idx] == ' ')) {
1,190!
345
        text[idx--] = '\0';
597✔
346
    }
347

348
    *len = idx + 1;
593✔
349
}
593✔
350

351

352

353
void btrim32(uint32_t *text, size_t *len)
597✔
354
/*
355
 *  Remove trailing whitespace from line (unicode and escape sequence enabled version).
356
 *  CHECK replace by bxs_rtrim() from bxstring module
357
 *
358
 *      text     string to trim
359
 *      len      pointer to the length of the string in characters
360
 *
361
 *  Both the string and the length will be modified as trailing whitespace is removed.
362
 *
363
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
364
 */
365
{
366
    if (text == NULL || len == 0) {
597!
367
        return;
×
368
    }
369

370
    const uint32_t *rest = text;
597✔
371
    int last_char_pos = -1;
597✔
372
    size_t step_invis;
373

374
    for (ucs4_t c = text[0]; c != char_nul; c = rest[0]) {
18,091✔
375
        if (c != char_esc) {
17,494✔
376
            if (!uc_is_c_whitespace(c) && !uc_is_property_white_space(c) && !uc_is_property_bidi_whitespace(c)) {
15,626!
377
                last_char_pos = (int) (rest - text);
11,976✔
378
            }
379
        }
380
        rest = advance_next32(rest, &step_invis);
17,494✔
381
    }
382

383
    /* If the last character is followed by an escape sequence, keep it (but only one). */
384
    if (last_char_pos >= 0) {
597✔
385
        rest = text + last_char_pos + 1;
581✔
386
        if (rest[0] == char_esc) {
581✔
387
            advance_next32(rest, &step_invis);
102✔
388
            last_char_pos += step_invis;
102✔
389
        }
390
    }
391

392
    set_char_at(text, (size_t) (last_char_pos + 1), char_nul);
597✔
393
    *len = (size_t) (last_char_pos + 1);
597✔
394
}
395

396

397

398
char *my_strnrstr(const char *s1, const char *s2, const size_t s2_len, int skip)
×
399
/*
400
 *  Return pointer to last occurrence of string s2 in string s1.
401
 *
402
 *      s1       string to search
403
 *      s2       string to search for in s1
404
 *      s2_len   length in characters of s2
405
 *      skip     number of finds to ignore before returning anything
406
 *
407
 *  RETURNS: pointer to last occurrence of string s2 in string s1
408
 *           NULL if not found or error
409
 *
410
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
411
 */
412
{
413
    char *p;
414
    int comp;
415

416
    if (!s2 || *s2 == '\0') {
×
417
        return (char *) s1;
×
418
    }
419
    if (!s1 || *s1 == '\0') {
×
420
        return NULL;
×
421
    }
422
    if (skip < 0) {
×
423
        skip = 0;
×
424
    }
425

426
    p = strrchr(s1, s2[0]);
×
427
    if (!p) {
×
428
        return NULL;
×
429
    }
430

431
    while (p >= s1) {
×
432
        comp = strncmp(p, s2, s2_len);
×
433
        if (comp == 0) {
×
434
            if (skip--) {
×
435
                --p;
×
436
            }
437
            else {
438
                return p;
×
439
            }
440
        }
441
        else {
442
            --p;
×
443
        }
444
    }
445

446
    return NULL;
×
447
}
448

449

450

451
size_t my_strrspn(const char *s, const char *accept)
1,204✔
452
{
453
    if (!s || *s == '\0') {
1,204✔
454
        return 0;
58✔
455
    }
456
    if (!accept || *accept == '\0') {
1,146✔
457
        return 0;
2✔
458
    }
459

460
    for (int i = strlen(s) - 1; i >= 0; i--) {
2,502✔
461
        size_t idx = (size_t) i;
1,689✔
462
        if (strchr(accept, s[idx]) == NULL) {
1,689✔
463
            return strlen(s) - (idx + 1);
331✔
464
        }
465
    }
466
    return strlen(s);
813✔
467
}
468

469

470

471
uint32_t *tabbify_indent(const size_t lineno, uint32_t *indentspc, const size_t indentspc_len)
1,043✔
472
{
473
    size_t i;
474
    uint32_t *result;
475
    size_t result_len;
476

477
    if (opt.tabexp != 'k') {
1,043✔
478
        return indentspc;
1,028✔
479
    }
480
    if (lineno >= input.num_lines) {
15!
481
        return NULL;
×
482
    }
483
    if (indentspc_len == 0) {
15!
484
        return new_empty_string32();
×
485
    }
486

487
    result = (uint32_t *) malloc((indentspc_len + 1) * sizeof(uint32_t));
15✔
488
    if (result == NULL) {
15!
489
        perror(PROJECT);
×
490
        return NULL;
×
491
    }
492
    u32_set(result, char_space, indentspc_len);
15✔
493
    set_char_at(result, indentspc_len, char_nul);
15✔
494
    result_len = indentspc_len;
15✔
495

496
    for (i = 0; i < input.lines[lineno].tabpos_len && input.lines[lineno].tabpos[i] < indentspc_len; ++i) {
18✔
497
        size_t tpos = input.lines[lineno].tabpos[i];
3✔
498
        size_t nspc = opt.tabstop - (tpos % opt.tabstop); /* no of spcs covered by tab */
3✔
499
        if (tpos + nspc > input.indent) {
3!
500
            break;
×
501
        }
502
        set_char_at(result, tpos, char_tab);
3✔
503
        result_len -= nspc - 1;
3✔
504
        set_char_at(result, result_len, char_nul);
3✔
505
    }
506

507
    return result;
15✔
508
}
509

510

511

512
char *nspaces(const size_t n)
793✔
513
{
514
    char *spaces = (char *) memset(malloc(n + 1), (int) ' ', n);
793✔
515
    spaces[n] = '\0';
793✔
516
    return spaces;
793✔
517
}
518

519

520

521
#if defined(DEBUG) || 0
522

523
/**
524
 *  Debugging Code: Display contents of input structure
525
 */
526
void print_input_lines(const char *heading)
527
{
528
    fprintf(stderr, "%d Input Lines%s:\n", (int) input.num_lines, heading != NULL ? heading : "");
529
    fprintf(stderr, "     [num_chars] \"real text\" [num_cols] \"ascii_text\"\n");
530
    for (size_t i = 0; i < input.num_lines; ++i) {
531
        char *outtext = bxs_to_output(input.lines[i].text);
532
        fprintf(stderr, "%4d [%02d] \"%s\"  [%02d] \"%s\"", (int) i,
533
                (int) input.lines[i].text->num_chars, outtext,
534
                (int) input.lines[i].text->num_columns, input.lines[i].text->ascii);
535
        BFREE(outtext);
536
        fprintf(stderr, "\tTabs: [");
537
        if (input.lines[i].tabpos != NULL) {
538
            for (size_t j = 0; j < input.lines[i].tabpos_len; ++j) {
539
                fprintf(stderr, "%d", (int) input.lines[i].tabpos[j]);
540
                if (j < input.lines[i].tabpos_len - 1) {
541
                    fprintf(stderr, ", ");
542
                }
543
            }
544
        }
545
        fprintf(stderr, "] (%d)", (int) input.lines[i].tabpos_len);
546
        fprintf(stderr, "\tinvisible=%d\n", (int) input.lines[i].text->num_chars_invisible);
547

548
        fprintf(stderr, "    visible_char=");
549
        if (input.lines[i].text->visible_char != NULL) {
550
            fprintf(stderr, "[");
551
            for (size_t j = 0; j < input.lines[i].text->num_chars_visible; j++) {
552
                fprintf(stderr, "%d%s", (int) input.lines[i].text->visible_char[j],
553
                    j == (input.lines[i].text->num_chars_visible - 1) ? "" : ", ");
554
            }
555
            fprintf(stderr, "]\n");
556
        }
557
        else {
558
            fprintf(stderr, "null\n");
559
        }
560

561
        fprintf(stderr, "      first_char=");
562
        if (input.lines[i].text->first_char != NULL) {
563
            fprintf(stderr, "[");
564
            for (size_t j = 0; j < input.lines[i].text->num_chars_visible; j++) {
565
                fprintf(stderr, "%d%s", (int) input.lines[i].text->first_char[j],
566
                    j == (input.lines[i].text->num_chars_visible - 1) ? "" : ", ");
567
            }
568
            fprintf(stderr, "]\n");
569
        }
570
        else {
571
            fprintf(stderr, "null\n");
572
        }
573
    }
574
    fprintf(stderr, " Longest line: %d columns\n", (int) input.maxline);
575
    fprintf(stderr, "  Indentation: %2d spaces\n", (int) input.indent);
576
    fprintf(stderr, "Final newline: %s\n", input.final_newline ? "yes" : "no");
577
}
578

579
#endif
580

581

582

583
/**
584
 * Analyze the multi-byte string in order to determine its metrics:
585
 * - number of visible columns it occupies
586
 * - number of escape characters (== number of escape sequences)
587
 * - the ASCII equivalent of the string
588
 * - the number of invisible characters in the string
589
 *
590
 * @param <s> the multi-byte string to analyze
591
 * @param <num_esc> pointer to where the number of escape sequences should be stored
592
 * @param <ascii> pointer to where the ASCII equivalent of the string should be stored
593
 * @param <posmap> pointer to the position map, which maps each position in <ascii> to a position in <s>
594
 * @returns the number of invisible characters in <s>
595
 */
596
size_t count_invisible_chars(const uint32_t *s, size_t *num_esc, char **ascii, size_t **posmap)
1,192✔
597
{
598
    size_t invis = 0; /* counts invisible column positions */
1,192✔
599
    *num_esc = 0;     /* counts the number of escape sequences found */
1,192✔
600

601
    if (is_empty(s)) {
1,192✔
602
        (*ascii) = (char *) strdup("");
55✔
603
        (*posmap) = NULL;
55✔
604
        return 0;
55✔
605
    }
606

607
    size_t buflen = (size_t) u32_strwidth(s, encoding) + 1;
1,137✔
608
    size_t map_size = BMAX((size_t) 5, buflen);
1,137✔
609
    size_t map_idx = 0;
1,137✔
610
    size_t *map = (size_t *) calloc(map_size, sizeof(size_t)); /* might not be enough if many double-wide chars */
1,137✔
611
    (*ascii) = (char *) calloc(buflen, sizeof(char));          /* maybe a little too much, but certainly enough */
1,137✔
612
    char *p = *ascii;
1,137✔
613

614
    size_t mb_idx = 0;
1,137✔
615
    size_t step_invis;
616
    const uint32_t *rest = s;
1,137✔
617

618
    for (ucs4_t c = s[0]; c != char_nul; c = rest[0]) {
4,634✔
619
        if (map_idx >= map_size - 4) {
3,497✔
620
            map_size = map_size * 2 + 1;
507✔
621
            map = (size_t *) realloc(map, map_size * sizeof(size_t));
507✔
622
        }
623

624
        if (c == char_esc) {
3,497!
625
            (*num_esc)++;
×
626
        }
627
        else if (is_ascii_printable(c)) {
3,497!
628
            *p = c & 0xff;
3,497✔
629
            map[map_idx++] = mb_idx;
3,497✔
630
            ++p;
3,497✔
631
        }
632
        else {
633
            int cols = uc_width(c, encoding);
×
634
            if (cols > 0) {
×
635
                memset(p, (int) 'x', cols);
×
636
                for (int i = 0; i < cols; i++) {
×
637
                    map[map_idx++] = mb_idx;
×
638
                }
639
                p += cols;
×
640
            }
641
        }
642

643
        rest = advance_next32(rest, &step_invis);
3,497✔
644

645
        mb_idx += BMAX((size_t) 1, step_invis);
3,497✔
646
        invis += step_invis;
3,497✔
647
    }
648

649
    *p = '\0';
1,137✔
650
    (*posmap) = map;
1,137✔
651
    return invis;
1,137✔
652
}
653

654

655

656
int is_csi_reset(const uint32_t *csi)
8,035✔
657
{
658
    ucs4_t puc = '\0';
8,035✔
659
    const uint32_t *rest = csi;
8,035✔
660
    size_t csi_pos = 0;
8,035✔
661
    while ((rest = u32_next(&puc, rest))) {
27,224✔
662
        switch(csi_pos) {
27,221✔
663
            case 0:
8,034✔
664
                if (puc != char_esc) {
8,034✔
665
                    return 0;
2✔
666
                }
667
                break;
8,032✔
668
            case 1:
8,032✔
669
                if (puc != '[' && puc != '(') {
8,032✔
670
                    return 0;
1✔
671
                }
672
                break;
8,031✔
673
            case 2:
8,029✔
674
                if (puc != '0') {
8,029✔
675
                    if (puc >= 0x40 && puc <= 0x7e) {
4,903!
676
                        return 1;
18✔
677
                    }
678
                    return 0;
4,885✔
679
                }
680
                break;
3,126✔
681
            default:
3,126✔
682
                return (puc >= 0x40 && puc <= 0x7e) ? 1 : 0;
3,126!
683
        }
684
        csi_pos++;
19,189✔
685
    }
686
    return 0;
3✔
687
}
688

689

690

691
void analyze_line_ascii(input_t *input_ptr, line_t *line)
1,248✔
692
{
693
    if (line->text->num_columns > input_ptr->maxline) {
1,248✔
694
        input_ptr->maxline = line->text->num_columns;
261✔
695
    }
696
}
1,248✔
697

698

699

700
int array_contains(char **array, const size_t array_len, const char *s)
×
701
{
702
    int result = 0;
×
703
    if (array != NULL && array_len > 0) {
×
704
        for (size_t i = 0; i < array_len; ++i) {
×
705
            if (strcmp(array[i], s) == 0) {
×
706
                result = 1;
×
707
                break;
×
708
            }
709
        }
710
    }
711
    return result;
×
712
}
713

714

715

716
int array_contains0(char **array, const char *s)
806✔
717
{
718
    int result = 0;
806✔
719
    if (array != NULL) {
806!
720
        for (size_t i = 0; array[i] != NULL; ++i) {
1,564✔
721
            if (strcasecmp(array[i], s) == 0) {
790✔
722
                result = 1;
32✔
723
                break;
32✔
724
            }
725
        }
726
    }
727
    return result;
806✔
728
}
729

730

731

732
int array_contains_bxs(bxstr_t **array, const size_t array_len, bxstr_t *s)
26✔
733
{
734
    int result = 0;
26✔
735
    if (array != NULL && array_len > 0) {
26!
736
        for (size_t i = 0; i < array_len; ++i) {
15✔
737
            if (bxs_strcmp(array[i], s) == 0) {
9✔
738
                result = 1;
1✔
739
                break;
1✔
740
            }
741
        }
742
    }
743
    return result;
26✔
744
}
745

746

747

748
size_t array_count0(char **array)
769✔
749
{
750
    size_t num_elems = 0;
769✔
751
    if (array != NULL) {
769!
752
        while (array[num_elems] != NULL) {
1,443✔
753
            ++num_elems;
674✔
754
        }
755
    }
756
    return num_elems;
769✔
757
}
758

759

760

761
char *trimdup(char *s, char *e)
18✔
762
{
763
    if (s > e || (s == e && *s == '\0')) {
18!
764
        return strdup("");
×
765
    }
766
    while (s <= e && (*s == ' ' || *s == '\t')) {
18!
767
        ++s;
×
768
    }
769
    while (e > s && (*e == ' ' || *e == '\t')) {
18!
770
        --e;
×
771
    }
772
    return bx_strndup(s, e - s + 1);
18✔
773
}
774

775

776

777
int tag_is_valid(char *tag)
17✔
778
{
779
    pcre2_code *pattern = get_pattern_ascii_id(1);
17✔
780
    return regex_match(pattern, tag);
17✔
781
}
782

783

784

785
int is_ascii_id(bxstr_t *s, int strict)
757✔
786
{
787
    if (s == NULL || s->num_chars == 0) {
757✔
788
        return 0;
4✔
789
    }
790
    pcre2_code *pattern = get_pattern_ascii_id(strict);
753✔
791
    return u32_regex_match(pattern, s->memory);
753✔
792
}
793

794

795

796
char *bx_strndup(const char *s, size_t n)
18✔
797
{
798
    if (s == NULL) {
18!
799
        return NULL;
×
800
    }
801

802
    size_t len = strlen(s);
18✔
803
    if (n < len) {
18!
804
        len = n;
×
805
    }
806

807
    char *result = (char *) malloc(len + 1);
18✔
808
    if (result == NULL) {
18!
809
        return NULL;
×
810
    }
811

812
    result[len] = '\0';
18✔
813
    return (char *) memcpy(result, s, len);
18✔
814
}
815

816

817

818
void bx_fprintf(FILE *stream, const char *format, ...)
8✔
819
{
820
    va_list va;
821
    va_start(va, format);
8✔
822
    vfprintf(stream, format, va);
8✔
823
    va_end(va);
8✔
824
}
8✔
825

826

827

828
FILE *bx_fopens(bxstr_t *pathname, char *mode)
177✔
829
{
830
    return bx_fopen(to_utf8(pathname->memory), mode);
177✔
831
}
832

833

834

835
FILE *bx_fopen(char *pathname, char *mode)
355✔
836
{
837
    /*
838
     * On Linux/UNIX and OS X (Mac), one can access files with non-ASCII file names by passing them to fopen() as UTF-8.
839
     * On Windows, a different function must be called. (Info: https://stackoverflow.com/a/35065142/1005481)
840
     */
841
    FILE *f = fopen(pathname, mode);
355✔
842
    // TODO Windows
843
    return f;
355✔
844
}
845

846

847
/* 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