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

ascii-boxes / boxes / 25961777024

16 May 2026 12:18PM UTC coverage: 82.852% (-4.5%) from 87.313%
25961777024

push

github

tsjensen
Add `build-windows` job to GitHub Actions workflow

2774 of 3691 branches covered (75.16%)

Branch coverage included in aggregate %.

4343 of 4899 relevant lines covered (88.65%)

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

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

20
#include "config.h"
21

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

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

37

38

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

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

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

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

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

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

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

64

65

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

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

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

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

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

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

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

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

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

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

101

102

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

146

147

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

170

171

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

184

185

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

198

199

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

208

209

210
static int non_empty_shapes_after(shape_line_ctx_t *shapes_relevant, size_t shape_idx)
858✔
211
{
212
    /* CHECK Can we use shape->is_blank_rightward? */
213
    for (size_t i = shape_idx + 1; i < SHAPES_PER_SIDE - 1; i++) {
1,292✔
214
        if (!is_shape_line_empty(shapes_relevant, i)) {
461✔
215
            return 1;
27✔
216
        }
217
    }
218
    return 0;
831✔
219
}
220

221

222

223
static int is_blank_between(uint32_t *start, uint32_t *end)
384✔
224
{
225
    for (uint32_t *p = start; p < end; p++) {
6,240✔
226
        if (!is_blank(*p)) {
5,858✔
227
            return 0;
2✔
228
        }
229
    }
230
    return 1;
382✔
231
}
232

233

234

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

251
    uint32_t *s = shape_line_ctx->text->memory;
8,596✔
252
    uint32_t *e = shape_line_ctx->text->memory + shape_line_ctx->text->num_chars;
8,596✔
253
    prefer_left = allow_left ? prefer_left : 0;
8,596✔
254
    size_t reduction_steps = shape_line_ctx->text->num_chars - *quality + 1;
8,596✔
255
    for (size_t i = 0; i < reduction_steps; i++) {
33,303✔
256
        if (prefer_left) {
26,927✔
257
            if (s < e && is_blank(*s)) {
8,642!
258
                s++;
4,370✔
259
            }
260
            else if (e > s && allow_right && is_blank(*(e - 1))) {
4,272!
261
                e--;
3,281✔
262
            }
263
            else {
264
                break;
265
            }
266
        }
267
        else {
268
            if (e > s && allow_right && is_blank(*(e - 1))) {
18,285✔
269
                e--;
13,321✔
270
            }
271
            else if (s < e && allow_left && is_blank(*s)) {
4,964✔
272
                s++;
3,735✔
273
            }
274
            else {
275
                break;
276
            }
277
        }
278
    }
279

280
    uint32_t *result = NULL;
8,596✔
281
    size_t new_quality = e - s;
8,596✔
282
    if (new_quality < *quality) {
8,596✔
283
        result = u32_strdup(s);
6,376✔
284
        set_char_at(result, new_quality, char_nul);
6,376✔
285
        *quality = new_quality;
6,376✔
286
    }
287
    return result;
8,596✔
288
}
289

290

291

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

295

296

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

323
    int result = 0;
18,612✔
324
    if (!anchored_left) {
18,612✔
325
        result = hmm_shiftable(shapes_relevant, cur_pos, shape_idx, end_pos, anchored_right);
397✔
326
    }
327
    else if (cur_pos > end_pos) {
18,215✔
328
        /* invalid input */
329
        result = 0;
121✔
330
    }
331
    else if (cur_pos == end_pos) {
18,094✔
332
        /* we are at the end, which is fine if there is nothing else to match */
333
        result = (shape_idx == (SHAPES_PER_SIDE - 1) && anchored_right)
229!
334
                || ((shapes_relevant[shape_idx].empty || bxs_is_blank(shapes_relevant[shape_idx].text))
383!
335
                    && !non_empty_shapes_after(shapes_relevant, shape_idx) ? 1 : 0);
154✔
336
    }
337
    else if (shape_idx >= SHAPES_PER_SIDE - 1) {
17,881✔
338
        /* no more shapes to try, which is fine if the rest of the line is blank */
339
        result = u32_is_blank(cur_pos);
55✔
340
    }
341
    else if (shapes_relevant[shape_idx].empty) {
17,826✔
342
        /* the current shape line is empty, try the next one */
343
        result = hmm(shapes_relevant, cur_pos, shape_idx + 1, end_pos, 1, anchored_right);
235✔
344
    }
345
    else {
346
        uint32_t *shape_line = u32_strdup(shapes_relevant[shape_idx].text->memory);
17,591✔
347
        size_t quality = shapes_relevant[shape_idx].text->num_chars;
17,591✔
348
        while (shape_line != NULL && quality > 0) {
35,807✔
349
            if (u32_strncmp(cur_pos, shape_line, quality) == 0) {
18,216✔
350
                BFREE(shape_line);
16,832!
351
                cur_pos = cur_pos + quality;
16,832✔
352
                if (cur_pos == end_pos && !non_empty_shapes_after(shapes_relevant, shape_idx)) {
16,832✔
353
                    result = 1; /* success */
547✔
354
                }
355
                else {
356
                    int rc = 0;
16,285✔
357
                    if (shapes_relevant[shape_idx].elastic) {
16,285✔
358
                        rc = hmm(shapes_relevant, cur_pos, shape_idx, end_pos, 1, anchored_right);
16,049✔
359
                    }
360
                    if (rc == 0) {
16,285✔
361
                        result = hmm(shapes_relevant, cur_pos, shape_idx + 1, end_pos, 1, anchored_right);
1,152✔
362
                    }
363
                    else {
364
                        result = rc;
15,133✔
365
                    }
366
                }
367
            }
368
            else if (!anchored_right) {
1,384✔
369
                shape_line = shorten(shapes_relevant + shape_idx, &quality, 0, 0, 1);
790✔
370
                if (is_debug_logging(MAIN)) {
790!
371
                    char *out_shape_line = u32_strconv_to_output(shape_line);
×
372
                    log_debug(__FILE__, MAIN, "hmm() - shape_line shortened to %d (\"%s\")\n",
×
373
                            (int) quality, out_shape_line);
374
                    BFREE(out_shape_line);
×
375
                }
376
            }
377
            else {
378
                BFREE(shape_line);
594!
379
            }
380
        }
381
    }
