[フレーム]

Class: StringScanner

Inherits:
Object show all
Defined in:
opal/stdlib/strscan.rb

Overview

backtick_javascript: true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ StringScanner

Returns a new instance of StringScanner.

6
7
8
9
10
11
12
# File 'opal/stdlib/strscan.rb', line 6
def initialize(string)
 @string = string
 @pos = 0
 @matched = nil
 @working = string
 @match = []
end

Instance Attribute Details

#matchedObject (readonly)

Returns the value of attribute matched.

4
5
6
# File 'opal/stdlib/strscan.rb', line 4
def matched
 @matched
end

#posObject

Returns the value of attribute pos.

4
5
6
# File 'opal/stdlib/strscan.rb', line 4
def pos
 @pos
end

#stringObject (readonly)

Returns the value of attribute string.

14
15
16
# File 'opal/stdlib/strscan.rb', line 14
def string
 @string
end

Instance Method Details

#[](idx) ⇒ Object

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
# File 'opal/stdlib/strscan.rb', line 64
def [](idx)
 if @match.empty?
 return nil
 end
 case idx
 when Symbol
 idx = idx.to_s
 when String 
 # noop
 else
 idx = ::Opal.coerce_to!(idx, Integer , :to_int)
 end
 %x{
 var match = #{@match};
 if (idx < 0) {
 idx += match.length;
 }
 if (idx < 0 || idx >= match.length) {
 return nil;
 }
 if (match[idx] == null) {
 return nil;
 }
 return match[idx];
 }
end

#beginning_of_line?Boolean Also known as: bol?

Returns:

16
17
18
# File 'opal/stdlib/strscan.rb', line 16
def beginning_of_line?
 `#{@pos} === 0 || #{@string}.charAt(#{@pos} - 1) === "\n"`
end

#check(pattern) ⇒ Object

95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'opal/stdlib/strscan.rb', line 95
def check(pattern)
 pattern = anchor(pattern)
 %x{
 var result = pattern.exec(#{@working});
 if (result == null) {
 return #{@matched} = nil;
 }
 return #{@matched} = result[0];
 }
end

#check_until(pattern) ⇒ Object

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'opal/stdlib/strscan.rb', line 109
def check_until(pattern)
 %x{
 var old_prev_pos = #{@prev_pos};
 var old_pos = #{@pos};
 var old_working = #{@working};
 var result = #{scan_until(pattern)};
 #{@prev_pos} = old_prev_pos;
 #{@pos} = old_pos;
 #{@working} = old_working;
 return result;
 }
end

#eos?Boolean

Returns:

129
130
131
# File 'opal/stdlib/strscan.rb', line 129
def eos?
 `#{@working}.length === 0`
end

#exist?(pattern) ⇒ Boolean

Returns:

133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'opal/stdlib/strscan.rb', line 133
def exist?(pattern)
 %x{
 var result = pattern.exec(#{@working});
 if (result == null) {
 return nil;
 }
 else if (result.index == 0) {
 return 0;
 }
 else {
 return result.index + 1;
 }
 }
end

#get_byteObject Also known as: getch

189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'opal/stdlib/strscan.rb', line 189
def get_byte
 %x{
 var result = nil;
 if (#{@pos} < #{@string}.length) {
 #{@prev_pos} = #{@pos};
 #{@pos} += 1;
 result = #{@matched} = #{@working}.substring(0, 1);
 #{@working} = #{@working}.substring(1);
 }
 else {
 #{@matched} = nil;
 }
 return result;
 }
end

#match?(pattern) ⇒ Boolean

Returns:

207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'opal/stdlib/strscan.rb', line 207
def match?(pattern)
 pattern = anchor(pattern)
 %x{
 var result = pattern.exec(#{@working});
 if (result == null) {
 return nil;
 }
 else {
 #{@prev_pos} = #{@pos};
 return result[0].length;
 }
 }
end

#matched_sizeObject

235
236
237
238
239
240
241
242
243
# File 'opal/stdlib/strscan.rb', line 235
def matched_size
 %x{
 if (#{@matched} === nil) {
 return nil;
 }
 return #{@matched}.length
 }
end

#peek(length) ⇒ Object

125
126
127
# File 'opal/stdlib/strscan.rb', line 125
def peek(length)
 `#{@working}.substring(0, length)`
end

#post_matchObject

245
246
247
248
249
250
251
252
253
# File 'opal/stdlib/strscan.rb', line 245
def post_match
 %x{
 if (#{@matched} === nil) {
 return nil;
 }
 return #{@string}.substr(#{@pos});
 }
end

#pre_matchObject

255
256
257
258
259
260
261
262
263
# File 'opal/stdlib/strscan.rb', line 255
def pre_match
 %x{
 if (#{@matched} === nil) {
 return nil;
 }
 return #{@string}.substr(0, #{@prev_pos});
 }
end

#resetObject

265
266
267
268
269
# File 'opal/stdlib/strscan.rb', line 265
def reset
 @working = @string
 @matched = nil
 @pos = 0
end

#restObject

271
272
273
# File 'opal/stdlib/strscan.rb', line 271
def rest
 @working
end

#rest?Boolean

Returns:

275
276
277
# File 'opal/stdlib/strscan.rb', line 275
def rest?
 `#{@working}.length !== 0`
end

#rest_sizeObject

279
280
281
# File 'opal/stdlib/strscan.rb', line 279
def rest_size
 rest.size
end

#scan(pattern) ⇒ Object

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'opal/stdlib/strscan.rb', line 20
def scan(pattern)
 pattern = anchor(pattern)
 %x{
 var result = pattern.exec(#{@working});
 if (result == null) {
 return #{@matched} = nil;
 }
 #{@prev_pos} = #{@pos};
 #{@pos} += result[0].length;
 #{@working} = #{@working}.substring(result[0].length);
 #{@matched} = result[0];
 #{@match} = result;
 return result[0];
 }
end

#scan_until(pattern) ⇒ Object

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'opal/stdlib/strscan.rb', line 39
def scan_until(pattern)
 pattern = anchor(pattern)
 %x{
 var working = #{@working}
 for(var i = 0; working.length != i; ++i) {
 var result = pattern.exec(working.substr(i));
 if (result !== null) {
 var matched_size = i + result[0].length
 var matched = working.substr(0, matched_size)
 #{@matched} = result[0]
 #{@match} = result
 #{@prev_pos} = #{@pos} + i; // Position of first character of matched
 #{@pos} += matched_size // Position one after last character of matched
 #{@working} = working.substr(matched_size)
 return matched
 }
 }
 return #{@matched} = nil;
 }
end

#skip(pattern) ⇒ Object

149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'opal/stdlib/strscan.rb', line 149
def skip(pattern)
 pattern = anchor(pattern)
 %x{
 var result = pattern.exec(#{@working});
 if (result == null) {
 #{@match} = [];
 return #{@matched} = nil;
 }
 else {
 var match_str = result[0];
 var match_len = match_str.length;
 #{@matched} = match_str;
 #{@match} = result;
 #{@prev_pos} = #{@pos};
 #{@pos} += match_len;
 #{@working} = #{@working}.substring(match_len);
 return match_len;
 }
 }
end

#skip_until(pattern) ⇒ Object

174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'opal/stdlib/strscan.rb', line 174
def skip_until(pattern)
 %x{
 var result = #{scan_until(pattern)};
 if (result === nil) {
 return nil;
 }
 else {
 #{@matched} = result.substr(-1);
 return result.length;
 }
 }
end

#terminateObject

283
284
285
286
# File 'opal/stdlib/strscan.rb', line 283
def terminate
 @match = nil
 self.pos = @string.length
end

#unscanObject

288
289
290
291
292
293
294
# File 'opal/stdlib/strscan.rb', line 288
def unscan
 @pos = @prev_pos
 @prev_pos = nil
 @match = nil
 self
end

AltStyle によって変換されたページ (->オリジナル) /