Skip to content

ADR 0043: The str method surface

Status: Final Type: Feature Created: 2026-08-04 Consolidated: 2026-08-07, absorbing 0052 (encode/decode; retired) Amended-By: 0069 (a split word used only as a dict key is looked up by its span of the string) Related-ADRs: 0025 (the header the domains rest on), 0026 (the ownership discipline all producers join), 0015 (the bytes side of the codec pair), 0044 (slicing, shared with lists), 0028 (the ASCII-repr domain rule this reuses). Provenance: see arbitrations A75, A129, A141.

Context

String methods are where a compiled subset most easily goes wrong: CPython's case conversions, predicates and whitespace rules follow Unicode tables no small runtime ships. The rule adopted here is that each method sits on exactly one of three semantic domains, chosen per method. The domains are byte-exact for all UTF-8, code-point-exact where an index is the answer, and ASCII-only with a runtime trap where a Unicode table would be needed. No method is approximated.

Decision

The domains, per method

  • Byte-exact for all UTF-8 input (substring search and splice cannot match across a code-point boundary): count, in, startswith, endswith, replace (including the empty-old case that inserts at each boundary, walked on lead bytes, with count("") == ncp + 1), split(sep), join, and zfill (width counted in ncp, the sign staying in front of the fill).
  • Code-point arithmetic, exact (where an index is the answer): find/rfind byte-search then convert the answer to CPython's code-point index; s[i] walks to the i-th lead byte (ASCII fast path when nbytes == ncp), bounds-checked in generated code with CPython's exact string index out of range text.
  • The ASCII domain, trapping beyond it (where Unicode tables would be needed): the five case conversions (including title()'s uppercase-after-any-non-letter rule, apostrophes and all, and capitalize() lowering the tail), the six is* predicates (all False on ""; islower/isupper need one cased byte), the no-argument strips (trapping only when the byte at a stripped boundary is ≥ 0x80; interior non-ASCII is fine), and split() with no argument (Unicode whitespace is a table). The trap names the method and the byte.

  • The optional arguments, each CPython's. strip(chars), lstrip(chars) and rstrip(chars) strip whole code points; a code point is in chars iff its UTF-8 bytes occur there, which is exact because a complete sequence cannot match across a boundary. split(sep, maxsplit) and replace(old, new, count) take CPython's -1 default for "all". The start and end of find, rfind, count and index clamp as a slice's bounds do, past 64 bits included (A191). index and rindex are find and rfind, raising CPython's ValueError("substring not found") where those return -1.

  • Padding. center, ljust and rjust reuse the f-string padder (0039), with center's own tie-break: it puts the odd column on the left when the width is odd, which ^ does not. The fill character is a one-character ASCII literal, the padder's domain.
  • splitlines() and splitlines(keepends) split at CPython's line boundaries, which are more than \n: \r\n as one, \r, \v, \f, \x1c to \x1e, U+0085, U+2028 and U+2029. They are matched on their UTF-8 bytes, so nothing traps; a boundary ends a line and does not separate two ("a\n" is one line, "" none). The helper lives in the list unit beside split, for split's reason.
  • "...".format(...) on a literal format string is rewritten to the f-string it means and lowered as one, for positional and named fields. The rewrite keeps CPython's evaluation of each argument once and in order: either every argument is used once in order, or every argument is a name or a constant. Anything else is refused.

Arity is exact: a form a method has in CPython and this ADR does not lower is refused by name, since a dropped argument would give different bytes on stdout.

Dispatch and placement

Dispatch keys on the receiver's value being a Str, so a field read, a method result and a subscript result all take methods with no special case anywhere (d.text.upper(), s.split(",")[0]). split and join live in the list unit, being the only methods that touch a list; a Str-unit home would make nearly all programs link the list runtime for two methods' sake.

for w in s.split(): walks the words without making the list. A loop over the no-argument split() of a str finds each word's start and end and makes the word a fresh Str, as each element of the list is; nothing else can see the list, so only its allocation goes. split() reads the whole string before the loop starts. The loop holds a reference to the string until it ends, so a body that rebinds the name the string came from walks the words it would have. If the string is non-ASCII, the first step traps before the body runs, at the byte split() traps on (A75). The receiver is evaluated once, before the loop knows the type. A word used only as a dict key is not made at all (0069).

encode and decode

Only the default codec exists. The explicit "utf-8" spelling is accepted as the same thing. Any other codec argument is refused by name.

  • Encode copies. A Str is already NUL-terminated UTF-8, so encoding is byte-copying. It must copy, because the bytes constructor borrows the buffer by design and a counted Str can be released while the bytes value lives; retaining instead would trade a dangling pointer for a lying counter.
  • Decode validates, and raises ValueError. CPython's UnicodeDecodeError is a ValueError, so the exception vocabulary does not grow. The validator walks the bytes once (counting code points for the allocator) and reproduces CPython's three message shapes byte-for-byte: 'utf-8' codec can't decode byte 0xNN in position N: invalid start byte / invalid continuation byte / unexpected end of data. The raise happens in the runtime. The raiser analysis learns .decode by method name, because a raise synthesized below the source AST must be visible to the scan or the exception is swallowed at the first frame boundary.
  • An embedded NUL in decoded bytes traps, naming the byte and position. This is a recorded, bounded, one-sided divergence from CPython, and it avoids a silent truncation.

Consequences

  • An accepted method call prints CPython's bytes on all backends. A refused form names what it needs. The trap boundary is a runtime property stated per method.
  • The domain assignment is the maintenance contract: a new method lands by choosing a domain first. A method that cannot sit exactly on one of the three does not land.
  • The word walk took 0067's wordfreq from 2.84× to 2.36× of C on c, since a list of 20,000 words was its largest allocation.
  • Producers join the ownership discipline as they are written (a split piece is a fresh reference the producer must release after the list retains it). The derived producer-classification guards hold the enumeration.

Alternatives considered

  • Approximate Unicode behaviour (ASCII rules applied to all input): rejected without discussion. Silently different output on non-ASCII input is the forbidden outcome; the trap converts it to a positioned stop.
  • Shipping Unicode tables: deferred. It would lift the ASCII domain wholesale when a program demands it; until then it is weight without a customer.
  • A general codec registry for encode/decode: rejected. One codec exists in the corpus, so a registry would be a second dispatch mechanism for a set of one.