Module: RipperRubyParser::Unescape Private

Included in:
SexpProcessor
Defined in:
lib/ripper_ruby_parser/unescape.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Implements string unescaping

Constant Summary collapse

ESCAPE_SEQUENCE_REGEXP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/\\(
  [0-7]{1,3}          | # octal character
  x[0-9a-fA-F]{1,2}   | # hex byte
  u[0-9a-fA-F]{4}     | # unicode character
  u{[0-9a-fA-F]{4,6}} | # unicode character
  M-\\C-.             | # meta-ctrl
  C-\\M-.             | # ctrl-meta
  M-\\c.              | # meta-ctrl (shorthand)
  c\\M-.              | # ctrl-meta (shorthand)
  C-.                 | # control (regular)
  c.                  | # control (shorthand)
  M-.                 | # meta
  \n                  | # line break
  .                     # other single character
)/x
SINGLE_LETTER_ESCAPES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  "a" => "\a",
  "b" => "\b",
  "e" => "\e",
  "f" => "\f",
  "n" => "\n",
  "r" => "\r",
  "s" => "\s",
  "t" => "\t",
  "v" => "\v"
}.freeze
SINGLE_LETTER_ESCAPES_REGEXP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Regexp.new("^[#{SINGLE_LETTER_ESCAPES.keys.join}]$")
DELIMITER_PAIRS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  "(" => "()",
  "<" => "<>",
  "[" => "[]",
  "{" => "{}"
}.freeze

Instance Method Summary collapse

Instance Method Details

#fix_encoding(string) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



95
96
97
98
99
100
101
# File 'lib/ripper_ruby_parser/unescape.rb', line 95

def fix_encoding(string)
  unless string.encoding == Encoding::UTF_8
    dup = string.dup.force_encoding Encoding::UTF_8
    return dup if dup.valid_encoding?
  end
  string
end

#simple_unescape(string, delimiter) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ripper_ruby_parser/unescape.rb', line 47

def simple_unescape(string, delimiter)
  delimiters = delimiter_regexp_pattern(delimiter)
  string.gsub(/
              \\ # a backslash
              (  # followed by a
                #{delimiters} | # delimiter or
                \\              # backslash
              )/x) do
                Regexp.last_match[1]
              end
end

#simple_unescape_wordlist_word(string, delimiter) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ripper_ruby_parser/unescape.rb', line 59

def simple_unescape_wordlist_word(string, delimiter)
  delimiters = delimiter_regexp_pattern(delimiter)
  string.gsub(/
              \\ # a backslash
              (  # followed by a
                #{delimiters} | # delimiter or
                \\            | # backslash or
                [ ]           | # space or
                \n              # newline
              )
              /x) do
                Regexp.last_match[1]
              end
end

#unescape(string) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ripper_ruby_parser/unescape.rb', line 74

def unescape(string)
  string = string.dup if string.frozen?
  string.force_encoding("ASCII-8BIT")
  result = string.gsub(ESCAPE_SEQUENCE_REGEXP) do
    bare = Regexp.last_match[1]
    if bare == "\n"
      ""
    else
      unescaped_value(bare).force_encoding("ASCII-8BIT")
    end
  end
  fix_encoding result
end

#unescape_regexp(string) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ripper_ruby_parser/unescape.rb', line 103

def unescape_regexp(string)
  string.gsub(/\\(\n|\\)/) do
    bare = Regexp.last_match[1]
    case bare
    when "\n"
      ""
    else
      "\\\\"
    end
  end
end

#unescape_wordlist_word(string) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



88
89
90
91
92
93
# File 'lib/ripper_ruby_parser/unescape.rb', line 88

def unescape_wordlist_word(string)
  string.gsub(ESCAPE_SEQUENCE_REGEXP) do
    bare = Regexp.last_match[1]
    unescaped_value(bare)
  end
end