module BerkeleyLibrary::Util::Strings

Constants

ASCII_0
ASCII_9

Public Instance Methods

ascii_numeric?(s) click to toggle source
# File lib/berkeley_library/util/strings.rb, line 8
def ascii_numeric?(s)
  s.chars.all? do |c|
    ord = c.ord
    ord >= ASCII_0 && ord <= ASCII_9
  end
end
diff_index(s1, s2) click to toggle source

Locates the point at which two strings differ

@return [Integer, nil] the index of the first character in either string

that differs from the other, or `nil` if the strings are identical,
or are not strings
# File lib/berkeley_library/util/strings.rb, line 20
def diff_index(s1, s2)
  return unless string_like?(s1, s2)

  shorter, longer = s1.size > s2.size ? [s2, s1] : [s1, s2]
  shorter.chars.each_with_index do |c, i|
    return i if c != longer[i]
  end
  shorter.length if shorter.length < longer.length # otherwise they're equal
end

Private Instance Methods

string_like?(*strs) click to toggle source
# File lib/berkeley_library/util/strings.rb, line 36
def string_like?(*strs)
  strs.all? { |s| s.respond_to?(:chars) && s.respond_to?(:size) }
end