Sha256: 5b90c0798252a820502a45fa48bd6da7c12d389a7bd5ac3bde191c43fecae880

Contents?: true

Size: 1.65 KB

Versions: 28

Compression:

Stored size: 1.65 KB

Contents

# encoding: UTF-8

# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

module TwitterCldr
  module Transforms
    module Filters

      class UnicodeFilter < FilterRule
        class << self
          def parse(rule_text, symbol_table)
            rule_text = Rule.remove_comment(rule_text).strip
            rule_text = rule_text[2..-2].strip if rule_text.start_with?('::')
            direction = direction_for(rule_text)

            re = TwitterCldr::Shared::UnicodeRegex.compile(
              clean_rule(rule_text, direction)
            )

            # filters are always just a unicode set
            new(re.elements.first.to_set.to_set, direction)
          end

          def accepts?(rule_text)
            !!(rule_text =~ /\A::[\s]*\(?[\s]*\[/)
          end

          private

          def direction_for(rule_text)
            if rule_text.start_with?('(')
              :backward
            else
              :forward
            end
          end

          def clean_rule(rule_text, direction)
            if direction == :backward
              rule_text[1..-2].strip
            else
              rule_text
            end
          end
        end

        attr_reader :charset, :direction

        def initialize(charset, direction)
          @charset = charset
          @direction = direction
        end

        def resolve(symbol_table)
          self
        end

        def matches?(cursor)
          charset.include?(cursor.text[cursor.position].ord)
        end

        def forward?
          direction == :forward
        end

        def backward?
          direction == :backward
        end
      end

    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
twitter_cldr-6.12.1 lib/twitter_cldr/transforms/filters/unicode_filter.rb
twitter_cldr-6.12.0 lib/twitter_cldr/transforms/filters/unicode_filter.rb
twitter_cldr-6.11.5 lib/twitter_cldr/transforms/filters/unicode_filter.rb
twitter_cldr-6.11.4 lib/twitter_cldr/transforms/filters/unicode_filter.rb
twitter_cldr-6.11.3 lib/twitter_cldr/transforms/filters/unicode_filter.rb
twitter_cldr-6.11.2 lib/twitter_cldr/transforms/filters/unicode_filter.rb
twitter_cldr-6.11.1 lib/twitter_cldr/transforms/filters/unicode_filter.rb
twitter_cldr-6.11.0 lib/twitter_cldr/transforms/filters/unicode_filter.rb
twitter_cldr-6.10.0 lib/twitter_cldr/transforms/filters/unicode_filter.rb
twitter_cldr-6.9.0 lib/twitter_cldr/transforms/filters/unicode_filter.rb
twitter_cldr-6.8.0 lib/twitter_cldr/transforms/filters/unicode_filter.rb
twitter_cldr-6.7.0 lib/twitter_cldr/transforms/filters/unicode_filter.rb
twitter_cldr-6.6.2 lib/twitter_cldr/transforms/filters/unicode_filter.rb
twitter_cldr-6.6.1 lib/twitter_cldr/transforms/filters/unicode_filter.rb
twitter_cldr-6.6.0 lib/twitter_cldr/transforms/filters/unicode_filter.rb
twitter_cldr-6.5.0 lib/twitter_cldr/transforms/filters/unicode_filter.rb
twitter_cldr-6.4.0 lib/twitter_cldr/transforms/filters/unicode_filter.rb
twitter_cldr-6.3.0 lib/twitter_cldr/transforms/filters/unicode_filter.rb
twitter_cldr-6.2.0 lib/twitter_cldr/transforms/filters/unicode_filter.rb
twitter_cldr-6.1.0 lib/twitter_cldr/transforms/filters/unicode_filter.rb