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

ascii-boxes / boxes / 27473318100

13 Jun 2026 05:05PM UTC coverage: 83.929% (+6.1%) from 77.822%
27473318100

push

github

tsjensen
Fix coverage measurement on macos-26

2624 of 3395 branches covered (77.29%)

Branch coverage included in aggregate %.

9 of 9 new or added lines in 2 files covered. (100.0%)

15 existing lines in 6 files now uncovered.

4066 of 4576 relevant lines covered (88.85%)

196072.68 hits per line

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

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

17
/*
18
 * Box removal, i.e. the deletion of boxes
19
 */
20

21
#include "config.h"
22

23
#include <stdint.h>
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <string.h>
27
#include <unistr.h>
28
#include <uniwidth.h>
29

30
#include "boxes.h"
31
#include "detect.h"
32
#include "logging.h"
33
#include "remove.h"
34
#include "shape.h"
35
#include "tools.h"
36
#include "unicode.h"
37

38

39

40
typedef struct _line_ctx_t {
41
    /** index of the first character of the west shape */
42
    size_t west_start;
43

44
    /** index of the character following the last character of the west shape. If equal to `west_start`, then no west
45
     *  shape was detected. */
46
    size_t west_end;
47

48
    /** the length in characters of the matched west shape part */
49
    size_t west_quality;
50

51
    /** index of the first character of the east shape */
52
    size_t east_start;
53

54
    /** index of the character following the last character of the east shape. If equal to `east_start`, then no east
55
     *  shape was detected.  */
56
    size_t east_end;
57

58
    /** the length in characters of the matched east shape part */
59
    size_t east_quality;
60

61
    /** the input line to which the above values refer. Will look very different depending on comparison type. */
62
    uint32_t *input_line_used;
63
} line_ctx_t;
64

65

66

67
typedef struct _remove_ctx_t {
68
    /** Array of flags indicating which sides of the box design are defined as empty. Access via `BTOP` etc. constants. */
69
    int empty_side[NUM_SIDES];
70

71
    /** Flag indicating that there are no invisible characters in the definition of the design we are removing. */
72
    int design_is_mono;
73

74
    /** Flag indicating that there are no invisible characters in the input. */
75
    int input_is_mono;
76

77
    /** Index into `input.lines` of the first line of the box (topmost box line). Lines above are blank. */
78
    size_t top_start_idx;
79

80
    /** Index into `input.lines` of the line following the last line of the top part of the box. If the top part of the
81
     *  box is empty or missing, this value will be equal to `top_start_idx`. */
82
    size_t top_end_idx;
83

84
    /** Index into `input.lines` of the first line of the bottom side of the box. */
85
    size_t bottom_start_idx;
86

87
    /** Index into `input.lines` of the line following the last line of the bottom part of the box. If the bottom part
88
     *  of the box is empty or missing, this value will be equal to `bottom_start_idx`. Lines below are blank. */
89
    size_t bottom_end_idx;
90

91
    /** The current comparison type. This changes whenever another comparison type is tried. */
92
    comparison_t comp_type;
93

94
    /** number of lines in `body` */
95
    size_t body_num_lines;
96

97
    /** Information on the vertical east and west shapes in body lines, one entry for each line between `top_end_idx`
98
     *  (inclusive) and `bottom_start_idx` (exclusive) */
99
    line_ctx_t *body;
100
} remove_ctx_t;
101

102

103

104
static void debug_print_remove_ctx(remove_ctx_t *ctx, char *heading)
320✔
105
{
106
    if (is_debug_logging(MAIN)) {
320!
107
        log_debug(__FILE__, MAIN, "Remove Context %s:\n", heading);
×
108
        log_debug(__FILE__, MAIN, "    - empty_side[BTOP] = %s\n", ctx->empty_side[BTOP] ? "true" : "false");
×
109
        log_debug(__FILE__, MAIN, "    - empty_side[BRIG] = %s\n", ctx->empty_side[BRIG] ? "true" : "false");
×
110
        log_debug(__FILE__, MAIN, "    - empty_side[BBOT] = %s\n", ctx->empty_side[BBOT] ? "true" : "false");
×
111
        log_debug(__FILE__, MAIN, "    - empty_side[BLEF] = %s\n", ctx->empty_side[BLEF] ? "true" : "false");
×
112
        log_debug(__FILE__, MAIN, "    - design_is_mono = %s\n", ctx->design_is_mono ? "true" : "false");
×
113
        log_debug(__FILE__, MAIN, "    - input_is_mono = %s\n", ctx->input_is_mono ? "true" : "false");
×
114
        log_debug(__FILE__, MAIN, "    - top_start_idx = %d\n", (int) ctx->top_start_idx);
×
115
        log_debug(__FILE__, MAIN, "    - top_end_idx = %d\n", (int) ctx->top_end_idx);
×
116
        log_debug(__FILE__, MAIN, "    - bottom_start_idx = %d\n", (int) ctx->bottom_start_idx);
×
117
        log_debug(__FILE__, MAIN, "    - bottom_end_idx = %d\n", (int) ctx->bottom_end_idx);
×
118
        log_debug(__FILE__, MAIN, "    - comp_type = %s\n", comparison_name[ctx->comp_type]);
×
119
        log_debug(__FILE__, MAIN, "    - body (%d lines):\n", (int) ctx->body_num_lines);
×
120
        for (size_t i = 0; i < ctx->body_num_lines; i++) {
×
121
            if (ctx->body[i].input_line_used != NULL) {
×
122
                char *out_input_line_used = u32_strconv_to_output(ctx->body[i].input_line_used);
×
123
                log_debug(__FILE__, MAIN, "        - lctx: \"%s\" (%d characters)\n", out_input_line_used,
×
124
                    (int) u32_strlen(ctx->body[i].input_line_used));
×
125
                BFREE(out_input_line_used);
×
126
            }
127
            else {
128
                log_debug(__FILE__, MAIN, "        - lctx: (null)\n");
×
129
            }
130
            bxstr_t *orgline = input.lines[ctx->top_end_idx + i].text;
×
131
            if (orgline != NULL) {
×
132
                char *out_orgline = bxs_to_output(orgline);
×
133
                log_debug(__FILE__, MAIN, "          orgl: \"%s\" (%d characters, %d columns)\n", out_orgline,
×
134
                    (int) orgline->num_chars, (int) orgline->num_columns);
×
135
                BFREE(out_orgline);
×
136
            }
137
            else {
138
                log_debug(__FILE__, MAIN, "          orgl: (null)\n");
×
139
            }
140
            log_debug(__FILE__, MAIN, "                west: %d-%d (quality: %d), east: %d-%d (quality: %d)\n",
×
141
                (int) ctx->body[i].west_start, (int) ctx->body[i].west_end, (int) ctx->body[i].west_quality,
×
142
                (int) ctx->body[i].east_start, (int) ctx->body[i].east_end, (int) ctx->body[i].east_quality);
×
143
        }
144
    }
145
}
320✔
146

147

148

149
static void debug_print_shapes_relevant(shape_line_ctx_t *shapes_relevant)
1,590✔
150
{
151
    if (is_debug_logging(MAIN)) {
1,590!
152
        log_debug(__FILE__, MAIN, "  shapes_relevant = {");
×
153
        for (size_t ds = 0; ds < SHAPES_PER_SIDE; ds++) {
×
154
            if (shapes_relevant[ds].empty) {
×
155
                log_debug_cont(MAIN, "-");
×
156
            }
157
            else {
158
                char *out_shp_text = bxs_to_output(shapes_relevant[ds].text);
×
159
                log_debug_cont(MAIN, "\"%s\"(%d%s)", out_shp_text, (int) shapes_relevant[ds].text->num_chars,
×
160
                    shapes_relevant[ds].elastic ? "E" : "");
×
161
                BFREE(out_shp_text);
×
162
            }
163
            if (ds < SHAPES_PER_SIDE - 1) {
×
164
                log_debug_cont(MAIN, ", ");
×
165
            }
166
        }
167
        log_debug_cont(MAIN, "}\n");
×
168
    }
169
}
1,590✔
170

