From 0bb84177daea64d90d34520a0ab5298ae6a763b7 Mon Sep 17 00:00:00 2001 From: Tobiasz Laskowski Date: Thu, 5 Feb 2026 18:58:19 +0000 Subject: [PATCH] [cpp] Optimise Bytes.ofString utf8 To use the current Utf8.encode method, we have to run getByteCount which iterates through the string to create a buffer which is the right size. However, since Utf8.encode is a public function, it has to validate that the size of the buffer is correct, which means it iterates through the string again. This new Utf8.encode method without a buffer argument creates its own array and returns it, avoiding the unnecessary check. --- std/cpp/_std/haxe/io/Bytes.hx | 9 +-------- std/cpp/encoding/Utf8.hx | 10 +++++++++- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/std/cpp/_std/haxe/io/Bytes.hx b/std/cpp/_std/haxe/io/Bytes.hx index e9ed48d60b8..655aded2827 100644 --- a/std/cpp/_std/haxe/io/Bytes.hx +++ b/std/cpp/_std/haxe/io/Bytes.hx @@ -253,14 +253,7 @@ class Bytes { if (Ascii.isEncoded(s)) { return s.asCharView().asBytesView().toBytes(); } else { - final count = Utf8.getByteCount(s); - final bytes = Bytes.alloc(Int64.toInt(count)); - - if (Utf8.encode(s, bytes.asView()) != count) { - throw new haxe.Exception('Failed to encode string to UTF8'); - } else { - return bytes; - } + return Bytes.ofData(Utf8.encode(s)); } } diff --git a/std/cpp/encoding/Utf8.hx b/std/cpp/encoding/Utf8.hx index eb247a1c204..d529c83c42a 100644 --- a/std/cpp/encoding/Utf8.hx +++ b/std/cpp/encoding/Utf8.hx @@ -52,6 +52,14 @@ extern class Utf8 { */ static overload function encode(codepoint:Char32, buffer:View):Int; + /** + * Encodes all characters in the string to UTF-8 bytes. + * + * @param string String to encode. + * @return Array containing UTF-8 bytes. + */ + static overload function encode(string:String):Array; + /** * Decodes all bytes in the buffer into a string. An empty string is returned if the buffer is empty. */ @@ -66,4 +74,4 @@ extern class Utf8 { * @return Number of bytes read to decode the codepoint. */ static function codepoint(buffer:View):Char32; -} \ No newline at end of file +}