382

383
    log_debug(__FILE__, MAIN, "hmm() - exit, result = %d\n", result);
18,612✔
384
    return result;
18,612✔
385
}
386

387

388

389
static int hmm_shiftable(shape_line_ctx_t *shapes_relevant, uint32_t *cur_pos, size_t shape_idx, uint32_t *end_pos,
397✔
390
        int anchored_right)
391
{
392
    int result = 0;
397✔
393
    int shapes_are_empty = 1;
397✔
394
    for (size_t i = shape_idx; i < SHAPES_PER_SIDE - 1; i++) {
752✔
395
        if (!is_shape_line_empty(shapes_relevant, i)) {
734✔
396
            shapes_are_empty = 0;
379✔
397
            int can_shorten_right = -1;
379✔
398
            size_t quality = shapes_relevant[i].text->num_chars;
379✔
399
            uint32_t *shape_line = shapes_relevant[i].text->memory;
379✔
400
            while (shape_line != NULL) {
699✔
401
                uint32_t *p = u32_strstr(cur_pos, shape_line);
685✔
402
                if (p != NULL && p < end_pos && is_blank_between(cur_pos, p)) {
685!
403
                    result = hmm(shapes_relevant, p + quality, i + (shapes_relevant[i].elastic ? 0 : 1),
365✔
404
                            end_pos, 1, anchored_right);
405
                    if (result == 0 && shapes_relevant[i].elastic) {
365!
406
                        result = hmm(shapes_relevant, p + quality, i + 1, end_pos, 1, anchored_right);
4✔
407
                    }
408
                    break;
365✔
409
                }
410
                if (can_shorten_right == -1) {
320✔
411
                    /* we can only shorten right if the east corner shape line is also empty */
412
                    can_shorten_right = non_empty_shapes_after(shapes_relevant, i)
264✔
413
                            || !is_shape_line_empty(shapes_relevant, SHAPES_PER_SIDE - 1) ? 0 : 1;
132✔
414
                }
415
                shape_line = shorten(shapes_relevant + i, &quality, 0, 1, can_shorten_right);
320✔
416
            }
417
            break;
379✔
418
        }
419
    }
420
    if (shapes_are_empty) {
397✔
421
        /* all shapes were empty, which is fine if line was blank */
422
        result = is_blank_between(cur_pos, end_pos);
18✔
423
    }
424
    return result;
397✔
425
}
426

427

428

429
static shape_line_ctx_t *prepare_comp_shapes_horiz(int hside, comparison_t comp_type, size_t shape_line_idx)
795✔
430
{
431
    shape_t *side_shapes = hside == BTOP ? north_side : south_side_rev;
795✔
432
    shape_line_ctx_t *shapes_relevant = (shape_line_ctx_t *) calloc(SHAPES_PER_SIDE, sizeof(shape_line_ctx_t));
795✔
433

434
    for (size_t i = 0; i < SHAPES_PER_SIDE; i++) {
4,770✔
435
        shapes_relevant[i].elastic = opt.design->shape[side_shapes[i]].elastic;
3,975✔
436
        shapes_relevant[i].empty = isempty(opt.design->shape + side_shapes[i]);
3,975✔
437
        if (!shapes_relevant[i].empty) {
3,975✔
438
            uint32_t *s = prepare_comp_shape(opt.design, side_shapes[i], shape_line_idx, comp_type, 0,
3,471✔
439
                    i == SHAPES_PER_SIDE - 1);
440
            shapes_relevant[i].text = bxs_from_unicode(s);
3,471✔
441
            BFREE(s);
3,471!
442
        }
443
    }
444

445
    return shapes_relevant;
795✔
446
}
447

448

449

450
static match_result_t *new_match_result(uint32_t *p, size_t p_idx, size_t len, int shiftable)
1,514✔
451
{
452
    match_result_t *result = (match_result_t *) calloc(1, sizeof(match_result_t));
1,514✔
453
    result->p = p;
1,514✔
454
    result->p_idx = p_idx;
1,514✔
455
    result->len = len;
1,514✔
456
    result->shiftable = shiftable;
1,514✔
457
    return result;
1,514✔
458
}
459

460

461

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

477
    if (vside == BLEF) {
1,523✔
478
        if (bxs_is_blank(shape_line)) {
769✔
479
            return new_match_result(input_line->memory, 0, 0, 1);
359✔
480
        }
481
        for (uint32_t *s = shape_line->memory; s == shape_line->memory || is_blank(*s); s++) {
417✔
482
            uint32_t *p = u32_strstr(input_line->memory, s);
413✔
483
            size_t p_idx = p != NULL ? p - input_line->memory : 0;
413✔
484
            if (p == NULL || p_idx > input_line->first_char[input_line->indent]) {
413✔
485
                continue;  /* not found or found too far in */
7✔
486
            }
487
            return new_match_result(p, p_idx, shape_line->num_chars - (s - shape_line->memory), 0);
406✔
488
        }
489
    }
490
    else {
491
        if (bxs_is_blank(shape_line)) {
754✔
492
            uint32_t *p = bxs_last_char_ptr(input_line);
329✔
493
            size_t p_idx = p - input_line->memory;
329✔
494
            return new_match_result(p, p_idx, 0, 1);
329✔
495
        }
496
        int slen = shape_line->num_chars;
425✔
497
        uint32_t *s = u32_strdup(shape_line->memory);
425✔
498
        for (; slen == (int) shape_line->num_chars || is_blank(s[slen]); slen--) {
435✔
499
            s[slen] = char_nul;
430✔
500
            uint32_t *p = u32_strnrstr(input_line->memory, s, slen);
430✔
501
            size_t p_idx = p != NULL ? p - input_line->memory : 0;
430✔
502
            if (p == NULL || p_idx + slen
430✔
503
                    < input_line->first_char[input_line->num_chars_visible - input_line->trailing]) {
422✔
504
                continue; /* not found or found too far in */
10✔
505
            }
506
            BFREE(s);
420!
507
            return new_match_result(p, p_idx, (size_t) slen, 0);
420✔
508
        }
509
        BFREE(s);
5!
510
    }
511
    return NULL;
9✔
512
}
513

514

515

516
static int match_horiz_line(remove_ctx_t *ctx, int hside, size_t input_line_idx, size_t shape_line_idx)
780✔
517
{
518
    log_debug(__FILE__, MAIN, "match_horiz_line(ctx, %s, %d, %d)\n",
780✔
519
                hside == BTOP ? "BTOP" : "BBOT", (int) input_line_idx, (int) shape_line_idx);
520

521
    int result = 0;
780✔
522
    for (comparison_t comp_type = 0; comp_type < NUM_COMPARISON_TYPES; comp_type++) {
879!
523
        if (!comp_type_is_viable(comp_type, ctx->input_is_mono, ctx->design_is_mono)) {
879✔
524
            continue;
84✔
525
        }
526
        ctx->comp_type = comp_type;
795✔
527
        log_debug(__FILE__, MAIN, "  Setting comparison type to: %s\n", comparison_name[comp_type]);
795✔
528

529
        shape_line_ctx_t *shapes_relevant = prepare_comp_shapes_horiz(hside, comp_type, shape_line_idx);
795✔
530
        debug_print_shapes_relevant(shapes_relevant);
795✔
531

532
        bxstr_t *input_prepped1 = bxs_from_unicode(prepare_comp_input(input_line_idx, 0, comp_type, 0, NULL, NULL));
795✔
533
        bxstr_t *input_prepped = bxs_rtrim(input_prepped1);
795✔
534
        bxs_append_spaces(input_prepped, opt.design->shape[NW].width + opt.design->shape[NE].width);
795✔
535
        bxs_free(input_prepped1);
795✔
536
        if (is_debug_logging(MAIN)) {
795!
537
            char *out_input_prepped = bxs_to_output(input_prepped);
×
538
            log_debug(__FILE__, MAIN, "  input_prepped = \"%s\"\n", out_input_prepped);
×
539
            BFREE(out_input_prepped);
×
540
        }
541

542
        uint32_t *cur_pos = input_prepped->memory;
795✔
543
        match_result_t *mrl = NULL;
795✔
544
        if (!ctx->empty_side[BLEF]) {
795✔
545
            mrl = match_outer_shape(BLEF, input_prepped, shapes_relevant[0].text);
763✔
546
            if (mrl != NULL) {
763✔
547
                cur_pos = mrl->p + mrl->len;
761✔
548
            }
549
        }
550

551
        uint32_t *end_pos = bxs_last_char_ptr(input_prepped);
795✔
552
        match_result_t *mrr = NULL;
795✔
553
        if (!ctx->empty_side[BRIG]) {
795✔
554
            mrr = match_outer_shape(BRIG, input_prepped, shapes_relevant[SHAPES_PER_SIDE - 1].text);
749✔
555
            if (mrr != NULL) {
749✔
556
                end_pos = mrr->p;
744✔
557
            }
558
        }
559
        if (is_debug_logging(MAIN)) {
795!
560
            char *out_cur_pos = u32_strconv_to_output(cur_pos);
×
561
            char *out_end_pos = u32_strconv_to_output(end_pos);
×
562
            log_debug(__FILE__, MAIN, "  cur_pos = \"%s\" (index %d)\n",
×
563
                    out_cur_pos, (int) BMAX(cur_pos - input_prepped->memory, 0));
×
564
            log_debug(__FILE__, MAIN, "  end_pos = \"%s\" (index %d)\n",
×
565
                    out_end_pos, (int) BMAX(end_pos - input_prepped->memory, 0));
×
566
            BFREE(out_cur_pos);
×
567
            BFREE(out_end_pos);
×
568
        }
569

570
        result = hmm(shapes_relevant, cur_pos, 1, end_pos, (mrl == NULL) || mrl->shiftable ? 0 : 1,
1,539✔
571
                (mrr == NULL) || mrr->shiftable ? 0 : 1);
744✔
572

573
        BFREE(mrl);
795✔
574
        BFREE(mrr);
795✔
575
        for (size_t i = 0; i < SHAPES_PER_SIDE; i++) {
4,770✔
576
            bxs_free(shapes_relevant[i].text);
3,975✔
577
        }
578
        BFREE(shapes_relevant);
795!
579

580
        if (result) {
795✔
581
            log_debug(__FILE__, MAIN, "Matched %s side line using comp_type=%s and shape_line_idx=%d\n",
780✔
582
                    hside == BTOP ? "top" : "bottom", comparison_name[comp_type], (int) shape_line_idx);
583
            break;
780✔
584
        }
585
    }
586

587
    return result;
780✔
588
}
589

590

591

592
static size_t find_top_side(remove_ctx_t *ctx)
136✔
593
{
594
    size_t result = ctx->top_start_idx;
136✔
595
    sentry_t *shapes = opt.design->shape;
136✔
596
    for (size_t input_line_idx = ctx->top_start_idx;
136✔
597
            input_line_idx < input.num_lines && input_line_idx < ctx->top_start_idx + shapes[NE].height;
526!
598
            input_line_idx++)
390✔
599
    {
600
        int matched = 0;
390✔
601
        size_t shape_lines_tested = 0;
390✔
602
        for (size_t shape_line_idx = (input_line_idx - ctx->top_start_idx) % shapes[NE].height;
390✔
603
                shape_lines_tested < shapes[NE].height;
390!
604
                shape_line_idx = (shape_line_idx + 1) % shapes[NE].height, shape_lines_tested++)
×
605
        {
606
            if (match_horiz_line(ctx, BTOP, input_line_idx, shape_line_idx)) {
390!
607
                matched = 1;
390✔
608
                break;
390✔
609
            }
610
        }
611
        if (!matched) {
390!
612
            break;
×
613
        }
614
        result = input_line_idx + 1;
390✔
615
    }
616
    return result;
136✔
617
}
618

619

620

