You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
length, substr, index and . count bytes, not characters, in a UTF-8 locale #151
gawk 5.3.2 prints 5 3 é, five and HÉLLO. POSIX defines these in characters, and substr here cuts é in half, so the output is not valid UTF-8. With LC_ALL=C, gawk counts bytes too (6).
length, substr, index work on bytes in interpreter/src/builtins.rs, and the regex is byte based. The lexer already reads the locale for \u escapes (lexer/src/locale_encoding.rs), so the locale is known.
Is character semantics in a UTF-8 locale planned? I can send a PR for the string built-ins if you want it.
Sorry for the delay, still a bit busy with exams until Wednesday 😅
That was a (slightly intentional) oversight. The problem is we need to treat all our strings as maybe UTF-8, so Rust strings are not suitable. It seems the recent Utf8Chunks std API is nice for our use cases, so we should use it to build a wrapper over &[u8] that enforces the correct semantics; that is, character units are UTF-8 codepoints when valid, or the individual bytes of the invalid chunks.
It'd be convenient to expose the character iterator as a flattened iterator version of Utf8Chunks, so maybe we have to build a wrapper type that's something like a "maybe valid char" (char or a zero-extended u8). This item could either be a newtype u32 or an union; the former is probably a safer bet, but I think it hardly matters since we have to treat it as a byte slice the moment we cross an I/O boundary or pass it onto the regex engine.
Because character iteration and other operations become consistent with this, we should be able to forbid byte-level indexing (or I would hope). We can still subslice with our Span type, which is just the new Range<usize>; this should be trivial to implement since our wrapper would be closed under arbitrary slicing of the inner byte slice. Ideally, this should be enough to allow us to map all input from vm/io.rs eagerly to this string type, since our MinRX layer returns Spans.
It would be intended of this wrapper to banish raw byte strings off of the interpreter. The rest of the codebase is a non-issue since it just use byte slices to represent user strings, but we can study that at another time.
Activity
Alonely0 commentedon Oct 31, 2026
Sorry for the delay, still a bit busy with exams until Wednesday 😅
That was a (slightly intentional) oversight. The problem is we need to treat all our strings as maybe UTF-8, so Rust strings are not suitable. It seems the recent
Utf8Chunksstd API is nice for our use cases, so we should use it to build a wrapper over&[u8]that enforces the correct semantics; that is, character units are UTF-8 codepoints when valid, or the individual bytes of the invalid chunks.It'd be convenient to expose the character iterator as a flattened iterator version of
Utf8Chunks, so maybe we have to build a wrapper type that's something like a "maybe valid char" (charor a zero-extendedu8). This item could either be a newtypeu32or anunion; the former is probably a safer bet, but I think it hardly matters since we have to treat it as a byte slice the moment we cross an I/O boundary or pass it onto the regex engine.Because character iteration and other operations become consistent with this, we should be able to forbid byte-level indexing (or I would hope). We can still subslice with our
Spantype, which is just the newRange<usize>; this should be trivial to implement since our wrapper would be closed under arbitrary slicing of the inner byte slice. Ideally, this should be enough to allow us to map all input fromvm/io.rseagerly to this string type, since our MinRX layer returnsSpans.It would be intended of this wrapper to banish raw byte strings off of the interpreter. The rest of the codebase is a non-issue since it just use byte slices to represent user strings, but we can study that at another time.