Module: RipperRubyParser::Unescape Private

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
  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 continuation
  .                   # 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}]$")

Class Method Summary collapse

Class 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.



76
77
78
79
80
81
82
# File 'lib/ripper_ruby_parser/unescape.rb', line 76

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) ⇒ 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.



39
40
41
42
43
44
45
46
# File 'lib/ripper_ruby_parser/unescape.rb', line 39

def simple_unescape(string)
  string.gsub(/\\(
    '   | # single quote
    \\    # backslash
  )/x) do
    Regexp.last_match[1]
  end
end

.simple_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.



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

def simple_unescape_wordlist_word(string)
  string.gsub(/\\(
    '   | # single quote
    \\  | # backslash
    \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.



58
59
60
61
62
63
64
65
66
67
# File 'lib/ripper_ruby_parser/unescape.rb', line 58

def unescape(string)
  string.gsub(ESCAPE_SEQUENCE_REGEXP) do
    bare = Regexp.last_match[1]
    if bare == "\n"
      ''
    else
      unescaped_value(bare)
    end
  end
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.



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ripper_ruby_parser/unescape.rb', line 84

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.



69
70
71
72
73
74
# File 'lib/ripper_ruby_parser/unescape.rb', line 69

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

.unescaped_value(bare) ⇒ 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.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ripper_ruby_parser/unescape.rb', line 96

def unescaped_value(bare)
  case bare
  when SINGLE_LETTER_ESCAPES_REGEXP
    SINGLE_LETTER_ESCAPES[bare]
  when /^x/
    bare[1..-1].to_i(16).chr
  when /^u/
    bare[1..-1].to_i(16).chr(Encoding::UTF_8)
  when /^(c|C-).$/
    (bare[-1].ord & 0b1001_1111).chr
  when /^M-.$/
    (bare[-1].ord | 0b1000_0000).chr
  when /^(M-\\C-|C-\\M-|M-\\c|c\\M-).$/
    (bare[-1].ord & 0b1001_1111 | 0b1000_0000).chr
  when /^[0-7]+/
    bare.to_i(8).chr
  when "\n"
    bare
  else
    bare
  end
end