From 7c94e43a22fd3eca3938643f8bdcb4b353df1aec Mon Sep 17 00:00:00 2001 From: Thomas Jensen Date: Tue, 19 Dec 2023 21:58:42 +0100 Subject: [PATCH] Fix some box removal bugs --- src/remove.c | 36 +++++++++++++++++++++++++++++++----- src/tools.c | 3 ++- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/remove.c b/src/remove.c index dde25fe7..1871a2bb 100644 --- a/src/remove.c +++ b/src/remove.c @@ -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; @@ -347,7 +348,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; @@ -369,6 +370,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); @@ -476,6 +482,7 @@ match_result_t *match_outer_shape(int vside, bxstr_t *input_line, bxstr_t *shape if (bxs_is_blank(shape_line)) { return new_match_result(input_line->memory, 0, 0, 1); } + // FIXME 'spring' box NW shape is not matched because it can't be shortened right -> shortening for (uint32_t *s = shape_line->memory; s == shape_line->memory || is_blank(*s); s++) { uint32_t *p = u32_strstr(input_line->memory, s); size_t p_idx = p != NULL ? p - input_line->memory : 0; @@ -922,6 +929,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; } @@ -934,6 +942,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; } } @@ -981,10 +990,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; } } @@ -1071,6 +1077,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); @@ -1091,6 +1113,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); diff --git a/src/tools.c b/src/tools.c index 2140e784..ccdc4c27 100644 --- a/src/tools.c +++ b/src/tools.c @@ -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) {