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

ascii-boxes / boxes / 6591907577

20 Oct 2023 06:56PM UTC coverage: 83.808% (+0.7%) from 83.13%
6591907577

push

github

tsjensen
fix ignore_invisible_all

2389 of 3136 branches covered (0.0%)

Branch coverage included in aggregate %.

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

3791 of 4238 relevant lines covered (89.45%)

8126.28 hits per line

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

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

16
/*
17
 * 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 "remove.h"
32
#include "shape.h"
33
#include "tools.h"
34
#include "unicode.h"
35

36

37

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

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

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

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

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

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

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

63

64

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

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

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

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

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

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

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

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

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

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

100

101

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

148

149

150
static void debug_print_shapes_relevant(shape_line_ctx_t *shapes_relevant)
64✔
151
{
152
    #ifdef DEBUG
153
        fprintf(stderr, "  shapes_relevant = {");
154
        for (size_t ds = 0; ds < SHAPES_PER_SIDE; ds++) {
155
            if (shapes_relevant[ds].empty) {
156
                fprintf(stderr, "-");
157
            }
158
            else {
159
                char *out_shp_text = bxs_to_output(shapes_relevant[ds].text);
160
                fprintf(stderr, "\"%s\"(%d%s)", out_shp_text, (int) shapes_relevant[ds].text->num_chars,
161
                    shapes_relevant[ds].elastic ? "E" : "");
162
                BFREE(out_shp_text);
163
            }
164
            if (ds < SHAPES_PER_SIDE - 1) {
165
                fprintf(stderr, ", ");
166
            }
167
        }
168
        fprintf(stderr, "}\n");
169
    #else
170
        UNUSED(shapes_relevant);
171
    #endif
172
}
64✔
173

174

175

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

188

189

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

202

203

204
static int is_shape_line_empty(shape_line_ctx_t *shapes_relevant, size_t shape_idx)
136✔
205
{
206
    if (shape_idx < SHAPES_PER_SIDE) {
136!
207
        return shapes_relevant[shape_idx].empty || bxs_is_blank(shapes_relevant[shape_idx].text);
136✔
208
    }
209
    return 1;
×
210
}
211

212

213

214
static int non_empty_shapes_after(shape_line_ctx_t *shapes_relevant, size_t shape_idx)
89✔
215
{
216
    for (size_t i = shape_idx + 1; i < SHAPES_PER_SIDE - 1; i++) {
134✔
217
        if (!is_shape_line_empty(shapes_relevant, i)) {
52✔
218
            return 1;
7✔
219
        }
220
    }
221
    return 0;
82✔
222
}
223

224

225

226
static int is_blank_between(uint32_t *start, uint32_t *end)
19✔
227
{
228
    for (uint32_t *p = start; p < end; p++) {
76✔
229
        if (!is_blank(*p)) {
58✔
230
            return 0;
1✔
231
        }
232
    }
233
    return 1;
18✔
234
}
235

236

237

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

254
    uint32_t *s = shape_line_ctx->text->memory;
592✔
255
    uint32_t *e = shape_line_ctx->text->memory + shape_line_ctx->text->num_chars;
592✔
256
    prefer_left = allow_left ? prefer_left : 0;
592✔
257
    size_t reduction_steps = shape_line_ctx->text->num_chars - *quality + 1;
592✔
258
    for (size_t i = 0; i < reduction_steps; i++) {
1,564✔
259
        if (prefer_left) {
1,222✔
260
            if (s < e && is_blank(*s)) {
450!
261
                s++;
110✔
262
            }
263
            else if (e > s && allow_right && is_blank(*(e - 1))) {
340!
264
                e--;
227✔
265
            }
266
            else {
267
                break;
268
            }
269
        }
270
        else {
271
            if (e > s && allow_right && is_blank(*(e - 1))) {
772!
272
                e--;
388✔
273
            }
274
            else if (s < e && allow_left && is_blank(*s)) {
384!
275
                s++;
247✔
276
            }
277
            else {
278
                break;
279
            }
280
        }
281
    }
282

283
    uint32_t *result = NULL;
592✔
284
    size_t new_quality = e - s;
592✔
285
    if (new_quality < *quality) {
592✔
286
        result = u32_strdup(s);
342✔
287
        set_char_at(result, new_quality, char_nul);
342✔
288
        *quality = new_quality;
342✔
289
    }
290
    return result;
592✔
291
}
292

293

294

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

298

299

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

326
    int result = 0;
872✔
327
    if (!anchored_left) {
872✔
328
        result = hmm_shiftable(shapes_relevant, cur_pos, shape_idx, end_pos, anchored_right);
31✔
329
    }
330
    else if (cur_pos > end_pos) {
841✔
331
        /* invalid input */
332
        result = 0;
13✔
333
    }
334
    else if (cur_pos == end_pos) {
828✔
335
        /* we are at the end, which is fine if there is nothing else to match */
336
        result = (shapes_relevant[shape_idx].empty || bxs_is_blank(shapes_relevant[shape_idx].text))
46✔
337
                && !non_empty_shapes_after(shapes_relevant, shape_idx) ? 1 : 0;
46!
338
    }
339
    else if (shape_idx >= SHAPES_PER_SIDE - 1) {
804✔
340
        /* no more shapes to try, which is fine if the rest of the line is blank */
341
        result = u32_is_blank(cur_pos);
1✔
342
    }
343
    else if (shapes_relevant[shape_idx].empty) {
803✔
344
        /* the current shape line is empty, try the next one */
345
        result = hmm(shapes_relevant, cur_pos, shape_idx + 1, end_pos, 1, anchored_right);
14✔
346
    }
347
    else {
348
        uint32_t *shape_line = u32_strdup(shapes_relevant[shape_idx].text->memory);
789✔
349
        size_t quality = shapes_relevant[shape_idx].text->num_chars;
789✔
350
        while (shape_line != NULL) {
1,590✔
351
            if (u32_strncmp(cur_pos, shape_line, quality) == 0) {
801✔
352
                BFREE(shape_line);
711!
353
                cur_pos = cur_pos + quality;
711✔
354
                if (cur_pos == end_pos && !non_empty_shapes_after(shapes_relevant, shape_idx)) {
711✔
355
                    result = 1; /* success */
44✔
356
                }
357
                else {
358
                    int rc = 0;
667✔
359
                    if (shapes_relevant[shape_idx].elastic) {
667✔
360
                        rc = hmm(shapes_relevant, cur_pos, shape_idx, end_pos, 1, anchored_right);
637✔
361
                    }
362
                    if (rc == 0) {
667✔
363
                        result = hmm(shapes_relevant, cur_pos, shape_idx + 1, end_pos, 1, anchored_right);
129✔
364
                    }
365
                    else {
366
                        result = rc;
538✔
367
                    }
368
                }
369
            }
370
            else if (!anchored_right) {
90✔
371
                shape_line = shorten(shapes_relevant + shape_idx, &quality, 0, 0, 1);
15✔
372
            }
373
            else {
374
                BFREE(shape_line);
75!
375
            }
376
        }
377
    }
378

379
    #ifdef DEBUG
380
        fprintf(stderr, "hmm() - exit, result = %d\n", result);
381
    #endif
382
    return result;
872✔
383
}
384

385

386

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

425

426

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

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

443
    return shapes_relevant;
64✔
444
}
445

446

447

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

458

459

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

475
    if (vside == BLEF) {
141✔
476
        if (bxs_is_blank(shape_line)) {
70✔
477
            return new_match_result(input_line->memory, 0, 0, 1);
27✔
478
        }
479
        for (uint32_t *s = shape_line->memory; s == shape_line->memory || is_blank(*s); s++) {
50✔
480
            uint32_t *p = u32_strstr(input_line->memory, s);
46✔
481
            size_t p_idx = p != NULL ? p - input_line->memory : 0;
46✔
482
            if (p == NULL || p_idx > input_line->first_char[input_line->indent]) {
46✔
483
                continue;  /* not found or found too far in */
7✔
484
            }
485
            return new_match_result(p, p_idx, shape_line->num_chars - (s - shape_line->memory), 0);
39✔
486
        }
487
    }
488
    else {
489
        if (bxs_is_blank(shape_line)) {
71✔
490
            uint32_t *p = bxs_last_char_ptr(input_line);
27✔
491
            size_t p_idx = p - input_line->memory;
27✔
492
            return new_match_result(p, p_idx, 0, 1);
27✔
493
        }
494
        int slen = shape_line->num_chars;
44✔
495
        uint32_t *s = u32_strdup(shape_line->memory);
44✔
496
        for (; slen == (int) shape_line->num_chars || is_blank(s[slen]); slen--) {
53✔
497
            s[slen] = char_nul;
49✔
498
            uint32_t *p = u32_strnrstr(input_line->memory, s, slen);
49✔
499
            size_t p_idx = p != NULL ? p - input_line->memory : 0;
49✔
500
            if (p == NULL || p_idx + slen
49✔
501
                    < input_line->first_char[input_line->num_chars_visible - input_line->trailing]) {
41✔
502
                continue; /* not found or found too far in */
9✔
503
            }
504
            BFREE(s);
40!
505
            return new_match_result(p, p_idx, (size_t) slen, 0);
40✔
506
        }
507
        BFREE(s);
4!
508
    }
509
    return NULL;
8✔
510
}
511

512

513

