Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions std/cpp/_std/haxe/io/Bytes.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
10 changes: 9 additions & 1 deletion std/cpp/encoding/Utf8.hx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ extern class Utf8 {
*/
static overload function encode(codepoint:Char32, buffer:View<UInt8>):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<UInt8>;

/**
* Decodes all bytes in the buffer into a string. An empty string is returned if the buffer is empty.
*/
Expand All @@ -66,4 +74,4 @@ extern class Utf8 {
* @return Number of bytes read to decode the codepoint.
*/
static function codepoint(buffer:View<UInt8>):Char32;
}
}
Loading