Skip to content

Commit 0bb8417

Browse files
committed
[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.
1 parent 8148309 commit 0bb8417

2 files changed

Lines changed: 10 additions & 9 deletions

File tree

std/cpp/_std/haxe/io/Bytes.hx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,7 @@ class Bytes {
253253
if (Ascii.isEncoded(s)) {
254254
return s.asCharView().asBytesView().toBytes();
255255
} else {
256-
final count = Utf8.getByteCount(s);
257-
final bytes = Bytes.alloc(Int64.toInt(count));
258-
259-
if (Utf8.encode(s, bytes.asView()) != count) {
260-
throw new haxe.Exception('Failed to encode string to UTF8');
261-
} else {
262-
return bytes;
263-
}
256+
return Bytes.ofData(Utf8.encode(s));
264257
}
265258
}
266259

std/cpp/encoding/Utf8.hx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ extern class Utf8 {
5252
*/
5353
static overload function encode(codepoint:Char32, buffer:View<UInt8>):Int;
5454

55+
/**
56+
* Encodes all characters in the string to UTF-8 bytes.
57+
*
58+
* @param string String to encode.
59+
* @return Array containing UTF-8 bytes.
60+
*/
61+
static overload function encode(string:String):Array<UInt8>;
62+
5563
/**
5664
* Decodes all bytes in the buffer into a string. An empty string is returned if the buffer is empty.
5765
*/
@@ -66,4 +74,4 @@ extern class Utf8 {
6674
* @return Number of bytes read to decode the codepoint.
6775
*/
6876
static function codepoint(buffer:View<UInt8>):Char32;
69-
}
77+
}

0 commit comments

Comments
 (0)