Skip to content

utf8.c: do not encode or decode after the terminating NUL byte - #285

Closed
Explorer09 wants to merge 1 commit into
jquast:masterfrom
Explorer09:utf8-decode-nul-byte
Closed

Explorer09 wants to merge 1 commit into
jquast:masterfrom
Explorer09:utf8-decode-nul-byte

Conversation

@Explorer09

Copy link
Copy Markdown
Contributor

A C string can terminate before the specified string size (n). By terminating the decode operation at the NUL byte, this function can be more useful for encoding and decoding strings in C.

This change affects function behavior.

The client code can enclose the wcwidth_decode_u32() call within a loop to retain the old behavior. On the other hand, without this behavior change, the client code will have to call strlen() (or strnlen()) to ensure it doesn't over-specify the length of the string. Such a procedure is more tedious.

A C string can terminate before the specified string size (`n`).
By terminating the decode operation at the NUL byte, this function can
be more useful for encoding and decoding strings in C.

**This change affects function behavior.**

The client code can enclose the wcwidth_decode_u32() call within a loop
to retain the old behavior. On the other hand, without this behavior
change, the client code will have to call strlen() (or strnlen()) to
ensure it doesn't over-specify the length of the string. Such a
procedure is more tedious.

Signed-off-by: Kang-Che Sung <[email protected]>
@jquast

jquast commented Oct 2, 2026

Copy link
Copy Markdown
Owner

This PR contains an error, using assignment = instead of equality, ==

But also that the behavior is intentional ! As a library for measuring strings for terminal width, NUL can be sent to a terminal, even used for timed delays in some cases, and so NUL is measured, as zero-width.

We do wish to be able to measure strings that contain NUL,

https://wcwidth.readthedocs.io/en/latest/libwcwidth.html#string-length-conventions

There is no NUL-terminated sentinel form; pass strlen(text) when the text is a C string. The length is authoritative: a NUL is an ordinary zero-width character that may appear anywhere, and it survives into transform output, whose *out_len is the true length.

https://wcwidth.readthedocs.io/en/latest/api_c.html#c.wcswidth_u8

n – number of UTF-8 bytes to process. Exactly n bytes are read, never treating the text as NUL-terminated; pass strlen(utf8) for a C string.

I will duplicate this documented behavior in the docstring of wcwidth_decode_u32, wcwidth_encode_u32.

@jquast jquast closed this Oct 2, 2026
jquast added a commit that referenced this pull request Oct 2, 2026
Try to prevent PR's like #285 by duplicating documentation about it, and making it shorter and more
direct
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants