Sha256: 3bc58e8637bd757a844c94002ab37255625197d6142e12e2174eb30344720a91

Contents?: true

Size: 1.01 KB

Versions: 12

Compression:

Stored size: 1.01 KB

Contents

module BerkeleyLibrary
  module Util
    module Strings

      ASCII_0 = '0'.ord
      ASCII_9 = '9'.ord

      def ascii_numeric?(s)
        s.chars.all? do |c|
          ord = c.ord
          ord >= ASCII_0 && ord <= ASCII_9
        end
      end

      # 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
      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

      class << self
        include Strings
      end

      private

      def string_like?(*strs)
        strs.all? { |s| s.respond_to?(:chars) && s.respond_to?(:size) }
      end

    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
berkeley_library-util-0.1.9 lib/berkeley_library/util/strings.rb
berkeley_library-util-0.1.8 lib/berkeley_library/util/strings.rb
berkeley_library-util-0.1.7 lib/berkeley_library/util/strings.rb
berkeley_library-util-0.1.6 lib/berkeley_library/util/strings.rb
berkeley_library-util-0.1.5 lib/berkeley_library/util/strings.rb
berkeley_library-util-0.1.4 lib/berkeley_library/util/strings.rb
berkeley_library-util-0.1.3 lib/berkeley_library/util/strings.rb
berkeley_library-util-0.1.2 lib/berkeley_library/util/strings.rb
berkeley_library-util-0.1.1 lib/berkeley_library/util/strings.rb
berkeley_library-util-0.1.0 lib/berkeley_library/util/strings.rb
berkeley_library-tind-0.4.1 lib/berkeley_library/util/strings.rb
berkeley_library-tind-0.4.0 lib/berkeley_library/util/strings.rb