Skip to content

Commit

Permalink
Add references between String equality, comparison methods (#10531)
Browse files Browse the repository at this point in the history
Co-authored-by: Vladislav Zarakovsky <[email protected]>
  • Loading branch information
straight-shoota and vlazar authored Dec 11, 2022
1 parent a37e97d commit 6acf031
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/string.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2963,12 +2963,22 @@ class String

# Returns `true` if this string is equal to `*other*.
#
# Comparison is done byte-per-byte: if a byte is different from the corresponding
# byte, `false` is returned and so on. This means two strings containing invalid
# Equality is checked byte-per-byte: if any byte is different from the corresponding
# byte, it returns `false`. This means two strings containing invalid
# UTF-8 byte sequences may compare unequal, even when they both produce the
# Unicode replacement character at the same string indices.
#
# See `#compare` for more comparison options.
# Thus equality is case-sensitive, as it is with the comparison operator (`#<=>`).
# `#compare` offers a case-insensitive alternative.
#
# ```
# "abcdef" == "abcde" # => false
# "abcdef" == "abcdef" # => true
# "abcdef" == "abcdefg" # => false
# "abcdef" == "ABCDEF" # => false
#
# "abcdef".compare("ABCDEF", case_sensitive: false) == 0 # => true
# ```
def ==(other : self) : Bool
return true if same?(other)
return false unless bytesize == other.bytesize
Expand All @@ -2995,6 +3005,8 @@ class String
# "abcdef" <=> "abcdefg" # => -1
# "abcdef" <=> "ABCDEF" # => 1
# ```
#
# The comparison is case-sensitive. `#compare` is a case-insensitive alternative.
def <=>(other : self) : Int32
return 0 if same?(other)
min_bytesize = Math.min(bytesize, other.bytesize)
Expand All @@ -3021,6 +3033,8 @@ class String
#
# "heIIo".compare("heııo", case_insensitive: true, options: Unicode::CaseOptions::Turkic) # => 0
# ```
#
# Case-sensitive only comparison is provided by the comparison operator `#<=>`.
def compare(other : String, case_insensitive = false, options = Unicode::CaseOptions::None) : Int32
return self <=> other unless case_insensitive

Expand Down

0 comments on commit 6acf031

Please sign in to comment.