Method: Janeway::Functions#translate_iregex_to_ruby_regex
- Defined in:
- lib/janeway/functions.rb
#translate_iregex_to_ruby_regex(iregex, anchor: true) ⇒ Regexp
Convert IRegexp format to ruby regexp equivalent, following the instructions in rfc9485.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/janeway/functions.rb', line 13 def translate_iregex_to_ruby_regex(iregex, anchor: true) # * For any unescaped dots (.) outside character classes (first # alternative of charClass production), replace the dot with [^\n\r]. chars = iregex.chars in_char_class = false indexes = [] chars.each_with_index do |char, i| case char when '[' then in_char_class = true when ']' in_char_class = false unless chars[i - 1] == '\\' # escaped ] does not close char class when '.' next if in_char_class || chars[i - 1] == '\\' # escaped dot indexes << i # replace this dot end end indexes.reverse_each do |i| chars[i] = '[^\n\r]' end # * Enclose the regexp in \A(?: and )\z. regex_str = anchor ? format('\A(?:%s)\z', chars.join) : chars.join Regexp.new(regex_str) end |