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{10FFFF}]/
LITERAL_REQUIRING_ESCAPE_PATTERN =
/[\/\f\n\r\t\v]/
ESCAPES =
Hash.new { |h, k| raise KeyError, "#{h}[#{k.inspect}]" }
.merge("\f\n\r\t\v".chars.to_h { |c| [c, Regexp.escape(c)] })
.merge('/' => '\\/')

Class Method Summary collapse

Methods inherited from Base

#convert

Class Method Details

.convert_astral_data(data) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/js_regex/converter/literal_converter.rb', line 25

def convert_astral_data(data)
  data.each_char.each_with_object(Node.new) do |char, node|
    if char.ord > 0xFFFF
      node << surrogate_substitution_for(char)
    else
      node << escape_incompatible_bmp_literals(char)
    end
  end
end

.convert_data(data, context) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/js_regex/converter/literal_converter.rb', line 13

def convert_data(data, context)
  if !context.u? && data =~ ASTRAL_PLANE_CODEPOINT_PATTERN
    if context.enable_u_option
      escape_incompatible_bmp_literals(data)
    else
      convert_astral_data(data)
    end
  else
    escape_incompatible_bmp_literals(data)
  end
end

.escape_incompatible_bmp_literals(data) ⇒ Object



39
40
41
# File 'lib/js_regex/converter/literal_converter.rb', line 39

def escape_incompatible_bmp_literals(data)
  data.gsub(LITERAL_REQUIRING_ESCAPE_PATTERN, ESCAPES)
end