-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use standard Julia mechanisms for LibGit2 StrArrayStruct and Buffer t…
…ypes. (#19962) * use standard Julia mechanisms for passing StrArrayStruct * rejig Buffer free function * update to use RefArray * add julia annotation to code block * typo * fix diff error * change unsafe_convert back to convert * update test
- Loading branch information
1 parent
b8971ea
commit 48e923f
Showing
10 changed files
with
131 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,15 @@ | ||
# This file is a part of Julia. License is MIT: http://julialang.org/license | ||
|
||
function StrArrayStruct{T<:AbstractString}(strs::T...) | ||
strcount = length(strs) | ||
for s in strs | ||
if Base.containsnul(s) | ||
throw("embedded NULs are not allowed in C strings: $(repr(s))") | ||
end | ||
end | ||
sa_strings = convert(Ptr{Cstring}, Libc.malloc(sizeof(Cstring) * strcount)) | ||
for i=1:strcount | ||
len = length(strs[i]) | ||
in_ptr = pointer(String(strs[i])) | ||
out_ptr = convert(Ptr{UInt8}, Libc.malloc(sizeof(UInt8) * (len + 1))) | ||
unsafe_copy!(out_ptr, in_ptr, len) | ||
unsafe_store!(out_ptr, zero(UInt8), len + 1) # NULL byte | ||
unsafe_store!(sa_strings, convert(Cstring, out_ptr), i) | ||
end | ||
return StrArrayStruct(sa_strings, strcount) | ||
end | ||
StrArrayStruct{T<:AbstractString}(strs::Vector{T}) = StrArrayStruct(strs...) | ||
|
||
function Base.convert(::Type{Vector{AbstractString}}, sa::StrArrayStruct) | ||
arr = Array{AbstractString}(sa.count) | ||
for i=1:sa.count | ||
arr[i] = unsafe_string(unsafe_load(sa.strings, i)) | ||
end | ||
return arr | ||
function Base.cconvert(::Type{Ptr{StrArrayStruct}}, x::Vector) | ||
str_ref = Base.cconvert(Ref{Cstring}, x) | ||
sa_ref = Ref(StrArrayStruct(Base.unsafe_convert(Ref{Cstring}, str_ref), length(x))) | ||
sa_ref, str_ref | ||
end | ||
function Base.unsafe_convert(::Type{Ptr{StrArrayStruct}}, rr::Tuple{Ref{StrArrayStruct}, Ref{Cstring}}) | ||
Base.unsafe_convert(Ptr{StrArrayStruct}, first(rr)) | ||
end | ||
|
||
function Base.copy(src::StrArrayStruct) | ||
dst_ptr = Ref(StrArrayStruct()) | ||
@check ccall((:git_strarray_copy, :libgit2), Cint, | ||
(Ptr{StrArrayStruct}, Ptr{StrArrayStruct}), | ||
dst_ptr, Ref(src)) | ||
return dst_ptr[] | ||
function Base.convert(::Type{Vector{String}}, sa::StrArrayStruct) | ||
[unsafe_string(unsafe_load(sa.strings, i)) for i = 1:sa.count] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.