171

172

173
static size_t find_first_line()
320✔
174
{
175
    size_t result = input.num_lines;
320✔
176
    for (size_t line_idx = 0; line_idx < input.num_lines; line_idx++) {
324!
177
        if (!bxs_is_blank(input.lines[line_idx].text)) {
324✔
178
            result = line_idx;
320✔
179
            break;
320✔
180
        }
181
    }
182
    return result;
320✔
183
}
184

185

186

187
static size_t find_last_line()
320✔
188
{
189
    size_t result = input.num_lines - 1;
320✔
190
    for (long line_idx = (long) input.num_lines - 1; line_idx >= 0; line_idx--) {
324!
191
        if (!bxs_is_blank(input.lines[line_idx].text)) {
324✔
192
            result = (size_t) line_idx;
320✔
193
            break;
320✔
194
        }
195
    }
196
    return result;
320✔
197
}
198

199

200

201
static int is_shape_line_empty(shape_line_ctx_t *shapes_relevant, size_t shape_idx)
2,650✔
202
{
203
    if (shape_idx < SHAPES_PER_SIDE) {
2,650!
204
        return shapes_relevant[shape_idx].empty || bxs_is_blank(shapes_relevant[shape_idx].text);
2,650✔
205
    }
206
    return 1;
×
207
}
208

209

210

211
static int non_empty_shapes_after(shape_line_ctx_t *shapes_relevant, size_t shape_idx)
1,716✔
212
{
213
    /* CHECK Can we use shape->is_blank_rightward? */
214
    for (size_t i = shape_idx + 1; i < SHAPES_PER_SIDE - 1; i++) {
2,584✔
215
        if (!is_shape_line_empty(shapes_relevant, i)) {
922✔
216
            return 1;
54✔
217
        }
218
    }
219
    return 0;
1,662✔
220
}
221

222

223

224
static int is_blank_between(uint32_t *start, uint32_t *end)
768✔
225
{
226
    for (uint32_t *p = start; p < end; p++) {
12,480✔
227
        if (!is_blank(*p)) {
11,716✔
228
            return 0;
4✔
229
        }
230
    }
231
    return 1;
764✔
232
}
233

234

235

236
/**
237
 * Take a shape line and shorten it by cutting off blanks from both ends.
238
 * @param shape_line_ctx info record on the shape line to work on. Contains the original shape line, unshortened.
239
 * @param quality (IN/OUT) the current quality, here the value that was last tested. We will reduce this by one.
240
 * @param prefer_left if 1, first cut all blanks from the start of the shape line, if 0, first cut at the end
241
 * @param allow_left if 1, blanks may be cut from the left of the shape line, if 0, we never cut from the left
242
 * @param allow_right if 1, blanks may be cut from the right of the shape line, if 0, we never cut from the right
243
 * @return the shortened shape line, in new memory, or NULL if further shortening was not possible
244
 */
245
uint32_t *shorten(shape_line_ctx_t *shape_line_ctx, size_t *quality, int prefer_left, int allow_left, int allow_right)
17,200✔
246
{
247
    if (shape_line_ctx == NULL || shape_line_ctx->text == NULL || quality == NULL
17,200✔
248
            || *quality > shape_line_ctx->text->num_chars) {
17,194✔
249
        return NULL;
8✔
250
    }
251

252
    uint32_t *s = shape_line_ctx->text->memory;
17,192✔
253
    uint32_t *e = shape_line_ctx->text->memory + shape_line_ctx->text->num_chars;
17,192✔
254
    prefer_left = allow_left ? prefer_left : 0;
17,192✔
255
    size_t reduction_steps = shape_line_ctx->text->num_chars - *quality + 1;
17,192✔
256
    for (size_t i = 0; i < reduction_steps; i++) {
66,606✔
257
        if (prefer_left) {
53,854✔
258
            if (s < e && is_blank(*s)) {
17,284!
259
                s++;
8,740✔
260
            }
261
            else if (e > s && allow_right && is_blank(*(e - 1))) {
8,544!
262
                e--;
6,562✔
263
            }
264
            else {
265
                break;
266
            }
267
        }
268
        else {
269
            if (e > s && allow_right && is_blank(*(e - 1))) {
36,570✔
270
                e--;
26,642✔
271
            }
272
            else if (s < e && allow_left && is_blank(*s)) {
9,928✔
273
                s++;
7,470✔
274
            }
275
            else {
276
                break;
277
            }
278
        }
279
    }
280

281
    uint32_t *result = NULL;
17,192✔
282
    size_t new_quality = e - s;
17,192✔
283
    if (new_quality < *quality) {
17,192✔
284
        result = u32_strdup(s);
12,752✔
285
        set_char_at(result, new_quality, char_nul);
12,752✔
286
        *quality = new_quality;
12,752✔
287
    }
288
    return result;
17,192✔
289
}
290

291

292

293
static int hmm_shiftable(shape_line_ctx_t *shapes_relevant, uint32_t *cur_pos, size_t shape_idx, uint32_t *end_pos,
294
        int anchored_right);
295

296

297

298
/**
299
 * (horizontal middle match)
300
 * Recursive helper function for match_horiz_line(), uses backtracking.
301
 * @param shapes_relevant the prepared shape lines to be concatenated
302
 * @param cur_pos current position in the input line being matched
303
 * @param shape_idx index into `shapes_relevant` indicating which shape to try now
304
 * @param end_pos first character of the east corner
305
 * @param anchored_left flag indicating that `cur_pos` is already "anchored" or still "shiftable". "Anchored" means
306
 *      that we have matched a non-blank shape line already (corner shape line was not blank). Else "shiftable".
307
 * @param anchored_right flag indicating that the east corner shape was not blank. If this is `false`, it means that
308
 *      a shape may be shortened right if only blank shape lines follow.
309
 * @return `== 1`: success;
310
 *         `== 0`: failed to match
311
 */
312
int hmm(shape_line_ctx_t *shapes_relevant, uint32_t *cur_pos, size_t shape_idx, uint32_t *end_pos, int anchored_left,
37,224✔
313
        int anchored_right)
314
{
315
    if (is_debug_logging(MAIN)) {
37,224!
316
        char *out_cur_pos = u32_strconv_to_output(cur_pos);
×
317
        char *out_end_pos = u32_strconv_to_output(end_pos);
×
318
        log_debug(__FILE__, MAIN, "hmm(shapes_relevant, \"%s\", %d, \"%s\", %s, %s) - enter\n", out_cur_pos,
×
319
                (int) shape_idx, out_end_pos, anchored_left ? "true" : "false", anchored_right ? "true" : "false");
320
        BFREE(out_cur_pos);
×
321
        BFREE(out_end_pos);
×
322
    }
323

324
    int result = 0;
37,224✔
325
    if (!anchored_left) {
37,224✔
326
        result = hmm_shiftable(shapes_relevant, cur_pos, shape_idx, end_pos, anchored_right);
794✔
327
    }
328
    else if (cur_pos > end_pos) {
36,430✔
329
        /* invalid input */
330
        result = 0;
242✔
331
    }
332
    else if (cur_pos == end_pos) {
36,188✔
333
        /* we are at the end, which is fine if there is nothing else to match */
334
        if (shape_idx == (SHAPES_PER_SIDE - 1) && anchored_right) {
426!
335
            result = 1;
32✔
336
        }
337
        else if (shapes_relevant[shape_idx].empty || bxs_is_blank(shapes_relevant[shape_idx].text)) {
394✔
338
            result = !non_empty_shapes_after(shapes_relevant, shape_idx);
308✔
339
        }
340
        else {
341
            result = 0;
86✔
342
        }
343
    }
344
    else if (shape_idx >= SHAPES_PER_SIDE - 1) {
35,762✔
345
        /* no more shapes to try, which is fine if the rest of the line is blank */
346
        result = u32_is_blank(cur_pos);
110✔
347
    }
348
    else if (shapes_relevant[shape_idx].empty) {
35,652✔
349
        /* the current shape line is empty, try the next one */
350
        result = hmm(shapes_relevant, cur_pos, shape_idx + 1, end_pos, 1, anchored_right);
470✔
351
    }
352
    else {
353
        uint32_t *shape_line = u32_strdup(shapes_relevant[shape_idx].text->memory);
35,182✔
354
        size_t quality = shapes_relevant[shape_idx].text->num_chars;
35,182✔
355
        while (shape_line != NULL && quality > 0) {
71,614✔
356
            if (u32_strncmp(cur_pos, shape_line, quality) == 0) {
36,432✔
357
                BFREE(shape_line);
33,664!
358
                cur_pos = cur_pos + quality;
33,664✔
359
                if (cur_pos == end_pos && !non_empty_shapes_after(shapes_relevant, shape_idx)) {
33,664✔
360
                    result = 1; /* success */
1,094✔
361
                }
362
                else {
363
                    int rc = 0;
32,570✔
364
                    if (shapes_relevant[shape_idx].elastic) {
32,570✔
365
                        rc = hmm(shapes_relevant, cur_pos, shape_idx, end_pos, 1, anchored_right);
32,098✔
366
                    }
367
                    if (rc == 0) {
32,570✔
368
                        result = hmm(shapes_relevant, cur_pos, shape_idx + 1, end_pos, 1, anchored_right);
2,304✔
369
                    }
370
                    else {
371
                        result = rc;
30,266✔
372
                    }
373
                }
374
            }
375
            else if (!anchored_right) {
2,768✔
376
                shape_line = shorten(shapes_relevant + shape_idx, &quality, 0, 0, 1);
1,580✔
377
                if (is_debug_logging(MAIN)) {
1,580!
378
                    char *out_shape_line = u32_strconv_to_output(shape_line);
×
379
                    log_debug(__FILE__, MAIN, "hmm() - shape_line shortened to %d (\"%s\")\n",
×
380
                            (int) quality, out_shape_line);
381
                    BFREE(out_shape_line);
×
382
                }
383
            }
384
            else {
385
                BFREE(shape_line);
1,188!
386
            }
387
        }
388
    }
389

390
    log_debug(__FILE__, MAIN, "hmm() - exit, result = %d\n", result);
37,224✔
391
    return result;
37,224✔
392
}
393

394

395

396
static int hmm_shiftable(shape_line_ctx_t *shapes_relevant, uint32_t *cur_pos, size_t shape_idx, uint32_t *end_pos,
794✔
397
        int anchored_right)
398
{
399
    int result = 0;
794✔
400
    int shapes_are_empty = 1;
794✔
401
    for (size_t i = shape_idx; i < SHAPES_PER_SIDE - 1; i++) {
1,504✔
402
        if (!is_shape_line_empty(shapes_relevant, i)) {
1,468✔
403
            shapes_are_empty = 0;
758✔
404
            int can_shorten_right = -1;
758✔
405
            size_t quality = shapes_relevant[i].text->num_chars;
758✔
406
            uint32_t *shape_line = shapes_relevant[i].text->memory;
758✔
407
            while (shape_line != NULL) {
1,398✔
408
                uint32_t *p = u32_strstr(cur_pos, shape_line);
1,370✔
409
                if (p != NULL && p < end_pos && is_blank_between(cur_pos, p)) {
1,370!
410
                    result = hmm(shapes_relevant, p + quality, i + (shapes_relevant[i].elastic ? 0 : 1),
730✔
411
                            end_pos, 1, anchored_right);
412
                    if (result == 0 && shapes_relevant[i].elastic) {
730!
413
                        result = hmm(shapes_relevant, p + quality, i + 1, end_pos, 1, anchored_right);
8✔
414
                    }
415
                    break;
730✔
416
                }
417
                if (can_shorten_right == -1) {
640✔
418
                    /* we can only shorten right if the east corner shape line is also empty */
419
                    can_shorten_right = non_empty_shapes_after(shapes_relevant, i)
528✔
420
                            || !is_shape_line_empty(shapes_relevant, SHAPES_PER_SIDE - 1) ? 0 : 1;
264✔
421
                }
422
                shape_line = shorten(shapes_relevant + i, &quality, 0, 1, can_shorten_right);
640✔
423
            }
424
            break;
758✔
425
        }
426
    }
427
    if (shapes_are_empty) {
794✔
428
        /* all shapes were empty, which is fine if line was blank */
429
        result = is_blank_between(cur_pos, end_pos);
36✔
430
    }
431
    return result;
794✔
432
}
433

434

435

436
static shape_line_ctx_t *prepare_comp_shapes_horiz(int hside, comparison_t comp_type, size_t shape_line_idx)
1,590✔
437
{
438
    shape_t *side_shapes = hside == BTOP ? north_side : south_side_rev;
1,590✔
439
    shape_line_ctx_t *shapes_relevant = (shape_line_ctx_t *) calloc(SHAPES_PER_SIDE, sizeof(shape_line_ctx_t));
1,590✔
440

441
    for (size_t i = 0; i < SHAPES_PER_SIDE; i++) {
9,540✔
442
        shapes_relevant[i].elastic = opt.design->shape[side_shapes[i]].elastic;
7,950✔
443
        shapes_relevant[i].empty = isempty(opt.design->shape + side_shapes[i]);
7,950✔
444
        if (!shapes_relevant[i].empty) {
7,950✔
445
            uint32_t *s = prepare_comp_shape(opt.design, side_shapes[i], shape_line_idx, comp_type, 0,
6,942✔
446
                    i == SHAPES_PER_SIDE - 1);
447
            shapes_relevant[i].text = bxs_from_unicode(s);
6,942✔
448
            BFREE(s);
6,942!
449
        }
450
    }
451

452
    return shapes_relevant;
1,590✔
453
}
454

455

456

457
static match_result_t *new_match_result(uint32_t *p, size_t p_idx, size_t len, int shiftable)
3,028✔
458
{
459
    match_result_t *result = (match_result_t *) calloc(1, sizeof(match_result_t));
3,028✔
460
    result->p = p;
3,028✔
461
    result->p_idx = p_idx;
3,028✔
462
    result->len = len;
3,028✔
463
    result->shiftable = shiftable;
3,028✔
464
    return result;
3,028✔
465
}
466

467

468

469
/**
470
 * Match a `shape_line` at the beginning (`vside` == `BLEF`) or the end (`vside` == `BRIG`) of an `input_line`.
471
 * Both `input_line` and `shape_line` may contain invisible characters, who are then matched, too, just like any other
472
 * characters.
473
 * @param vside BLEF or BRIG
474
 * @param input_line the input line to examine. We expect that it was NOT trimmed.
475
 * @param shape_line the shape line to match, also NOT trimmed
476
 * @return pointer to the match result (in existing memory of `input_line->memory`), or `NULL` if no match
477
 */
