Skip to content

Commit

Permalink
Merge pull request #83 from JuliaLang/sgj/cstring
Browse files Browse the repository at this point in the history
add Cstring/Cwstring
  • Loading branch information
jakebolewski committed May 12, 2015
2 parents 64edf37 + 1d52f5f commit f64958f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ Currently, the `@compat` macro supports the following syntaxes:

* For all unsigned integer types to their equivalents with uppercase `I`. [#8907](https://github.com/JuliaLang/julia/pull/8907)

* `Cstring` and `Cwstring` for `Ptr{Cchar}` and `Ptr{Cwchar_t}`, respectively:
these should be used for passing NUL-terminated strings to `ccall`. (In
Julia 0.4, using these types also checks whether the string has embedded
NUL characters [#10994](https://github.com/JuliaLang/julia/pull/10994).)

## New functions

* `eachindex`, as in `for i in eachindex(A)`, can be used in julia 0.3. This is the recommended way to iterate over each index in an `AbstractArray`. On julia 0.3 `eachindex` just returns `1:length(A)`, but in julia 0.4 it can return a more sophisticated iterator.
Expand Down
8 changes: 8 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,12 @@ if VERSION < v"0.4.0-dev+2254"
export Val
end

if VERSION < v"0.4.0-dev+4603"
# used for C string arguments to ccall
# (in Julia 0.4, these types also check for embedded NUL chars)
const Cstring = Ptr{Cchar}
const Cwstring = Ptr{Cwchar_t}
export Cstring, Cwstring
end

end # module
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,9 @@ begin
@test firstlast(Val{true}) == "First"
@test firstlast(Val{false}) == "Last"
end

# Cstring
let s = "foo", w = wstring("foo")
@test reinterpret(Ptr{Cchar}, Compat.unsafe_convert(Cstring, s)) == pointer(s)
@test reinterpret(Ptr{Cwchar_t}, Compat.unsafe_convert(Cwstring, w)) == pointer(w)
end

0 comments on commit f64958f

Please sign in to comment.