Module: Unnatural::Scan

Defined in:
lib/unnatural/scan.rb

Overview

Compares strings using a ‘StringScanner`. Pure ruby. Tends to be outperformed by `Unnatural::Substitution` and `Unnatural::Split` when sorting short strings via the global sort function, but its comparison function is the fastest of the pure-ruby algorithms.

Class Method Summary collapse

Class Method Details

.compare(a_string, b_string) ⇒ Object

rubocop: disable Metrics/AbcSize rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/MethodLength rubocop: disable Metrics/PerceivedComplexity



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/unnatural/scan.rb', line 25

def self.compare(a_string, b_string)
  if a_string.is_a?(Array) && b_string.is_a?(Array)
    a_string = a_string.first
    b_string = b_string.first
  end

  a = StringScanner.new(a_string)
  b = StringScanner.new(b_string)

  loop do
    a_number = a.scan(/\d+/)
    b_number = b.scan(/\d+/)

    if a_number.nil? && b_number.nil?
    # move on
    elsif a_number.nil?
      return +1
    elsif b_number.nil?
      return -1
    else
      compare = a_number.to_i <=> b_number.to_i
      return compare unless compare == 0
    end

    a_segment = a.scan(/\D+/)
    b_segment = b.scan(/\D+/)

    if a_segment.nil? && b_segment.nil?
    # move on
    elsif a_segment.nil?
      return -1
    elsif b_segment.nil?
      return +1
    else
      compare = a_segment.casecmp(b_segment)
      return compare unless compare == 0
    end

    return 0 if a.eos? && b.eos?
    return -1 if a.eos?
    return +1 if b.eos?
  end
end

.sort(enumerable) ⇒ Object



9
10
11
# File 'lib/unnatural/scan.rb', line 9

def self.sort(enumerable)
  enumerable.sort { |a, b| compare(a, b) }
end

.sort_by(enumerable) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
# File 'lib/unnatural/scan.rb', line 13

def self.sort_by(enumerable)
  raise ArgumentError, 'Block expected but none given' unless block_given?
  enumerable
    .map { |e| [(yield e), e] }
    .sort { |a, b| compare(a, b) }
    .map { |ary| ary[1] }
end