Skip to content

Commit

Permalink
added Cstring (JuliaLang/julia#10994)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj committed May 4, 2015
1 parent d013435 commit 923613c
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 @@ -50,6 +50,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 @@ -345,4 +345,12 @@ if VERSION < v"0.4.0-dev+4524"
export isdiag
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 @@ -261,3 +261,9 @@ end
@test isdiag(diagm([1,2,3,4]))
@test !isdiag([1 2; 3 4])
@test isdiag(5)

# Cstring
let s = "foo", w = wstring("foo")
@test Compat.unsafe_convert(Cstring, s) == pointer(s)
@test Compat.unsafe_convert(Cwstring, w) == pointer(w)
end

0 comments on commit 923613c

Please sign in to comment.