Class: JsRegex::Converter::LiteralConverter

Inherits:
Base
  • Object
show all
Defined in:
lib/js_regex/converter/literal_converter.rb

Overview

Template class implementation.

Constant Summary collapse

ASTRAL_PLANE_CODEPOINT_PATTERN =
/[\u{10000}-\u{FFFFF}]/

Class Method Summary collapse

Methods inherited from Base

#convert

Class Method Details

.convert_astral_data(data) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/js_regex/converter/literal_converter.rb', line 20

def convert_astral_data(data)
  data.each_char.each_with_object(Node.new) do |char, node|
    if char =~ ASTRAL_PLANE_CODEPOINT_PATTERN
      node << surrogate_substitution_for(char)
    else
      node << escape_incompatible_bmp_literals(char)
    end
  end
end

.convert_data(data) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/js_regex/converter/literal_converter.rb', line 12

def convert_data(data)
  if data =~ ASTRAL_PLANE_CODEPOINT_PATTERN
    convert_astral_data(data)
  else
    escape_incompatible_bmp_literals(data)
  end
end

.escape_incompatible_bmp_literals(data) ⇒ Object



30
31
32
# File 'lib/js_regex/converter/literal_converter.rb', line 30

def escape_incompatible_bmp_literals(data)
  data.gsub('/', '\\/').gsub(/[\f\n\r\t]/) { |lit| Regexp.escape(lit) }
end