621
static size_t find_bottom_side(remove_ctx_t *ctx)
138✔
622
{
623
    size_t result = ctx->bottom_end_idx;
138✔
624
    sentry_t *shapes = opt.design->shape;
138✔
625
    for (long input_line_idx = (long) ctx->bottom_end_idx - 1;
138✔
626
            input_line_idx >= 0 && input_line_idx >= (long) ctx->bottom_end_idx - (long) shapes[SE].height;
528!
627
            input_line_idx--)
390✔
628
    {
629
        int matched = 0;
390✔
630
        size_t shape_lines_tested = 0;
390✔
631
        for (long shape_line_idx = shapes[SE].height - (ctx->bottom_end_idx - input_line_idx);
390✔
632
                shape_line_idx >= 0 && shape_lines_tested < shapes[SE].height;
390!
633
                shape_lines_tested++,
×
634
                shape_line_idx = shape_line_idx == 0 ? (long) (shapes[SE].height - 1) : (long) (shape_line_idx - 1))
×
635
        {
636
            if (match_horiz_line(ctx, BBOT, input_line_idx, shape_line_idx)) {
390!
637
                matched = 1;
390✔
638
                break;
390✔
639
            }
640
        }
641
        if (!matched) {
390!
642
            break;
×
643
        }
644
        result = input_line_idx;
390✔
645
    }
646
    return result;
138✔
647
}
648

649

650

651
static size_t count_shape_lines(shape_t side_shapes[])
292✔
652
{
653
    size_t result = 0;
292✔
654
    for (size_t i = 0; i < SHAPES_PER_SIDE - CORNERS_PER_SIDE; i++) {
1,168✔
655
        if (!isempty(opt.design->shape + side_shapes[i])) {
876✔
656
            result += opt.design->shape[side_shapes[i]].height;
368✔
657
        }
658
    }
659
    return result;
292✔
660
}
661

662

663
static shape_line_ctx_t **prepare_comp_shapes_vert(int vside, comparison_t comp_type)
292✔
664
{
665
    shape_t west_side_shapes[SHAPES_PER_SIDE - CORNERS_PER_SIDE] = {WNW, W, WSW};
292✔
666
    shape_t east_side_shapes[SHAPES_PER_SIDE - CORNERS_PER_SIDE] = {ENE, E, ESE};
292✔
667
    shape_t side_shapes[SHAPES_PER_SIDE - CORNERS_PER_SIDE];
668
    if (vside == BLEF) {
292✔
669
        memcpy(side_shapes, west_side_shapes, (SHAPES_PER_SIDE - CORNERS_PER_SIDE) * sizeof(shape_t));
157✔
670
    }
671
    else {
672
        memcpy(side_shapes, east_side_shapes, (SHAPES_PER_SIDE - CORNERS_PER_SIDE) * sizeof(shape_t));
135✔
673
    }
674

675
    size_t num_shape_lines = count_shape_lines(side_shapes);
292✔
676

677
    shape_line_ctx_t **shape_lines = (shape_line_ctx_t **) calloc(num_shape_lines + 1, sizeof(shape_line_ctx_t *));
292✔
678
    for (size_t i = 0; i < num_shape_lines; i++) {
854✔
679
        shape_lines[i] = (shape_line_ctx_t *) calloc(1, sizeof(shape_line_ctx_t));
562✔
680
    }
681

682
    for (size_t shape_idx = 0, i = 0; shape_idx < SHAPES_PER_SIDE - CORNERS_PER_SIDE; shape_idx++) {
1,168✔
683
        if (!isempty(opt.design->shape + side_shapes[shape_idx])) {
876✔
684
            int deep_empty = isdeepempty(opt.design->shape + side_shapes[shape_idx]);
368✔
685
            for (size_t slno = 0; slno < opt.design->shape[side_shapes[shape_idx]].height; slno++, i++) {
930✔
686
                uint32_t *s = prepare_comp_shape(opt.design, side_shapes[shape_idx], slno, comp_type, 0, 0);
562✔
687
                shape_lines[i]->text = bxs_from_unicode(s);
562✔
688
                shape_lines[i]->empty = deep_empty;
562✔
689
                shape_lines[i]->elastic = opt.design->shape[side_shapes[shape_idx]].elastic;
562✔
690
                BFREE(s);
562!
691
            }
692
        }
693
    }
694

695
    return shape_lines;
292✔
696
}
697

698

699

700
static void free_shape_lines(shape_line_ctx_t **shape_lines)
318✔
701
{
702
    if (shape_lines != NULL) {
318✔
703
        for (shape_line_ctx_t **p = shape_lines; *p != NULL; p++) {
854✔
704
            bxs_free((*p)->text);
562✔
705
            BFREE(*p);
562!
706
        }
707
        BFREE(shape_lines);
292!
708
    }
709
}
318✔
710

711

712

713
static void match_vertical_side(remove_ctx_t *ctx, int vside, shape_line_ctx_t **shape_lines, uint32_t *input_line,
1,616✔
714
    size_t line_idx, size_t input_length, size_t input_indent, size_t input_trailing)
