Skip to content

printf/format_string %Ns width padding uses byte length, not display width #154

Description

@tobert

Found while reviewing #130 (REPL table/column alignment had the same class of bug).

`apply_string_padding` in `crates/kaish-kernel/src/tools/builtin/format_string.rs` (around line 513-516) computes the padding for `printf`'s `%Ns` width specifier using `val.len()` — UTF-8 byte count, not display width:

```rust
fn apply_string_padding(spec: &FormatSpec, val: &str, output: &mut String) {
let width = spec.width.unwrap_or(0);
if width > val.len() {
let pad_count = width - val.len();
...
```

A CJK or emoji argument to `printf '%10s'` will be over-padded (byte length > display width), misaligning output the same way the REPL's table renderer did pre-#130.

Fix shape: use `unicode_width::UnicodeWidthStr::width()` for the padding calculation, same as #130's fix in `kaish-repl/src/format.rs`. Would need unicode-width as a kaish-kernel dependency (already used by kaish-repl).

Low priority — narrow surface (printf %Ns with non-ASCII args specifically), but same bug class as #130 so worth tracking.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions