Skip to content

Commit

Permalink
Add Compat.StringVector (#348)
Browse files Browse the repository at this point in the history
* Add Compat.StringVector
  • Loading branch information
TotalVerb authored and stevengj committed Apr 19, 2017
1 parent 47f5741 commit 49e0235
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ Currently, the `@compat` macro supports the following syntaxes:

* `bswap` is supported for `Complex` arguments on 0.5 and below. ([#21346])

* `Compat.StringVector` is supported on 0.5 and below. On 0.6 and later, it aliases `Base.StringVector`. This function allocates a `Vector{UInt8}` whose data can be made into a `String` in constant time; that is, without copying. On 0.5 and later, use `String(...)` with the vector allocated by `StringVector` as an argument to create a string without copying. Note that if 0.4 support is needed, `Compat.UTF8String(...)` should be used instead. ([#19449])

## Renamed functions

* `pointer_to_array` and `pointer_to_string` have been replaced with `unsafe_wrap(Array, ...)` and `unsafe_wrap(String, ...)` respectively
Expand Down
7 changes: 7 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1464,6 +1464,13 @@ if VERSION < v"0.6.0-pre.beta.102"
Base.bswap(z::Complex) = Complex(bswap(real(z)), bswap(imag(z)))
end

# https://github.com/JuliaLang/julia/pull/19449
if VERSION < v"0.6.0-dev.1988"
StringVector(n::Integer) = Vector{UInt8}(n)
else
using Base: StringVector
end

include("to-be-deprecated.jl")

end # module Compat
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,15 @@ let zbuf = IOBuffer([0xbf, 0xc0, 0x00, 0x00, 0x40, 0x20, 0x00, 0x00,
@test bswap(z2) === 3.5 - 4.5im
end

# PR 19449
using Compat: StringVector
@test length(StringVector(5)) == 5
@test String(fill!(StringVector(5), 0x61)) == "aaaaa"

let x = fill!(StringVector(5), 0x61)
@test pointer(x) == pointer(Compat.UTF8String(x))
end

include("to-be-deprecated.jl")

nothing

0 comments on commit 49e0235

Please sign in to comment.