Land a character written past a short row's terminator

A grid row is a NUL-terminated string, and `putchar_at()` wrote the character,
advanced and terminated -- so `CHAR 1, 40, 1, "#"` on an otherwise empty row
stored the `#` at column 40 with `text[1][0]` still `'\0'`, and the render loop,
which stops at the terminator, drew nothing at all.

The silent nothing is the trap. The write succeeded, the cursor moved, the
stdout mirror showed the character, and only the window stayed blank -- so the
program looked right everywhere except where it mattered. The documented
truncation on the way *back* is fine and is unaffected.

`putchar_at()` now fills the gap with spaces before storing.

**Padded there rather than in `sink_moveto()`**, which TODO.md proposed: this way
`moveto` stays read-only -- the objection that entry raised against its own
suggestion -- and a row is only ever padded when a character actually arrives.

The pad fills from the terminator rather than replacing it. The buffer is not
cleared between rows, so replacing only the terminator would expose the tail of
whatever longer row used to be there: writing "ABCDEFGHIJ", truncating it to
"ABCX", then writing at column 7 must give "ABCX   Z" and not "ABCX FGZ".
tests/akgl_backends.c asserts all three cases, and the middle one keeps the
truncation pinned.

TODO.md section 6 item 32, struck.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-02 00:13:42 -04:00
parent 8594d8471d
commit a1dcfcacf3
5 changed files with 89 additions and 7 deletions

View File

@@ -305,6 +305,53 @@ static akerr_ErrorContext AKERR_NOIGNORE *test_sink_grid_and_wrap(void)
SUCCEED_RETURN(errctx);
}
/**
* @brief A character placed past a short row's end pads rather than vanishing.
*
* A row is a NUL-terminated string, so `CHAR 1, 6, 0, "#"` on an otherwise empty
* row used to store the `#` at column 6 with `text[0][0]` still `'\0'` -- and
* the render loop, which stops at the terminator, drew nothing at all. The write
* succeeded, the cursor moved, the stdout mirror showed the character, and the
* window stayed blank. That silent nothing is the trap; the documented
* truncation on the way back is fine and is asserted here too.
*
* The padding has to be spaces rather than whatever is in the buffer, because
* the buffer is not cleared between rows: the gap holds the tail of some longer
* row that used to be here, which is the second case below.
*
* TODO.md section 6 item 32.
*/
static akerr_ErrorContext AKERR_NOIGNORE *test_sink_writes_past_a_short_row(void)
{
PREPARE_ERROR(errctx);
PASS(errctx, akbasic_sink_init_akgl(&AKGLSINK, &AKGLSINKSTATE, akgl_renderer, font,
TARGET_SIZE, TARGET_SIZE));
PASS(errctx, AKGLSINK.moveto(&AKGLSINK, 6, 0));
PASS(errctx, AKGLSINK.write(&AKGLSINK, "#"));
TEST_REQUIRE_STR(AKGLSINKSTATE.text[0], " #");
/*
* Writing back over a long row still truncates it -- that half is the
* documented behaviour and the reason a program builds a row whole.
*/
PASS(errctx, AKGLSINK.clear(&AKGLSINK));
PASS(errctx, AKGLSINK.write(&AKGLSINK, "ABCDEFGHIJ"));
PASS(errctx, AKGLSINK.moveto(&AKGLSINK, 3, 0));
PASS(errctx, AKGLSINK.write(&AKGLSINK, "X"));
TEST_REQUIRE_STR(AKGLSINKSTATE.text[0], "ABCX");
/*
* And a write past *that* terminator pads with spaces rather than exposing
* the "EFGHIJ" still sitting in the buffer behind it. A pad that only
* replaced the terminator itself would leave those visible.
*/
PASS(errctx, AKGLSINK.moveto(&AKGLSINK, 7, 0));
PASS(errctx, AKGLSINK.write(&AKGLSINK, "Z"));
TEST_REQUIRE_STR(AKGLSINKSTATE.text[0], "ABCX Z");
SUCCEED_RETURN(errctx);
}
/** @brief The sink's text actually reaches the target when the host renders. */
static akerr_ErrorContext AKERR_NOIGNORE *test_sink_renders(void)
{
@@ -562,6 +609,7 @@ int main(void)
CATCH(errctx, test_paint_fills_region());
CATCH(errctx, test_shape_roundtrip());
CATCH(errctx, test_sink_grid_and_wrap());
CATCH(errctx, test_sink_writes_past_a_short_row());
CATCH(errctx, test_sink_renders());
CATCH(errctx, test_input_backend());
CATCH(errctx, test_sprite_from_file());