Skip to content

Commit 1751d89

Browse files
Kasper JungeRalphify
authored andcommitted
workspace: record BOM-guard drop in parse_frontmatter
Co-authored-by: Ralphify <noreply@ralphify.co>
1 parent a6f4c47 commit 1751d89

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# `_frontmatter.py` coverage
2+
3+
Valid at: a6f4c47
4+
5+
## Recent changes
6+
7+
- a6f4c47 — dropped the `if text.startswith(_UTF8_BOM):` guard in
8+
`parse_frontmatter` before the `text = text.removeprefix(_UTF8_BOM)`
9+
call. Python's `str.removeprefix` is already a no-op (returns the
10+
same string object) when the prefix is absent, so the guard was
11+
purely decorative dead code. Behavior preserved:
12+
- BOM-prefixed input still gets stripped (pinned by
13+
`test_utf8_bom_does_not_break_frontmatter` in
14+
`tests/test_frontmatter.py`).
15+
- Non-BOM input is passed through unchanged (exercised by every
16+
other parse test in the file).
17+
- The CPython implementation returns the same object identity when
18+
no prefix match occurs, so there's no allocation overhead either.
19+
20+
## Shape of the module
21+
22+
- `parse_frontmatter(text)` — public entry point. Strips optional
23+
UTF-8 BOM, splits on `---` delimiters via `_extract_frontmatter_block`,
24+
runs `yaml.safe_load`, strips HTML comments from the body with
25+
`_strip_html_comments`, returns `(dict, body)`.
26+
- `serialize_frontmatter(frontmatter, body)` — inverse; emits
27+
`---`-delimited blocks only when the frontmatter is non-empty *or*
28+
the body would otherwise be mis-parsed as frontmatter.
29+
- Constants: `RALPH_MARKER`, `FIELD_*`, `CMD_FIELD_*`, `NAME_RE`,
30+
`VALID_NAME_CHARS_MSG`. All imported by `cli.py` and
31+
`_resolver.py`; each one is reused across modules so centralisation
32+
is justified.
33+
34+
## Verified live (grepped, confirmed used)
35+
36+
- `_FRONTMATTER_DELIMITER` — used 4× in `serialize_frontmatter` (plus
37+
2× in `_extract_frontmatter_block`).
38+
- `_FENCE_OR_COMMENT_RE` — used in `_strip_html_comments`.
39+
- `_UTF8_BOM` — used in `parse_frontmatter` (BOM strip).
40+
41+
## Potential future wins (not yet taken)
42+
43+
- `_extract_frontmatter_block` splits `text` on `"\n"` up front, then
44+
re-joins slices for the body — the body slice is re-joined even when
45+
the input is tiny. Could stream with `str.find` + `str.index` to
46+
avoid the list allocation, but this function runs once per
47+
iteration and the input is ~1 KB in practice; not worth the churn.
48+
- The `serialize_frontmatter` `needs_delimiters` expression uses
49+
`body.lstrip().startswith(...)` which allocates a new stripped
50+
string just to check a prefix. Could collapse via
51+
`re.match(r"\s*---", body)` but the current form reads cleanly;
52+
revisit only if this becomes hot.

workspace/ralphs/improve-codebase/iterations.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
One line per iteration: `<sha> <summary>`.
44

5+
a6f4c47 refactor: drop redundant BOM-startswith guard before `removeprefix` in `parse_frontmatter``str.removeprefix` already returns the string unchanged when the prefix is absent, so the `if text.startswith(_UTF8_BOM):` wrapper was dead defensive code. Behavior preserved — `test_utf8_bom_does_not_break_frontmatter` still pins the BOM-stripping path and every other test exercises the no-BOM path. Same "drop the guard when the operation already handles the no-match case" shape as 5337d88 (empty dict → `" · ".join([])`), 4ccfa9a (empty list → `" · ".join([])`), and 8cb0d47 (redundant `max(..., 1)` floor).
6+
57
3a8908d refactor: use `next(reversed(...), None)` in `enter_fullscreen` history fallback — the compound `if initial_id is None and self._iteration_history:` guard mixed two concerns (only fall back when nothing is live, *and* avoid `next(reversed({}))` raising StopIteration). Switching to the `next(it, default)` sentinel idiom lets the standard default handle empty-dict case so the outer `if` only encodes the "fall back when nothing live" intent. Behavior preserved — the existing `if initial_id is None or panel_for(...) is None:` next branch still prints "no iterations yet" and returns False, covered by `test_enter_without_iteration_prints_hint`.
68

79
59b0e34 refactor: inline `_fullscreen_page_size()` into the space/b lambdas in `_handle_fullscreen_key` — the `page` local was computed unconditionally in the non-exit branch but only consumed by the page-down (" ") and page-up ("b") action lambdas. j/k/g/G/[/] now skip the call entirely; space/b still compute it exactly once per keypress, now at action-invocation time under the same lock. Same Phase 4 "narrow the scope of a one-branch local" shape as 134078d / ef176bf / b19625e.

0 commit comments

Comments
 (0)