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-oldcase that inserts at each boundary, walked on lead bytes, withcount("") == ncp + 1),split(sep),join, andzfill(width counted inncp, the sign staying in front of the fill). - Code-point arithmetic, exact (where an index is the answer):
find/rfindbyte-search then convert the answer to CPython's code-point index;s[i]walks to the i-th lead byte (ASCII fast path whennbytes == ncp), bounds-checked in generated code with CPython's exactstring index out of rangetext. -
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, andcapitalize()lowering the tail), the sixis*predicates (all False on"";islower/isupperneed one cased byte), the no-argument strips (trapping only when the byte at a stripped boundary is ≥ 0x80; interior non-ASCII is fine), andsplit()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)andrstrip(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)andreplace(old, new, count)take CPython's -1 default for "all". Thestartandendoffind,rfind,countandindexclamp as a slice's bounds do, past 64 bits included (A191).indexandrindexarefindandrfind, raising CPython'sValueError("substring not found")where those return -1. - Padding.
center,ljustandrjustreuse the f-string padder (0039), withcenter'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()andsplitlines(keepends)split at CPython's line boundaries, which are more than\n:\r\nas one,\r,\v,\f,\x1cto\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 besidesplit, forsplit'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
Stris already NUL-terminated UTF-8, so encoding is byte-copying. It must copy, because thebytesconstructor borrows the buffer by design and a countedStrcan be released while the bytes value lives; retaining instead would trade a dangling pointer for a lying counter. - Decode validates, and raises
ValueError. CPython'sUnicodeDecodeErroris aValueError, 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.decodeby 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
wordfreqfrom 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.