Skip to content

Commit

Permalink
Fix some box removal bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
tsjensen committed Dec 21, 2023
1 parent 59aaff4 commit 80691ef
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
41 changes: 34 additions & 7 deletions src/remove.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ static int is_shape_line_empty(shape_line_ctx_t *shapes_relevant, size_t shape_i

static int non_empty_shapes_after(shape_line_ctx_t *shapes_relevant, size_t shape_idx)
{
/* CHECK Can we use shape->is_blank_rightward? */
for (size_t i = shape_idx + 1; i < SHAPES_PER_SIDE - 1; i++) {
if (!is_shape_line_empty(shapes_relevant, i)) {
return 1;
Expand Down Expand Up @@ -333,8 +334,9 @@ int hmm(shape_line_ctx_t *shapes_relevant, uint32_t *cur_pos, size_t shape_idx,
}
else if (cur_pos == end_pos) {
/* we are at the end, which is fine if there is nothing else to match */
result = (shapes_relevant[shape_idx].empty || bxs_is_blank(shapes_relevant[shape_idx].text))
&& !non_empty_shapes_after(shapes_relevant, shape_idx) ? 1 : 0;
result = (shape_idx == (SHAPES_PER_SIDE - 1) && anchored_right)
|| ((shapes_relevant[shape_idx].empty || bxs_is_blank(shapes_relevant[shape_idx].text))
&& !non_empty_shapes_after(shapes_relevant, shape_idx) ? 1 : 0);
}
else if (shape_idx >= SHAPES_PER_SIDE - 1) {
/* no more shapes to try, which is fine if the rest of the line is blank */
Expand All @@ -347,7 +349,7 @@ int hmm(shape_line_ctx_t *shapes_relevant, uint32_t *cur_pos, size_t shape_idx,
else {
uint32_t *shape_line = u32_strdup(shapes_relevant[shape_idx].text->memory);
size_t quality = shapes_relevant[shape_idx].text->num_chars;
while (shape_line != NULL) {
while (shape_line != NULL && quality > 0) {
if (u32_strncmp(cur_pos, shape_line, quality) == 0) {
BFREE(shape_line);
cur_pos = cur_pos + quality;
Expand All @@ -369,6 +371,11 @@ int hmm(shape_line_ctx_t *shapes_relevant, uint32_t *cur_pos, size_t shape_idx,
}
else if (!anchored_right) {
shape_line = shorten(shapes_relevant + shape_idx, &quality, 0, 0, 1);
#ifdef DEBUG
char *out_shape_line = u32_strconv_to_output(shape_line);
fprintf(stderr, "hmm() - shape_line shortened to %d (\"%s\")\n", (int) quality, out_shape_line);
BFREE(out_shape_line);
#endif
}
else {
BFREE(shape_line);
Expand Down Expand Up @@ -533,6 +540,7 @@ static int match_horiz_line(remove_ctx_t *ctx, int hside, size_t input_line_idx,

bxstr_t *input_prepped1 = bxs_from_unicode(prepare_comp_input(input_line_idx, 0, comp_type, 0, NULL, NULL));
bxstr_t *input_prepped = bxs_rtrim(input_prepped1);
bxs_append_spaces(input_prepped, opt.design->shape[NW].width + opt.design->shape[NE].width);
bxs_free(input_prepped1);
#ifdef DEBUG
char *out_input_prepped = bxs_to_output(input_prepped);
Expand Down Expand Up @@ -922,6 +930,7 @@ static void killblank(remove_ctx_t *ctx)
fprintf(stderr, "Killing leading blank line in box body.\n");
#endif
++(ctx->top_end_idx);
--(ctx->body_num_lines);
++lines_removed;
}

Expand All @@ -934,6 +943,7 @@ static void killblank(remove_ctx_t *ctx)
fprintf(stderr, "Killing trailing blank line in box body.\n");
#endif
--(ctx->bottom_start_idx);
--(ctx->body_num_lines);
++lines_removed;
}
}
Expand Down Expand Up @@ -981,10 +991,7 @@ static void remove_top_from_input(remove_ctx_t *ctx)
}
memmove(input.lines + ctx->top_start_idx, input.lines + ctx->top_end_idx,
(input.num_lines - ctx->top_end_idx) * sizeof(line_t));
size_t num_lines_removed = ctx->top_end_idx - ctx->top_start_idx;
input.num_lines -= num_lines_removed;
ctx->bottom_start_idx -= num_lines_removed;
ctx->bottom_end_idx -= num_lines_removed;
input.num_lines -= ctx->top_end_idx - ctx->top_start_idx;
}
}

Expand Down Expand Up @@ -1071,6 +1078,22 @@ static void remove_bottom_from_input(remove_ctx_t *ctx)



static void remove_default_padding(remove_ctx_t *ctx, int num_blanks)
{
if (num_blanks > 0) {
for (size_t body_line_idx = 0; body_line_idx < ctx->body_num_lines; body_line_idx++) {
size_t input_line_idx = ctx->top_start_idx + body_line_idx; /* top_start_idx, because top was removed! */
bxstr_t *temp = bxs_cut_front(input.lines[input_line_idx].text, (size_t) num_blanks);
free_line_text(input.lines + input_line_idx);
input.lines[input_line_idx].text = temp;
}
input.indent -= (size_t) num_blanks;
input.maxline -= (size_t) num_blanks;
}
}



static void apply_results_to_input(remove_ctx_t *ctx)
{
remove_vertical_from_input(ctx);
Expand All @@ -1091,6 +1114,10 @@ static void apply_results_to_input(remove_ctx_t *ctx)
input.indent = input.lines[j].text->indent;
}
}
if (ctx->empty_side[BLEF]) {
/* If the side were not open, default padding would have been removed when the side was removed. */
remove_default_padding(ctx, BMIN((int) input.indent, opt.design->padding[BLEF]));
}

size_t num_lines_removed = BMAX(ctx->top_end_idx - ctx->top_start_idx, (size_t) 0)
+ BMAX(ctx->bottom_end_idx - ctx->bottom_start_idx, (size_t) 0);
Expand Down
3 changes: 2 additions & 1 deletion src/tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,8 @@ char *nspaces(const size_t n)
#if defined(DEBUG) || 0

/**
* Debugging Code: Display contents of input structure
* Debugging Code: Display contents of input structure
* @param heading a heading to show for identification of the printed lines
*/
void print_input_lines(const char *heading)
{
Expand Down

0 comments on commit 80691ef

Please sign in to comment.