use cppsize
when constructing String
from StdString
#378
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently,
std::string
s that have null bytes in them cannot be converted toString
s without truncation using the StdLib-providedString
constructor.Base.unsafe_string
with one argument assumes the pointed-to string is null-terminated, butstd::string
s are not necessarily null-terminated.Base.unsafe_string
supports an optional second argument (thanks @omus for pointing this out!) which is the number of bytes in the pointed-to string; this PR usescppsize
to get that. I've also added some small tests to confirm how null-byte containing strings are handled (from theString
-to-std::string
direction and vice-versa); I didn't change the defaultStdString
constructor to use the length of the juliaString
, although IMO whether that default should be changed is IMO up for debate, but given that it's already supported and thelength
-less method is the default I'm assuming that's for a good reason.This change is technically breaking: if you were relying on the (IMO bugged) behavior of
String(::StdString)
discarding anything starting from the first null byte, you'll get different behavior. How you want to handle the version bump here is up to you; I'd probably release it as a patch (bugfix) but for something that's deep in some fiddly codebases it might be better to be safe rather than sorry here.If you do want to make a breaking release with this, I'd also change the handling of the
String
-to-StdString
method to use the string length by default, but as with this change that's breaking.The handling of null bytes has bitten us quite a few times though, interacting with APIs taht generate strings with null bytes in them introduces very weird and difficult to debug errors.