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

ascii-boxes / boxes / 6518013212

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

push

github

tsjensen
remove

2349 of 3210 branches covered (0.0%)

Branch coverage included in aggregate %.

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

3767 of 4321 relevant lines covered (87.18%)

7801.63 hits per line

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

86.39
/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)
8✔
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
}
8✔
147

148

149

150
static void debug_print_shapes_relevant(shape_line_ctx_t *shapes_relevant)
36✔
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
}
36✔
173

174

175

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

188

189

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

202

203

204
static int is_shape_line_empty(shape_line_ctx_t *shapes_relevant, size_t shape_idx)
43✔
205
{
206
    if (shape_idx < SHAPES_PER_SIDE) {
43!
207
        return shapes_relevant[shape_idx].empty || bxs_is_blank(shapes_relevant[shape_idx].text);
43✔
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)
52✔
215
{
216
    for (size_t i = shape_idx + 1; i < SHAPES_PER_SIDE - 1; i++) {
73✔
217
        if (!is_shape_line_empty(shapes_relevant, i)) {
27✔
218
            return 1;
6✔
219
        }
220
    }
221
    return 0;
46✔
222
}
223

224

225

226
static int is_blank_between(uint32_t *start, uint32_t *end)
6✔
227
{
228
    for (uint32_t *p = start; p < end; p++) {
28✔
229
        if (!is_blank(*p)) {
22!
230
            return 0;
×
231
        }
232
    }
233
    return 1;
6✔
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)
509✔
248
{
249
    if (shape_line_ctx == NULL || shape_line_ctx->text == NULL || quality == NULL
509✔
250
            || *quality > shape_line_ctx->text->num_chars) {
506✔
251
        return NULL;
4✔
252
    }
253

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

283
    uint32_t *result = NULL;
505✔
284
    size_t new_quality = e - s;
505✔
285
    if (new_quality < *quality) {
505✔
286
        result = u32_strdup(s);
270✔
287
        set_char_at(result, new_quality, char_nul);
270✔
288
        *quality = new_quality;
270✔
289
    }
290
    return result;
505✔
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,
777✔
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;
777✔
327
    if (!anchored_left) {
777✔
328
        result = hmm_shiftable(shapes_relevant, cur_pos, shape_idx, end_pos, anchored_right);
7✔
329
    }
330
    else if (cur_pos > end_pos) {
770✔
331
        /* invalid input */
332
        result = 0;
13✔
333
    }
334
    else if (cur_pos == end_pos) {
757✔
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))
24!
337
                && !non_empty_shapes_after(shapes_relevant, shape_idx) ? 1 : 0;
24!
338
    }
339
    else if (shape_idx >= SHAPES_PER_SIDE - 1) {
744✔
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) {
743✔
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);
729✔
349
        size_t quality = shapes_relevant[shape_idx].text->num_chars;
729✔
350
        while (shape_line != NULL) {
1,470✔
351
            if (u32_strncmp(cur_pos, shape_line, quality) == 0) {
741✔
352
                BFREE(shape_line);
669!
353
                cur_pos = cur_pos + quality;
669✔
354
                if (cur_pos == end_pos && !non_empty_shapes_after(shapes_relevant, shape_idx)) {
669✔
355
                    result = 1; /* success */
41✔
356
                }
357
                else {
358
                    int rc = 0;
628✔
359
                    if (shapes_relevant[shape_idx].elastic) {
628✔
360
                        rc = hmm(shapes_relevant, cur_pos, shape_idx, end_pos, 1, anchored_right);
600✔
361
                    }
362
                    if (rc == 0) {
628✔
363
                        result = hmm(shapes_relevant, cur_pos, shape_idx + 1, end_pos, 1, anchored_right);
110✔
364
                    }
365
                    else {
366
                        result = rc;
518✔
367
                    }
368
                }
369
            }
370
            else if (!anchored_right) {
72✔
371
                shape_line = shorten(shapes_relevant + shape_idx, &quality, 0, 0, 1);
14✔
372
            }
373
            else {
374
                BFREE(shape_line);
58!
375
            }
376
        }
377
    }
378

379
    #ifdef DEBUG
380
        fprintf(stderr, "hmm() - exit, result = %d\n", result);
381
    #endif
382
    return result;
777✔
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,
7✔
388
        int anchored_right)
