-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use standard Julia mechanisms for LibGit2 StrArrayStruct and Buffer types. #19962
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d7b9772
use standard Julia mechanisms for passing StrArrayStruct
simonbyrne 0484ed4
rejig Buffer free function
simonbyrne ff24f57
update to use RefArray
simonbyrne c12c1b2
add julia annotation to code block
simonbyrne f55832c
typo
simonbyrne c3407aa
fix diff error
simonbyrne e03195e
change unsafe_convert back to convert
simonbyrne 931964f
update test
simonbyrne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vtjnash Does this look okay?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes