# STR -- bounded NUL-terminated byte strings (r003 STAGED candidate)

**RedPill / HD61700 / lib-set-01.** Six source-level implementations, one include per public method. A client includes exactly what it uses. **HD61 assembled, owner emulator capture now available; six-method candidate NOT native-PASS or stable accepted.** Original r001 public contracts retained; private labels/status references revised for HD61 assembly in r002a. Current design basis: `docs/STR_LIBRARY_REQUIREMENTS_PROPOSAL_20261102.md`.

## Selection / composition

```asm
#INCLUDE (lib/str/str_abi.inc)    ; exactly one
#INCLUDE (lib/str/str_copy.inc)   ; needed by STR_APPEND
#INCLUDE (lib/str/str_append.inc)
#INCLUDE (lib/str/str_index.inc)  ; only if needed
```

`STR_APPEND` explicitly calls `STR_COPY` and therefore **requires** its inclusion. Other core methods are self-contained and require `str_abi.inc` for status EQU symbols. No module silently includes other code, and core STR needs no VT22, RS232C, keyboard, LCD, Input or B9 routines.

## Public methods

| Source | Label | Inputs | Outputs |
|---|---|---|---|
| `str_len.inc` | `STR_LEN` | `$10/11` source, `$12` capacity | `$0` status; `$8` length |
| `str_copy.inc` | `STR_COPY` | `$10/11` source, `$12` cap; `$13/14` destination, `$15` cap | `$0` status; `$8` copied text length |
| `str_cmp.inc` | `STR_CMP` | same two bounded input spans | `$0` status; `$8` FFh less, 00h equal, 01h greater |
| `str_append.inc` | `STR_APPEND` | same source and writable destination span | `$0` status; `$8` total text length |
| `str_skipws.inc` | `STR_SKIPWS` | `$10/11` source, `$12` cap, `$13` start offset | `$0` status; `$8` resulting offset, `$9` skipped count |
| `str_index.inc` | `STR_INDEX` | `$10/11` haystack, `$12` cap; `$13/14` needle, `$15` cap | `$0` status; `$8` first zero-based offset |

HD61-compatible EQU names: `STR_OK=0`, `STR_UNTR=1`, `STR_NOSP=2`, `STR_BCAP=3`, `STR_BOFF=4`, `STR_NFND=5`. Semantic statuses: `OK=0`, `UNTERMINATED=1`, `NO_SPACE=2`, `BAD_CAP=3`, `BAD_OFFSET=4`, `NOT_FOUND=5`. Return bytes are zero on error (except the documented success `STR_CMP` FFh result); `$9` has a *public* meaning **only** for `STR_SKIPWS`.

## Representation and environment

- NUL (`00h`) terminates each byte string inside its declared capacity; length excludes NUL, capacity includes it. Zero capacity is rejected. Maximum capacity 255, maximum valid text length 254. Bytes compare unsigned; no locale or Unicode.
- Input pointer pairs are 16-bit little-endian register pairs. `UA` must already select the required RAM bank. Both source/destination must reside in that bank; every declared span must fit without 16-bit address wrap. `$SY` must address a register containing 1 (Lab `$30=1`). STR r003 uses immediate literal zero for initialization and **has no `$SZ` precondition**. The current Lab maps `$SZ` to volatile register `$0` via `PSR SZ,0`; never treat `$SZ` as a literal zero. `SX` selects `$31` and that register must remain zero for callers that use `(IX+$SX)`. Public STR functions leave `$30/$31` unchanged.
- Functions are non-stateful and allocate no private RAM. They do not manage CPU initialization, bank switching, serial or UI. `IX` and flags are clobbered; each method's header lists register effects. Input argument registers are preserved.
- `STR_LEN`, `STR_CMP`, `STR_SKIPWS`, `STR_INDEX` read only and prevalidate all required strings through NUL. `STR_COPY` and `STR_APPEND` preflight before mutation, support overlapping source/destination with memmove-style behavior, and avoid mutation on documented validation/space errors.
- `STR_SKIPWS` recognizes precisely 09h (HT), 0Ah (LF), 0Dh (CR), 20h (space). Start offset must lie from zero through the string's length inclusive. `STR_INDEX` is case-sensitive; empty needle matches at zero, absent match returns `NOT_FOUND`.

## Source checks and native test-ROM candidate

- `src/tests/test_str_core_r002.py`: source-subset interpreter with independent Python reference oracles, 3,772/3,772 cases locally passed. It interprets `SBC`/`SBCW` as **nonmutating compares** rather than arithmetic subtracts. The older r001 test interpreter is superseded for qualification because it interpreted those comparisons incorrectly.
- `src/tests/str_preflight_r002.py`: checks include-once, explicit `STR_APPEND -> STR_COPY` dependency, label duplicates, and negative control fixtures.
- `src/str-test01.s`, `src/apps/str-test01-*.inc/.s`: separate standalone ROM source candidate with 17 deterministic test cases and 24x80 ASCII VT22 report; owner `:t` transmits through RS232C. STR methods themselves do no I/O.
- `src/tests/str_test01_expected.json` and `src/tests/check_str_test01_capture.py`: strict 1968-byte raw capture validator. `src/tests/test_str_rom_vectors.py` verifies all declared test cases through source-subset execution.

**Next gates:** HD61 Rev 0.45 real assembly and listing; owner PB-2000C emulator run; capture raw :t bytes; independent host checker; separately assess AI-1000 and physical hardware. Preserve SHA-256 and provenance. Do not infer native PASS from source test results or an invented report.

*Praeteritum ad futurum servamus.*

## r002a HD61 symbol correction (2026-11-02)

Native assembler reported LABEL Length is Too Long on old `STR_ST_UNTERMINATED`. In this working candidate, all new defined STR/test-app labels are now <=15 characters; status equates are <=8. Public entry points are unchanged. The strict label-length preflight is now required. The over-8-character **input filename** warning for `str-test01.s` is separate and nonfatal. Use the byte-identical generated alias `src/strt01.s` to avoid that filename warning. No native assembly PASS has yet been observed. Details: `docs/STR_R002A_HD61_LABEL_FIX_20261102.md`.

## r003 source correction: volatile SZ alias (2026-11-02)

Owner PB-2000C emulator report supplied 17 real cases: 11 status/value mismatches despite intact sampled buffers and guards. The HD61 assembly listing confirms the compiled source still used `LD $n,$SZ` as a zero initializer. Actual startup `PSR SZ,0` selects **working register `$0`**, the mutable STR status register. A native-aware source interpreter reproduces **17/17 exact owner status/return pairs** on the previous code, including all the errors, while the corrected candidate gives **17/17 expected pairs**. Every STR `LD $n,$SZ` zero initialization was replaced with `LD $n,0`, a literal immediate. This does not change public labels, statuses, API arguments, selected algorithm or any accepted numerical donor. See `docs/STR_R003_NATIVE_ALIAS_DIAGNOSIS_20261102.md` and `src/tests/test_str_native_alias_r003.py`. **Real HD61 reassembly of this new revision and a new owner emulator capture remain pending**. The old 3,772 source tests were falsely favorable because their VM modeled `$SZ` as constant zero; that model was corrected.

**Drive custody:** This r003 version is staged; the existing active `src/lib/str` files remain r002a pending in-place integration (connector replacement blocked). Do not treat this package README as evidence that the active repository was modified.
