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

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

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



23
24
25
26
27
28
29
30
# File 'lib/ripper_ruby_parser/unescape.rb', line 23

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ripper_ruby_parser/unescape.rb', line 32

def unescape(string)
  string.gsub(/\\(
    [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
      .                   # single-character
  )/x) do
    bare = Regexp.last_match[1]
    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
    else
      bare
    end
  end
end