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

ascii-boxes / boxes / 11084497071

28 Sep 2024 01:00PM UTC coverage: 87.312% (-1.6%) from 88.939%
11084497071

push

github

tsjensen
Remove unused function array_contains() from 'tools' module

3132 of 3809 branches covered (82.23%)

Branch coverage included in aggregate %.

5091 of 5609 relevant lines covered (90.76%)

175335.5 hits per line

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

86.68
/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)
320✔
104
{
105
    if (is_debug_logging(MAIN)) {
320!
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
}
320✔
145

146

147

148
static void debug_print_shapes_relevant(shape_line_ctx_t *shapes_relevant)
1,590✔
149
{
150
    if (is_debug_logging(MAIN)) {
1,590!
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
}
1,590✔
169

170

171

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

184

185

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

198

199

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

208

209

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

221

222

223
static int is_blank_between(uint32_t *start, uint32_t *end)
768✔
224
{
225
    for (uint32_t *p = start; p < end; p++) {
12,480✔
226
        if (!is_blank(*p)) {
11,716✔
227
            return 0;
4✔
228
        }
229
    }
5,856✔
230
    return 1;
764✔
231
}
384✔
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)
17,200✔
245
{
246
    if (shape_line_ctx == NULL || shape_line_ctx->text == NULL || quality == NULL
17,200✔
247
            || *quality > shape_line_ctx->text->num_chars) {
17,195✔
248
        return NULL;
8✔
249
    }
250

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

280
    uint32_t *result = NULL;
17,192✔
281
    size_t new_quality = e - s;
17,192✔
282
    if (new_quality < *quality) {
17,192✔
283
        result = u32_strdup(s);
12,752✔
284
        set_char_at(result, new_quality, char_nul);
12,752✔
285
        *quality = new_quality;
12,752✔
286
    }
6,376✔
287
    return result;
17,192✔
288
}
8,600✔
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,
37,224✔
312
        int anchored_right)
313
{
314
    if (is_debug_logging(MAIN)) {
37,224✔
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;
37,224✔
324
    if (!anchored_left) {
37,224✔
325
        result = hmm_shiftable(shapes_relevant, cur_pos, shape_idx, end_pos, anchored_right);
794✔
326
    }
397✔
327
    else if (cur_pos > end_pos) {
36,430✔
328
        /* invalid input */
329
        result = 0;
242✔
330
    }
121✔
331
    else if (cur_pos == end_pos) {
36,188✔
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)
639✔
334
                || ((shapes_relevant[shape_idx].empty || bxs_is_blank(shapes_relevant[shape_idx].text))
750!
335
                    && !non_empty_shapes_after(shapes_relevant, shape_idx) ? 1 : 0);
351✔
336
    }
213✔
337
    else if (shape_idx >= SHAPES_PER_SIDE - 1) {
35,762✔
338
        /* no more shapes to try, which is fine if the rest of the line is blank */
339
        result = u32_is_blank(cur_pos);
110✔
340
    }
55✔
341
    else if (shapes_relevant[shape_idx].empty) {
35,652✔
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);
470✔
344
    }
235✔
345
    else {
346
        uint32_t *shape_line = u32_strdup(shapes_relevant[shape_idx].text->memory);
35,182✔
347
        size_t quality = shapes_relevant[shape_idx].text->num_chars;
35,182✔
348
        while (shape_line != NULL && quality > 0) {
71,614✔
349
            if (u32_strncmp(cur_pos, shape_line, quality) == 0) {
36,432✔
350
                BFREE(shape_line);
33,664✔
351
                cur_pos = cur_pos + quality;
33,664✔
352
                if (cur_pos == end_pos && !non_empty_shapes_after(shapes_relevant, shape_idx)) {
33,664✔
353
                    result = 1; /* success */
1,094✔
354
                }
547✔
355
                else {
356
                    int rc = 0;
32,570✔
357
                    if (shapes_relevant[shape_idx].elastic) {
32,570✔
358
                        rc = hmm(shapes_relevant, cur_pos, shape_idx, end_pos, 1, anchored_right);
32,098✔
359
                    }
16,049✔
360
                    if (rc == 0) {
32,570✔
361
                        result = hmm(shapes_relevant, cur_pos, shape_idx + 1, end_pos, 1, anchored_right);
2,304✔
362
                    }
1,152✔
363
                    else {
364
                        result = rc;
30,266✔
365
                    }
366
                }
367
            }
16,832✔
368
            else if (!anchored_right) {
2,768✔
369
                shape_line = shorten(shapes_relevant + shape_idx, &quality, 0, 0, 1);
1,580✔
370
                if (is_debug_logging(MAIN)) {
1,580!
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
            }
790✔
377
            else {
378
                BFREE(shape_line);
1,188✔
379
            }
380
        }
381
    }
382

383
    log_debug(__FILE__, MAIN, "hmm() - exit, result = %d\n", result);
37,224✔
384
    return result;
37,224✔
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,
794✔
390
        int anchored_right)
391
{
392
    int result = 0;
794✔
393
    int shapes_are_empty = 1;
794✔
394
    for (size_t i = shape_idx; i < SHAPES_PER_SIDE - 1; i++) {
1,504✔
395
        if (!is_shape_line_empty(shapes_relevant, i)) {
1,468✔
396
            shapes_are_empty = 0;
758✔
397
            int can_shorten_right = -1;
758✔
398
            size_t quality = shapes_relevant[i].text->num_chars;
758✔
399
            uint32_t *shape_line = shapes_relevant[i].text->memory;
758✔
400
            while (shape_line != NULL) {
1,398✔
401
                uint32_t *p = u32_strstr(cur_pos, shape_line);
1,370✔
402
                if (p != NULL && p < end_pos && is_blank_between(cur_pos, p)) {
1,370!
403
                    result = hmm(shapes_relevant, p + quality, i + (shapes_relevant[i].elastic ? 0 : 1),
1,095✔
404
                            end_pos, 1, anchored_right);
365✔
405
                    if (result == 0 && shapes_relevant[i].elastic) {
730!
406
                        result = hmm(shapes_relevant, p + quality, i + 1, end_pos, 1, anchored_right);
8✔
407
                    }
4✔
408
                    break;
730✔
409
                }
410
                if (can_shorten_right == -1) {
640✔
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)
526✔
413
                            || !is_shape_line_empty(shapes_relevant, SHAPES_PER_SIDE - 1) ? 0 : 1;
264✔
414
                }
132✔
415
                shape_line = shorten(shapes_relevant + i, &quality, 0, 1, can_shorten_right);
640✔
416
            }
417
            break;
758✔
418
        }
419
    }
355✔
420
    if (shapes_are_empty) {
794✔
421
        /* all shapes were empty, which is fine if line was blank */
422
        result = is_blank_between(cur_pos, end_pos);
36✔
423
    }
18✔
424
    return result;
794✔
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)
1,590✔
430
{
431
    shape_t *side_shapes = hside == BTOP ? north_side : south_side_rev;
1,590✔
432
    shape_line_ctx_t *shapes_relevant = (shape_line_ctx_t *) calloc(SHAPES_PER_SIDE, sizeof(shape_line_ctx_t));
1,590✔
433

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

445
    return shapes_relevant;
1,590✔
446
}
447

448

449

