Skip to content
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

Improve docs for String#rindex! #15132

Merged
merged 5 commits into from
Oct 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 58 additions & 12 deletions src/string.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3473,8 +3473,8 @@ class String
# ```
# "Hello, World".rindex('o') # => 8
# "Hello, World".rindex('Z') # => nil
# "Hello, World".rindex("o", 5) # => 4
# "Hello, World".rindex("W", 2) # => nil
# "Hello, World".rindex('o', 5) # => 4
# "Hello, World".rindex('W', 2) # => nil
# ```
def rindex(search : Char, offset = size - 1)
# If it's ASCII we can delegate to slice
Expand Down Expand Up @@ -3519,7 +3519,16 @@ class String
end
end

# :ditto:
# Returns the index of the _last_ appearance of *search* in the string,
# If *offset* is present, it defines the position to _end_ the search
# (characters beyond this point are ignored).
#
# ```
# "Hello, World".rindex("orld") # => 8
# "Hello, World".rindex("snorlax") # => nil
# "Hello, World".rindex("o", 5) # => 4
# "Hello, World".rindex("W", 2) # => nil
# ```
def rindex(search : String, offset = size - search.size) : Int32?
offset += size if offset < 0
return if offset < 0
Expand Down Expand Up @@ -3572,7 +3581,16 @@ class String
end
end

# :ditto:
# Returns the index of the _last_ appearance of *search* in the string,
# If *offset* is present, it defines the position to _end_ the search
# (characters beyond this point are ignored).
#
# ```
# "Hello, World".rindex(/world/i) # => 7
# "Hello, World".rindex(/world/) # => nil
# "Hello, World".rindex(/o/, 5) # => 4
# "Hello, World".rindex(/W/, 2) # => nil
# ```
def rindex(search : Regex, offset = size, *, options : Regex::MatchOptions = Regex::MatchOptions::None) : Int32?
offset += size if offset < 0
return nil unless 0 <= offset <= size
Expand All @@ -3586,21 +3604,49 @@ class String
match_result.try &.begin
end

# :ditto:
#
# Returns the index of the _last_ appearance of *search* in the string,
# If *offset* is present, it defines the position to _end_ the search
# (characters beyond this point are ignored).
# Raises `Enumerable::NotFoundError` if *search* does not occur in `self`.
def rindex!(search : Regex, offset = size, *, options : Regex::MatchOptions = Regex::MatchOptions::None) : Int32
rindex(search, offset, options: options) || raise Enumerable::NotFoundError.new
#
# ```
# "Hello, World".rindex!('o') # => 8
# "Hello, World".rindex!('Z') # raises Enumerable::NotFoundError
# "Hello, World".rindex!('o', 5) # => 4
# "Hello, World".rindex!('W', 2) # raises Enumerable::NotFoundError
# ```
Blacksmoke16 marked this conversation as resolved.
Show resolved Hide resolved
def rindex!(search : Char, offset = size - 1) : Int32
rindex(search, offset) || raise Enumerable::NotFoundError.new
end

# :ditto:
# Returns the index of the _last_ appearance of *search* in the string,
# If *offset* is present, it defines the position to _end_ the search
# (characters beyond this point are ignored).
# Raises `Enumerable::NotFoundError` if *search* does not occur in `self`.
#
# ```
# "Hello, World".rindex!("orld") # => 8
# "Hello, World".rindex!("snorlax") # raises Enumerable::NotFoundError
# "Hello, World".rindex!("o", 5) # => 4
# "Hello, World".rindex!("W", 2) # raises Enumerable::NotFoundError
# ```
def rindex!(search : String, offset = size - search.size) : Int32
rindex(search, offset) || raise Enumerable::NotFoundError.new
end

# :ditto:
def rindex!(search : Char, offset = size - 1) : Int32
rindex(search, offset) || raise Enumerable::NotFoundError.new
# Returns the index of the _last_ appearance of *search* in the string,
# If *offset* is present, it defines the position to _end_ the search
# (characters beyond this point are ignored).
# Raises `Enumerable::NotFoundError` if *search* does not occur in `self`.
#
# ```
# "Hello, World".rindex!(/world/i) # => 7
# "Hello, World".rindex!(/world/) # raises Enumerable::NotFoundError
# "Hello, World".rindex!(/o/, 5) # => 4
# "Hello, World".rindex!(/W/, 2) # raises Enumerable::NotFoundError
# ```
def rindex!(search : Regex, offset = size, *, options : Regex::MatchOptions = Regex::MatchOptions::None) : Int32
rindex(search, offset, options: options) || raise Enumerable::NotFoundError.new
end

# Searches separator or pattern (`Regex`) in the string, and returns
Expand Down
Loading