Skip to content
Kward Search API index

Class: Kward::NativeShell::Pattern::Bracket

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/shell/native_pattern.rb

Overview

Test alternatives separately: libc fnmatch can commit to a shorter element in a mixed bracket even when a longer element would match.

Instance Method Summary collapse

Constructor Details

#initialize(locale, elements, negated) ⇒ Bracket

Returns a new instance of Bracket.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kward/shell/native_pattern.rb', line 16

def initialize(locale, elements, negated)
  @locale, @negated = locale, negated
  @elements = []
  until elements.empty?
    first = elements.shift
    if elements.length >= 2 && elements.first.first == :dash
      elements.shift
      @elements << [:range, first, elements.shift]
    else
      @elements << first
    end
  end
end

Instance Method Details

#match?(value) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kward/shell/native_pattern.rb', line 30

def match?(value)
  return false unless @locale.collating_element?(value)
  matched = @elements.any? do |kind, text, last|
    case kind
    when :range
      lower, upper = endpoint(text), endpoint(last)
      lower && upper && @locale.compare(lower, value) <= 0 && @locale.compare(value, upper) <= 0
    when :symbol then @locale.symbol?(text, value)
    when :equivalent then @locale.equivalent?(text, value)
    when :class then value.length == 1 && @locale.bracket?("[[:#{text}:]]", value)
    else value == text
    end
  end
  @negated ? !matched : matched
end