rubysl-strscan 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.travis.yml +7 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE +25 -0
  6. data/README.md +29 -0
  7. data/Rakefile +1 -0
  8. data/lib/rubysl/strscan.rb +2 -0
  9. data/lib/rubysl/strscan/strscan.rb +286 -0
  10. data/lib/rubysl/strscan/version.rb +5 -0
  11. data/lib/strscan.rb +1 -0
  12. data/rubysl-strscan.gemspec +22 -0
  13. data/spec/append_spec.rb +10 -0
  14. data/spec/beginning_of_line_spec.rb +6 -0
  15. data/spec/bol_spec.rb +6 -0
  16. data/spec/check_spec.rb +15 -0
  17. data/spec/check_until_spec.rb +14 -0
  18. data/spec/clear_spec.rb +24 -0
  19. data/spec/concat_spec.rb +10 -0
  20. data/spec/dup_spec.rb +38 -0
  21. data/spec/element_reference_spec.rb +48 -0
  22. data/spec/empty_spec.rb +24 -0
  23. data/spec/eos_spec.rb +6 -0
  24. data/spec/exist_spec.rb +23 -0
  25. data/spec/get_byte_spec.rb +6 -0
  26. data/spec/getbyte_spec.rb +27 -0
  27. data/spec/getch_spec.rb +43 -0
  28. data/spec/initialize_spec.rb +27 -0
  29. data/spec/inspect_spec.rb +19 -0
  30. data/spec/match_spec.rb +27 -0
  31. data/spec/matched_size_spec.rb +6 -0
  32. data/spec/matched_spec.rb +40 -0
  33. data/spec/matchedsize_spec.rb +26 -0
  34. data/spec/must_C_version_spec.rb +7 -0
  35. data/spec/peek_spec.rb +7 -0
  36. data/spec/peep_spec.rb +24 -0
  37. data/spec/pointer_spec.rb +10 -0
  38. data/spec/pos_spec.rb +10 -0
  39. data/spec/post_match_spec.rb +27 -0
  40. data/spec/pre_match_spec.rb +40 -0
  41. data/spec/reset_spec.rb +14 -0
  42. data/spec/rest_size_spec.rb +6 -0
  43. data/spec/rest_spec.rb +47 -0
  44. data/spec/restsize_spec.rb +24 -0
  45. data/spec/scan_full_spec.rb +29 -0
  46. data/spec/scan_spec.rb +81 -0
  47. data/spec/scan_until_spec.rb +22 -0
  48. data/spec/search_full_spec.rb +29 -0
  49. data/spec/shared/bol.rb +25 -0
  50. data/spec/shared/concat.rb +30 -0
  51. data/spec/shared/eos.rb +17 -0
  52. data/spec/shared/extract_range.rb +22 -0
  53. data/spec/shared/extract_range_matched.rb +22 -0
  54. data/spec/shared/get_byte.rb +28 -0
  55. data/spec/shared/matched_size.rb +21 -0
  56. data/spec/shared/peek.rb +44 -0
  57. data/spec/shared/pos.rb +83 -0
  58. data/spec/shared/rest_size.rb +18 -0
  59. data/spec/shared/terminate.rb +17 -0
  60. data/spec/skip_spec.rb +17 -0
  61. data/spec/skip_until_spec.rb +17 -0
  62. data/spec/string_spec.rb +39 -0
  63. data/spec/terminate_spec.rb +6 -0
  64. data/spec/unscan_spec.rb +28 -0
  65. metadata +201 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f4200d0f56475b659a5460631c71d16c4fae6b8e
