class TwitterCldr::Transforms::Conversions::Side
Attributes
after_context[R]
before_context[R]
cursor_offset[R]
key[R]
Public Class Methods
new(before_context, key, after_context, cursor_offset)
click to toggle source
# File lib/twitter_cldr/transforms/conversions/side.rb, line 24 def initialize(before_context, key, after_context, cursor_offset) @before_context = before_context @key = key @after_context = after_context @cursor_offset = cursor_offset end
Public Instance Methods
codepoints()
click to toggle source
# File lib/twitter_cldr/transforms/conversions/side.rb, line 79 def codepoints if first_elem = key_u_regexp.elements.first first_elem.codepoints else [] end end
has_codepoints?()
click to toggle source
# File lib/twitter_cldr/transforms/conversions/side.rb, line 70 def has_codepoints? if first_elem = key_u_regexp.elements.first first_elem.respond_to?(:codepoints) && !first_elem.codepoints.empty? else false end end
match(cursor)
click to toggle source
# File lib/twitter_cldr/transforms/conversions/side.rb, line 31 def match(cursor) if before_match = match_before(cursor) if key_match = match_key(cursor, before_match) if after_match = match_after(cursor, key_match) SideMatch.new( before_match.offset(0), key_match.offset(0), after_match.offset(0), before_match.captures + key_match.captures + after_match.captures ) end end end end
match_after(cursor, key_match)
click to toggle source
# File lib/twitter_cldr/transforms/conversions/side.rb, line 64 def match_after(cursor, key_match) if match = after_context_regexp.match(cursor.text, key_match.end(0)) match if match.begin(0) == key_match.end(0) end end
match_before(cursor)
click to toggle source
# File lib/twitter_cldr/transforms/conversions/side.rb, line 48 def match_before(cursor) cursor.text.scan(before_context_regexp) do match = Regexp.last_match if match.end(0) >= cursor.position && match.begin(0) <= cursor.position return match end end nil end
match_key(cursor, before_match)
click to toggle source
# File lib/twitter_cldr/transforms/conversions/side.rb, line 58 def match_key(cursor, before_match) if match = key_regexp.match(cursor.text, before_match.end(0)) match if match.begin(0) == before_match.end(0) end end
Private Instance Methods
after_context_regexp()
click to toggle source
# File lib/twitter_cldr/transforms/conversions/side.rb, line 101 def after_context_regexp @after_context_regexp ||= compile_regexp(after_context).to_regexp end
before_context_regexp()
click to toggle source
# File lib/twitter_cldr/transforms/conversions/side.rb, line 89 def before_context_regexp @before_context_regexp ||= compile_regexp(before_context).to_regexp end
compile_regexp(regexp_str)
click to toggle source
This is a pretty big hack. The problem we're trying to solve here is that regular negated character classes don't match the ends of strings. CLDR's transform rules expect the after context to match the end of a string, since, in a sense, “nothing” is always part of a negated character class. “I want to match on anything but these specific characters” should also include no characters. Accordingly, this function adds “z” to the ends of negated character classes. Hopefully this works for all cases.
# File lib/twitter_cldr/transforms/conversions/side.rb, line 113 def compile_regexp(regexp_str) TwitterCldr::Shared::UnicodeRegex.compile(regexp_str).tap do |re| re.elements.each_with_index do |element, idx| if element.type == :character_class && element.negated? repl = TwitterCldr::Shared::UnicodeRegex.compile( "(?:#{element.to_regexp_str[3..-2]}|\\z)" ) re.elements[idx..idx] = repl.elements end end end end
key_regexp()
click to toggle source
# File lib/twitter_cldr/transforms/conversions/side.rb, line 97 def key_regexp @key_regexp ||= key_u_regexp.to_regexp end
key_u_regexp()
click to toggle source
# File lib/twitter_cldr/transforms/conversions/side.rb, line 93 def key_u_regexp @key_u_regexp ||= compile_regexp(Rule.regexp_token_string(key)) end