Skip to content

Commit

Permalink
Add return type restrictions to methods related to String (#10583)
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin authored Jun 7, 2021
1 parent 6b9fa80 commit f159bce
Show file tree
Hide file tree
Showing 13 changed files with 183 additions and 183 deletions.
66 changes: 33 additions & 33 deletions src/char.cr
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct Char
# 'b' - 'a' # => 1
# 'c' - 'a' # => 2
# ```
def -(other : Char)
def -(other : Char) : Int32
ord - other.ord
end

Expand All @@ -70,7 +70,7 @@ struct Char
# ```
# 'f' + "oo" # => "foo"
# ```
def +(str : String)
def +(str : String) : String
bytesize = str.bytesize + self.bytesize
String.new(bytesize) do |buffer|
count = 0
Expand Down Expand Up @@ -142,7 +142,7 @@ struct Char

# Returns `true` if this char is an ASCII character
# (codepoint is in (0..127))
def ascii?
def ascii? : Bool
ord < 128
end

Expand All @@ -155,7 +155,7 @@ struct Char
# 'z'.ascii_number? # => false
# 'z'.ascii_number?(36) # => true
# ```
def ascii_number?(base : Int = 10)
def ascii_number?(base : Int = 10) : Bool
!!to_i?(base)
end

Expand All @@ -165,7 +165,7 @@ struct Char
# '1'.number? # => true
# 'a'.number? # => false
# ```
def number?
def number? : Bool
ascii? ? ascii_number? : Unicode.number?(self)
end

Expand All @@ -177,7 +177,7 @@ struct Char
# 'G'.ascii_lowercase? # => false
# '.'.ascii_lowercase? # => false
# ```
def ascii_lowercase?
def ascii_lowercase? : Bool
'a' <= self <= 'z'
end

Expand All @@ -189,7 +189,7 @@ struct Char
# 'G'.lowercase? # => false
# '.'.lowercase? # => false
# ```
def lowercase?
def lowercase? : Bool
ascii? ? ascii_lowercase? : Unicode.lowercase?(self)
end

Expand All @@ -201,7 +201,7 @@ struct Char
# 'c'.ascii_uppercase? # => false
# '.'.ascii_uppercase? # => false
# ```
def ascii_uppercase?
def ascii_uppercase? : Bool
'A' <= self <= 'Z'
end

Expand All @@ -213,7 +213,7 @@ struct Char
# 'c'.uppercase? # => false
# '.'.uppercase? # => false
# ```
def uppercase?
def uppercase? : Bool
ascii? ? ascii_uppercase? : Unicode.uppercase?(self)
end

Expand All @@ -224,7 +224,7 @@ struct Char
# 'á'.ascii_letter? # => false
# '8'.ascii_letter? # => false
# ```
def ascii_letter?
def ascii_letter? : Bool
ascii_lowercase? || ascii_uppercase?
end

Expand All @@ -235,7 +235,7 @@ struct Char
# 'á'.letter? # => true
# '8'.letter? # => false
# ```
def letter?
def letter? : Bool
ascii? ? ascii_letter? : Unicode.letter?(self)
end

Expand All @@ -246,7 +246,7 @@ struct Char
# '8'.ascii_alphanumeric? # => true
# '.'.ascii_alphanumeric? # => false
# ```
def ascii_alphanumeric?
def ascii_alphanumeric? : Bool
ascii_letter? || ascii_number?
end

Expand All @@ -257,7 +257,7 @@ struct Char
# '8'.alphanumeric? # => true
# '.'.alphanumeric? # => false
# ```
def alphanumeric?
def alphanumeric? : Bool
letter? || number?
end

Expand All @@ -268,7 +268,7 @@ struct Char
# '\t'.ascii_whitespace? # => true
# 'b'.ascii_whitespace? # => false
# ```
def ascii_whitespace?
def ascii_whitespace? : Bool
self == ' ' || 9 <= ord <= 13
end

Expand All @@ -279,7 +279,7 @@ struct Char
# '\t'.whitespace? # => true
# 'b'.whitespace? # => false
# ```
def whitespace?
def whitespace? : Bool
ascii? ? ascii_whitespace? : Unicode.whitespace?(self)
end

Expand All @@ -291,7 +291,7 @@ struct Char
# 'F'.hex? # => true
# 'g'.hex? # => false
# ```
def hex?
def hex? : Bool
ascii_number? 16
end

Expand Down Expand Up @@ -322,7 +322,7 @@ struct Char
# '\\'.in_set? "\\A" # => false
# '\\'.in_set? "X-\\w" # => true
# ```
def in_set?(*sets : String)
def in_set?(*sets : String) : Bool
if sets.size > 1
return sets.all? { |set| in_set?(set) }
end
Expand Down Expand Up @@ -388,7 +388,7 @@ struct Char
# 'x'.downcase # => 'x'
# '.'.downcase # => '.'
# ```
def downcase(options = Unicode::CaseOptions::None)
def downcase(options = Unicode::CaseOptions::None) : Char
Unicode.downcase(self, options)
end

Expand All @@ -415,7 +415,7 @@ struct Char
# 'X'.upcase # => 'X'
# '.'.upcase # => '.'
# ```
def upcase(options = Unicode::CaseOptions::None)
def upcase(options = Unicode::CaseOptions::None) : Char
Unicode.upcase(self, options)
end

Expand Down Expand Up @@ -446,7 +446,7 @@ struct Char
# ```
#
# This method allows creating a `Range` of chars.
def succ
def succ : Char
(ord + 1).chr
end

Expand All @@ -456,7 +456,7 @@ struct Char
# 'b'.pred # => 'a'
# 'ぃ'.pred # => 'あ'
# ```
def pred
def pred : Char
(ord - 1).chr
end

Expand All @@ -471,17 +471,17 @@ struct Char
# char.control? # => true
# end
# ```
def ascii_control?
def ascii_control? : Bool
ord < 0x20 || (0x7F <= ord <= 0x9F)
end

# Returns `true` if this char is a control character according to unicode.
def control?
def control? : Bool
ascii? ? ascii_control? : Unicode.control?(self)
end

# Returns `true` if this is char is a mark character according to unicode.
def mark?
def mark? : Bool
Unicode.mark?(self)
end

Expand Down Expand Up @@ -521,7 +521,7 @@ struct Char
# 'あ'.dump # => "'\\u{3042}'"
# '\u0012'.dump # => "'\\u{12}'"
# ```
def dump
def dump : String
dump_or_inspect do |io|
if ascii_control? || ord >= 0x80
io << "\\u{"
Expand Down Expand Up @@ -637,7 +637,7 @@ struct Char
# '8'.to_f # => 8.0
# 'c'.to_f # raises ArgumentError
# ```
def to_f
def to_f : Float64
to_f64
end

Expand All @@ -649,27 +649,27 @@ struct Char
# '8'.to_f? # => 8.0
# 'c'.to_f? # => nil
# ```
def to_f?
def to_f? : Float64?
to_f64?
end

# See also: `to_f`.
def to_f32
def to_f32 : Float32
to_i.to_f32
end

# See also: `to_f?`.
def to_f32?
def to_f32? : Float32?
to_i?.try &.to_f32
end

# Same as `to_f`.
def to_f64
def to_f64 : Float64
to_i.to_f64
end

# Same as `to_f?`.
def to_f64?
def to_f64? : Float64?
to_i?.try &.to_f64
end

Expand Down Expand Up @@ -732,7 +732,7 @@ struct Char
# 'a'.bytesize # => 1
# '好'.bytesize # => 3
# ```
def bytesize
def bytesize : Int32
# See http://en.wikipedia.org/wiki/UTF-8#Sample_code

c = ord
Expand All @@ -759,7 +759,7 @@ struct Char
# 'a'.bytes # => [97]
# 'あ'.bytes # => [227, 129, 130]
# ```
def bytes
def bytes : Array(UInt8)
bytes = [] of UInt8
each_byte do |byte|
bytes << byte
Expand Down
8 changes: 4 additions & 4 deletions src/char/reader.cr
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ struct Char
# reader.has_next? # => true
# reader.peek_next_char # => '\0'
# ```
def has_next?
def has_next? : Bool
!@end
end

Expand All @@ -94,7 +94,7 @@ struct Char
# reader = Char::Reader.new("ab")
# reader.next_char # => 'b'
# ```
def next_char
def next_char : Char
@pos += @current_char_width
if @pos > @string.bytesize
raise IndexError.new
Expand All @@ -113,7 +113,7 @@ struct Char
# reader.peek_next_char # => 'b'
# reader.current_char # => 'a'
# ```
def peek_next_char
def peek_next_char : Char
next_pos = @pos + @current_char_width

if next_pos > @string.bytesize
Expand All @@ -127,7 +127,7 @@ struct Char

# Returns `true` if there are characters before
# the current one.
def has_previous?
def has_previous? : Bool
@pos > 0
end

Expand Down
2 changes: 1 addition & 1 deletion src/int.cr
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ struct Int
# ```
# 97.chr # => 'a'
# ```
def chr
def chr : Char
unless 0 <= self <= Char::MAX_CODEPOINT
raise ArgumentError.new("#{self} out of char range")
end
Expand Down
4 changes: 2 additions & 2 deletions src/levenshtein.cr
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ module Levenshtein
end
end

def best_match
def best_match : String?
@best_entry.try &.value
end

Expand All @@ -114,7 +114,7 @@ module Levenshtein
sn.best_match
end

def self.find(name, all_names, tolerance = nil)
def self.find(name, all_names, tolerance = nil) : String?
find(name, tolerance) do |similar|
all_names.each do |a_name|
similar.test(a_name)
Expand Down
6 changes: 3 additions & 3 deletions src/regex.cr
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ class Regex
# Regex.error?("(foo|bar)") # => nil
# Regex.error?("(foo|bar") # => "missing ) at 8"
# ```
def self.error?(source)
def self.error?(source) : String?
re = LibPCRE.compile(source, (Options::UTF_8 | Options::NO_UTF8_CHECK | Options::DUPNAMES), out errptr, out erroffset, nil)
if re
nil
Expand Down Expand Up @@ -368,7 +368,7 @@ class Regex
# re.match("Skiing") # => Regex::MatchData("Skiing")
# re.match("sledding") # => Regex::MatchData("sledding")
# ```
def +(other)
def +(other) : Regex
Regex.union(self, other)
end

Expand Down Expand Up @@ -421,7 +421,7 @@ class Regex
# /at/ =~ "input data" # => 7
# /ax/ =~ "input data" # => nil
# ```
def =~(other : String)
def =~(other : String) : Int32?
match = self.match(other)
$~ = match
match.try &.begin(0)
Expand Down
Loading

0 comments on commit f159bce

Please sign in to comment.