450
static match_result_t *new_match_result(uint32_t *p, size_t p_idx, size_t len, int shiftable)
3,028✔
451
{
452
    match_result_t *result = (match_result_t *) calloc(1, sizeof(match_result_t));
3,028✔
453
    result->p = p;
3,028✔
454
    result->p_idx = p_idx;
3,028✔
455
    result->len = len;
3,028✔
456
    result->shiftable = shiftable;
3,028✔
457
    return result;
3,028✔
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)
3,054✔
472
{
473
    if (input_line == NULL || input_line->num_chars == 0 || shape_line == NULL || shape_line->num_chars == 0) {
3,054!
474
        return NULL;
8✔
475
    }
476

477
    if (vside == BLEF) {
3,046✔
478
        if (bxs_is_blank(shape_line)) {
1,538✔
479
            return new_match_result(input_line->memory, 0, 0, 1);
718✔
480
        }
481
        for (uint32_t *s = shape_line->memory; s == shape_line->memory || is_blank(*s); s++) {
834✔
482
            uint32_t *p = u32_strstr(input_line->memory, s);
826✔
483
            size_t p_idx = p != NULL ? p - input_line->memory : 0;
826✔
484
            if (p == NULL || p_idx > input_line->first_char[input_line->indent]) {
826✔
485
                continue;  /* not found or found too far in */
14✔
486
            }
487
            return new_match_result(p, p_idx, shape_line->num_chars - (s - shape_line->memory), 0);
812✔
488
        }
489
    }
4✔
490
    else {
491
        if (bxs_is_blank(shape_line)) {
1,508✔
492
            uint32_t *p = bxs_last_char_ptr(input_line);
658✔
493
            size_t p_idx = p - input_line->memory;
658✔
494
            return new_match_result(p, p_idx, 0, 1);
658✔
495
        }
496
        int slen = shape_line->num_chars;
850✔
497
        uint32_t *s = u32_strdup(shape_line->memory);
850✔
498
        for (; slen == (int) shape_line->num_chars || is_blank(s[slen]); slen--) {
870✔
499
            s[slen] = char_nul;
860✔
500
            uint32_t *p = u32_strnrstr(input_line->memory, s, slen);
860✔
501
            size_t p_idx = p != NULL ? p - input_line->memory : 0;
860✔
502
            if (p == NULL || p_idx + slen
860✔
503
                    < input_line->first_char[input_line->num_chars_visible - input_line->trailing]) {
844✔
504
                continue; /* not found or found too far in */
20✔
505
            }
506
            BFREE(s);
840!
507
            return new_match_result(p, p_idx, (size_t) slen, 0);
840✔
508
        }
509
        BFREE(s);
10✔
510
    }
511
    return NULL;
18✔
512
}
1,527✔
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)
1,560✔
517
{
518
    log_debug(__FILE__, MAIN, "match_horiz_line(ctx, %s, %d, %d)\n",
1,560✔
519
                hside == BTOP ? "BTOP" : "BBOT", (int) input_line_idx, (int) shape_line_idx);
780✔
520

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

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

532
        bxstr_t *input_prepped1 = bxs_from_unicode(prepare_comp_input(input_line_idx, 0, comp_type, 0, NULL, NULL));
1,590✔
533
        bxstr_t *input_prepped = bxs_rtrim(input_prepped1);
1,590✔
534
        bxs_append_spaces(input_prepped, opt.design->shape[NW].width + opt.design->shape[NE].width);
1,590✔
535
        bxs_free(input_prepped1);
1,590✔
536
        if (is_debug_logging(MAIN)) {
1,590✔
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;
1,590✔
543
        match_result_t *mrl = NULL;
1,590✔
544
        if (!ctx->empty_side[BLEF]) {
1,590✔
545
            mrl = match_outer_shape(BLEF, input_prepped, shapes_relevant[0].text);
1,526✔
546
            if (mrl != NULL) {
1,526✔
547
                cur_pos = mrl->p + mrl->len;
1,522✔
548
            }
761✔
549
        }
763✔
550

551
        uint32_t *end_pos = bxs_last_char_ptr(input_prepped);
1,590✔
552
        match_result_t *mrr = NULL;
1,590✔
553
        if (!ctx->empty_side[BRIG]) {
1,590✔
554
            mrr = match_outer_shape(BRIG, input_prepped, shapes_relevant[SHAPES_PER_SIDE - 1].text);
1,498✔
555
            if (mrr != NULL) {
1,498✔
556
                end_pos = mrr->p;
1,488✔
557
            }
744✔
558
        }
749✔
559
        if (is_debug_logging(MAIN)) {
1,590✔
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,
3,078✔
571
                (mrr == NULL) || mrr->shiftable ? 0 : 1);
1,539✔
572

573
        BFREE(mrl);
1,590✔
574
        BFREE(mrr);
1,590✔
575
        for (size_t i = 0; i < SHAPES_PER_SIDE; i++) {
9,540✔
576
            bxs_free(shapes_relevant[i].text);
7,950✔
577
        }
3,975✔
578
        BFREE(shapes_relevant);
1,590✔
579

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

587
    return result;
1,560✔
588
}
589

590

591

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

619

620

621
static size_t find_bottom_side(remove_ctx_t *ctx)
276✔
622
{
623
    size_t result = ctx->bottom_end_idx;
276✔
624
    sentry_t *shapes = opt.design->shape;
276✔
625
    for (long input_line_idx = (long) ctx->bottom_end_idx - 1;
1,194✔
626
            input_line_idx >= 0 && input_line_idx >= (long) ctx->bottom_end_idx - (long) shapes[SE].height;
1,056✔
627
            input_line_idx--)
780✔
628
    {
629
        int matched = 0;
780✔
630
        size_t shape_lines_tested = 0;
780✔
631
        for (long shape_line_idx = shapes[SE].height - (ctx->bottom_end_idx - input_line_idx);
1,170!
632
                shape_line_idx >= 0 && shape_lines_tested < shapes[SE].height;
780!
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)) {
780✔
637
                matched = 1;
780✔
638
                break;
780✔
639
            }
640
        }
641
        if (!matched) {
780!
642
            break;
×
643
        }
644
        result = input_line_idx;
780✔
645
    }