389
{
390
    int result = 0;
7✔
391
    int shapes_are_empty = 1;
7✔
392
    for (size_t i = shape_idx; i < SHAPES_PER_SIDE - 1; i++) {
14✔
393
        if (!is_shape_line_empty(shapes_relevant, i)) {
13✔
394
            shapes_are_empty = 0;
6✔
395
            int can_shorten_right = -1;
6✔
396
            size_t quality = shapes_relevant[i].text->num_chars;
6✔
397
            uint32_t *shape_line = shapes_relevant[i].text->memory;
6✔
398
            while (shape_line != NULL) {
15✔
399
                uint32_t *p = u32_strstr(cur_pos, shape_line);
14✔
400
                if (p != NULL && p < end_pos && is_blank_between(cur_pos, p)) {
14!
401
                    result = hmm(shapes_relevant, p + quality, i + (shapes_relevant[i].elastic ? 0 : 1),
5✔
402
                            end_pos, 1, anchored_right);
403
                    break;
5✔
404
                }
405
                if (can_shorten_right == -1) {
9✔
406
                    /* we can only shorten right if the east corner shape line is also empty */
407
                    can_shorten_right = non_empty_shapes_after(shapes_relevant, i)
6✔
408
                            || !is_shape_line_empty(shapes_relevant, SHAPES_PER_SIDE - 1) ? 0 : 1;
3!
409
                }
410
                shape_line = shorten(shapes_relevant + i, &quality, 0, 1, can_shorten_right);
9✔
411
            }
412
            break;
6✔
413
        }
414
    }
415
    if (shapes_are_empty) {
7✔
416
        /* all shapes were empty, which is fine if line was blank */
417
        result = is_blank_between(cur_pos, end_pos);
1✔
418
    }
419
    return result;
7✔
420
}
421

422

423

424
static shape_line_ctx_t *prepare_comp_shapes_horiz(int hside, comparison_t comp_type, size_t shape_line_idx)
36✔
425
{
426
    shape_t *side_shapes = hside == BTOP ? north_side : south_side_rev;
36✔
427
    shape_line_ctx_t *shapes_relevant = (shape_line_ctx_t *) calloc(SHAPES_PER_SIDE, sizeof(shape_line_ctx_t));
36✔
428

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

440
    return shapes_relevant;
36✔
441
}
442

443

444

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

455

456

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

472
    if (vside == BLEF) {
85✔
473
        if (bxs_is_blank(shape_line)) {
42✔
474
            return new_match_result(input_line->memory, 0, 0, 1);
3✔
475
        }
476
        for (uint32_t *s = shape_line->memory; s == shape_line->memory || is_blank(*s); s++) {
44✔
477
            uint32_t *p = u32_strstr(input_line->memory, s);
42✔
478
            size_t p_idx = p != NULL ? p - input_line->memory : 0;
42✔
479
            if (p == NULL || p_idx > input_line->first_char[input_line->indent]) {
42✔
480
                continue;  /* not found or found too far in */
5✔
481
            }
482
            return new_match_result(p, p_idx, shape_line->num_chars - (s - shape_line->memory), 0);
37✔
483
        }
484
    }
485
    else {
486
        if (bxs_is_blank(shape_line)) {
43✔
487
            uint32_t *p = bxs_last_char_ptr(input_line);
3✔
488
            size_t p_idx = p - input_line->memory;
3✔
489
            return new_match_result(p, p_idx, 0, 1);
3✔
490
        }
491
        int slen = shape_line->num_chars;
40✔
492
        uint32_t *s = u32_strdup(shape_line->memory);
40✔
493
        for (; slen == (int) shape_line->num_chars || is_blank(s[slen]); slen--) {
47✔
494
            s[slen] = char_nul;
45✔
495
            uint32_t *p = u32_strnrstr(input_line->memory, s, slen, 0);
45✔
496
            size_t p_idx = p != NULL ? p - input_line->memory : 0;
45✔
497
            if (p == NULL || p_idx + slen
45✔
498
                    < input_line->first_char[input_line->num_chars_visible - input_line->trailing]) {
39✔
499
                continue; /* not found or found too far in */
7✔
500
            }
501
            BFREE(s);
38!
502
            return new_match_result(p, p_idx, (size_t) slen, 0);
38✔
503
        }
504
        BFREE(s);
2!
505
    }
506
    return NULL;
4✔
507
}
508

509

510

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

518
    int result = 0;