715
{
716
    line_ctx_t *line_ctx = ctx->body + (line_idx - ctx->top_end_idx);
1,616✔
717

718
    for (shape_line_ctx_t **shape_line_ctx = shape_lines; *shape_line_ctx != NULL; shape_line_ctx++) {
5,488✔
719
        if ((*shape_line_ctx)->empty) {
3,872!
720
            continue;
×
721
        }
722

723
        size_t max_quality = (*shape_line_ctx)->text->num_chars;
3,872✔
724
        size_t quality = max_quality;
3,872✔
725
        uint32_t *shape_text = (*shape_line_ctx)->text->memory;
3,872✔
726
        uint32_t *to_free = NULL;
3,872✔
727
        while(shape_text != NULL) {
11,515✔
728
            uint32_t *p;
729
            if (vside == BLEF) {
9,289✔
730
                p = u32_strstr(input_line, shape_text);
4,169✔
731
            }
732
            else {
733
                p = u32_strnrstr(input_line, shape_text, quality);
5,120✔
734
            }
735
            BFREE(to_free);
9,289✔
736
            shape_text = NULL;
9,289✔
737

738
            if ((p == NULL)
9,289✔
739
                    || (vside == BLEF && ((size_t) (p - input_line) > input_indent + (max_quality - quality)))
2,828✔
740
                    || (vside == BRIG && ((size_t) (p - input_line) < input_length - input_trailing - quality))) {
2,668✔
741
                shape_text = shorten(*shape_line_ctx, &quality, vside == BLEF, 1, 1);
7,469✔
742
                to_free = shape_text;
7,469✔
743
            }
744
            else if (vside == BLEF) {
1,820✔
745
                if (quality > line_ctx->west_quality) {
1,000✔
746
                    line_ctx->west_start = (size_t) (p - input_line);
892✔
747
                    line_ctx->west_end = line_ctx->west_start + quality;
892✔
748
                    line_ctx->west_quality = quality;
892✔
749
                    BFREE(line_ctx->input_line_used);
892✔
750
                    line_ctx->input_line_used = u32_strdup(input_line);
892✔
751
                    break;
892✔
752
                }
753
            }
754
            else if (vside == BRIG) {
820!
755
                if (quality > line_ctx->east_quality) {
820✔
756
                    line_ctx->east_start = (size_t) (p - input_line);
754✔
757
                    line_ctx->east_end = line_ctx->east_start + quality;
754✔
758
                    line_ctx->east_quality = quality;
754✔
759
                    BFREE(line_ctx->input_line_used);
754✔
760
                    line_ctx->input_line_used = u32_strdup(input_line);
754✔
761
                    break;
754✔
762
                }
763
            }
764
        }
765
    }
766
}
1,616✔
767

768

769

770
static int sufficient_body_quality(remove_ctx_t *ctx)
159✔
771
{
772
    size_t num_body_lines = ctx->bottom_start_idx - ctx->top_end_idx;
159✔
773
    size_t total_quality = 0;
159✔
774
    line_ctx_t *body = ctx->body;
159✔
775
    for (size_t body_line_idx = 0; body_line_idx < num_body_lines; body_line_idx++) {
1,036✔
776
        line_ctx_t line_ctx = body[body_line_idx];
877✔
777
        total_quality += line_ctx.east_quality + line_ctx.west_quality;
877✔
778
    }
779

780
    size_t max_quality = 0;
159✔
781
    if (!ctx->empty_side[BLEF]) {
159✔
782
        max_quality += opt.design->shape[NW].width;
157✔
783
    }
784
    if (!ctx->empty_side[BRIG]) {
159✔
785
        max_quality += opt.design->shape[NE].width;
135✔
786
    }
787
    max_quality = max_quality * num_body_lines;
159✔
788

789
    /* If we manage to match 50%, then it is unlikely to improve with a different comparison mode. */
790
    int sufficient = (max_quality == 0 && total_quality == 0)
×
791
            || (max_quality > 0 && (total_quality > 0.5 * max_quality));
159!
792
    log_debug(__FILE__, MAIN, "sufficient_body_quality() found body match quality of %d/%d (%s).\n",
159✔
793
            (int) total_quality, (int) max_quality, sufficient ? "sufficient" : "NOT sufficient");
794

795
    return sufficient;
159✔
796
}
797

798

799

800
static void reset_body(remove_ctx_t *ctx)
159✔
801
{
802
    if (ctx->body != NULL) {
159!
803
        for (size_t i = 0; i < ctx->body_num_lines; i++) {
1,036✔
804
            BFREE(ctx->body[i].input_line_used);
877!
805
        }
806
        memset(ctx->body, 0, ctx->body_num_lines * sizeof(line_ctx_t));
159✔
807
    }
808
}
159✔
809

810

811

812
static void find_vertical_shapes(remove_ctx_t *ctx)
160✔
813
{
814
    int west_empty = ctx->empty_side[BLEF];
160✔
815
    int east_empty = ctx->empty_side[BRIG];
160✔
816
    if (west_empty && east_empty) {
160✔
817
        return;
2✔
818
    }
819

820
    for (comparison_t comp_type = 0; comp_type < NUM_COMPARISON_TYPES; comp_type++) {
191✔
821
        if (!comp_type_is_viable(comp_type, ctx->input_is_mono, ctx->design_is_mono)) {
185✔
822
            continue;
26✔
823
        }
824
        ctx->comp_type = comp_type;
159✔
825
        log_debug(__FILE__, MAIN, "find_vertical_shapes(): comp_type = %s\n", comparison_name[comp_type]);
159✔
826
        reset_body(ctx);
159✔
827

828
        shape_line_ctx_t **shape_lines_west = NULL;
159✔
829
        if (!west_empty) {
159✔
830
            shape_lines_west = prepare_comp_shapes_vert(BLEF, comp_type);
157✔
831
        }
832
        shape_line_ctx_t **shape_lines_east = NULL;
159✔
833
        if (!east_empty) {
159✔
834
            shape_lines_east = prepare_comp_shapes_vert(BRIG, comp_type);
135✔
835
        }
836

837
        for (size_t input_line_idx = ctx->top_end_idx; input_line_idx < ctx->bottom_start_idx; input_line_idx++) {
1,036✔
838
            size_t input_indent = 0;
877✔
839
            size_t input_trailing = 0;
877✔
840
            uint32_t *input_line = prepare_comp_input(input_line_idx, 0, comp_type, 0, &input_indent, &input_trailing);
877✔
841
            size_t input_length = u32_strlen(input_line);
877✔
842

843
            if (!west_empty) {
877✔
844
                match_vertical_side(ctx, BLEF, shape_lines_west,
867✔
845
                        input_line, input_line_idx, input_length, input_indent, input_trailing);
846
            }
847
            if (!east_empty) {
877✔
848
                match_vertical_side(ctx, BRIG, shape_lines_east,
749✔
849
                        input_line, input_line_idx, input_length, input_indent, input_trailing);
850
            }
851
        }
852

853
        free_shape_lines(shape_lines_west);
159✔
854
        free_shape_lines(shape_lines_east);
159✔
855

856
        if (sufficient_body_quality(ctx)) {
159✔
857
            break;
152✔
858
        }
859
    }
860
}
861

862

863

864
/**
865
 * If the user didn't specify a design to remove, autodetect it.
866
 * Since this requires knowledge of all available designs, the entire config file had to be parsed (earlier).
867
 */
868
static void detect_design_if_needed()
161✔
869
{
870
    if (opt.design_choice_by_user == 0) {
161✔
871
        design_t *tmp = autodetect_design();
8✔
872
        if (tmp) {
8✔
873
            opt.design = tmp;
7✔
874
            log_debug(__FILE__, MAIN, "Design autodetection: Removing box of design \"%s\".\n", opt.design->name);
7✔
875
        }
876
        else {
877
            fprintf(stderr, "%s: Box design autodetection failed. Use -d option.\n", PROJECT);
1✔
878
            exit(EXIT_FAILURE);
1✔
879
        }
880
    }
881
    else {
882
        log_debug(__FILE__, MAIN, "Design was chosen by user: %s\n", opt.design->name);
153✔
883
    }
884
}
160✔
885

886

887

888
static void free_line_text(line_t *line)
1,779✔
889
{
890
    BFREE(line->cache_visible);
1,779✔
891
    bxs_free(line->text);
1,779✔
892
    line->text = NULL;
1,779✔
893
}
1,779✔
894

895

896

897
static void free_line(line_t *line)
881✔
898
{
899
    free_line_text(line);
881✔
900
    BFREE(line->tabpos);
881!
901
    line->tabpos_len = 0;
881✔
902
}
881✔
903

904

905

906
static void killblank(remove_ctx_t *ctx)
160✔
907
{
908
    size_t lines_removed = 0;
160✔
909
    size_t max_lines_removable = opt.mend && !opt.killblank ? (size_t) BMAX(opt.design->padding[BTOP], 0) : SIZE_MAX;
160!
910
    while (ctx->top_end_idx < ctx->bottom_start_idx && lines_removed < max_lines_removable
371✔
911
            && empty_line(input.lines + ctx->top_end_idx))
338!
912
    {
913
        log_debug(__FILE__, MAIN, "Killing leading blank line in box body.\n");
51✔
914
        ++(ctx->top_end_idx);
51✔
915
        --(ctx->body_num_lines);
51✔
916
        ++lines_removed;
51✔
917
    }
918

919
    lines_removed = 0;
160✔
920
    max_lines_removable = opt.mend && !opt.killblank ? (size_t) BMAX(opt.design->padding[BBOT], 0) : SIZE_MAX;
160!
921
    while (ctx->bottom_start_idx > ctx->top_end_idx && lines_removed < max_lines_removable
370✔
922
            && empty_line(input.lines + ctx->bottom_start_idx - 1))
336!
923
    {
924
        log_debug(__FILE__, MAIN, "Killing trailing blank line in box body.\n");
50✔
925
        --(ctx->bottom_start_idx);
50✔
926
        --(ctx->body_num_lines);
50✔
927
        ++lines_removed;
50✔
928
    }
929
}
160✔
930

931

932

933
static int org_is_not_blank(bxstr_t *org_line, comparison_t comp_type, size_t idx)
960✔
934
{
935
    if (comp_type == literal || comp_type == ignore_invisible_shape) {
960!
936
        return !is_blank(org_line->memory[idx]);
952✔
937
    }
938
    return !is_blank(org_line->memory[org_line->visible_char[idx]]);
8✔
939
}
940

941

942

943
static size_t max_chars_line(bxstr_t *org_line, comparison_t comp_type)
1,015✔
944
{
945
    return (comp_type == literal || comp_type == ignore_invisible_shape)
17✔
946
            ? org_line->num_chars : org_line->num_chars_visible;
1,032✔
947
}
948

949

950

951
static size_t confirmed_padding(bxstr_t *org_line, comparison_t comp_type, size_t start_idx, size_t num_padding)
866✔
952
{
953
    size_t result = 0;
866✔
954
    size_t max_chars = max_chars_line(org_line, comp_type);
866✔
955
    for (size_t i = start_idx; i < BMIN(max_chars, start_idx + num_padding); i++) {
1,826✔
956
        if (org_is_not_blank(org_line, comp_type, i)) {
960!
957
            break;
×
958
        }
959
        result++;
960✔
960
    }
961
    return result;
866✔
962
}
963

964

965

966
static void remove_top_from_input(remove_ctx_t *ctx)
160✔
967
{
968
    if (ctx->top_end_idx > ctx->top_start_idx) {
160✔
969
        for (size_t j = ctx->top_start_idx; j < ctx->top_end_idx; ++j) {
578✔
970
            free_line(input.lines + j);
441✔
971
        }
972
        memmove(input.lines + ctx->top_start_idx, input.lines + ctx->top_end_idx,
137✔
973
                (input.num_lines - ctx->top_end_idx) * sizeof(line_t));
137✔
974
        input.num_lines -= ctx->top_end_idx - ctx->top_start_idx;
137✔
975
    }
976
}
160✔
977

978

979

980
static size_t calculate_start_idx(remove_ctx_t *ctx, size_t body_line_idx)
888✔
981
{
982
    size_t input_line_idx = ctx->top_end_idx + body_line_idx;
888✔
983
    line_ctx_t *lctx = ctx->body + body_line_idx;
888✔
984
    bxstr_t *org_line = input.lines[input_line_idx].text;
888✔
985

986
    size_t s_idx = 0;
888✔
987
    if (lctx->west_quality > 0) {
888✔
988
        s_idx = lctx->west_end + confirmed_padding(org_line, ctx->comp_type, lctx->west_end, opt.design->padding[BLEF]);
866✔
989
    }
990
    if (ctx->comp_type == ignore_invisible_input || ctx->comp_type == ignore_invisible_all) {
888✔
991
        /* our line context worked with visible characters only, convert back to org_line */
992
        s_idx = org_line->first_char[s_idx];
15✔
993
    }
994
    return s_idx;
888✔
995
}
996

997

998

999
static size_t calculate_end_idx(remove_ctx_t *ctx, size_t body_line_idx)
888✔
1000
{
1001
    size_t input_line_idx = ctx->top_end_idx + body_line_idx;
888✔
1002
    line_ctx_t *lctx = ctx->body + body_line_idx;
888✔
1003
    bxstr_t *org_line = input.lines[input_line_idx].text;
888✔
1004

1005
    size_t e_idx = lctx->east_quality > 0 ? lctx->east_start : max_chars_line(org_line, ctx->comp_type);
888✔
1006
    if (ctx->comp_type == ignore_invisible_input || ctx->comp_type == ignore_invisible_all) {
888✔
1007
        e_idx = org_line->first_char[e_idx];
15✔
1008
    }
1009
    return e_idx;
888✔
1010
}
1011

1012

1013

1014
static void remove_vertical_from_input(remove_ctx_t *ctx)
160✔
1015
{
1016
    for (size_t body_line_idx = 0; body_line_idx < ctx->body_num_lines; body_line_idx++) {
1,048✔
1017
        size_t input_line_idx = ctx->top_end_idx + body_line_idx;
888✔
1018
        bxstr_t *org_line = input.lines[input_line_idx].text;
888✔
1019
        size_t s_idx = calculate_start_idx(ctx, body_line_idx);
888✔
1020
        size_t e_idx = calculate_end_idx(ctx, body_line_idx);
888✔
1021
        log_debug(__FILE__, MAIN, "remove_vertical_from_input(): body_line_idx=%d, input_line_idx=%d, s_idx=%d, "
888✔
1022
                "e_idx=%d, input.indent=%d\n", (int) body_line_idx, (int) input_line_idx, (int) s_idx, (int) e_idx,
1023
                (int) input.indent);
888✔
1024

1025
        bxstr_t *temp2 = bxs_substr(org_line, s_idx, e_idx);
888✔
1026
        if (opt.indentmode == 'b' || opt.indentmode == '\0') {
1,775!
1027
            /* restore indentation */
1028
            bxstr_t *temp = bxs_prepend_spaces(temp2, input.indent);
887✔
1029
            free_line_text(input.lines + input_line_idx);
887✔
1030
            input.lines[input_line_idx].text = temp;
887✔
1031
            bxs_free(temp2);
887✔
1032
        }
1033
        else {
1034
            /* remove indentation */
1035
            free_line_text(input.lines + input_line_idx);
1✔
1036
            input.lines[input_line_idx].text = temp2;
1✔
1037
        }
1038
    }
1039
}
160✔
1040

1041

1042

1043
static void remove_bottom_from_input(remove_ctx_t *ctx)
160✔
1044
{
1045
    if (ctx->bottom_end_idx > ctx->bottom_start_idx) {
160✔
1046
        for (size_t j = ctx->bottom_start_idx; j < ctx->bottom_end_idx; ++j) {
579✔
1047
            free_line(input.lines + j);
440✔
1048
        }
1049
        if (ctx->bottom_end_idx < input.num_lines) {
139✔
1050
            memmove(input.lines + ctx->bottom_start_idx, input.lines + ctx->bottom_end_idx,
1✔
1051
                    (input.num_lines - ctx->bottom_end_idx) * sizeof(line_t));
1✔
1052
        }
1053
        input.num_lines -= ctx->bottom_end_idx - ctx->bottom_start_idx;
139✔
1054
    }
1055
}
160✔
1056

1057

1058

1059
static void remove_default_padding(remove_ctx_t *ctx, int num_blanks)
4✔
1060
{
1061
    if (num_blanks > 0) {
4✔
1062
        for (size_t body_line_idx = 0; body_line_idx < ctx->body_num_lines; body_line_idx++) {
12✔
1063
            size_t input_line_idx = ctx->top_start_idx + body_line_idx; /* top_start_idx, because top was removed! */
10✔
1064
            bxstr_t *temp = bxs_cut_front(input.lines[input_line_idx].text, (size_t) num_blanks);
10✔
1065
            free_line_text(input.lines + input_line_idx);
10✔
1066
            input.lines[input_line_idx].text = temp;
10✔
1067
        }
1068
        input.indent -= (size_t) num_blanks;
2✔
1069
        input.maxline -= (size_t) num_blanks;
2✔
1070
    }
1071
}
4✔
1072

1073

1074

1075
static void apply_results_to_input(remove_ctx_t *ctx)
160✔
1076
{
1077
    remove_vertical_from_input(ctx);
160✔
1078

1079
    if (opt.killblank || opt.mend) {
160!
1080
        killblank(ctx);
160✔
1081
    }
1082
    remove_bottom_from_input(ctx);
160✔
1083
    remove_top_from_input(ctx);
160✔
1084

1085
    input.maxline = 0;
160✔
1086
    input.indent = SIZE_MAX;
160✔
1087
    for (size_t j = 0; j < input.num_lines; ++j) {
951✔
1088
        if (input.lines[j].text->num_columns > input.maxline) {
791✔
1089
            input.maxline = input.lines[j].text->num_columns;
326✔
1090
        }
1091
        if (input.lines[j].text->indent < input.indent) {
791✔
1092
            input.indent = input.lines[j].text->indent;
161✔
1093
        }
1094
    }
1095
    if (ctx->empty_side[BLEF]) {
160✔
1096
        /* If the side were not open, default padding would have been removed when the side was removed. */
1097
        remove_default_padding(ctx, BMIN((int) input.indent, opt.design->padding[BLEF]));
4✔
1098
    }
1099

1100
    size_t num_lines_removed = BMAX(ctx->top_end_idx - ctx->top_start_idx, (size_t) 0)
160✔
1101
            + BMAX(ctx->bottom_end_idx - ctx->bottom_start_idx, (size_t) 0);
160✔
1102
    memset(input.lines + input.num_lines, 0, num_lines_removed * sizeof(line_t));
160✔
1103

1104
    if (is_debug_logging(MAIN)) {
160!
1105
        print_input_lines(" (remove_box) after box removal");
×
1106
        log_debug(__FILE__, MAIN, "Number of lines shrunk by %d.\n", (int) num_lines_removed);
×
1107
    }
1108
}
160✔
1109

1110

1111

1112
int remove_box()
161✔
1113
{
1114
    detect_design_if_needed();
161✔
1115

1116
    remove_ctx_t *ctx = (remove_ctx_t *) calloc(1, sizeof(remove_ctx_t));
160✔
1117
    ctx->empty_side[BTOP] = empty_side(opt.design->shape, BTOP);
160✔
1118
    ctx->empty_side[BRIG] = empty_side(opt.design->shape, BRIG);
160✔
1119
    ctx->empty_side[BBOT] = empty_side(opt.design->shape, BBOT);
160✔
1120
    ctx->empty_side[BLEF] = empty_side(opt.design->shape, BLEF);
160✔
1121
    log_debug(__FILE__, MAIN, "Empty sides? Top: %d, Right: %d, Bottom: %d, Left: %d\n",
160✔
1122
            ctx->empty_side[BTOP], ctx->empty_side[BRIG], ctx->empty_side[BBOT], ctx->empty_side[BLEF]);
1123

1124
    ctx->design_is_mono = design_is_mono(opt.design);
160✔
1125
    ctx->input_is_mono = input_is_mono();
160✔
1126

1127
    ctx->top_start_idx = find_first_line();
160✔
1128
    if (ctx->top_start_idx >= input.num_lines) {
160!
1129
        return 0;  /* all lines were already blank, so there is no box to remove */
×
1130
    }
1131

1132
    if (ctx->empty_side[BTOP]) {
160✔
1133
        ctx->top_end_idx = ctx->top_start_idx;
24✔
1134
    }
1135
    else {
1136
        ctx->top_end_idx = find_top_side(ctx);
136✔
1137
    }
1138
    log_debug(__FILE__, MAIN, "ctx->top_start_idx = %d, ctx->top_end_idx = %d\n", (int) ctx->top_start_idx,
160✔
1139
            (int) ctx->top_end_idx);
160✔
1140

1141
    ctx->bottom_end_idx = find_last_line() + 1;
160✔
1142
    if (ctx->empty_side[BBOT]) {
160✔
1143
        ctx->bottom_start_idx = ctx->bottom_end_idx;
22✔
1144
    }
1145
    else {
1146
        ctx->bottom_start_idx = find_bottom_side(ctx);
138✔
1147
    }
1148
    log_debug(__FILE__, MAIN, "ctx->bottom_start_idx = %d, ctx->bottom_end_idx = %d\n", (int) ctx->bottom_start_idx,
160✔
1149
            (int) ctx->bottom_end_idx);
160✔
1150
    if (ctx->bottom_start_idx > ctx->top_end_idx) {
160!
1151
        ctx->body_num_lines = ctx->bottom_start_idx - ctx->top_end_idx;
160✔
1152
    }
1153

1154
    if (ctx->body_num_lines > 0) {
160!
1155
        ctx->body = (line_ctx_t *) calloc(ctx->body_num_lines, sizeof(line_ctx_t));
160✔
1156
        find_vertical_shapes(ctx);
160✔
1157
    }
1158

1159
    debug_print_remove_ctx(ctx, "before apply_results_to_input()");
160✔
1160
    apply_results_to_input(ctx);
160✔
1161

1162
    if (ctx->body != NULL) {
160!
1163
        for (size_t i = 0; i < ctx->body_num_lines; i++) {
947✔
1164
            BFREE(ctx->body[i].input_line_used);
787✔
1165
        }
1166
        BFREE(ctx->body);
160!
1167
    }
1168
    BFREE(ctx);
160!
1169
    return 0;
160✔
1170
}
1171

1172

1173

1174
void output_input(const int trim_only)
160✔
1175
{
1176
    size_t indent;
1177
    int ntabs, nspcs;
1178

1179
    log_debug(__FILE__, MAIN, "output_input() - enter (trim_only=%d)\n", trim_only);
160✔
1180

1181
    for (size_t j = 0; j < input.num_lines; ++j) {
951✔
1182
        if (input.lines[j].text == NULL) {
791!
1183
            continue;
×
1184
        }
1185
        bxstr_t *temp = bxs_rtrim(input.lines[j].text);
791✔
1186
        bxs_free(input.lines[j].text);
791✔
1187
        input.lines[j].text = temp;
791✔
1188
        if (trim_only) {
791✔
1189
            continue;
388✔
1190
        }
1191

1192
        char *indentspc = NULL;
403✔
1193
        if (opt.tabexp == 'u') {
403✔
1194
            indent = input.lines[j].text->indent;
4✔
1195
            ntabs = indent / opt.tabstop;
4✔
1196
            nspcs = indent % opt.tabstop;
4✔
1197
            indentspc = (char *) malloc(ntabs + nspcs + 1);
4✔
1198
            if (indentspc == NULL) {
4!
1199
                perror(PROJECT);
×
1200
                return;
×
1201
            }
1202
            memset(indentspc, (int) '\t', ntabs);
4✔
1203
            memset(indentspc + ntabs, (int) ' ', nspcs);
4✔
1204
            indentspc[ntabs + nspcs] = '\0';
4✔
1205
        }
1206
        else if (opt.tabexp == 'k') {
399!
1207
            uint32_t *indent32 = tabbify_indent(j, NULL, input.indent);
×
1208
            indentspc = u32_strconv_to_output(indent32);
×
1209
            BFREE(indent32);
×
1210
            indent = input.indent;
×
1211
        }
1212
        else {
1213
            indentspc = (char *) strdup("");
399✔
1214
            indent = 0;
399✔
1215
        }
1216

1217
        char *outtext = u32_strconv_to_output(bxs_first_char_ptr(input.lines[j].text, indent));
403✔
1218
        fprintf(opt.outfile, "%s%s%s", indentspc, outtext,
403✔
1219
                (input.final_newline || j < input.num_lines - 1 ? opt.eol : ""));
403!
1220
        BFREE(outtext);
403!
1221
        BFREE(indentspc);
403!
1222
    }
1223
}
1224

1225

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