390✔
646
    return result;
276✔
647
}
648

649

650

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

662

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

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

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

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

695
    return shape_lines;
584✔
696
}
697

698

699

700
static void free_shape_lines(shape_line_ctx_t **shape_lines)
636✔
701
{
702
    if (shape_lines != NULL) {
636✔
703
        for (shape_line_ctx_t **p = shape_lines; *p != NULL; p++) {
1,708✔
704
            bxs_free((*p)->text);
1,124✔
705
            BFREE(*p);
1,124!
706
        }
562✔
707
        BFREE(shape_lines);
584!
708
    }
292✔
709
}
636✔
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,
3,232✔
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);
3,232✔
717

718
    for (shape_line_ctx_t **shape_line_ctx = shape_lines; *shape_line_ctx != NULL; shape_line_ctx++) {
10,976✔
719
        if ((*shape_line_ctx)->empty) {
6,424!
720
            continue;
×
721
        }
722

723
        size_t max_quality = (*shape_line_ctx)->text->num_chars;
6,424✔
724
        size_t quality = max_quality;
6,424✔
725
        uint32_t *shape_text = (*shape_line_ctx)->text->memory;
6,424✔
726
        uint32_t *to_free = NULL;
6,424✔
727
        while(shape_text != NULL) {
23,030✔
728
            uint32_t *p;
729
            if (vside == BLEF) {
18,578✔
730
                p = u32_strstr(input_line, shape_text);
8,338✔
731
            }
4,169✔
732
            else {
733
                p = u32_strnrstr(input_line, shape_text, quality);
10,240✔
734
            }
735
            BFREE(to_free);
18,578✔
736
            shape_text = NULL;
18,578✔
737

738
            if ((p == NULL)
20,246✔
739
                    || (vside == BLEF && ((size_t) (p - input_line) > input_indent + (max_quality - quality)))
12,117✔
740
                    || (vside == BRIG && ((size_t) (p - input_line) < input_length - input_trailing - quality))) {
5,496✔
741
                shape_text = shorten(*shape_line_ctx, &quality, vside == BLEF, 1, 1);
16,258✔
742
                to_free = shape_text;
16,258✔
743
            }
8,789✔
744
            else if (vside == BLEF) {
3,640✔
745
                if (quality > line_ctx->west_quality) {
2,000✔
746
                    line_ctx->west_start = (size_t) (p - input_line);
1,784✔
747
                    line_ctx->west_end = line_ctx->west_start + quality;
1,784✔
748
                    line_ctx->west_quality = quality;
1,784✔
749
                    BFREE(line_ctx->input_line_used);
1,784✔
750
                    line_ctx->input_line_used = u32_strdup(input_line);
1,784✔
751
                    break;
1,784✔
752
                }
753
            }
108✔
754
            else if (vside == BRIG) {
1,640✔
755
                if (quality > line_ctx->east_quality) {
1,640✔
756
                    line_ctx->east_start = (size_t) (p - input_line);
1,508✔
757
                    line_ctx->east_end = line_ctx->east_start + quality;
1,508✔
758
                    line_ctx->east_quality = quality;
1,508✔
759
                    BFREE(line_ctx->input_line_used);
1,508✔
760
                    line_ctx->input_line_used = u32_strdup(input_line);
1,508✔
761
                    break;
1,508✔
762
                }
763
            }
66✔
764
        }
765
    }
3,872✔
766
}
4,552✔
767

768

769

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

780
    size_t max_quality = 0;
318✔
781
    if (!ctx->empty_side[BLEF]) {
318✔
782
        max_quality += opt.design->shape[NW].width;
314✔
783
    }
157✔
784
    if (!ctx->empty_side[BRIG]) {
318✔
785
        max_quality += opt.design->shape[NE].width;
270✔
786
    }