36✔
519
    for (comparison_t comp_type = 0; comp_type < NUM_COMPARISON_TYPES; comp_type++) {
48!
520
        if (!comp_type_is_viable(comp_type, ctx->input_is_mono, ctx->design_is_mono)) {
48✔
521
            continue;
12✔
522
        }
523
        ctx->comp_type = comp_type;
36✔
524

525
        shape_line_ctx_t *shapes_relevant = prepare_comp_shapes_horiz(hside, comp_type, shape_line_idx);
36✔
526
        debug_print_shapes_relevant(shapes_relevant);
36✔
527

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

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

556
        if (cur_pos && end_pos) {
36!
557
            result = hmm(shapes_relevant, cur_pos, 1, end_pos, mrl->shiftable ? 0 : 1, mrr->shiftable ? 0 : 1);
36✔
558
        }
559

560
        BFREE(mrl);
36!
561
        BFREE(mrr);
36!
562
        for (size_t i = 0; i < SHAPES_PER_SIDE; i++) {
216✔
563
            bxs_free(shapes_relevant[i].text);
180✔
564
        }
565
        BFREE(shapes_relevant);
36!
566

567
        if (result) {
36!
568
            #ifdef DEBUG
569
                fprintf(stderr, "Matched %s side line using comp_type=%s and shape_line_idx=%d\n",
570
                    hside == BTOP ? "top" : "bottom", comparison_name[comp_type], (int) shape_line_idx);
571
            #endif
572
            break;
36✔
573
        }
574
    }
575

576
    return result;
36✔
577
}
578

579

580

581
static size_t find_top_side(remove_ctx_t *ctx)
8✔
582
{
583
    size_t result = ctx->top_start_idx;
8✔
584
    sentry_t *shapes = opt.design->shape;
8✔
585
    for (size_t input_line_idx = ctx->top_start_idx, shape_line_count = 0;
8✔
586
            input_line_idx < input.num_lines && shape_line_count < shapes[NE].height;
26!
587
            input_line_idx++, shape_line_count++)
18✔
588
    {
589
        int matched = 0;
18✔
590
        for (size_t shape_line_idx = input_line_idx - ctx->top_start_idx;;
18✔
591
                shape_line_idx = (shape_line_idx + 1) % shapes[NE].height)
×
592
        {
593
            if (match_horiz_line(ctx, BTOP, input_line_idx, shape_line_idx)) {
18!
594
                matched = 1;
18✔
595
                break;
18✔
596
            }
597
        }
598
        if (!matched) {
18!
599
            break;
×
600
        }
601
        result = input_line_idx + 1;
18✔
602
    }
603
    return result;
8✔
604
}
605

606

607

608
static size_t find_bottom_side(remove_ctx_t *ctx)
8✔
609
{
610
    size_t result = ctx->top_start_idx;
8✔
611
    sentry_t *shapes = opt.design->shape;
8✔
612
    for (long input_line_idx = (long) ctx->bottom_end_idx - 1, shape_line_count = (long) shapes[SE].height - 1;
8✔
613
            input_line_idx >= 0 && shape_line_count >= 0;
26!
614
            input_line_idx--, shape_line_count--)
18✔
615
    {
616
        int matched = 0;
18✔
617
        for (size_t shape_line_idx = shapes[SE].height - (ctx->bottom_end_idx - input_line_idx);;
18✔
618
                shape_line_idx = (shape_line_idx + 1) % shapes[SE].height)
×
619
        {
620
            if (match_horiz_line(ctx, BBOT, input_line_idx, shape_line_idx)) {
18!
621
                matched = 1;
18✔
622
                break;
18✔
623
            }
624
        }
625
        if (!matched) {
18!
626
            break;
×
627
        }
628
        result = input_line_idx;
18✔
629
    }
630
    return result;
8✔
631
}
632

633

634

635
static size_t count_shape_lines(shape_t side_shapes[])
16✔
636
{
637
    size_t result = 0;
16✔
638
    for (size_t i = 0; i < SHAPES_PER_SIDE - CORNERS_PER_SIDE; i++) {
64✔
639
        if (!isempty(opt.design->shape + side_shapes[i])) {
48✔
640
            result += opt.design->shape[side_shapes[i]].height;
24✔
641
        }
642
    }
643
    return result;
16✔
644
}
645

646

