Skip to content

Commit

Permalink
Improve docs for String#rindex! (#15132)
Browse files Browse the repository at this point in the history
Co-authored-by: Johannes Müller <[email protected]>
  • Loading branch information
BigBoyBarney and straight-shoota authored Oct 31, 2024
1 parent d353bb4 commit 8635bce
Showing 1 changed file with 58 additions and 12 deletions.
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
# ```
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

0 comments on commit 8635bce

Please sign in to comment.