514
static int match_horiz_line(remove_ctx_t *ctx, int hside, size_t input_line_idx, size_t shape_line_idx)
50✔
515
{
516
    #ifdef DEBUG
517
        fprintf(stderr, "match_horiz_line(ctx, %s, %d, %d)\n",
518
                hside == BTOP ? "BTOP" : "BBOT", (int) input_line_idx, (int) shape_line_idx);
519
    #endif
520

521
    int result = 0;
50✔
522
    for (comparison_t comp_type = 0; comp_type < NUM_COMPARISON_TYPES; comp_type++) {
104!
523
        if (!comp_type_is_viable(comp_type, ctx->input_is_mono, ctx->design_is_mono)) {
104✔
524
            continue;
40✔
525
        }
526
        ctx->comp_type = comp_type;
64✔
527
        #ifdef DEBUG
528
            fprintf(stderr, "  Setting comparison type to: %s\n", comparison_name[comp_type]);
529
        #endif
530

531
        shape_line_ctx_t *shapes_relevant = prepare_comp_shapes_horiz(hside, comp_type, shape_line_idx);
64✔
532
        debug_print_shapes_relevant(shapes_relevant);
64✔
533

534
        uint32_t *cur_pos = NULL;
64✔
535
        bxstr_t *input_prepped1 = bxs_from_unicode(prepare_comp_input(input_line_idx, 0, comp_type, 0, NULL, NULL));
64✔
536
        bxstr_t *input_prepped = bxs_rtrim(input_prepped1);
64✔
537
        bxs_free(input_prepped1);
64✔
538
        #ifdef DEBUG
539
            char *out_input_prepped = bxs_to_output(input_prepped);
540
            fprintf(stderr, "  input_prepped = \"%s\"\n", out_input_prepped);
541
            BFREE(out_input_prepped);
542
        #endif
543
        match_result_t *mrl = match_outer_shape(BLEF, input_prepped, shapes_relevant[0].text);
64✔
544
        if (mrl != NULL) {
64✔
545
            cur_pos = mrl->p + mrl->len;
62✔
546
        }
547

548
        uint32_t *end_pos = NULL;
64✔
549
        match_result_t *mrr = match_outer_shape(BRIG, input_prepped, shapes_relevant[SHAPES_PER_SIDE - 1].text);
64✔
550
        if (mrr != NULL) {
64✔
551
            end_pos = mrr->p;
62✔
552
        }
553
        #ifdef DEBUG
554
            char *out_cur_pos = u32_strconv_to_output(cur_pos);
555
            char *out_end_pos = u32_strconv_to_output(end_pos);
556
            fprintf(stderr, "  cur_pos = \"%s\" (index %d)\n", out_cur_pos, (int) BMAX(cur_pos - input_prepped->memory, 0));
557
            fprintf(stderr, "  end_pos = \"%s\" (index %d)\n", out_end_pos, (int) BMAX(end_pos - input_prepped->memory, 0));
558
            BFREE(out_cur_pos);
559
            BFREE(out_end_pos);
560
        #endif
561

562
        if (cur_pos && end_pos) {
64!
563
            result = hmm(shapes_relevant, cur_pos, 1, end_pos, mrl->shiftable ? 0 : 1, mrr->shiftable ? 0 : 1);
62✔
564
        }
565

566
        BFREE(mrl);
64✔
567
        BFREE(mrr);
64✔
568
        for (size_t i = 0; i < SHAPES_PER_SIDE; i++) {
384✔
569
            bxs_free(shapes_relevant[i].text);
320✔
570
        }
571
        BFREE(shapes_relevant);
64!
572

573
        if (result) {
64✔
574
            #ifdef DEBUG
575
                fprintf(stderr, "Matched %s side line using comp_type=%s and shape_line_idx=%d\n",
576
                    hside == BTOP ? "top" : "bottom", comparison_name[comp_type], (int) shape_line_idx);
577
            #endif
578
            break;
50✔
579
        }
580
    }
581

582
    return result;
50✔
583
}
584

585

586

587
static size_t find_top_side(remove_ctx_t *ctx)
9✔
588
{
589
    size_t result = ctx->top_start_idx;
9✔
590
    sentry_t *shapes = opt.design->shape;
9✔
591
    for (size_t input_line_idx = ctx->top_start_idx, shape_line_count = 0;
9✔
592
            input_line_idx < input.num_lines && shape_line_count < shapes[NE].height;
34!
593
            input_line_idx++, shape_line_count++)
25✔
594
    {
595
        int matched = 0;
25✔
596
        for (size_t shape_line_idx = input_line_idx - ctx->top_start_idx;;
25✔
597
                shape_line_idx = (shape_line_idx + 1) % shapes[NE].height)
×
598
        {
599
            if (match_horiz_line(ctx, BTOP, input_line_idx, shape_line_idx)) {
25!
600
                matched = 1;
25✔
601
                break;
25✔
602
            }
603
        }
604
        if (!matched) {
25!
605
            break;
×
606
        }
607
        result = input_line_idx + 1;
25✔
608
    }
609
    return result;
9✔
610
}
611

612

613

614
static size_t find_bottom_side(remove_ctx_t *ctx)
9✔
615
{
616
    size_t result = ctx->top_start_idx;
9✔
617
    sentry_t *shapes = opt.design->shape;
9✔
618
    for (long input_line_idx = (long) ctx->bottom_end_idx - 1, shape_line_count = (long) shapes[SE].height - 1;
9✔
619
            input_line_idx >= 0 && shape_line_count >= 0;
34!
620
            input_line_idx--, shape_line_count--)
25✔
621
    {
622
        int matched = 0;
25✔
623
        for (size_t shape_line_idx = shapes[SE].height - (ctx->bottom_end_idx - input_line_idx);;
25✔
624
                shape_line_idx = (shape_line_idx + 1) % shapes[SE].height)
×
625
        {
626
            if (match_horiz_line(ctx, BBOT, input_line_idx, shape_line_idx)) {
25!
627
                matched = 1;
25✔
628
                break;
25✔
629
            }
630
        }
631
        if (!matched) {
25!
632
            break;
×
633
        }
634
        result = input_line_idx;
25✔
635
    }
636
    return result;
9✔
637
}
638

639

640

641
static size_t count_shape_lines(shape_t side_shapes[])
20✔
642
{
643
    size_t result = 0;
20✔
644
    for (size_t i = 0; i < SHAPES_PER_SIDE - CORNERS_PER_SIDE; i++) {
80✔
645
        if (!isempty(opt.design->shape + side_shapes[i])) {
60✔
646
            result += opt.design->shape[side_shapes[i]].height;
28✔
647
        }
648
    }
649
    return result;
20✔
650
}
651

652

653
static shape_line_ctx_t **prepare_comp_shapes_vert(int vside, comparison_t comp_type)
20✔
654
{
655
    shape_t west_side_shapes[SHAPES_PER_SIDE - CORNERS_PER_SIDE] = {WNW, W, WSW};
20✔
656
    shape_t east_side_shapes[SHAPES_PER_SIDE - CORNERS_PER_SIDE] = {ENE, E, ESE};
20✔
657
    shape_t side_shapes[SHAPES_PER_SIDE - CORNERS_PER_SIDE];
658
    if (vside == BLEF) {
20✔
659
        memcpy(side_shapes, west_side_shapes, (SHAPES_PER_SIDE - CORNERS_PER_SIDE) * sizeof(shape_t));
10✔
660
    }
661
    else {
662
        memcpy(side_shapes, east_side_shapes, (SHAPES_PER_SIDE - CORNERS_PER_SIDE) * sizeof(shape_t));
10✔
663
    }
664

665
    size_t num_shape_lines = count_shape_lines(side_shapes);
20✔
666

667
    shape_line_ctx_t **shape_lines = (shape_line_ctx_t **) calloc(num_shape_lines + 1, sizeof(shape_line_ctx_t *));
20✔
668
    for (size_t i = 0; i < num_shape_lines; i++) {
74✔
669
        shape_lines[i] = (shape_line_ctx_t *) calloc(1, sizeof(shape_line_ctx_t));
54✔
670
    }
671

672
    for (size_t shape_idx = 0, i = 0; shape_idx < SHAPES_PER_SIDE - CORNERS_PER_SIDE; shape_idx++) {
80✔
673
        if (!isempty(opt.design->shape + side_shapes[shape_idx])) {
60✔
674
            int deep_empty = isdeepempty(opt.design->shape + side_shapes[shape_idx]);
28✔
675
            for (size_t slno = 0; slno < opt.design->shape[side_shapes[shape_idx]].height; slno++, i++) {
82✔
676
                uint32_t *s = prepare_comp_shape(opt.design, side_shapes[shape_idx], slno, comp_type, 0, 0);
54✔
677
                shape_lines[i]->text = bxs_from_unicode(s);
54✔
678
                shape_lines[i]->empty = deep_empty;
54✔
679
                shape_lines[i]->elastic = opt.design->shape[side_shapes[shape_idx]].elastic;
54✔
680
                BFREE(s);
54!
681
            }
682
        }
683
    }
684

685
    return shape_lines;
20✔
686
}
687

688

689

690
static void free_shape_lines(shape_line_ctx_t **shape_lines)
20✔
691
{
692
    if (shape_lines != NULL) {
20!
693
        for (shape_line_ctx_t **p = shape_lines; *p != NULL; p++) {
74✔
694
            bxs_free((*p)->text);
54✔
695
            BFREE(*p);
54!
696
        }
697
        BFREE(shape_lines);
20!
698
    }
699
}
20✔
700

701

702

703
static void match_vertical_side(remove_ctx_t *ctx, int vside, shape_line_ctx_t **shape_lines, uint32_t *input_line,
70✔
704
    size_t line_idx, size_t input_length, size_t input_indent, size_t input_trailing)
705
{
706
    line_ctx_t *line_ctx = ctx->body + (line_idx - ctx->top_end_idx);
70✔
707

708
    for (shape_line_ctx_t **shape_line_ctx = shape_lines; *shape_line_ctx != NULL; shape_line_ctx++) {
412✔
709
        if ((*shape_line_ctx)->empty) {
342!
710
            continue;
×
711
        }
712

713
        size_t max_quality = (*shape_line_ctx)->text->num_chars;
342✔
714
        size_t quality = max_quality;
342✔
715
        uint32_t *shape_text = (*shape_line_ctx)->text->memory;
342✔
716
        uint32_t *to_free = NULL;
342✔
717
        while(shape_text != NULL) {
854✔
718
            uint32_t *p;
719
            if (vside == BLEF) {
580✔
720
                p = u32_strstr(input_line, shape_text);
283✔
721
            }
722
            else {
723
                p = u32_strnrstr(input_line, shape_text, quality);
297✔
724
            }
725
            BFREE(to_free);
580✔
726
            shape_text = NULL;
580✔
727

728
            if (p == NULL) {
580✔
729
                shape_text = shorten(*shape_line_ctx, &quality, vside == BLEF, 1, 1);
467✔
730
                to_free = shape_text;
467✔
731
            }
732
            else if (vside == BLEF && ((size_t) (p - input_line) <= input_indent + (max_quality - quality))) {
113✔
733
                if (quality > line_ctx->west_quality) {
56✔
734
                    line_ctx->west_start = (size_t) (p - input_line);
38✔
735
                    line_ctx->west_end = line_ctx->west_start + quality;
38✔
736
                    line_ctx->west_quality = quality;
38✔
737
                    BFREE(line_ctx->input_line_used);
38✔
738
                    line_ctx->input_line_used = u32_strdup(input_line);
38✔
739
                    break;
38✔
740
                }
741
            }
742
            else if (vside == BRIG && ((size_t) (p - input_line) >= input_length - input_trailing - quality)) {
57✔
743
                if (quality > line_ctx->east_quality) {
40✔
744
                    line_ctx->east_start = (size_t) (p - input_line);
30✔
745
                    line_ctx->east_end = line_ctx->east_start + quality;
30✔
746
                    line_ctx->east_quality = quality;
30✔
747
                    BFREE(line_ctx->input_line_used);
30!
748
                    line_ctx->input_line_used = u32_strdup(input_line);
30✔
749
                    break;
30✔
750
                }
751
            }
752
        }
753
    }
754
}
70✔
755

756

757

758
static int sufficient_body_quality(remove_ctx_t *ctx)
10✔
759
{
760
    size_t num_body_lines = ctx->bottom_start_idx - ctx->top_end_idx;
10✔
761
    size_t total_quality = 0;
10✔
762
    line_ctx_t *body = ctx->body;
10✔
763
    for (size_t body_line_idx = 0; body_line_idx < num_body_lines; body_line_idx++) {
45✔
764
        line_ctx_t line_ctx = body[body_line_idx];
35✔
765
        total_quality += line_ctx.east_quality + line_ctx.west_quality;
35✔
766
    }
767

768
    size_t max_quality = (opt.design->shape[NW].width + opt.design->shape[NE].width) * num_body_lines;
10✔
769
    /* If we manage to match 50%, then it is unlikely to improve with a different comparison mode. */
770
    int sufficient = total_quality > 0.5 * max_quality;
10✔
771
    #ifdef DEBUG
772
        fprintf(stderr, "sufficient_body_quality() found body match quality of %d/%d (%s).\n",
773
                (int) total_quality, (int) max_quality, sufficient ? "sufficient" : "NOT sufficient");
774
    #endif
775
    return sufficient;
10✔
776
}
777

778

779

780
static void reset_body(remove_ctx_t *ctx)
1✔
781
{
782
    if (ctx->body != NULL) {
1!
783
        for (size_t i = 0; i < ctx->body_num_lines; i++) {
2✔
784
            BFREE(ctx->body[i].input_line_used);
1!
785
        }
786
        memset(ctx->body, 0, ctx->body_num_lines * sizeof(line_ctx_t));
1✔
787
    }
788
}
1✔
789

790

791

792
static void find_vertical_shapes(remove_ctx_t *ctx)
9✔
793
{
794
    int west_empty = ctx->empty_side[BLEF];
9✔
795
    int east_empty = ctx->empty_side[BRIG];
9✔
796
    if (west_empty && east_empty) {
9!
797
        return;
×
798
    }
799

800
    for (comparison_t comp_type = 0; comp_type < NUM_COMPARISON_TYPES; comp_type++) {
15!
801
        if (!comp_type_is_viable(comp_type, ctx->input_is_mono, ctx->design_is_mono)) {
15✔
802
            continue;
5✔
803
        }
804
        ctx->comp_type = comp_type;
10✔
805

806
        shape_line_ctx_t **shape_lines_west = NULL;
10✔
807
        if (!west_empty) {
10!
808
            shape_lines_west = prepare_comp_shapes_vert(BLEF, comp_type);
10✔
809
        }
810
        shape_line_ctx_t **shape_lines_east = NULL;
10✔
811
        if (!east_empty) {
10!
812
            shape_lines_east = prepare_comp_shapes_vert(BRIG, comp_type);
10✔
813
        }
814

815
        for (size_t input_line_idx = ctx->top_end_idx; input_line_idx < ctx->bottom_start_idx; input_line_idx++) {
45✔
816
            size_t input_indent = 0;
35✔
817
            size_t input_trailing = 0;
35✔
818
            uint32_t *input_line = prepare_comp_input(input_line_idx, 0, comp_type, 0, &input_indent, &input_trailing);
35✔
819
            size_t input_length = u32_strlen(input_line);
35✔
820

821
            if (!west_empty) {
35!
822
                match_vertical_side(ctx, BLEF, shape_lines_west,
35✔
823
                        input_line, input_line_idx, input_length, input_indent, input_trailing);
824
            }
825
            if (!east_empty) {
35!
826
                match_vertical_side(ctx, BRIG, shape_lines_east,
35✔
827
                        input_line, input_line_idx, input_length, input_indent, input_trailing);
828
            }
829
        }
830

831
        free_shape_lines(shape_lines_west);
10✔
832
        free_shape_lines(shape_lines_east);
10✔
833

834
        if (sufficient_body_quality(ctx)) {
10✔
835
            break;
9✔
836
        }
837
        reset_body(ctx);
1✔
838
    }
839
}
840

841

842