647
static shape_line_ctx_t **prepare_comp_shapes_vert(int vside, comparison_t comp_type)
16✔
648
{
649
    shape_t west_side_shapes[SHAPES_PER_SIDE - CORNERS_PER_SIDE] = {WNW, W, WSW};
16✔
650
    shape_t east_side_shapes[SHAPES_PER_SIDE - CORNERS_PER_SIDE] = {ENE, E, ESE};
16✔
651
    shape_t side_shapes[SHAPES_PER_SIDE - CORNERS_PER_SIDE];
652
    if (vside == BLEF) {
16✔
653
        memcpy(side_shapes, west_side_shapes, (SHAPES_PER_SIDE - CORNERS_PER_SIDE) * sizeof(shape_t));
8✔
654
    }
655
    else {
656
        memcpy(side_shapes, east_side_shapes, (SHAPES_PER_SIDE - CORNERS_PER_SIDE) * sizeof(shape_t));
8✔
657
    }
658

659
    size_t num_shape_lines = count_shape_lines(side_shapes);
16✔
660

661
    shape_line_ctx_t **shape_lines = (shape_line_ctx_t **) calloc(num_shape_lines + 1, sizeof(shape_line_ctx_t *));
16✔
662
    for (size_t i = 0; i < num_shape_lines; i++) {
66✔
663
        shape_lines[i] = (shape_line_ctx_t *) calloc(1, sizeof(shape_line_ctx_t));
50✔
664
    }
665

666
    for (size_t shape_idx = 0, i = 0; shape_idx < SHAPES_PER_SIDE - CORNERS_PER_SIDE; shape_idx++) {
64✔
667
        if (!isempty(opt.design->shape + side_shapes[shape_idx])) {
48✔
668
            int deep_empty = isdeepempty(opt.design->shape + side_shapes[shape_idx]);
24✔
669
            for (size_t slno = 0; slno < opt.design->shape[side_shapes[shape_idx]].height; slno++, i++) {
74✔
670
                uint32_t *s = prepare_comp_shape(opt.design, side_shapes[shape_idx], slno, comp_type, 0, 0);
50✔
671
                shape_lines[i]->text = bxs_from_unicode(s);
50✔
672
                shape_lines[i]->empty = deep_empty;
50✔
673
                shape_lines[i]->elastic = opt.design->shape[side_shapes[shape_idx]].elastic;
50✔
674
                BFREE(s);
50!
675
            }
676
        }
677
    }
678

679
    return shape_lines;
16✔
680
}
681

682

683

684
static void free_shape_lines(shape_line_ctx_t **shape_lines)
16✔
685
{
686
    if (shape_lines != NULL) {
16!
687
        for (shape_line_ctx_t **p = shape_lines; *p != NULL; p++) {
66✔
688
            bxs_free((*p)->text);
50✔
689
            BFREE(*p);
50!
690
        }
691
        BFREE(shape_lines);
16!
692
    }
693
}
16✔
694

695

696

697
static void match_vertical_side(remove_ctx_t *ctx, int vside, shape_line_ctx_t **shape_lines, uint32_t *input_line,
66✔
698
    size_t line_idx, size_t input_length, size_t input_indent, size_t input_trailing)
699
{
700
    line_ctx_t *line_ctx = ctx->body + (line_idx - ctx->top_end_idx);
66✔
701

702
    for (shape_line_ctx_t **shape_line_ctx = shape_lines; *shape_line_ctx != NULL; shape_line_ctx++) {
404✔
703
        if ((*shape_line_ctx)->empty) {
338!
704
            continue;
×
705
        }
706

707
        size_t max_quality = (*shape_line_ctx)->text->num_chars;
338✔
708
        size_t quality = max_quality;
338✔
709
        uint32_t *shape_text = (*shape_line_ctx)->text->memory;
338✔
710
        uint32_t *to_free = NULL;
338✔
711
        while(shape_text != NULL) {
848✔
712
            uint32_t *p;
713
            if (vside == BLEF) {
576✔
714
                p = u32_strstr(input_line, shape_text);
281✔
715
            }
716
            else {
717
                p = u32_strnrstr(input_line, shape_text, quality, 0);
295✔
718
            }
719
            BFREE(to_free);
576✔
720
            shape_text = NULL;
576✔
721

722
            if (p == NULL) {
576✔
723
                shape_text = shorten(*shape_line_ctx, &quality, vside == BLEF, 1, 1);
465✔
724
                to_free = shape_text;
465✔
725
            }
726
            else if (vside == BLEF && ((size_t) (p - input_line) <= input_indent + (max_quality - quality))) {
111✔
727
                if (quality > line_ctx->west_quality) {
55✔
728
                    line_ctx->west_start = (size_t) (p - input_line);
37✔
729
                    line_ctx->west_end = line_ctx->west_start + quality;
37✔
730
                    line_ctx->west_quality = quality;
37✔
731
                    BFREE(line_ctx->input_line_used);
37✔
732
                    line_ctx->input_line_used = u32_strdup(input_line);
37✔
733
                    break;
37✔
734
                }
735
            }
736
            else if (vside == BRIG && ((size_t) (p - input_line) >= input_length - input_trailing - quality)) {
56✔
737
                if (quality > line_ctx->east_quality) {
39✔
738
                    line_ctx->east_start = (size_t) (p - input_line);
29✔
739
                    line_ctx->east_end = line_ctx->east_start + quality;
29✔
740
                    line_ctx->east_quality = quality;
29✔
741
                    BFREE(line_ctx->input_line_used);
29!
742
                    line_ctx->input_line_used = u32_strdup(input_line);
29✔
743
                    break;
29✔
744
                }
745
            }
746
        }
747
    }
748
}
66✔
749

750

751

752
static int sufficient_body_quality(remove_ctx_t *ctx)
8✔
753
{
754
    size_t num_body_lines = ctx->bottom_start_idx - ctx->top_end_idx;
8✔
755
    size_t total_quality = 0;
8✔
756
    line_ctx_t *body = ctx->body;
8✔
757
    for (size_t body_line_idx = 0; body_line_idx < num_body_lines; body_line_idx++) {
41✔
758
        line_ctx_t line_ctx = body[body_line_idx];
33✔
759
        total_quality += line_ctx.east_quality + line_ctx.west_quality;
33✔
760
    }
761

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

772

773

774
static void reset_body(remove_ctx_t *ctx)
×
775
{
776
    if (ctx->body != NULL) {
×
777
        for (size_t i = 0; i < ctx->body_num_lines; i++) {
×
778
            BFREE(ctx->body[i].input_line_used);
×
779
        }
780
        memset(ctx->body, 0, ctx->body_num_lines * sizeof(line_ctx_t));
×
781
    }
782
}
×
783

784

785

786
static void find_vertical_shapes(remove_ctx_t *ctx)
8✔
787
{
788
    int west_empty = ctx->empty_side[BLEF];
8✔
789
    int east_empty = ctx->empty_side[BRIG];
8✔
790
    if (west_empty && east_empty) {
8!
791
        return;
×
792
    }
793

794
    for (comparison_t comp_type = 0; comp_type < NUM_COMPARISON_TYPES; comp_type++) {
11!
795
        if (!comp_type_is_viable(comp_type, ctx->input_is_mono, ctx->design_is_mono)) {
11✔
796
            continue;
3✔
797
        }
798
        ctx->comp_type = comp_type;
8✔
799

800
        shape_line_ctx_t **shape_lines_west = NULL;
8✔
801
        if (!west_empty) {
8!
802
            shape_lines_west = prepare_comp_shapes_vert(BLEF, comp_type);
8✔
803
        }
804
        shape_line_ctx_t **shape_lines_east = NULL;
8✔
805
        if (!east_empty) {
8!
806
            shape_lines_east = prepare_comp_shapes_vert(BRIG, comp_type);
8✔
807
        }
808

809
        for (size_t input_line_idx = ctx->top_end_idx; input_line_idx < ctx->bottom_start_idx; input_line_idx++) {
41✔
810
            size_t input_indent = 0;
33✔
811
            size_t input_trailing = 0;
33✔
812
            uint32_t *input_line = prepare_comp_input(input_line_idx, 0, comp_type, 0, &input_indent, &input_trailing);
33✔
813
            size_t input_length = u32_strlen(input_line);
33✔
814

815
            if (!west_empty) {
33!
816
                match_vertical_side(ctx, BLEF, shape_lines_west,
33✔
817
                        input_line, input_line_idx, input_length, input_indent, input_trailing);
818
            }
819
            if (!east_empty) {
33!
820
                match_vertical_side(ctx, BRIG, shape_lines_east,
33✔
821
                        input_line, input_line_idx, input_length, input_indent, input_trailing);
822
            }
823
        }
824

825
        free_shape_lines(shape_lines_west);
8✔
826
        free_shape_lines(shape_lines_east);
8✔
827

828
        if (sufficient_body_quality(ctx)) {
8!
829
            break;
8✔
830
        }
831
        reset_body(ctx);
×
832
    }
833
}
834

835

836

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

863

864

865
static void free_line_text(line_t *line)
74✔
866
{
867
    BFREE(line->cache_visible);
74✔
868
    bxs_free(line->text);
74✔
869
    line->text = NULL;
74✔
870
}
74✔
871

872

873

874
static void free_line(line_t *line)
41✔
875
{
876
    free_line_text(line);
41✔
877
    BFREE(line->tabpos);
41!
878
    line->tabpos_len = 0;
41✔
879
}
41✔
880

881

882

883
static void killblank(remove_ctx_t *ctx)
7✔
884
{
885
    while (ctx->top_end_idx < ctx->bottom_start_idx && empty_line(input.lines + ctx->top_end_idx)) {
10!
886
        #ifdef DEBUG
887
            fprintf(stderr, "Killing leading blank line in box body.\n");
888
        #endif
889
        ++(ctx->top_end_idx);
3✔
890
    }
891
    while (ctx->bottom_start_idx > ctx->top_end_idx && empty_line(input.lines + ctx->bottom_start_idx - 1)) {
9!
892
        #ifdef DEBUG
893
            fprintf(stderr, "Killing trailing blank line in box body.\n");
894
        #endif
895
        --(ctx->bottom_start_idx);
2✔
896
    }
897
}
7✔
898

899

900

901
static int org_is_not_blank(bxstr_t *org_line, comparison_t comp_type, size_t idx)
29✔
902
{
903
    if (comp_type == literal || comp_type == ignore_invisible_shape) {
29!
904
        return !is_blank(org_line->memory[idx]);
21✔
905
    }
906
    return !is_blank(org_line->memory[org_line->visible_char[idx]]);
8✔
907
}
908

909

910

911
static size_t max_chars_line(bxstr_t *org_line, comparison_t comp_type)
38✔
912
{
913
    return (comp_type == literal || comp_type == ignore_invisible_shape)
13!
914
            ? org_line->num_chars : org_line->num_chars_visible;
51✔
915
}
916

917

918

919
static size_t confirmed_padding(bxstr_t *org_line, comparison_t comp_type, size_t start_idx, size_t num_padding)
33✔
920
{
921
    size_t result = 0;
33✔
922
    size_t max_chars = max_chars_line(org_line, comp_type);
33✔
923
    for (size_t i = start_idx; i < BMIN(max_chars, start_idx + num_padding); i++) {
62✔
924
        if (org_is_not_blank(org_line, comp_type, i)) {
29!
925
            break;
×
926
        }
927
        result++;
29✔
928
    }
929
    return result;
33✔
930
}
931

932

933

934
static void remove_top_from_input(remove_ctx_t *ctx)
8✔
935
{
936
    if (ctx->top_end_idx > ctx->top_start_idx) {
8!
937
        for (size_t j = ctx->top_start_idx; j < ctx->top_end_idx; ++j) {
29✔
938
            free_line(input.lines + j);
21✔
939
        }
940
        memmove(input.lines + ctx->top_start_idx, input.lines + ctx->top_end_idx,
8✔
941
                (input.num_lines - ctx->top_end_idx) * sizeof(line_t));
8✔
942
        size_t num_lines_removed = ctx->top_end_idx - ctx->top_start_idx;
8✔
943
        input.num_lines -= num_lines_removed;
8✔
944
        ctx->bottom_start_idx -= num_lines_removed;
8✔
945
        ctx->bottom_end_idx -= num_lines_removed;
8✔
946
    }
947
}
8✔
948

949

950

951
static size_t calculate_start_idx(remove_ctx_t *ctx, size_t body_line_idx)
33✔
952
{
953
    size_t input_line_idx = ctx->top_end_idx + body_line_idx;
33✔
954
    line_ctx_t *lctx = ctx->body + body_line_idx;
33✔
955
    bxstr_t *org_line = input.lines[input_line_idx].text;
33✔
956

957
    size_t s_idx = 0;
33✔
958
    if (lctx->west_quality > 0) {
33!
959
        s_idx = lctx->west_end + confirmed_padding(org_line, ctx->comp_type, lctx->west_end, opt.design->padding[BLEF]);
33✔
960
    }
961
    if (ctx->comp_type == ignore_invisible_input || ctx->comp_type == ignore_invisible_all) {
33!
962
        /* our line context worked with visible characters only, convert back to org_line */
963
        s_idx = org_line->first_char[s_idx];
12✔
964
    }
965
    return s_idx;
33✔
966
}
967

968

969

970
static size_t calculate_end_idx(remove_ctx_t *ctx, size_t body_line_idx)
33✔
971
{
972
    size_t input_line_idx = ctx->top_end_idx + body_line_idx;
33✔
973
    line_ctx_t *lctx = ctx->body + body_line_idx;
33✔
974
    bxstr_t *org_line = input.lines[input_line_idx].text;
33✔
975

976
    size_t e_idx = lctx->east_quality > 0 ? lctx->east_start : max_chars_line(org_line, ctx->comp_type);
33✔
977
    if (ctx->comp_type == ignore_invisible_input || ctx->comp_type == ignore_invisible_all) {
33!
978
        e_idx = org_line->first_char[e_idx];
12✔
979
    }
980
    return e_idx;
33✔
981
}
982

983

984

985
static void remove_vertical_from_input(remove_ctx_t *ctx)
8✔
986
{
987
    for (size_t body_line_idx = 0; body_line_idx < ctx->body_num_lines; body_line_idx++) {
41✔
988
        size_t input_line_idx = ctx->top_end_idx + body_line_idx;
33✔
989
        bxstr_t *org_line = input.lines[input_line_idx].text;
33✔
990
        size_t s_idx = calculate_start_idx(ctx, body_line_idx);
33✔
991
        size_t e_idx = calculate_end_idx(ctx, body_line_idx);
33✔
992
        #ifdef DEBUG
993
            fprintf(stderr, "remove_vertical_from_input(): body_line_idx=%d, input_line_idx=%d, s_idx=%d, e_idx=%d, "
994
                    "input.indent=%d\n", (int) body_line_idx, (int) input_line_idx, (int) s_idx, (int) e_idx,
995
                    (int) input.indent);
996
        #endif
997

998
        bxstr_t *temp2 = bxs_substr(org_line, s_idx, e_idx);
33✔
999
        bxstr_t *temp = bxs_prepend_spaces(temp2, input.indent);
33✔
1000
        free_line_text(input.lines + input_line_idx);
33✔
1001
        input.lines[input_line_idx].text = temp;
33✔
1002
        bxs_free(temp2);
33✔
1003
    }
1004
}
8✔
1005

1006

1007

1008
static void remove_bottom_from_input(remove_ctx_t *ctx)
8✔
1009
{
1010
    if (ctx->bottom_end_idx > ctx->bottom_start_idx) {
8!
1011
        for (size_t j = ctx->bottom_start_idx; j < ctx->bottom_end_idx; ++j) {
28✔
1012
            free_line(input.lines + j);
20✔
1013
        }
1014
        if (ctx->bottom_end_idx < input.num_lines) {
8!
1015
            memmove(input.lines + ctx->bottom_start_idx, input.lines + ctx->bottom_end_idx,
×
1016
                    (input.num_lines - ctx->bottom_end_idx) * sizeof(line_t));
×
1017
        }
1018
        input.num_lines -= ctx->bottom_end_idx - ctx->bottom_start_idx;
8✔
1019
    }
1020
}
8✔
1021

1022

1023

1024
static void apply_results_to_input(remove_ctx_t *ctx)
8✔
1025
{
1026
    remove_vertical_from_input(ctx);
8✔
1027

1028
    if (opt.killblank) {
8✔
1029
        killblank(ctx);
7✔
1030
    }
1031
    remove_bottom_from_input(ctx);
8✔
1032
    remove_top_from_input(ctx);
8✔
1033

1034
    input.maxline = 0;
8✔
1035
    input.indent = SIZE_MAX;
8✔
1036
    for (size_t j = 0; j < input.num_lines; ++j) {
36✔
1037
        if (input.lines[j].text->num_columns > input.maxline) {
28✔
1038
            input.maxline = input.lines[j].text->num_columns;
11✔
1039
        }
1040
        if (input.lines[j].text->indent < input.indent) {
28✔
1041
            input.indent = input.lines[j].text->indent;
9✔
1042
        }
1043
    }
1044

1045
    size_t num_lines_removed = BMAX(ctx->top_end_idx - ctx->top_start_idx, (size_t) 0)
8✔
1046
            + BMAX(ctx->bottom_end_idx - ctx->bottom_start_idx, (size_t) 0);
8✔
1047
    memset(input.lines + input.num_lines, 0, num_lines_removed * sizeof(line_t));
8✔
1048

1049
    #ifdef DEBUG
1050
        print_input_lines(" (remove_box) after box removal");
1051
        fprintf(stderr, "Number of lines shrunk by %d.\n", (int) num_lines_removed);
1052
    #endif
1053
}
8✔
1054

1055

1056

1057
int remove_box()
8✔
1058
{
1059
    detect_design_if_needed();
8✔
1060

1061
    remove_ctx_t *ctx = (remove_ctx_t *) calloc(1, sizeof(remove_ctx_t));
8✔
1062
    ctx->empty_side[BTOP] = empty_side(opt.design->shape, BTOP);
8✔
1063
    ctx->empty_side[BRIG] = empty_side(opt.design->shape, BRIG);
8✔
1064
    ctx->empty_side[BBOT] = empty_side(opt.design->shape, BBOT);
8✔
1065
    ctx->empty_side[BLEF] = empty_side(opt.design->shape, BLEF);
8✔
1066
    #ifdef DEBUG
1067
        fprintf(stderr, "Empty sides? Top: %d, Right: %d, Bottom: %d, Left: %d\n",
1068
                ctx->empty_side[BTOP], ctx->empty_side[BRIG], ctx->empty_side[BBOT], ctx->empty_side[BLEF]);
1069
    #endif
1070

1071
    ctx->design_is_mono = design_is_mono(opt.design);
8✔
1072
    ctx->input_is_mono = input_is_mono();
8✔
1073

1074
    ctx->top_start_idx = find_first_line();
8✔
1075
    if (ctx->top_start_idx >= input.num_lines) {
8!
1076
        return 0;  /* all lines were already blank, so there is no box to remove */
×
1077
    }
1078

1079
    if (ctx->empty_side[BTOP]) {
8!
1080
        ctx->top_end_idx = ctx->top_start_idx;
×
1081
    }
1082
    else {
1083
        ctx->top_end_idx = find_top_side(ctx);
8✔
1084
    }
1085

1086
    ctx->bottom_end_idx = find_last_line() + 1;
8✔
1087
    if (ctx->empty_side[BBOT]) {
8!
1088
        ctx->bottom_start_idx = ctx->bottom_end_idx;
×
1089
    }
1090
    else {
1091
        ctx->bottom_start_idx = find_bottom_side(ctx);
8✔
1092
        if (ctx->bottom_start_idx > ctx->top_end_idx) {
8!
1093
            ctx->body_num_lines = ctx->bottom_start_idx - ctx->top_end_idx;
8✔
1094
        }
1095
    }
1096

1097
    if (ctx->body_num_lines > 0) {
8!
1098
        ctx->body = (line_ctx_t *) calloc(ctx->body_num_lines, sizeof(line_ctx_t));
8✔
1099
        find_vertical_shapes(ctx);
8✔
1100
    }
1101

1102
    debug_print_remove_ctx(ctx, "before apply_results_to_input()");
8✔
1103
    apply_results_to_input(ctx);
8✔
1104

1105
    if (ctx->body != NULL) {
8!
1106
        for (size_t i = 0; i < ctx->body_num_lines; i++) {
41✔
1107
            BFREE(ctx->body[i].input_line_used);
33!
1108
        }
1109
        BFREE(ctx->body);
8!
1110
    }
1111
    BFREE(ctx);
8!
1112
    return 0;
8✔
1113
}
1114

1115

1116

1117
void output_input(const int trim_only)
8✔
1118
{
1119
    size_t indent;
1120
    int ntabs, nspcs;
1121

1122
    #ifdef DEBUG
1123
        fprintf(stderr, "output_input() - enter (trim_only=%d)\n", trim_only);
1124
    #endif
1125
    for (size_t j = 0; j < input.num_lines; ++j) {
36✔
1126
        if (input.lines[j].text == NULL) {
28!
1127
            continue;
×
1128
        }
1129
        bxstr_t *temp = bxs_rtrim(input.lines[j].text);
28✔
1130
        bxs_free(input.lines[j].text);
28✔
1131
        input.lines[j].text = temp;
28✔
1132
        if (trim_only) {
28✔
1133
            continue;
8✔
1134
        }
1135

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

1161
        char *outtext = u32_strconv_to_output(bxs_first_char_ptr(input.lines[j].text, indent));
20✔
1162
        fprintf(opt.outfile, "%s%s%s", indentspc, outtext,
20✔
1163
                (input.final_newline || j < input.num_lines - 1 ? opt.eol : ""));
20!
1164
        BFREE(outtext);
20!
1165
        BFREE(indentspc);
20!
1166
    }
1167
}
1168

1169

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