Skip to content

Commit dea6750

Browse files
committed
added Cstring (JuliaLang/julia#10994)
1 parent d013435 commit dea6750

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ Currently, the `@compat` macro supports the following syntaxes:
5050

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

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

5560
* `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.

src/Compat.jl

+8
Original file line numberDiff line numberDiff line change
@@ -345,4 +345,12 @@ if VERSION < v"0.4.0-dev+4524"
345345
export isdiag
346346
end
347347

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

test/runtests.jl

+6
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,9 @@ end
261261
@test isdiag(diagm([1,2,3,4]))
262262
@test !isdiag([1 2; 3 4])
263263
@test isdiag(5)
264+
265+
# Cstring
266+
let s = "foo", w = wstring("foo")
267+
@test reinterpret(Ptr{Cchar}, Compat.unsafe_convert(Cstring, s)) == pointer(s)
268+
@test reinterpret(Ptr{Cwchar_t}, Compat.unsafe_convert(Cwstring, w)) == pointer(w)
269+
end

0 commit comments

Comments
 (0)