Skip to content

length, substr, index and . count bytes, not characters, in a UTF-8 locale #151

Open

Description

@jayhemnani9910

In a UTF-8 locale, string functions and regexps treat each byte as a character:

$ echo héllo | awk '{print length($0), index($0, "l"), substr($0, 2, 1)}'
6 4 �
$ echo héllo | awk '/^.....$/ {print "five"}'
$ echo héllo | awk '{print toupper($0)}'
HéLLO

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.

Checked on main 67e0816, LANG=en_US.UTF-8.

Activity

  1. Alonely0 commented on Oct 31, 2026

    @Alonely0
    Collaborator

    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.

  2. added
    bug
    on Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions