I was playing around with Dart strings and noticed this:
print("\x00nullbyte".length);
print("\x00nullbyte");
If you run this, you'll find that the length is 9, which includes the null byte. But there is no output.
Trusting Google engineers more than myself, programming-wise, I'm thinking there might be a reason for that. What could it be?
The Dart string has length 9, and contains all nine code units. NUL characters are perfectly valid in a Dart string. They are not valid in C strings though, where they mark the end of the string. When printing, the string is eventually converted to a C-string to call the system library's output function. At that point, the system library sees only the NUL character and prints nothing.
Try:
main() { print("ab\x00cd"); } // prints "ab".
The String.length function works entirely on the Dart String object, and doesn't go through the C strlen function. It's unaffected by the limits of C.
Arguably, the Dart print functionality should detect NUL characters and print the rest of the string anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With