Skip to content

Add Uchar.{utf_8_decode_length_of_byte,max_utf_8_decode_length} - #13796

Merged
Octachron merged 2 commits into
ocaml:trunkfrom
dbuenzli:utf-8-decode-tools
Feb 13, 2025
Merged

Octachron merged 2 commits into
ocaml:trunkfrom
dbuenzli:utf-8-decode-tools

Conversation

@dbuenzli

@dbuenzli dbuenzli commented Feb 12, 2025 •

Copy link
Copy Markdown
Contributor

As said in #13760 I'm moving and exposing some UTF-8 decoding functionality from string.ml to Uchar.

In general Uchar.utf_8_decode_length_of_byte is useful when you arrive in the last Uchar.max_utf_8_decode_length bytes of a fixed sized buffer and want to determine if decodes will stay in the current buffer or not.

(There's a also a commit that moves the Pqueue changes entry in the working version, it was added to 5.3)

@nojb nojb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

"since" annotations need to be added.

A second approval is needed before merging.

Comment thread stdlib/uchar.mli Resolved
Comment thread stdlib/uchar.mli Resolved

@Octachron Octachron left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this is the kind of bit counting which is better written down once and for all.

Comment thread stdlib/uchar.mli
(** [utf_8_decode_length_of_byte byte] is the number of bytes, from 1
to {!max_utf8_decode_length}, that a valid UTF-8 decode starting
with byte [byte] would consume or [0] if [byte] cannot start a
valid decode.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks this rewording address my fear that a reader might be surprised that
utf_8_decode_length_of_byte s.(0) (* ≠ 0 *) might be different from Uchar.decode_length (String.get_utf_8_uchar s 0).
Explaining the 0 exceptional case at the end is also nice.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that became my worry too.

@Octachron
Octachron merged commit 23b4f9d into ocaml:trunk Feb 13, 2025
@dbuenzli
dbuenzli deleted the utf-8-decode-tools branch February 13, 2025 14:59
Comment thread stdlib/uchar.mli

val utf_8_decode_length_of_byte : char -> int
(** [utf_8_decode_length_of_byte byte] is the number of bytes, from 1
to {!max_utf8_decode_length}, that a valid UTF-8 decode starting

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops: utf8 => utf_8

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can someone push a fix directly perhaps ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

3 participants