478
match_result_t *match_outer_shape(int vside, bxstr_t *input_line, bxstr_t *shape_line)
3,054✔
479
{
480
    if (input_line == NULL || input_line->num_chars == 0 || shape_line == NULL || shape_line->num_chars == 0) {
3,054!
481
        return NULL;
8✔
482
    }
483

484
    if (vside == BLEF) {
3,046✔
485
        if (bxs_is_blank(shape_line)) {
1,538✔
486
            return new_match_result(input_line->memory, 0, 0, 1);
718✔
487
        }
488
        for (uint32_t *s = shape_line->memory; s == shape_line->memory || is_blank(*s); s++) {
834✔
489
            uint32_t *p = u32_strstr(input_line->memory, s);
826✔
490
            size_t p_idx = p != NULL ? p - input_line->memory : 0;
826✔
491
            if (p == NULL || p_idx > input_line->first_char[input_line->indent]) {
826✔
492
                continue;  /* not found or found too far in */
14✔
493
            }
494
            return new_match_result(p, p_idx, shape_line->num_chars - (s - shape_line->memory), 0);
812✔
495
        }
496
    }
497
    else {
498
        if (bxs_is_blank(shape_line)) {
1,508✔
499
            uint32_t *p = bxs_last_char_ptr(input_line);
658✔
500
            size_t p_idx = p - input_line->memory;
658✔
501
            return new_match_result(p, p_idx, 0, 1);
658✔
502
        }
503
        int slen = shape_line->num_chars;
850✔
504
        uint32_t *s = u32_strdup(shape_line->memory);
850✔
505
        for (; slen == (int) shape_line->num_chars || is_blank(s[slen]); slen--) {
870✔
506
            s[slen] = char_nul;
860✔
507
            uint32_t *p = u32_strnrstr(input_line->memory, s, slen);
860✔
508
            size_t p_idx = p != NULL ? p - input_line->memory : 0;
860✔
509
            if (p == NULL || p_idx + slen
860✔
510
                    < input_line->first_char[input_line->num_chars_visible - input_line->trailing]) {
844✔
511
                continue; /* not found or found too far in */
20✔
512
            }
513
            BFREE(s);
840!
514
            return new_match_result(p, p_idx, (size_t) slen, 0);
840✔
515
        }
516
        BFREE(s);
10!
517
    }
518
    return NULL;
18✔
519
}
520

521

522

523
static int match_horiz_line(remove_ctx_t *ctx, int hside, size_t input_line_idx, size_t shape_line_idx)
1,560✔
524
{
525
    log_debug(__FILE__, MAIN, "match_horiz_line(ctx, %s, %d, %d)\n",
1,560✔
526
                hside == BTOP ? "BTOP" : "BBOT", (int) input_line_idx, (int) shape_line_idx);
527

528
    int result = 0;
1,560✔
529
    for (comparison_t comp_type = 0; comp_type < NUM_COMPARISON_TYPES; comp_type++) {
1,758!
530
        if (!comp_type_is_viable(comp_type, ctx->input_is_mono, ctx->design_is_mono)) {
1,758✔
531
            continue;
168✔
532
        }
533
        ctx->comp_type = comp_type;
1,590✔
534
        log_debug(__FILE__, MAIN, "  Setting comparison type to: %s\n", comparison_name[comp_type]);
1,590✔
535

536
        shape_line_ctx_t *shapes_relevant = prepare_comp_shapes_horiz(hside, comp_type, shape_line_idx);
1,590✔
537
        debug_print_shapes_relevant(shapes_relevant);
1,590✔
538

539
        bxstr_t *input_prepped1 = bxs_from_unicode(prepare_comp_input(input_line_idx, 0, comp_type, 0, NULL, NULL));
1,590✔
540
        bxstr_t *input_prepped = bxs_rtrim(input_prepped1);
1,590✔
541
        bxs_append_spaces(input_prepped, opt.design->shape[NW].width + opt.design->shape[NE].width);
1,590✔
542
        bxs_free(input_prepped1);
1,590✔
543
        if (is_debug_logging(MAIN)) {
1,590!
544
            char *out_input_prepped = bxs_to_output(input_prepped);
×
545
            log_debug(__FILE__, MAIN, "  input_prepped = \"%s\"\n", out_input_prepped);
×
546
            BFREE(out_input_prepped);
×
547
        }
548

549
        uint32_t *cur_pos = input_prepped->memory;
1,590✔
550
        match_result_t *mrl = NULL;
1,590✔
551
        if (!ctx->empty_side[BLEF]) {
1,590✔
552
            mrl = match_outer_shape(BLEF, input_prepped, shapes_relevant[0].text);
1,526✔
553
            if (mrl != NULL) {
1,526✔
554
                cur_pos = mrl->p + mrl->len;
1,522✔
555
            }
556
        }
557

558
        uint32_t *end_pos = bxs_last_char_ptr(input_prepped);
1,590✔
559
        match_result_t *mrr = NULL;
1,590✔
560
        if (!ctx->empty_side[BRIG]) {
1,590✔
561
            mrr = match_outer_shape(BRIG, input_prepped, shapes_relevant[SHAPES_PER_SIDE - 1].text);
1,498✔
562
            if (mrr != NULL) {
1,498✔
563
                end_pos = mrr->p;
1,488✔
564
            }
565
        }
566
        if (is_debug_logging(MAIN)) {
1,590!
567
            char *out_cur_pos = u32_strconv_to_output(cur_pos);
×
568
            char *out_end_pos = u32_strconv_to_output(end_pos);
×
569
            log_debug(__FILE__, MAIN, "  cur_pos = \"%s\" (index %d)\n",
×
570
                    out_cur_pos, (int) BMAX(cur_pos - input_prepped->memory, 0));
×
571
            log_debug(__FILE__, MAIN, "  end_pos = \"%s\" (index %d)\n",
×
572
                    out_end_pos, (int) BMAX(end_pos - input_prepped->memory, 0));
×
573
            BFREE(out_cur_pos);
×
574
            BFREE(out_end_pos);
×
575
        }
576

577
        result = hmm(shapes_relevant, cur_pos, 1, end_pos, (mrl == NULL) || mrl->shiftable ? 0 : 1,
3,078✔
578
                (mrr == NULL) || mrr->shiftable ? 0 : 1);
1,488✔
579

580
        BFREE(mrl);
1,590✔
581
        BFREE(mrr);
1,590✔
582
        for (size_t i = 0; i < SHAPES_PER_SIDE; i++) {
9,540✔
583
            bxs_free(shapes_relevant[i].text);
7,950✔
584
        }
585
        BFREE(shapes_relevant);
1,590!
586

587
        if (result) {
1,590✔
588
            log_debug(__FILE__, MAIN, "Matched %s side line using comp_type=%s and shape_line_idx=%d\n",
1,560✔
589
                    hside == BTOP ? "top" : "bottom", comparison_name[comp_type], (int) shape_line_idx);
590
            break;
1,560✔
591
        }
592
    }
593

594
    return result;
1,560✔
595
}
596

597

598

599
static size_t find_top_side(remove_ctx_t *ctx)
272✔
600
{
601
    size_t result = ctx->top_start_idx;
272✔
602
    sentry_t *shapes = opt.design->shape;
272✔
603
    for (size_t input_line_idx = ctx->top_start_idx;
272✔
604
            input_line_idx < input.num_lines && input_line_idx < ctx->top_start_idx + shapes[NE].height;
1,052!
605
            input_line_idx++)
780✔
606
    {
607
        int matched = 0;
780✔
608
        size_t shape_lines_tested = 0;
780✔
609
        for (size_t shape_line_idx = (input_line_idx - ctx->top_start_idx) % shapes[NE].height;
780✔
610
                shape_lines_tested < shapes[NE].height;
780!
611
                shape_line_idx = (shape_line_idx + 1) % shapes[NE].height, shape_lines_tested++)
×
612
        {
613
            if (match_horiz_line(ctx, BTOP, input_line_idx, shape_line_idx)) {
780!
614
                matched = 1;
780✔
615
                break;
780✔
616
            }
617
        }
618
        if (!matched) {
780!
619
            break;
×
620
        }
621
        result = input_line_idx + 1;
780✔
622
    }
623
    return result;
272✔
624
}
625

