Skip to content

Commit

Permalink
Add ability to turn unicode regexes into strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
camertron committed Jan 1, 2016
1 parent e93b5ef commit a495cf7
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
26 changes: 25 additions & 1 deletion lib/twitter_cldr/parsers/unicode_regex/character_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CharacterClass < Component
close_bracket: :open_bracket
}

# Character classes can include set operations (eg. union, intersection, etc).
# Character classes can include set operations (eg. union, intersection, etc).
BinaryOperator = Struct.new(:operator, :left, :right)
UnaryOperator = Struct.new(:operator, :child)

Expand Down Expand Up @@ -50,10 +50,34 @@ def to_set
evaluate(root)
end

def to_s
stringify(root)
end

private

attr_reader :root

def stringify(node)
case node
when UnaryOperator, BinaryOperator
op_str = case node.operator
when :negate then '^'
when :union, :pipe then ''
when :dash then '-'
when :ampersand then '&'
end

left = stringify(node.left)
right = stringify(node.right)

"#{left}#{op_str}#{right}"

else
node.to_s
end
end

def evaluate(node)
case node
when UnaryOperator, BinaryOperator
Expand Down
4 changes: 4 additions & 0 deletions lib/twitter_cldr/parsers/unicode_regex/character_range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def to_set
)
end

def to_s
"#{initial.to_s}-#{final.to_s}"
end

end
end
end
Expand Down
8 changes: 8 additions & 0 deletions lib/twitter_cldr/parsers/unicode_regex/character_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ def to_set
)
end

def to_s
if property_value
"[:#{property_name}=#{property_value}:]"
else
"[:#{property_name}:]"
end
end

private

def codepoints
Expand Down
4 changes: 4 additions & 0 deletions lib/twitter_cldr/parsers/unicode_regex/literal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ def to_set
end
end

def to_s
text
end

private

def set_for_special_char(char)
Expand Down
9 changes: 6 additions & 3 deletions lib/twitter_cldr/parsers/unicode_regex/unicode_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ def to_set
end

def to_regexp_str
cps = codepoints.is_a?(Array) ? codepoints : [codepoints]
array_to_regex(cps)
array_to_regex(Array(codepoints))
end

def to_s
to_regexp_str
end

end
end
end
end
end
6 changes: 6 additions & 0 deletions lib/twitter_cldr/shared/unicode_regex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ def to_regexp_str
@regexp_str ||= elements.map(&:to_regexp_str).join
end

def to_s
@elements.inject('') do |ret, element|
ret + element.to_s
end
end

end
end
end

0 comments on commit a495cf7

Please sign in to comment.