843
/**
844
 * If the user didn't specify a design to remove, autodetect it.
845
 * Since this requires knowledge of all available designs, the entire config file had to be parsed (earlier).
846
 */
847
static void detect_design_if_needed()
9✔
848
{
849
    if (opt.design_choice_by_user == 0) {
9✔
850
        design_t *tmp = autodetect_design();
5✔
851
        if (tmp) {
5!
852
            opt.design = tmp;
5✔
853
            #ifdef DEBUG
854
                fprintf(stderr, "Design autodetection: Removing box of design \"%s\".\n", opt.design->name);
855
            #endif
856
        }
857
        else {
858
            fprintf(stderr, "%s: Box design autodetection failed. Use -d option.\n", PROJECT);
×
859
            exit(EXIT_FAILURE);
×
860
        }
861
    }
862
    #ifdef DEBUG
863
    else {
864
        fprintf(stderr, "Design was chosen by user: %s\n", opt.design->name);
865
    }
866
    #endif
867
}
9✔
868

869

870

871
static void free_line_text(line_t *line)
89✔
872
{
873
    BFREE(line->cache_visible);
89✔
874
    bxs_free(line->text);
89✔
875
    line->text = NULL;
89✔
876
}
89✔
877

878

879

880
static void free_line(line_t *line)
55✔
881
{
882
    free_line_text(line);
55✔
883
    BFREE(line->tabpos);
55!
884
    line->tabpos_len = 0;
55✔
885
}
55✔
886

887

888

889
static void killblank(remove_ctx_t *ctx)
8✔
890
{
891
    while (ctx->top_end_idx < ctx->bottom_start_idx && empty_line(input.lines + ctx->top_end_idx)) {
11!
892
        #ifdef DEBUG
893
            fprintf(stderr, "Killing leading blank line in box body.\n");
894
        #endif
895
        ++(ctx->top_end_idx);
3✔
896
    }
897
    while (ctx->bottom_start_idx > ctx->top_end_idx && empty_line(input.lines + ctx->bottom_start_idx - 1)) {
10!
898
        #ifdef DEBUG
899
            fprintf(stderr, "Killing trailing blank line in box body.\n");
900
        #endif
901
        --(ctx->bottom_start_idx);
2✔
902
    }
903
}
8✔
904

905

906

907
static int org_is_not_blank(bxstr_t *org_line, comparison_t comp_type, size_t idx)
29✔
908
{
909
    if (comp_type == literal || comp_type == ignore_invisible_shape) {
29!
910
        return !is_blank(org_line->memory[idx]);
21✔
911
    }
912
    return !is_blank(org_line->memory[org_line->visible_char[idx]]);
8✔
913
}
914

915

916

917
static size_t max_chars_line(bxstr_t *org_line, comparison_t comp_type)
39✔
918
{
919
    return (comp_type == literal || comp_type == ignore_invisible_shape)
14!
920
            ? org_line->num_chars : org_line->num_chars_visible;
53✔
921
}
922

923

924

925
static size_t confirmed_padding(bxstr_t *org_line, comparison_t comp_type, size_t start_idx, size_t num_padding)
34✔
926
{
927
    size_t result = 0;
34✔
928
    size_t max_chars = max_chars_line(org_line, comp_type);
34✔
929
    for (size_t i = start_idx; i < BMIN(max_chars, start_idx + num_padding); i++) {
63✔
930
        if (org_is_not_blank(org_line, comp_type, i)) {
29!
931
            break;
×
932
        }
933
        result++;
29✔
934
    }
935
    return result;
34✔
936
}
937

938

939

940
static void remove_top_from_input(remove_ctx_t *ctx)
9✔
941
{
942
    if (ctx->top_end_idx > ctx->top_start_idx) {
9!
943
        for (size_t j = ctx->top_start_idx; j < ctx->top_end_idx; ++j) {
37✔
944
            free_line(input.lines + j);
28✔
945
        }
946
        memmove(input.lines + ctx->top_start_idx, input.lines + ctx->top_end_idx,
9✔
947
                (input.num_lines - ctx->top_end_idx) * sizeof(line_t));
9✔
948
        size_t num_lines_removed = ctx->top_end_idx - ctx->top_start_idx;
9✔
949
        input.num_lines -= num_lines_removed;
9✔
950
        ctx->bottom_start_idx -= num_lines_removed;
9✔
951
        ctx->bottom_end_idx -= num_lines_removed;
9✔
952
    }
953
}
9✔
954

955

956

957
static size_t calculate_start_idx(remove_ctx_t *ctx, size_t body_line_idx)
34✔
958
{
959
    size_t input_line_idx = ctx->top_end_idx + body_line_idx;
34✔
960
    line_ctx_t *lctx = ctx->body + body_line_idx;
34✔
961
    bxstr_t *org_line = input.lines[input_line_idx].text;
34✔
962

963
    size_t s_idx = 0;
34✔
964
    if (lctx->west_quality > 0) {
34!
965
        s_idx = lctx->west_end + confirmed_padding(org_line, ctx->comp_type, lctx->west_end, opt.design->padding[BLEF]);
34✔
966
    }
967
    if (ctx->comp_type == ignore_invisible_input || ctx->comp_type == ignore_invisible_all) {
34✔
968
        /* our line context worked with visible characters only, convert back to org_line */
969
        s_idx = org_line->first_char[s_idx];
13✔
970
    }
971
    return s_idx;
34✔
972
}
973

974

975

976
static size_t calculate_end_idx(remove_ctx_t *ctx, size_t body_line_idx)
34✔
977
{
978
    size_t input_line_idx = ctx->top_end_idx + body_line_idx;
34✔
979
    line_ctx_t *lctx = ctx->body + body_line_idx;
34✔
980
    bxstr_t *org_line = input.lines[input_line_idx].text;
34✔
981

982
    size_t e_idx = lctx->east_quality > 0 ? lctx->east_start : max_chars_line(org_line, ctx->comp_type);
34✔
983
    if (ctx->comp_type == ignore_invisible_input || ctx->comp_type == ignore_invisible_all) {
34✔
984
        e_idx = org_line->first_char[e_idx];
13✔
985
    }
986
    return e_idx;
34✔
987
}
988

989

990

991
static void remove_vertical_from_input(remove_ctx_t *ctx)
9✔
992
{
993
    for (size_t body_line_idx = 0; body_line_idx < ctx->body_num_lines; body_line_idx++) {
43✔
994
        size_t input_line_idx = ctx->top_end_idx + body_line_idx;
34✔
995
        bxstr_t *org_line = input.lines[input_line_idx].text;
34✔
996
        size_t s_idx = calculate_start_idx(ctx, body_line_idx);
34✔
997
        size_t e_idx = calculate_end_idx(ctx, body_line_idx);
34✔
998
        #ifdef DEBUG
999
            fprintf(stderr, "remove_vertical_from_input(): body_line_idx=%d, input_line_idx=%d, s_idx=%d, e_idx=%d, "
1000
                    "input.indent=%d\n", (int) body_line_idx, (int) input_line_idx, (int) s_idx, (int) e_idx,
1001
                    (int) input.indent);
1002
        #endif
1003

1004
        bxstr_t *temp2 = bxs_substr(org_line, s_idx, e_idx);
34✔
1005
        bxstr_t *temp = bxs_prepend_spaces(temp2, input.indent);
34✔
1006
        free_line_text(input.lines + input_line_idx);
34✔
1007
        input.lines[input_line_idx].text = temp;
34✔
1008
        bxs_free(temp2);
34✔
1009
    }
1010
}
9✔
1011

1012

1013

1014
static void remove_bottom_from_input(remove_ctx_t *ctx)
9✔
1015
{
1016
    if (ctx->bottom_end_idx > ctx->bottom_start_idx) {
9!
1017
        for (size_t j = ctx->bottom_start_idx; j < ctx->bottom_end_idx; ++j) {
36✔
1018
            free_line(input.lines + j);
27✔
1019
        }
1020
        if (ctx->bottom_end_idx < input.num_lines) {
9!
1021
            memmove(input.lines + ctx->bottom_start_idx, input.lines + ctx->bottom_end_idx,
×
1022
                    (input.num_lines - ctx->bottom_end_idx) * sizeof(line_t));
×
1023
        }
1024
        input.num_lines -= ctx->bottom_end_idx - ctx->bottom_start_idx;
9✔
1025
    }
1026
}
9✔
1027

1028

1029

1030
static void apply_results_to_input(remove_ctx_t *ctx)
9✔
1031
{
1032
    remove_vertical_from_input(ctx);
9✔
1033

1034
    if (opt.killblank) {
9✔
1035
        killblank(ctx);
8✔
1036
    }
1037
    remove_bottom_from_input(ctx);
9✔
1038
    remove_top_from_input(ctx);
9✔
1039

1040
    input.maxline = 0;
9✔
1041
    input.indent = SIZE_MAX;
9✔
1042
    for (size_t j = 0; j < input.num_lines; ++j) {
38✔
1043
        if (input.lines[j].text->num_columns > input.maxline) {
29✔
1044
            input.maxline = input.lines[j].text->num_columns;
12✔
1045
        }
1046
        if (input.lines[j].text->indent < input.indent) {
29✔
1047
            input.indent = input.lines[j].text->indent;
10✔
1048
        }
1049
    }
1050

1051
    size_t num_lines_removed = BMAX(ctx->top_end_idx - ctx->top_start_idx, (size_t) 0)
9✔
1052
            + BMAX(ctx->bottom_end_idx - ctx->bottom_start_idx, (size_t) 0);
9✔
1053
    memset(input.lines + input.num_lines, 0, num_lines_removed * sizeof(line_t));
9✔
1054

1055
    #ifdef DEBUG
1056
        print_input_lines(" (remove_box) after box removal");
1057
        fprintf(stderr, "Number of lines shrunk by %d.\n", (int) num_lines_removed);
1058
    #endif
1059
}
9✔
1060

1061

1062

