Skip to content

Commit

Permalink
Optimize String#index(Char) and #rindex(Char) for invalid UTF-8 (#…
Browse files Browse the repository at this point in the history
…14461)

Co-authored-by: Johannes Müller <straightshoota@gmail.com>
Co-authored-by: Sijawusz Pur Rahnama <sija@sija.pl>
  • Loading branch information
3 people committed Apr 17, 2024
1 parent 54a7631 commit 4dd5814
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/string.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3334,7 +3334,11 @@ class String
# ```
def index(search : Char, offset = 0) : Int32?
# If it's ASCII we can delegate to slice
if search.ascii? && single_byte_optimizable?
if single_byte_optimizable?
# With `single_byte_optimizable?` there are only ASCII characters and invalid UTF-8 byte
# sequences and we can immediately reject any non-ASCII codepoint.
return unless search.ascii?

return to_slice.fast_index(search.ord.to_u8, offset)
end

Expand Down Expand Up @@ -3450,7 +3454,11 @@ class String
# ```
def rindex(search : Char, offset = size - 1)
# If it's ASCII we can delegate to slice
if search.ascii? && single_byte_optimizable?
if single_byte_optimizable?
# With `single_byte_optimizable?` there are only ASCII characters and invalid UTF-8 byte
# sequences and we can immediately reject any non-ASCII codepoint.
return unless search.ascii?

return to_slice.rindex(search.ord.to_u8, offset)
end

Expand Down

0 comments on commit 4dd5814

Please sign in to comment.