626

627

628
static size_t find_bottom_side(remove_ctx_t *ctx)
276✔
629
{
630
    size_t result = ctx->bottom_end_idx;
276✔
631
    sentry_t *shapes = opt.design->shape;
276✔
632
    for (long input_line_idx = (long) ctx->bottom_end_idx - 1;
276✔
633
            input_line_idx >= 0 && input_line_idx >= (long) ctx->bottom_end_idx - (long) shapes[SE].height;
1,056!
634
            input_line_idx--)
780✔
635
    {
636
        int matched = 0;
780✔
637
        size_t shape_lines_tested = 0;
780✔
638
        for (long shape_line_idx = shapes[SE].height - (ctx->bottom_end_idx - input_line_idx);
780✔
639
                shape_line_idx >= 0 && shape_lines_tested < shapes[SE].height;
780!
640
                shape_lines_tested++,
×
641
                shape_line_idx = shape_line_idx == 0 ? (long) (shapes[SE].height - 1) : (long) (shape_line_idx - 1))
×
642
        {
643
            if (match_horiz_line(ctx, BBOT, input_line_idx, shape_line_idx)) {
780!
644
                matched = 1;
780✔
645
                break;
780✔
646
            }
647
        }
648
        if (!matched) {
780!
649
            break;
×
650
        }
651
        result = input_line_idx;
780✔
652
    }
653
    return result;
276✔
654
}
655

656

657

658
static size_t count_shape_lines(shape_t side_shapes[])
584✔
659
{
660
    size_t result = 0;
584✔
661
    for (size_t i = 0; i < SHAPES_PER_SIDE - CORNERS_PER_SIDE; i++) {
2,336✔
662
        if (!isempty(opt.design->shape + side_shapes[i])) {
1,752✔
663
            result += opt.design->shape[side_shapes[i]].height;
736✔
664
        }
665
    }
666
    return result;
584✔
667
}
668

669

670
static shape_line_ctx_t **prepare_comp_shapes_vert(int vside, comparison_t comp_type)
584✔
671
{
672
    shape_t west_side_shapes[SHAPES_PER_SIDE - CORNERS_PER_SIDE] = {WNW, W, WSW};
584✔
673
    shape_t east_side_shapes[SHAPES_PER_SIDE - CORNERS_PER_SIDE] = {ENE, E, ESE};
584✔
674
    shape_t side_shapes[SHAPES_PER_SIDE - CORNERS_PER_SIDE];
675
    if (vside == BLEF) {
584✔
676
        memcpy(side_shapes, west_side_shapes, (SHAPES_PER_SIDE - CORNERS_PER_SIDE) * sizeof(shape_t));
314✔
677
    }
678
    else {
679
        memcpy(side_shapes, east_side_shapes, (SHAPES_PER_SIDE - CORNERS_PER_SIDE) * sizeof(shape_t));
270✔
680
    }
681

682
    size_t num_shape_lines = count_shape_lines(side_shapes);
584✔
683

684
    shape_line_ctx_t **shape_lines = (shape_line_ctx_t **) calloc(num_shape_lines + 1, sizeof(shape_line_ctx_t *));
584✔
685
    for (size_t i = 0; i < num_shape_lines; i++) {
1,708✔
686
        shape_lines[i] = (shape_line_ctx_t *) calloc(1, sizeof(shape_line_ctx_t));
1,124✔
687
    }
688

689
    for (size_t shape_idx = 0, i = 0; shape_idx < SHAPES_PER_SIDE - CORNERS_PER_SIDE; shape_idx++) {
2,336✔
690
        if (!isempty(opt.design->shape + side_shapes[shape_idx])) {
1,752✔
691
            int deep_empty = isdeepempty(opt.design->shape + side_shapes[shape_idx]);
736✔
692
            for (size_t slno = 0; slno < opt.design->shape[side_shapes[shape_idx]].height; slno++, i++) {
1,860✔
693
                uint32_t *s = prepare_comp_shape(opt.design, side_shapes[shape_idx], slno, comp_type, 0, 0);
1,124✔
694
                shape_lines[i]->text = bxs_from_unicode(s);
1,124✔
695
                shape_lines[i]->empty = deep_empty;
1,124✔
696
                shape_lines[i]->elastic = opt.design->shape[side_shapes[shape_idx]].elastic;
1,124✔
697
                BFREE(s);
1,124!
698
            }
699
        }
700
    }
701

702
    return shape_lines;
584✔
703
}
704

705

706

707
static void free_shape_lines(shape_line_ctx_t **shape_lines)
636✔
708
{
709
    if (shape_lines != NULL) {
636✔
710
        for (shape_line_ctx_t **p = shape_lines; *p != NULL; p++) {
1,708✔
711
            bxs_free((*p)->text);
1,124✔
712
            BFREE(*p);
1,124!
713
        }
714
        BFREE(shape_lines);
584!
715
    }
716
}
636✔
717

718

719

720
static void match_vertical_side(remove_ctx_t *ctx, int vside, shape_line_ctx_t **shape_lines, uint32_t *input_line,
3,232✔
721
    size_t line_idx, size_t input_length, size_t input_indent, size_t input_trailing)
722
{
723
    line_ctx_t *line_ctx = ctx->body + (line_idx - ctx->top_end_idx);
3,232✔
724

725
    for (shape_line_ctx_t **shape_line_ctx = shape_lines; *shape_line_ctx != NULL; shape_line_ctx++) {
10,976✔
726
        if ((*shape_line_ctx)->empty) {
7,744!
727
            continue;
×
728
        }
729

730
        size_t max_quality = (*shape_line_ctx)->text->num_chars;
7,744✔
731
        size_t quality = max_quality;
7,744✔
732
        uint32_t *shape_text = (*shape_line_ctx)->text->memory;
7,744✔
733
        uint32_t *to_free = NULL;
7,744✔
734
        while(shape_text != NULL) {
23,030✔
735
            uint32_t *p;
736
            if (vside == BLEF) {
18,578✔
737
                p = u32_strstr(input_line, shape_text);
8,338✔
738
            }
739
            else {
740
                p = u32_strnrstr(input_line, shape_text, quality);
10,240✔
741
            }
742
            BFREE(to_free);
18,578✔
743
            shape_text = NULL;
18,578✔
744

745
            if ((p == NULL)
18,578✔
746
                    || (vside == BLEF && ((size_t) (p - input_line) > input_indent + (max_quality - quality)))
5,656✔
747
                    || (vside == BRIG && ((size_t) (p - input_line) < input_length - input_trailing - quality))) {
5,336✔
748
                shape_text = shorten(*shape_line_ctx, &quality, vside == BLEF, 1, 1);
14,938✔
749
                to_free = shape_text;
14,938✔
750
            }
751
            else if (vside == BLEF) {
3,640✔
752
                if (quality > line_ctx->west_quality) {
2,000✔
753
                    line_ctx->west_start = (size_t) (p - input_line);
1,784✔
754
                    line_ctx->west_end = line_ctx->west_start + quality;
1,784✔
755
                    line_ctx->west_quality = quality;
1,784✔
756
                    BFREE(line_ctx->input_line_used);
1,784✔
757
                    line_ctx->input_line_used = u32_strdup(input_line);
1,784✔
758
                    break;
1,784✔
759
                }
760
            }
761
            else if (vside == BRIG) {
1,640!
762
                if (quality > line_ctx->east_quality) {
1,640✔
763
                    line_ctx->east_start = (size_t) (p - input_line);
1,508✔
764
                    line_ctx->east_end = line_ctx->east_start + quality;
1,508✔
765
                    line_ctx->east_quality = quality;
1,508✔
766
                    BFREE(line_ctx->input_line_used);
1,508✔
767
                    line_ctx->input_line_used = u32_strdup(input_line);
1,508✔
768
                    break;
1,508✔
769
                }
770
            }
771
        }
772
    }
773
}
3,232✔
774

775

776

777
static int sufficient_body_quality(remove_ctx_t *ctx)
318✔
778
{
779
    size_t num_body_lines = ctx->bottom_start_idx - ctx->top_end_idx;
318✔
780
    size_t total_quality = 0;
318✔
781
    line_ctx_t *body = ctx->body;
318✔
782
    for (size_t body_line_idx = 0; body_line_idx < num_body_lines; body_line_idx++) {
2,072✔
783
        line_ctx_t line_ctx = body[body_line_idx];
1,754✔
784
        total_quality += line_ctx.east_quality + line_ctx.west_quality;
1,754✔
785
    }
786

787
    size_t max_quality = 0;
318✔
788
    if (!ctx->empty_side[BLEF]) {
318✔
789
        max_quality += opt.design->shape[NW].width;
314✔
790
    }
791
    if (!ctx->empty_side[BRIG]) {
318✔
792
        max_quality += opt.design->shape[NE].width;
270✔
793
    }
794
    max_quality = max_quality * num_body_lines;
318✔
795

796
    /* If we manage to match 50%, then it is unlikely to improve with a different comparison mode. */
797
    int sufficient = (max_quality == 0 && total_quality == 0)
×
798
            || (max_quality > 0 && (total_quality > 0.5 * max_quality));
318!
799
    log_debug(__FILE__, MAIN, "sufficient_body_quality() found body match quality of %d/%d (%s).\n",
318✔
800
            (int) total_quality, (int) max_quality, sufficient ? "sufficient" : "NOT sufficient");
801

802
    return sufficient;
318✔
803
}
804

805

806

807
static void reset_body(remove_ctx_t *ctx)
318✔
808
{
809
    if (ctx->body != NULL) {
318!
810
        for (size_t i = 0; i < ctx->body_num_lines; i++) {
2,072✔
811
            BFREE(ctx->body[i].input_line_used);
1,754!
812
        }
813
        memset(ctx->body, 0, ctx->body_num_lines * sizeof(line_ctx_t));
318✔
814
    }
815
}
318✔
816

817

818

819
static void find_vertical_shapes(remove_ctx_t *ctx)
320✔
820
{
821
    int west_empty = ctx->empty_side[BLEF];
320✔
822
    int east_empty = ctx->empty_side[BRIG];
320✔
823
    if (west_empty && east_empty) {
320✔
824
        return;
4✔
825
    }
826

827
    for (comparison_t comp_type = 0; comp_type < NUM_COMPARISON_TYPES; comp_type++) {
382✔
828
        if (!comp_type_is_viable(comp_type, ctx->input_is_mono, ctx->design_is_mono)) {
370✔
829
            continue;
52✔
830
        }
831
        ctx->comp_type = comp_type;
318✔
832
        log_debug(__FILE__, MAIN, "find_vertical_shapes(): comp_type = %s\n", comparison_name[comp_type]);
318✔
833
        reset_body(ctx);
318✔
834

835
        shape_line_ctx_t **shape_lines_west = NULL;
318✔
836
        if (!west_empty) {
318✔
837
            shape_lines_west = prepare_comp_shapes_vert(BLEF, comp_type);
314✔
838
        }
839
        shape_line_ctx_t **shape_lines_east = NULL;
318✔
840
        if (!east_empty) {
318✔
841
            shape_lines_east = prepare_comp_shapes_vert(BRIG, comp_type);
270✔
842
        }
843

844
        for (size_t input_line_idx = ctx->top_end_idx; input_line_idx < ctx->bottom_start_idx; input_line_idx++) {
2,072✔
845
            size_t input_indent = 0;
1,754✔
846
            size_t input_trailing = 0;
1,754✔
847
            uint32_t *input_line = prepare_comp_input(input_line_idx, 0, comp_type, 0, &input_indent, &input_trailing);
1,754✔
848
            size_t input_length = u32_strlen(input_line);
1,754✔
849

850
            if (!west_empty) {
1,754✔
851
                match_vertical_side(ctx, BLEF, shape_lines_west,
1,734✔
852
                        input_line, input_line_idx, input_length, input_indent, input_trailing);
853
            }
854
            if (!east_empty) {
1,754✔
855
                match_vertical_side(ctx, BRIG, shape_lines_east,
1,498✔
856
                        input_line, input_line_idx, input_length, input_indent, input_trailing);
857
            }
858
        }
859

860
        free_shape_lines(shape_lines_west);
318✔
861
        free_shape_lines(shape_lines_east);
318✔
862

863
        if (sufficient_body_quality(ctx)) {
318✔
864
            break;
304✔
865
        }
866
    }
867
}
868

869

870

871
/**
872
 * If the user didn't specify a design to remove, autodetect it.
873
 * Since this requires knowledge of all available designs, the entire config file had to be parsed (earlier).
874
 */
875
static void detect_design_if_needed()
322✔
876
{
877
    if (opt.design_choice_by_user == 0) {
322✔
878
        design_t *tmp = autodetect_design();
16✔
879
        if (tmp) {
16✔
880
            opt.design = tmp;
14✔
881
            log_debug(__FILE__, MAIN, "Design autodetection: Removing box of design \"%s\".\n", opt.design->name);
14✔
882
        }
883
        else {
884
            fprintf(stderr, "%s: Box design autodetection failed. Use -d option.\n", PROJECT);
2✔
885
            exit(EXIT_FAILURE);
2✔
886
        }
887
    }
888
    else {
889
        log_debug(__FILE__, MAIN, "Design was chosen by user: %s\n", opt.design->name);
306✔
890
    }
891
}
320✔
892

893

894

895
static void free_line_text(line_t *line)
3,558✔
896
{
897
    BFREE(line->cache_visible);
3,558✔
898
    bxs_free(line->text);
3,558✔
899
    line->text = NULL;
3,558✔
900
}
3,558✔
901

902

903

904
static void free_line(line_t *line)
1,762✔
905
{
906
    free_line_text(line);
1,762✔
907
    BFREE(line->tabpos);
1,762!
908
    line->tabpos_len = 0;
1,762✔
909
}
1,762✔
910

911

912

913
static void killblank(remove_ctx_t *ctx)
320✔
914
{
915
    size_t lines_removed = 0;
320✔
916
    size_t max_lines_removable = opt.mend && !opt.killblank ? (size_t) BMAX(opt.design->padding[BTOP], 0) : SIZE_MAX;
320!
917
    while (ctx->top_end_idx < ctx->bottom_start_idx && lines_removed < max_lines_removable
742✔
918
            && empty_line(input.lines + ctx->top_end_idx))
676!
919
    {
920
        log_debug(__FILE__, MAIN, "Killing leading blank line in box body.\n");
102✔
921
        ++(ctx->top_end_idx);
102✔
922
        --(ctx->body_num_lines);
102✔
923
        ++lines_removed;
102✔
924
    }
925

926
    lines_removed = 0;
320✔
927
    max_lines_removable = opt.mend && !opt.killblank ? (size_t) BMAX(opt.design->padding[BBOT], 0) : SIZE_MAX;
320!
928
    while (ctx->bottom_start_idx > ctx->top_end_idx && lines_removed < max_lines_removable
740✔
929
            && empty_line(input.lines + ctx->bottom_start_idx - 1))
672!
930
    {
931
        log_debug(__FILE__, MAIN, "Killing trailing blank line in box body.\n");
100✔
932
        --(ctx->bottom_start_idx);
100✔
933
        --(ctx->body_num_lines);
100✔
934
        ++lines_removed;
100✔
935
    }
936
}
320✔
937

938

939

940
static int org_is_not_blank(bxstr_t *org_line, comparison_t comp_type, size_t idx)
1,920✔
941
{
942
    if (comp_type == literal || comp_type == ignore_invisible_shape) {
1,920!
943
        return !is_blank(org_line->memory[idx]);
1,904✔
944
    }
945
    return !is_blank(org_line->memory[org_line->visible_char[idx]]);
16✔
946
}
947

948

949

950
static size_t max_chars_line(bxstr_t *org_line, comparison_t comp_type)
2,030✔
951
{
952
    return (comp_type == literal || comp_type == ignore_invisible_shape)
34✔
953
            ? org_line->num_chars : org_line->num_chars_visible;
2,064✔
954
}
955

956

957

958
static size_t confirmed_padding(bxstr_t *org_line, comparison_t comp_type, size_t start_idx, size_t num_padding)
1,732✔
959
{
960
    size_t result = 0;
1,732✔
961
    size_t max_chars = max_chars_line(org_line, comp_type);
1,732✔
962
    for (size_t i = start_idx; i < BMIN(max_chars, start_idx + num_padding); i++) {
3,652✔
963
        if (org_is_not_blank(org_line, comp_type, i)) {
1,920!
964
            break;
×
965
        }
966
        result++;
1,920✔
967
    }
968
    return result;
1,732✔
969
}
970

971

972

973
static void remove_top_from_input(remove_ctx_t *ctx)
320✔
974
{
975
    if (ctx->top_end_idx > ctx->top_start_idx) {
320✔
976
        for (size_t j = ctx->top_start_idx; j < ctx->top_end_idx; ++j) {
1,156✔
977
            free_line(input.lines + j);
882✔
978
        }
979
        memmove(input.lines + ctx->top_start_idx, input.lines + ctx->top_end_idx,
274✔
980
                (input.num_lines - ctx->top_end_idx) * sizeof(line_t));
137✔
981
        input.num_lines -= ctx->top_end_idx - ctx->top_start_idx;
274✔
982
    }
983
}
320✔
984

985

986

987
static size_t calculate_start_idx(remove_ctx_t *ctx, size_t body_line_idx)
1,776✔
988
{
989
    size_t input_line_idx = ctx->top_end_idx + body_line_idx;
1,776✔
990
    line_ctx_t *lctx = ctx->body + body_line_idx;
1,776✔
991
    bxstr_t *org_line = input.lines[input_line_idx].text;
1,776✔
992

993
    size_t s_idx = 0;
1,776✔
994
    if (lctx->west_quality > 0) {
1,776✔
995
        s_idx = lctx->west_end + confirmed_padding(org_line, ctx->comp_type, lctx->west_end, opt.design->padding[BLEF]);
1,732✔
996
    }
997
    if (ctx->comp_type == ignore_invisible_input || ctx->comp_type == ignore_invisible_all) {
1,776✔
998
        /* our line context worked with visible characters only, convert back to org_line */
999
        s_idx = org_line->first_char[s_idx];
30✔
1000
    }
1001
    return s_idx;
1,776✔
1002
}
1003

1004

1005

1006
static size_t calculate_end_idx(remove_ctx_t *ctx, size_t body_line_idx)
1,776✔
1007
{
1008
    size_t input_line_idx = ctx->top_end_idx + body_line_idx;
1,776✔
1009
    line_ctx_t *lctx = ctx->body + body_line_idx;
1,776✔
1010
    bxstr_t *org_line = input.lines[input_line_idx].text;
1,776✔
1011

1012
    size_t e_idx = lctx->east_quality > 0 ? lctx->east_start : max_chars_line(org_line, ctx->comp_type);
1,776✔
1013
    if (ctx->comp_type == ignore_invisible_input || ctx->comp_type == ignore_invisible_all) {
1,776✔
1014
        e_idx = org_line->first_char[e_idx];
30✔
1015
    }
1016
    return e_idx;
1,776✔
1017
}
1018

1019

1020

1021
static void remove_vertical_from_input(remove_ctx_t *ctx)
320✔
1022
{
1023
    for (size_t body_line_idx = 0; body_line_idx < ctx->body_num_lines; body_line_idx++) {
2,096✔
1024
        size_t input_line_idx = ctx->top_end_idx + body_line_idx;
1,776✔
1025
        bxstr_t *org_line = input.lines[input_line_idx].text;
1,776✔
1026
        size_t s_idx = calculate_start_idx(ctx, body_line_idx);
1,776✔
1027
        size_t e_idx = calculate_end_idx(ctx, body_line_idx);
1,776✔
1028
        log_debug(__FILE__, MAIN, "remove_vertical_from_input(): body_line_idx=%d, input_line_idx=%d, s_idx=%d, "
1,776✔
1029
                "e_idx=%d, input.indent=%d\n", (int) body_line_idx, (int) input_line_idx, (int) s_idx, (int) e_idx,
1030
                (int) input.indent);
1,776✔
1031

1032
        bxstr_t *temp2 = bxs_substr(org_line, s_idx, e_idx);
1,776✔
1033
        if (opt.indentmode == 'b' || opt.indentmode == '\0') {
3,550!
1034
            /* restore indentation */
1035
            bxstr_t *temp = bxs_prepend_spaces(temp2, input.indent);
1,774✔
1036
            free_line_text(input.lines + input_line_idx);
1,774✔
1037
            input.lines[input_line_idx].text = temp;
1,774✔
1038
            bxs_free(temp2);
1,774✔
1039
        }
1040
        else {
1041
            /* remove indentation */
1042
            free_line_text(input.lines + input_line_idx);
2✔
1043
            input.lines[input_line_idx].text = temp2;
2✔
1044
        }
1045
    }
1046
}
320✔
1047

1048

1049

1050
static void remove_bottom_from_input(remove_ctx_t *ctx)
320✔
1051
{
1052
    if (ctx->bottom_end_idx > ctx->bottom_start_idx) {
320✔
1053
        for (size_t j = ctx->bottom_start_idx; j < ctx->bottom_end_idx; ++j) {
1,158✔
1054
            free_line(input.lines + j);
880✔
1055
        }
1056
        if (ctx->bottom_end_idx < input.num_lines) {
278✔
1057
            memmove(input.lines + ctx->bottom_start_idx, input.lines + ctx->bottom_end_idx,
2✔
1058
                    (input.num_lines - ctx->bottom_end_idx) * sizeof(line_t));
1✔
1059
        }
1060
        input.num_lines -= ctx->bottom_end_idx - ctx->bottom_start_idx;
278✔
1061
    }
1062
}
320✔
1063

1064

1065

1066
static void remove_default_padding(remove_ctx_t *ctx, int num_blanks)
8✔
1067
{
1068
    if (num_blanks > 0) {
8✔
1069
        for (size_t body_line_idx = 0; body_line_idx < ctx->body_num_lines; body_line_idx++) {
24✔
1070
            size_t input_line_idx = ctx->top_start_idx + body_line_idx; /* top_start_idx, because top was removed! */
20✔
1071
            bxstr_t *temp = bxs_cut_front(input.lines[input_line_idx].text, (size_t) num_blanks);
20✔
1072
            free_line_text(input.lines + input_line_idx);
20✔
1073
            input.lines[input_line_idx].text = temp;
20✔
1074
        }
1075
        input.indent -= (size_t) num_blanks;
4✔
1076
        input.maxline -= (size_t) num_blanks;
4✔
1077
    }
1078
}
8✔
1079

1080

1081

1082
static void apply_results_to_input(remove_ctx_t *ctx)
320✔
1083
{
1084
    remove_vertical_from_input(ctx);
320✔
1085

1086
    if (opt.killblank || opt.mend) {
320!
1087
        killblank(ctx);
320✔
1088
    }
1089
    remove_bottom_from_input(ctx);
320✔
1090
    remove_top_from_input(ctx);
320✔
1091

1092
    input.maxline = 0;
320✔
1093
    input.indent = SIZE_MAX;
320✔
1094
    for (size_t j = 0; j < input.num_lines; ++j) {
1,902✔
1095
        if (input.lines[j].text->num_columns > input.maxline) {
1,582✔
1096
            input.maxline = input.lines[j].text->num_columns;
652✔
1097
        }
1098
        if (input.lines[j].text->indent < input.indent) {
1,582✔
1099
            input.indent = input.lines[j].text->indent;
322✔
1100
        }
1101
    }
1102
    if (ctx->empty_side[BLEF]) {
320✔
1103
        /* If the side were not open, default padding would have been removed when the side was removed. */
1104
        remove_default_padding(ctx, BMIN((int) input.indent, opt.design->padding[BLEF]));
8✔
1105
    }
1106

1107
    size_t num_lines_removed = BMAX(ctx->top_end_idx - ctx->top_start_idx, (size_t) 0)
320✔
1108
            + BMAX(ctx->bottom_end_idx - ctx->bottom_start_idx, (size_t) 0);
320✔
1109
    memset(input.lines + input.num_lines, 0, num_lines_removed * sizeof(line_t));
320✔
1110

1111
    if (is_debug_logging(MAIN)) {
320!
1112
        print_input_lines(" (remove_box) after box removal");
×
1113
        log_debug(__FILE__, MAIN, "Number of lines shrunk by %d.\n", (int) num_lines_removed);
×
1114
    }
1115
}
320✔
1116

1117

1118

1119
int remove_box()
322✔
1120
{
1121
    detect_design_if_needed();
322✔
1122

1123
    remove_ctx_t *ctx = (remove_ctx_t *) calloc(1, sizeof(remove_ctx_t));
320✔
1124
    ctx->empty_side[BTOP] = empty_side(opt.design->shape, BTOP);
320✔
1125
    ctx->empty_side[BRIG] = empty_side(opt.design->shape, BRIG);
320✔
1126
    ctx->empty_side[BBOT] = empty_side(opt.design->shape, BBOT);
320✔
1127
    ctx->empty_side[BLEF] = empty_side(opt.design->shape, BLEF);
320✔
1128
    log_debug(__FILE__, MAIN, "Empty sides? Top: %d, Right: %d, Bottom: %d, Left: %d\n",
320✔
1129
            ctx->empty_side[BTOP], ctx->empty_side[BRIG], ctx->empty_side[BBOT], ctx->empty_side[BLEF]);
1130

1131
    ctx->design_is_mono = design_is_mono(opt.design);
320✔
1132
    ctx->input_is_mono = input_is_mono();
320✔
1133

1134
    ctx->top_start_idx = find_first_line();
320✔
1135
    if (ctx->top_start_idx >= input.num_lines) {
320!
1136
        return 0;  /* all lines were already blank, so there is no box to remove */
×
1137
    }
1138

1139
    if (ctx->empty_side[BTOP]) {
320✔
1140
        ctx->top_end_idx = ctx->top_start_idx;
48✔
1141
    }
1142
    else {
1143
        ctx->top_end_idx = find_top_side(ctx);
272✔
1144
    }
1145
    log_debug(__FILE__, MAIN, "ctx->top_start_idx = %d, ctx->top_end_idx = %d\n", (int) ctx->top_start_idx,
320✔
1146
            (int) ctx->top_end_idx);
320✔
1147

1148
    ctx->bottom_end_idx = find_last_line() + 1;
320✔
1149
    if (ctx->empty_side[BBOT]) {
320✔
1150
        ctx->bottom_start_idx = ctx->bottom_end_idx;
44✔
1151
    }
1152
    else {
1153
        ctx->bottom_start_idx = find_bottom_side(ctx);
276✔
1154
    }
1155
    log_debug(__FILE__, MAIN, "ctx->bottom_start_idx = %d, ctx->bottom_end_idx = %d\n", (int) ctx->bottom_start_idx,
320✔
1156
            (int) ctx->bottom_end_idx);
320✔
1157
    if (ctx->bottom_start_idx > ctx->top_end_idx) {
320!
1158
        ctx->body_num_lines = ctx->bottom_start_idx - ctx->top_end_idx;
320✔
1159
    }
1160

1161
    if (ctx->body_num_lines > 0) {
320!
1162
        ctx->body = (line_ctx_t *) calloc(ctx->body_num_lines, sizeof(line_ctx_t));
320✔
1163
        find_vertical_shapes(ctx);
320✔
1164
    }
1165

1166
    debug_print_remove_ctx(ctx, "before apply_results_to_input()");
320✔
1167
    apply_results_to_input(ctx);
320✔
1168

1169
    if (ctx->body != NULL) {
320!
1170
        for (size_t i = 0; i < ctx->body_num_lines; i++) {
1,894✔
1171
            BFREE(ctx->body[i].input_line_used);
1,574✔
1172
        }
1173
        BFREE(ctx->body);
320!
1174
    }
1175
    BFREE(ctx);
320!
1176
    return 0;
320✔
1177
}
1178

1179

1180

1181
void output_input(const int trim_only)
320✔
1182
{
1183
    size_t indent;
1184
    int ntabs, nspcs;
1185

1186
    log_debug(__FILE__, MAIN, "output_input() - enter (trim_only=%d)\n", trim_only);
320✔
1187

1188
    for (size_t j = 0; j < input.num_lines; ++j) {
1,902✔
1189
        if (input.lines[j].text == NULL) {
1,582!
1190
            continue;
×
1191
        }
1192
        bxstr_t *temp = bxs_rtrim(input.lines[j].text);
1,582✔
1193
        bxs_free(input.lines[j].text);
1,582✔
1194
        input.lines[j].text = temp;
1,582✔
1195
        if (trim_only) {
1,582✔
1196
            continue;
776✔
1197
        }
1198

1199
        char *indentspc = NULL;
806✔
1200
        if (opt.tabexp == 'u') {
806✔
1201
            indent = input.lines[j].text->indent;
8✔
1202
            ntabs = indent / opt.tabstop;
8✔
1203
            nspcs = indent % opt.tabstop;
8✔
1204
            indentspc = (char *) malloc(ntabs + nspcs + 1);
8✔
1205
            if (indentspc == NULL) {
8!
1206
                perror(PROJECT);
×
1207
                return;
×
1208
            }
1209
            memset(indentspc, (int) '\t', ntabs);
8✔
1210
            memset(indentspc + ntabs, (int) ' ', nspcs);
8✔
1211
            indentspc[ntabs + nspcs] = '\0';
8✔
1212
        }
1213
        else if (opt.tabexp == 'k') {
798!
1214
            uint32_t *indent32 = tabbify_indent(j, NULL, input.indent);
×
1215
            indentspc = u32_strconv_to_output(indent32);
×
1216
            BFREE(indent32);
×
1217
            indent = input.indent;
×
1218
        }
1219
        else {
1220
            indentspc = (char *) strdup("");
798✔
1221
            indent = 0;
798✔
1222
        }
1223

1224
        char *outtext = u32_strconv_to_output(bxs_first_char_ptr(input.lines[j].text, indent));
806✔
UNCOV
1225
        fprintf(opt.outfile, "%s%s%s", indentspc, outtext,
×
1226
                (input.final_newline || j < input.num_lines - 1 ? opt.eol : ""));
806!
1227
        BFREE(outtext);
806!
1228
        BFREE(indentspc);
806!
1229
    }
1230
}
1231

1232

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