4
+ data.tar.gz: ed5788af588ec949556a0bd826377d12b638d362
5
+ SHA512:
6
+ metadata.gz: 3ed31472f343021f4e9a5509f26c72b3f0830ea0030939f63174c496320641a324f2b3d33db0ec4f3135d4393fcb8867a8c7a9eeb8bd2dc4f6550fec5661d26e
7
+ data.tar.gz: 5ee46b380ad0ab22cced43a2a06aeadc104dfcc4271ccc0f396c4b881c4a0623e93fc3736311703a776c4fe78ff28a8ab4465da6c9ea63ae3885047d5d7af1e4
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ env:
3
+ - RUBYLIB=lib
4
+ script: bundle exec mspec
5
+ rvm:
6
+ - 1.8.7
7
+ - rbx-nightly-18mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rubysl-strscan.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ Copyright (c) 2013, Brian Shirai
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ 3. Neither the name of the library nor the names of its contributors may be
13
+ used to endorse or promote products derived from this software without
14
+ specific prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT,
20
+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23
+ OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Rubysl::Strscan
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rubysl-strscan'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rubysl-strscan
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,2 @@
1
+ require "rubysl/strscan/strscan"
2
+ require "rubysl/strscan/version"
@@ -0,0 +1,286 @@
1
+ class ScanError < StandardError; end
2
+
3
+ class StringScanner
4
+ Id = "None$Id".freeze
5
+ Version = "1.0.0".freeze
6
+
7
+ attr_reader :pos, :match, :prev_pos
8
+
9
+ def pos=(n)
10
+ n = Integer(n)
11
+
12
+ n += @string.bytesize if n < 0
13
+
14
+ if n < 0 or n > @string.bytesize
15
+ raise RangeError, "index out of range (#{n})"
16
+ end
17
+
18
+ @pos = n
19
+ end
20
+
21
+ alias_method :pointer, :pos
22
+ alias_method :pointer=, :pos=
23
+
24
+ def [](n)
25
+ raise TypeError, "Bad argument #{n.inspect}" unless n.respond_to? :to_int
26
+ @match[n] if @match
27
+ end
28
+
29
+ def bol?
30
+ @pos == 0 or @string.getbyte(pos-1) == 10
31
+ end
32
+
33
+ alias_method :beginning_of_line?, :bol?
34
+
35
+ def check(pattern)
36
+ _scan pattern, false, true, true
37
+ end
38
+
39
+ def check_until(pattern)
40
+ _scan pattern, false, true, false
41
+ end
42
+
43
+ def clear
44
+ warn "StringScanner#clear is obsolete; use #terminate instead" if $VERBOSE
45
+ terminate
46
+ end
47
+
48
+ def concat(str)
49
+ @string << StringValue(str)
50
+ self
51
+ end
52
+ alias_method :<<, :concat
53
+
54
+ def empty?
55
+ warn "StringScanner#empty? is obsolete; use #eos? instead?" if $VERBOSE
56
+ eos?
57
+ end
58
+
59
+ def eos?
60
+ @pos >= @string.bytesize
61
+ end
62
+
63
+ def exist?(pattern)
64
+ _scan pattern, false, false, false
65
+ end
66
+
67
+ def get_byte
68
+ scan(/./mn)
69
+ end
70
+
71
+ def getbyte
72
+ warn "StringScanner#getbyte is obsolete; use #get_byte instead" if $VERBOSE
73
+ get_byte
74
+ end
75
+
76
+ def getch
77
+ scan(/./m)
78
+ end
79
+
80
+ def initialize(string, dup=false)
81
+ if string.instance_of? String
82
+ @original = string
83
+ @string = string
84
+ else
85
+ @original = StringValue(string)
86
+ @string = String.new @original
87
+ end
88
+
89
+ reset_state
90
+ end
91
+
92
+ def inspect
93
+ if defined? @string
94
+ if eos?
95
+ str = "#<StringScanner fin>"
96
+ else
97
+ if string.bytesize - pos > 5
98
+ rest = "#{string[pos..pos+4]}..."
99
+ else
100
+ rest = string[pos..string.bytesize]
101
+ end
102
+
103
+ if pos > 0
104
+ if pos > 5
105
+ prev = "...#{string[pos-5...pos]}"
106
+ else
107
+ prev = string[0...pos]
108
+ end
109
+
110
+ str = "#<StringScanner #{pos}/#{string.bytesize} #{prev.inspect} @ #{rest.inspect}>"
111
+ else
112
+ str = "#<StringScanner #{pos}/#{string.bytesize} @ #{rest.inspect}>"
113
+ end
114
+ end
115
+
116
+ str.taint if @string.tainted?
117
+ return str
118
+ else
119
+ "#<StringScanner (uninitialized)>"
120
+ end
121
+ end
122
+
123
+ def match?(pattern)
124
+ _scan pattern, false, false, true
125
+ end
126
+
127
+ def matched
128
+ @match.to_s if @match
129
+ end
130
+
131
+ def matched?
132
+ !!@match
133
+ end
134
+
135
+ def matched_size
136
+ @match.to_s.bytesize if @match
137
+ end
138
+
139
+ def matchedsize
140
+ warn "StringScanner#matchedsize is obsolete; use #matched_size instead" if $VERBOSE
141
+ matched_size
142
+ end
143
+
144
+ def post_match
145
+ @match.post_match if @match
146
+ end
147
+
148
+ def pre_match
149
+ @string.byteslice(0, match.full.at(0)) if @match
150
+ end
151
+
152
+ def reset_state
153
+ @prev_pos = @pos = 0
154
+ @match = nil
155
+ end
156
+ private :reset_state
157
+
158
+ def reset
159
+ reset_state
160
+ self
161
+ end
162
+
163
+ def rest
164
+ @string.byteslice(@pos, @string.bytesize - @pos)
165
+ end
166
+
167
+ def rest?
168
+ return !eos?
169
+ end
170
+
171
+ def rest_size
172
+ @string.bytesize - @pos
173
+ end
174
+
175
+ def restsize
176
+ warn "StringScanner#restsize is obsolete; use #rest_size instead" if $VERBOSE
177
+ rest_size
178
+ end
179
+
180
+ def scan(pattern)
181
+ _scan pattern, true, true, true
182
+ end
183
+
184
+ def scan_until(pattern)
185
+ _scan pattern, true, true, false
186
+ end
187
+
188
+ def scan_full(pattern, advance_pos, getstr)
189
+ _scan pattern, advance_pos, getstr, true
190
+ end
191
+
192
+ def search_full(pattern, advance_pos, getstr)
193
+ _scan pattern, advance_pos, getstr, false
194
+ end
195
+
196
+ def self.must_C_version
197
+ self
198
+ end
199
+
200
+ def skip(pattern)
201
+ _scan pattern, true, false, true
202
+ end
203
+
204
+ def skip_until(pattern)
205
+ _scan pattern, true, false, false
206
+ end
207
+
208
+ def string
209
+ @original
210
+ end
211
+
212
+ def string=(string)
213
+ reset_state
214
+
215
+ if string.instance_of? String
216
+ @original = string
217
+ @string = string
218
+ else
219
+ @original = StringValue(string)
220
+ @string = String.new @original
221
+ end
222
+ end
223
+
224
+ def terminate
225
+ @match = nil
226
+ @pos = string.bytesize
227
+ self
228
+ end
229
+
230
+ def unscan
231
+ raise ScanError if @match.nil?
232
+ @pos = @prev_pos
233
+ @prev_pos = nil
234
+ @match = nil
235
+ self
236
+ end
237
+
238
+ def peek(len)
239
+ raise ArgumentError if len < 0
240
+ return "" if len.zero?
241
+
242
+ begin
243
+ return @string[pos, len]
244
+ rescue TypeError
245
+ raise RangeError, "offset outside of possible range"
246
+ end
247
+ end
248
+
249
+ def peep(len)
250
+ warn "StringScanner#peep is obsolete; use #peek instead" if $VERBOSE
251
+ peek len
252
+ end
253
+
254
+ def _scan(pattern, advance_pos, getstr, headonly)
255
+ unless pattern.kind_of? Regexp
256
+ raise TypeError, "bad pattern argument: #{pattern.inspect}"
257
+ end
258
+
259
+ @match = nil
260
+
261
+ if headonly
262
+ # NOTE - match_start is an Oniguruma feature that Rubinius exposes.
263
+ # We use it here to avoid creating a new Regexp with '^' prepended.
264
+ @match = pattern.match_start @string, @pos
265
+ else
266
+ # NOTE - search_from is an Oniguruma feature that Rubinius exposes.
267
+ # We use it so we can begin the search in the middle of the string
268
+ @match = pattern.search_from @string, @pos
269
+ end
270
+
271
+ return nil unless @match
272
+
273
+ fin = @match.full.at(1)
274
+
275
+ @prev_pos = @pos
276
+
277
+ @pos = fin if advance_pos
278
+
279
+ width = fin - @prev_pos
280
+
281
+ return width unless getstr
282
+
283
+ @string.byteslice(@prev_pos, width)
284
+ end
285
+ private :_scan
286
+ end
@@ -0,0 +1,5 @@
1
+ module RubySL
2
+ module StringScanner
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
data/lib/strscan.rb ADDED
@@ -0,0 +1 @@
1
+ require "rubysl/strscan"
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ require './lib/rubysl/strscan/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "rubysl-strscan"
6
+ spec.version = RubySL::StringScanner::VERSION
7
+ spec.authors = ["Brian Shirai"]
8
+ spec.email = ["brixen@gmail.com"]
9
+ spec.description = %q{Ruby standard library strscan.}
10
+ spec.summary = %q{Ruby standard library strscan.}
11
+ spec.homepage = "https://github.com/rubysl/rubysl-strscan"
12
+ spec.license = "BSD"
13
+
14
+ spec.files = `git ls-files`.split($/)
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.3"
20
+ spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_development_dependency "mspec", "~> 1.5"
22
+ end
@@ -0,0 +1,10 @@
1
+ require File.expand_path('../shared/concat.rb', __FILE__)
2
+ require 'strscan'
3
+
4
+ describe "StringScanner#<<" do
5
+ it_behaves_like :strscan_concat, :<<
6
+ end
7
+
8
+ describe "StringScanner#<< when passed a Fixnum" do
9
+ it_behaves_like :strscan_concat_fixnum, :<<
10
+ end
@@ -0,0 +1,6 @@
1
+ require File.expand_path('../shared/bol.rb', __FILE__)
2
+ require 'strscan'
3
+
4
+ describe "StringScanner#beginning_of_line?" do
5
+ it_behaves_like(:strscan_bol, :beginning_of_line?)
6
+ end