1063
int remove_box()
9✔
1064
{
1065
    detect_design_if_needed();
9✔
1066

1067
    remove_ctx_t *ctx = (remove_ctx_t *) calloc(1, sizeof(remove_ctx_t));
9✔
1068
    ctx->empty_side[BTOP] = empty_side(opt.design->shape, BTOP);
9✔
1069
    ctx->empty_side[BRIG] = empty_side(opt.design->shape, BRIG);
9✔
1070
    ctx->empty_side[BBOT] = empty_side(opt.design->shape, BBOT);
9✔
1071
    ctx->empty_side[BLEF] = empty_side(opt.design->shape, BLEF);
9✔
1072
    #ifdef DEBUG
1073
        fprintf(stderr, "Empty sides? Top: %d, Right: %d, Bottom: %d, Left: %d\n",
1074
                ctx->empty_side[BTOP], ctx->empty_side[BRIG], ctx->empty_side[BBOT], ctx->empty_side[BLEF]);
1075
    #endif
1076

1077
    ctx->design_is_mono = design_is_mono(opt.design);
9✔
1078
    ctx->input_is_mono = input_is_mono();
9✔
1079

1080
    ctx->top_start_idx = find_first_line();
9✔
1081
    if (ctx->top_start_idx >= input.num_lines) {
9!
1082
        return 0;  /* all lines were already blank, so there is no box to remove */
×
1083
    }
1084

1085
    if (ctx->empty_side[BTOP]) {
9!
1086
        ctx->top_end_idx = ctx->top_start_idx;
×
1087
    }
1088
    else {
1089
        ctx->top_end_idx = find_top_side(ctx);
9✔
1090
    }
1091

1092
    ctx->bottom_end_idx = find_last_line() + 1;
9✔
1093
    if (ctx->empty_side[BBOT]) {
9!
1094
        ctx->bottom_start_idx = ctx->bottom_end_idx;
×
1095
    }
1096
    else {
1097
        ctx->bottom_start_idx = find_bottom_side(ctx);
9✔
1098
        if (ctx->bottom_start_idx > ctx->top_end_idx) {
9!
1099
            ctx->body_num_lines = ctx->bottom_start_idx - ctx->top_end_idx;
9✔
1100
        }
1101
    }
1102

1103
    if (ctx->body_num_lines > 0) {
9!
1104
        ctx->body = (line_ctx_t *) calloc(ctx->body_num_lines, sizeof(line_ctx_t));
9✔
1105
        find_vertical_shapes(ctx);
9✔
1106
    }
1107

1108
    debug_print_remove_ctx(ctx, "before apply_results_to_input()");
9✔
1109
    apply_results_to_input(ctx);
9✔
1110

1111
    if (ctx->body != NULL) {
9!
1112
        for (size_t i = 0; i < ctx->body_num_lines; i++) {
43✔
1113
            BFREE(ctx->body[i].input_line_used);
34!
1114
        }
1115
        BFREE(ctx->body);
9!
1116
    }
1117
    BFREE(ctx);
9!
1118
    return 0;
9✔
1119
}
1120

1121

1122

1123
void output_input(const int trim_only)
9✔
1124
{
1125
    size_t indent;
1126
    int ntabs, nspcs;
1127

1128
    #ifdef DEBUG
1129
        fprintf(stderr, "output_input() - enter (trim_only=%d)\n", trim_only);
1130
    #endif
1131
    for (size_t j = 0; j < input.num_lines; ++j) {
38✔
1132
        if (input.lines[j].text == NULL) {
29!
1133
            continue;
×
1134
        }
1135
        bxstr_t *temp = bxs_rtrim(input.lines[j].text);
29✔
1136
        bxs_free(input.lines[j].text);
29✔
1137
        input.lines[j].text = temp;
29✔
1138
        if (trim_only) {
29✔
1139
            continue;
8✔
1140
        }
1141

1142
        char *indentspc = NULL;
21✔
1143
        if (opt.tabexp == 'u') {
21✔
1144
            indent = input.lines[j].text->indent;
4✔
1145
            ntabs = indent / opt.tabstop;
4✔
1146
            nspcs = indent % opt.tabstop;
4✔
1147
            indentspc = (char *) malloc(ntabs + nspcs + 1);
4✔
1148
            if (indentspc == NULL) {
4!
1149
                perror(PROJECT);
×
1150
                return;
×
1151
            }
1152
            memset(indentspc, (int) '\t', ntabs);
4✔
1153
            memset(indentspc + ntabs, (int) ' ', nspcs);
4✔
1154
            indentspc[ntabs + nspcs] = '\0';
4✔
1155
        }
1156
        else if (opt.tabexp == 'k') {
17!
1157
            uint32_t *indent32 = tabbify_indent(j, NULL, input.indent);
×
1158
            indentspc = u32_strconv_to_output(indent32);
×
1159
            BFREE(indent32);
×
1160
            indent = input.indent;
×
1161
        }
1162
        else {
1163
            indentspc = (char *) strdup("");
17✔
1164
            indent = 0;
17✔
1165
        }
1166

1167
        char *outtext = u32_strconv_to_output(bxs_first_char_ptr(input.lines[j].text, indent));
21✔
1168
        fprintf(opt.outfile, "%s%s%s", indentspc, outtext,
21✔
1169
                (input.final_newline || j < input.num_lines - 1 ? opt.eol : ""));
21!
1170
        BFREE(outtext);
21!
1171
        BFREE(indentspc);
21!
1172
    }
1173
}
1174

1175

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