Module: Lang::Tag::Filtering

Included in:
Grandfathered, Langtag, Privateuse
Defined in:
lib/lang/tag/filtering.rb

Overview

Basic and extended filtering. RFC 4647, Sections 3.3.1, 3.3.2.

Constant Summary collapse

WILDCARD =
'*'.freeze

Instance Method Summary collapse

Instance Method Details

#matched_by_basic_range?(range) ⇒ Boolean Also known as: has_prefix?

Checks if the basic language-range passed matches self.

Example

tag = Lang::Tag('de-Latn-DE')
tag.matched_by_basic_range?('de-Latn-DE') #=> true
tag.matched_by_basic_range?('de-Latn') #=> true
tag.matched_by_basic_range?('*') #=> true
tag.matched_by_basic_range?('de-La') #=> false
tag.matched_by_basic_range?('de-de') #=> false
tag.matched_by_basic_range?('malformedlangtag') #=> false

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/lang/tag/filtering.rb', line 102

def matched_by_basic_range?(range)
  if range.kind_of?(Composition)
    s = range.composition
  elsif range.respond_to?(:to_str)
    s = range.to_str.downcase
    return true if s == WILDCARD
  else
    return false
  end

  composition == s ||
  composition.index(s + HYPHEN) == 0
end

#matched_by_extended_range?(range) ⇒ Boolean

Checks if the extended language-range (in the shortest notation) passed matches self.

Example

Lang::Tag('de-DE').matched_by_extended_range?('de-*-DE) #=> true
Lang::Tag('de-DE-x-goethe').matched_by_extended_range?('de-*-DE) #=> true
Lang::Tag('de-Latn-DE').matched_by_extended_range?('de-*-DE) #=> true
Lang::Tag('de-Latf-DE').matched_by_extended_range?('de-*-DE) #=> true
Lang::Tag('de-x-DE').matched_by_extended_range?('de-*-DE) #=> false
Lang::Tag('de-Deva').matched_by_extended_range?('de-*-DE) #=> false

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/lang/tag/filtering.rb', line 50

def matched_by_extended_range?(range)

  subtags = decomposition.dup
  subranges = range.to_str.downcase.split(HYPHEN_SPLITTER)

  subrange = subranges.shift
  subtag = subtags.shift

  while subrange
    if subrange == WILDCARD
      subrange = subranges.shift
    elsif subtag == nil
      return false
    elsif subtag == subrange
      subtag = subtags.shift
      subrange = subranges.shift
    elsif subtag.size == 1
      return false
    else
      subtag = subtags.shift
    end
  end
  true
rescue
  false
end