-
-
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.
Various fixes to byte / bytearray search (#54579)
This was originally intended as a targeted fix to #54578, but I ran into a bunch of smaller issues with this code that also needed to be solved and it turned out to be difficult to fix them with small, trivial PRs. I would also like to refactor this whole file, but I want these correctness fixes to be merged first, because a larger refactoring has higher risk of getting stuck without getting reviewed and merged. ## Larger things that needs decisions * The internal union `Base.ByteArray` has been deleted. Instead, the unions `DenseInt8` and `DenseUInt8` have been added. These more comprehensively cover the types that was meant, e.g. `Memory{UInt8}` was incorrectly not covered by the former. As stated in the TODO, the concept of a "memory backed dense byte array" is needed throughout Julia, so this ideally needs to be implemented as a single type and used throughout Base. The fix here is a decent temporary solution. See #53178 #54581 * The `findall` docstring between two arrays was incorrectly not attached to the method - now it is. **Note that this change _changes_ the documentation** since it includes a docstring that was previously missed. Hence, it's an API addition. * Added a new minimal `testhelpers/OffsetDenseArrays.jl` which provide a `DenseVector` with offset axes for testing purposes. ## Trivial fixes * `findfirst(==(Int8(-1)), [0xff])` and similar findlast, findnext and findprev is no longer buggy, see #54578 * `findfirst([0x0ff], Int8[-1])` is similarly no longer buggy, see #54578 * `findnext(==('\xa6'), "æ", 1)` and `findprev(==('\xa6'), "æa", 2)` no longer incorrectly throws an error * The byte-oriented find* functions now work correctly with offset arrays * Fixed incorrect use of `GC.@preserve`, where the pointer was taken before the preserve block. * More of the optimised string methods now also apply to `SubString{String}` Closes #54578 Co-authored-by: Martin Holters <[email protected]>
- Loading branch information
1 parent
a7c9235
commit 56451d8
Showing
4 changed files
with
170 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
""" | ||
module OffsetDenseArrays | ||
A minimal implementation of an offset array which is also <: DenseArray. | ||
""" | ||
module OffsetDenseArrays | ||
|
||
struct OffsetDenseArray{A <: DenseVector, T} <: DenseVector{T} | ||
x::A | ||
offset::Int | ||
end | ||
OffsetDenseArray(x::AbstractVector{T}, i::Integer) where {T} = OffsetDenseArray{typeof(x), T}(x, Int(i)) | ||
|
||
Base.size(x::OffsetDenseArray) = size(x.x) | ||
Base.pointer(x::OffsetDenseArray) = pointer(x.x) | ||
|
||
function Base.getindex(x::OffsetDenseArray, i::Integer) | ||
@boundscheck checkbounds(x.x, i - x.offset) | ||
x.x[i - x.offset] | ||
end | ||
|
||
function Base.setindex(x::OffsetDenseArray, v, i::Integer) | ||
@boundscheck checkbounds(x.x, i - x.offset) | ||
x.x[i - x.offset] = v | ||
end | ||
|
||
IndexStyle(::Type{<:OffsetDenseArray}) = Base.IndexLinear() | ||
Base.axes(x::OffsetDenseArray) = (x.offset + 1 : x.offset + length(x.x),) | ||
Base.keys(x::OffsetDenseArray) = only(axes(x)) | ||
|
||
end # module |