Repository navigation
Description
The array-returning cpp.encoding.Utf8.encode returns null for an empty string. It should return a non-null array of length zero, so callers can use the result without treating empty text as missing data.
Reproduced against current main at 5f7cfef66ed4d9543c6c1a2763bdc2148433435c, using Haxe nightly 5.0.0-preview.1+fe63b35 with runtime API 500, MSVC 19.44.35213 on Windows x64 and GCC 13.3 on Linux x86-64 under WSL. Builds use HXCPP_M64, default GC settings and smart strings.
var bytes = cpp.encoding.Utf8.encode("");
trace(bytes == null); // trueThe empty-string branch of Utf8::encode(const String&) returns literal 0 from an Array<uint8_t> function. That constructs a null array. The overload that writes into a caller-provided view correctly returns a byte count of zero; that return value does not work for the array-returning overload.
Return a real empty byte array and keep null-input validation separate. This is a runtime-only fix.
A conventional regression is prepared in test/regression/Utf8EmptyArray/{Main.hx,build.hxml,stdout.txt}. Expected output has all three checks true. Actual output:
empty UTF8 encoding returns an empty array: false
ASCII UTF8 encoding remains intact: true
Unicode UTF8 encoding remains intact: true
class Main {
static function main() {
var empty = true;
var ascii = true;
var unicode = true;
#if (haxe_ver >= 5 && !haxe_5_preview_1)
var bytes = cpp.encoding.Utf8.encode("");
empty = bytes != null && bytes.length == 0;
var plain = cpp.encoding.Utf8.encode("abc");
ascii = plain != null && haxe.io.Bytes.ofData(plain).toString() == "abc";
var wide = cpp.encoding.Utf8.encode("\u0100");
unicode = wide != null && haxe.io.Bytes.ofData(wide).toString() == "\u0100";
#end
Sys.println("empty UTF8 encoding returns an empty array: " + empty);
Sys.println("ASCII UTF8 encoding remains intact: " + ascii);
Sys.println("Unicode UTF8 encoding remains intact: " + unicode);
}
}Build with the Haxe nightly using haxe -main Main -cpp bin -D HXCPP_M64, then run bin/Main (bin/Main.exe on Windows). This exercises API 500; older compiler configurations skip the new encoding API.
Activity
Fix three cpp.encoding.Utf8.encode faults at API level 500
Fix three cpp.encoding.Utf8.encode faults at API level 500