-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversion of String to Vector{UInt8} #24388
Comments
It does seem like observing the mutation of a string should need to involve something |
If we want to be on a safe side then Regarding the names - there are probably people with more experience with naming conventions in Base than me. Maybe we can use In summary the proposal would be to:
If the above are agreed I can make an appropriate PR. |
That does sound good. Another possible name suggestion would be to make this a method of |
Maybe we should add this to triage, since this would be a potentially breaking change. |
This is going to be a performance disaster. The main problem is that |
Changing From my point of view the key problem is to make |
As long as some |
I think the following would be safe and efficient:
|
Being able to "destroy" arrays like that would be generally useful for APIs where a new object "takes control" of the array's contents and anyone else mutating it after that is generally dangerous. And not just for arrays, if we had deterministic finalization a la #7721, being able to make any usage of the finalized object an error after finalization would be ideal. |
With this, we might want to export |
Accepted by triage. We can go with the plan in #24388 (comment). No need to export It would be good to know how many uses of |
This reminds me of a possible If we make |
Another idea: |
|
safer vector<->string conversions, fixing #24388
Current behaviour of julia> VERSION
v"1.0.1"
julia> s, ss = "FOO", SubString("FOO")
("FOO", "FOO")
julia> v, vv = Vector{UInt8}(s), Vector{UInt8}(ss)
(UInt8[0x46, 0x4f, 0x4f], UInt8[0x46, 0x4f, 0x4f])
julia> v[2], vv[2] = 42, 42
(42, 42)
julia> v, vv
(UInt8[0x46, 0x2a, 0x4f], UInt8[0x46, 0x2a, 0x4f])
julia> s, ss
("F*O", "FOO")
julia> s, ss = "FOO", SubString("FOO")
("FOO", "FOO")
julia> v, vv = unsafe_wrap(Vector{UInt8}, pointer(s), ncodeunits(s)),
unsafe_wrap(Vector{UInt8}, pointer(ss), ncodeunits(ss))
(UInt8[0x46, 0x4f, 0x4f], UInt8[0x46, 0x4f, 0x4f])
julia> v[2], vv[2] = 42, 42
(42, 42)
julia> s, ss
("F*O", "F*O") |
Woops, good catch! AFAICT #25241 deprecated Since strings are supposed to be immutable, I'd say it's OK to change this to make a copy in 1.1. That would qualify as a "minor change". |
Consider the following code:
Now changing
v
mutatess
.Since strings should be immutable I would it find it more natural to:
reinterpret(UInt8, s::String)
that would perform such in-place conversion (as this is what one would expect fromreinterpret
);convert(Vector{UInt8}, s::String)
allocate a new vector (so that users do not accidentally mutate strings).Additionally this would be consistent with the general behavior that
Vector{UInt8}(s)
forAbstractString
other thanString
would create a copy.A non-breaking alternative would be to add an information to the documentation that this conversion is in-place.
The text was updated successfully, but these errors were encountered: