ocaml: phase 6 Printf width specifiers %5d/%-5d/%05d/%4s (+5 tests, 538 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s

The Printf walker now parses optional flags + width digits between
'%' and the spec letter:

  -  left-align (default is right-align)
  0  zero-pad (default is space-pad; only honoured when not left-aligned)
  Nd... decimal width digits (any number)

After formatting the argument into a base string with the existing
spec dispatch (%d/%i/%u/%s/%f/%c/%b/%x/%X/%o), the result is padded
to the requested width.

Workaround: width and spec_pos are returned packed as
  width * 1000000 + spec_pos
because the parser does not yet support tuple destructuring in let
('let (a, b) = expr in body' fails with 'expected ident'). TODO: lift
that limitation; for now the encoding round-trips losslessly for any
practical width.

  Printf.sprintf '%5d'  42      = '   42'
  Printf.sprintf '%-5d|' 42     = '42   |'
  Printf.sprintf '%05d' 42      = '00042'
  Printf.sprintf '%4s' 'hi'     = '  hi'
  Printf.sprintf 'hi=%-3d, hex=%04x' 9 15 = 'hi=9  , hex=000f'
This commit is contained in:
2026-05-09 03:25:50 +00:00
parent cb14a07413
commit 7e64695a74
3 changed files with 108 additions and 25 deletions

View File

@@ -407,6 +407,17 @@ _Newest first._
binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree *
'a tree`) with insert + in-order traversal. Tests parametric ADT,
recursive match, List.append, List.fold_left.
- 2026-05-09 Phase 6 — Printf width specifiers `%5d` / `%-5d` /
`%05d` / `%4s` etc. (+5 tests, 538 total). Walker now parses
optional `-` (left-align) and `0` (zero-pad) flags after `%`, then
optional decimal width digits, then the spec letter. After
formatting the arg into a base string, pads to the width using
spaces (or zeros if `0` flag and not `-`). Encoded width+spec_pos
return as `width * 1000000 + spec_pos` because the parser does not
yet support tuple destructuring in `let` (TODO: lift that
limitation; for now this round-trips losslessly for any practical
width). Examples: `%5d` 42 = " 42", `%-5d|` 42 = "42 |",
`%05d` 42 = "00042".
- 2026-05-09 Phase 6 — Printf.sprintf adds %i, %u (aliases of %d),
%x (lowercase hex), %X (uppercase hex), %o (octal) (+5 tests, 533
total). New host primitives `_int_to_hex_lower`, `_int_to_hex_upper`,