forked from rubinius/rubinius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrscan.rb
286 lines (222 loc) · 5.17 KB
/
strscan.rb
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
class ScanError < StandardError; end
class StringScanner
Id = "None$Id".freeze
Version = "1.0.0".freeze
attr_reader :pos, :match, :prev_pos
def pos=(n)
n = Integer(n)
n += @string.size if n < 0
if n < 0 or n > @string.size
raise RangeError, "index out of range (#{n})"
end
@pos = n
end
alias_method :pointer, :pos
alias_method :pointer=, :pos=
def [](n)
raise TypeError, "Bad argument #{n.inspect}" unless n.respond_to? :to_int
@match[n] if @match
end
def bol?
@pos == 0 or @string[pos-1] == ?\n
end
alias_method :beginning_of_line?, :bol?
def check(pattern)
_scan pattern, false, true, true
end
def check_until(pattern)
_scan pattern, false, true, false
end
def clear
warn "StringScanner#clear is obsolete; use #terminate instead" if $VERBOSE
terminate
end
def concat(str)
@string << StringValue(str)
self
end
alias_method :<<, :concat
def empty?
warn "StringScanner#empty? is obsolete; use #eos? instead?" if $VERBOSE
eos?
end
def eos?
@pos >= @string.size
end
def exist?(pattern)
_scan pattern, false, false, false
end
def get_byte
scan(/./mn)
end
def getbyte
warn "StringScanner#getbyte is obsolete; use #get_byte instead" if $VERBOSE
get_byte
end
def getch
scan(/./m)
end
def initialize(string, dup=false)
if string.instance_of? String
@original = string
@string = string
else
@original = StringValue(string)
@string = String.new @original
end
reset_state
end
def inspect
if defined? @string
if eos?
str = "#<StringScanner fin>"
else
if string.size - pos > 5
rest = "#{string[pos..pos+4]}..."
else
rest = string[pos..string.size]
end
if pos > 0
if pos > 5
prev = "...#{string[pos-5...pos]}"
else
prev = string[0...pos]
end
str = "#<StringScanner #{pos}/#{string.size} #{prev.inspect} @ #{rest.inspect}>"
else
str = "#<StringScanner #{pos}/#{string.size} @ #{rest.inspect}>"
end
end
str.taint if @string.tainted?
return str
else
"#<StringScanner (uninitialized)>"
end
end
def match?(pattern)
_scan pattern, false, false, true
end
def matched
@match.to_s if @match
end
def matched?
!!@match
end
def matched_size
@match.to_s.size if @match
end
def matchedsize
warn "StringScanner#matchedsize is obsolete; use #matched_size instead" if $VERBOSE
matched_size
end
def post_match
@match.post_match if @match
end
def pre_match
@string.substring(0, match.begin(0)) if @match
end
def reset_state
@prev_pos = @pos = 0
@match = nil
end
private :reset_state
def reset
reset_state
self
end
def rest
@string.substring(@pos, rest_size)
end
def rest?
return !eos?
end
def rest_size
@string.size - @pos
end
def restsize
warn "StringScanner#restsize is obsolete; use #rest_size instead" if $VERBOSE
rest_size
end
def scan(pattern)
_scan pattern, true, true, true
end
def scan_until(pattern)
_scan pattern, true, true, false
end
def scan_full(pattern, advance_pos, getstr)
_scan pattern, advance_pos, getstr, true
end
def search_full(pattern, advance_pos, getstr)
_scan pattern, advance_pos, getstr, false
end
def self.must_C_version
self
end
def skip(pattern)
_scan pattern, true, false, true
end
def skip_until(pattern)
_scan pattern, true, false, false
end
def string
@original
end
def string=(string)
reset_state
if string.instance_of? String
@original = string
@string = string
else
@original = StringValue(string)
@string = String.new @original
end
end
def terminate
@match = nil
@pos = string.size
self
end
def unscan
raise ScanError if @match.nil?
@pos = @prev_pos
@prev_pos = nil
@match = nil
self
end
def peek(len)
raise ArgumentError if len < 0
return "" if len.zero?
begin
return @string[pos, len]
rescue TypeError
raise RangeError, "offset outside of possible range"
end
end
def peep(len)
warn "StringScanner#peep is obsolete; use #peek instead" if $VERBOSE
peek len
end
def _scan(pattern, advance_pos, getstr, headonly)
unless pattern.kind_of? Regexp
raise TypeError, "bad pattern argument: #{pattern.inspect}"
end
@match = nil
if headonly
# NOTE - match_start is an Oniguruma feature that Rubinius exposes.
# We use it here to avoid creating a new Regexp with '^' prepended.
@match = pattern.match_start @string, @pos
else
# NOTE - search_from is an Oniguruma feature that Rubinius exposes.
# We use it so we can begin the search in the middle of the string
@match = pattern.search_from @string, @pos
end
return nil unless @match
fin = @match.end(0)
@prev_pos = @pos
@pos = fin if advance_pos
width = fin - @prev_pos
return width unless getstr
@string.substring(@prev_pos, width)
end
private :_scan
end