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 +}