135✔
787
    max_quality = max_quality * num_body_lines;
318✔
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)
318!
791
            || (max_quality > 0 && (total_quality > 0.5 * max_quality));
318!
792
    log_debug(__FILE__, MAIN, "sufficient_body_quality() found body match quality of %d/%d (%s).\n",
318✔
793
            (int) total_quality, (int) max_quality, sufficient ? "sufficient" : "NOT sufficient");
159✔
794

795
    return sufficient;
318✔
796
}
797

798

799

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

810

811

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

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

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

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

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

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

856
        if (sufficient_body_quality(ctx)) {
318✔
857
            break;
304✔
858
        }
859
    }
7✔
860
}
160✔
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()
322✔
869
{
870
    if (opt.design_choice_by_user == 0) {
322✔
871
        design_t *tmp = autodetect_design();
16✔
872
        if (tmp) {
16✔
873
            opt.design = tmp;
14✔
874
            log_debug(__FILE__, MAIN, "Design autodetection: Removing box of design \"%s\".\n", opt.design->name);
14✔
875
        }
7✔
876
        else {
877
            fprintf(stderr, "%s: Box design autodetection failed. Use -d option.\n", PROJECT);
2✔
878
            exit(EXIT_FAILURE);
2✔
879
        }
880
    }
7✔
881
    else {
882
        log_debug(__FILE__, MAIN, "Design was chosen by user: %s\n", opt.design->name);
306✔
883
    }
884
}
320✔
885

886

887

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

895

896

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

904

905

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

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

931

932

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

941

942

943
static size_t max_chars_line(bxstr_t *org_line, comparison_t comp_type)
2,030✔
944
{
945
    return (comp_type == literal || comp_type == ignore_invisible_shape)
1,032✔
946
            ? org_line->num_chars : org_line->num_chars_visible;
2,047✔
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)
1,732✔
952
{
953
    size_t result = 0;
1,732✔
954
    size_t max_chars = max_chars_line(org_line, comp_type);
1,732✔
955
    for (size_t i = start_idx; i < BMIN(max_chars, start_idx + num_padding); i++) {
3,652✔
956
        if (org_is_not_blank(org_line, comp_type, i)) {
1,920!
957
            break;
×
958
        }
959
        result++;
1,920✔
960
    }
960✔
961
    return result;
1,732✔
962
}
963

964

965

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

978

979

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

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

997

998

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

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

1012

1013

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

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

1041

1042

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

1057

1058

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

1073

1074

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

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

1085
    input.maxline = 0;
320✔
1086
    input.indent = SIZE_MAX;
320✔
1087
    for (size_t j = 0; j < input.num_lines; ++j) {
1,902✔
1088
        if (input.lines[j].text->num_columns > input.maxline) {
1,582✔
1089
            input.maxline = input.lines[j].text->num_columns;
652✔
1090
        }
326✔
1091
        if (input.lines[j].text->indent < input.indent) {
1,582✔
1092
            input.indent = input.lines[j].text->indent;
322✔
1093
        }
161✔
1094
    }
791✔
1095
    if (ctx->empty_side[BLEF]) {
320✔
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]));
8!
1098
    }
4✔
1099

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

1104
    if (is_debug_logging(MAIN)) {
320!
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
}
320✔
1109

1110

1111

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

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

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

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

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

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

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

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

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

1172

1173

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

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

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

1192
        char *indentspc = NULL;
806✔
1193
        if (opt.tabexp == 'u') {
806✔
1194
            indent = input.lines[j].text->indent;
8✔
1195
            ntabs = indent / opt.tabstop;
8✔
1196
            nspcs = indent % opt.tabstop;
8✔
1197
            indentspc = (char *) malloc(ntabs + nspcs + 1);
8✔
1198
            if (indentspc == NULL) {
8!
1199
                perror(PROJECT);
×
1200
                return;
×
1201
            }
1202
            memset(indentspc, (int) '\t', ntabs);
8✔
1203
            memset(indentspc + ntabs, (int) ' ', nspcs);
8✔
1204
            indentspc[ntabs + nspcs] = '\0';
8✔
1205
        }
4✔
1206
        else if (opt.tabexp == 'k') {
798!
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("");
798✔
1214
            indent = 0;
798✔
1215
        }
1216

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