summaryrefslogtreecommitdiff
path: root/spec/ruby/language/regexp/escapes_spec.rb
blob: 16a4d8c23b2bf62b3ed12551be61fb19c38ce7e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- encoding: binary -*-
require_relative '../../spec_helper'
require_relative '../fixtures/classes'

# TODO: synchronize with spec/core/regexp/new_spec.rb -
#       escaping is also tested there
describe "Regexps with escape characters" do
  it "supports escape sequences" do
    /\t/.match("\t").to_a.should == ["\t"] # horizontal tab
    /\v/.match("\v").to_a.should == ["\v"] # vertical tab
    /\n/.match("\n").to_a.should == ["\n"] # newline
    /\r/.match("\r").to_a.should == ["\r"] # return
    /\f/.match("\f").to_a.should == ["\f"] # form feed
    /\a/.match("\a").to_a.should == ["\a"] # bell
    /\e/.match("\e").to_a.should == ["\e"] # escape

    # \nnn         octal char            (encoded byte value)
  end

  it "supports quoting meta-characters via escape sequence" do
    # parenthesis, etc
    /\(/.match("(").to_a.should == ["("]
    /\)/.match(")").to_a.should == [")"]
    /\[/.match("[").to_a.should == ["["]
    /\]/.match("]").to_a.should == ["]"]
    /\{/.match("{").to_a.should == ["{"]
    /\}/.match("}").to_a.should == ["}"]
    /\</.match("<").to_a.should == ["<"]
    /\>/.match(">").to_a.should == [">"]
    # alternation separator
    /\|/.match("|").to_a.should == ["|"]
    # quantifiers
    /\?/.match("?").to_a.should == ["?"]
    /\./.match(".").to_a.should == ["."]
    /\*/.match("*").to_a.should == ["*"]
    /\+/.match("+").to_a.should == ["+"]
    # line anchors
    /\^/.match("^").to_a.should == ["^"]
    /\$/.match("$").to_a.should == ["$"]
  end

  it "supports quoting meta-characters via escape sequence when used as a terminator" do
    # parenthesis, etc
    # %r[[, %r((, etc literals - are forbidden
    %r(\().match("(").to_a.should == ["("]
    %r(\)).match(")").to_a.should == [")"]
    %r)\().match("(").to_a.should == ["("]
    %r)\)).match(")").to_a.should == [")"]

    %r[\[].match("[").to_a.should == ["["]
    %r[\]].match("]").to_a.should == ["]"]
    %r]\[].match("[").to_a.should == ["["]
    %r]\]].match("]").to_a.should == ["]"]

    %r{\{}.match("{").to_a.should == ["{"]
    %r{\}}.match("}").to_a.should == ["}"]
    %r}\{}.match("{").to_a.should == ["{"]
    %r}\}}.match("}").to_a.should == ["}"]

    %r<\<>.match("<").to_a.should == ["<"]
    %r<\>>.match(">").to_a.should == [">"]
    %r>\<>.match("<").to_a.should == ["<"]
    %r>\>>.match(">").to_a.should == [">"]

    # alternation separator
    %r|\||.match("|").to_a.should == ["|"]
    # quantifiers
    %r?\??.match("?").to_a.should == ["?"]
    %r.\...match(".").to_a.should == ["."]
    %r*\**.match("*").to_a.should == ["*"]
    %r+\++.match("+").to_a.should == ["+"]
    # line anchors
    %r^\^^.match("^").to_a.should == ["^"]
    %r$\$$.match("$").to_a.should == ["$"]
  end

  it "supports quoting non-meta-characters via escape sequence when used as a terminator" do
    non_meta_character_terminators = [
      '!', '"', '#', '%', '&', "'", ',', '-', ':', ';', '@', '_', '`', '/', '=', '~'
    ]

    non_meta_character_terminators.each do |c|
      pattern = eval("%r" + c + "\\" + c + c)
      pattern.match(c).to_a.should == [c]
    end
  end

  it "does not change semantics of escaped non-meta-character when used as a terminator" do
    all_terminators = [*("!".."/"), *(":".."@"), *("[".."`"), *("{".."~")]
    meta_character_terminators = ["$", "^", "*", "+", ".", "?", "|", "}", ")", ">", "]"]
    special_cases = ['(', '{', '[', '<', '\\']

    # it should be equivalent to
    #   [ '!', '"', '#', '%', '&', "'", ',', '-', ':', ';', '@', '_', '`', '/', '=', '~' ]
    non_meta_character_terminators = all_terminators - meta_character_terminators - special_cases

    non_meta_character_terminators.each do |c|
      pattern = eval("%r" + c + "\\" + c + c)
      pattern.should == /#{c}/
    end
  end

  it "does not change semantics of escaped meta-character when used as a terminator" do
    meta_character_terminators = ["$", "^", "*", "+", ".", "?", "|", "}", ")", ">", "]"]

    meta_character_terminators.each do |c|
      pattern = eval("%r" + c + "\\" + c + c)
      pattern.should == eval("/\\#{c}/")
    end
  end

  it "allows any character to be escaped" do
    /\y/.match("y").to_a.should == ["y"]
  end

  it "supports \\x (hex characters)" do
    /\xA/.match("\nxyz").to_a.should == ["\n"]
    /\x0A/.match("\n").to_a.should == ["\n"]
    /\xAA/.match("\nA").should be_nil
    /\x0AA/.match("\nA").to_a.should == ["\nA"]
    /\xAG/.match("\nG").to_a.should == ["\nG"]
    # Non-matches
    -> { eval('/\xG/') }.should raise_error(SyntaxError)

    # \x{7HHHHHHH} wide hexadecimal char (character code point value)
  end

  it "supports \\c (control characters)" do
    #/\c \c@\c`/.match("\00\00\00").to_a.should == ["\00\00\00"]
    /\c#\cc\cC/.match("\03\03\03").to_a.should == ["\03\03\03"]
    /\c'\cG\cg/.match("\a\a\a").to_a.should == ["\a\a\a"]
    /\c(\cH\ch/.match("\b\b\b").to_a.should == ["\b\b\b"]
    /\c)\cI\ci/.match("\t\t\t").to_a.should == ["\t\t\t"]
    /\c*\cJ\cj/.match("\n\n\n").to_a.should == ["\n\n\n"]
    /\c+\cK\ck/.match("\v\v\v").to_a.should == ["\v\v\v"]
    /\c,\cL\cl/.match("\f\f\f").to_a.should == ["\f\f\f"]
    /\c-\cM\cm/.match("\r\r\r").to_a.should == ["\r\r\r"]

    /\cJ/.match("\r").should be_nil

    # Parsing precedence
    /\cJ+/.match("\n\n").to_a.should == ["\n\n"] # Quantifiers apply to entire escape sequence
    /\\cJ/.match("\\cJ").to_a.should == ["\\cJ"]
    -> { eval('/[abc\x]/') }.should raise_error(SyntaxError) # \x is treated as a escape sequence even inside a character class
    # Syntax error
    -> { eval('/\c/') }.should raise_error(SyntaxError)

    # \cx          control char          (character code point value)
    # \C-x         control char          (character code point value)
    # \M-x         meta  (x|0x80)        (character code point value)
    # \M-\C-x      meta control char     (character code point value)
  end

  it "handles three digit octal escapes starting with 0" do
    /[\000-\b]/.match("\x00")[0].should == "\x00"
  end

  it "handles control escapes with \\C-x syntax" do
    /\C-*\C-J\C-j/.match("\n\n\n")[0].should == "\n\n\n"
  end

  it "supports the \\K keep operator" do
    /a\Kb/.match("ab")[0].should == "b"
  end

  it "supports the \\R line break escape" do
    /\R/.match("\n")[0].should == "\n"
  end
end