Module: Srx::IcuRegex

Defined in:
lib/srx/icu_regex.rb

Overview

Utilities for handling SRX (ICU) regular expressions

Constant Summary collapse

HEX_PATTERN =
/(?<!\\)(?:\\\\)*\\x(?<hex>\{[a-f0-9]{1,6}\})/i.freeze
OCTAL_PATTERN =
/(?<!\\)(?:\\\\)*\\0(?<oct>[0-7]{1,3})/i.freeze

Class Method Summary collapse

Class Method Details

.compile(icu_regex) ⇒ Regexp

Parameters:

  • icu_regex (String)

Returns:

  • (Regexp)


24
25
26
# File 'lib/srx/icu_regex.rb', line 24

def compile(icu_regex)
  Regexp.new(to_ruby(icu_regex))
end

.to_ruby(icu_regex) ⇒ String

Parameters:

  • icu_regex (String)

Returns:

  • (String)


12
13
14
15
16
17
18
19
20
# File 'lib/srx/icu_regex.rb', line 12

def to_ruby(icu_regex)
  result = icu_regex.dup
  result.gsub!(HEX_PATTERN, '\u\k<hex>')
  result.gsub!(OCTAL_PATTERN) do |m|
    $LAST_MATCH_INFO['oct'].to_i(8).then { |o| o <= 255 ? format(%q(\u{%x}), o) : m }
  end

  result
end