rubysl-strscan 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -0,0 +1,17 @@
1
+ describe :strscan_eos, :shared => true do
2
+ before :each do
3
+ @s = StringScanner.new("This is a test")
4
+ end
5
+
6
+ it "Returns true if the scan pointer is at the end of the string" do
7
+ @s.terminate
8
+ @s.send(@method).should be_true
9
+
10
+ s = StringScanner.new('')
11
+ s.send(@method).should be_true
12
+ end
13
+
14
+ it "Returns false if the scan pointer is not at the end of the string" do
15
+ @s.send(@method).should be_false
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ describe :extract_range, :shared => true do
2
+ it "returns an instance of String when passed a String subclass" do
3
+ cls = Class.new(String)
4
+ sub = cls.new("abc")
5
+
6
+ s = StringScanner.new(sub)
7
+ ch = s.send(@method)
8
+ ch.should_not be_kind_of(cls)
9
+ ch.should be_an_instance_of(String)
10
+ end
11
+
12
+ it "taints the returned String if the input was tainted" do
13
+ str = 'abc'
14
+ str.taint
15
+
16
+ s = StringScanner.new(str)
17
+
18
+ s.send(@method).tainted?.should be_true
19
+ s.send(@method).tainted?.should be_true
20
+ s.send(@method).tainted?.should be_true
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ describe :extract_range_matched, :shared => true do
2
+ it "returns an instance of String when passed a String subclass" do
3
+ cls = Class.new(String)
4
+ sub = cls.new("abc")
5
+
6
+ s = StringScanner.new(sub)
7
+ s.scan(/\w{1}/)
8
+
9
+ ch = s.send(@method)
10
+ ch.should_not be_kind_of(cls)
11
+ ch.should be_an_instance_of(String)
12
+ end
13
+
14
+ it "taints the returned String if the input was tainted" do
15
+ str = 'abc'
16
+ str.taint
17
+
18
+ s = StringScanner.new(str)
19
+ s.scan(/\w{1}/)
20
+ s.send(@method).tainted?.should be_true
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ describe :strscan_get_byte, :shared => true do
2
+ it "scans one byte and returns it" do
3
+ s = StringScanner.new('abc5.')
4
+ s.send(@method).should == 'a'
5
+ s.send(@method).should == 'b'
6
+ s.send(@method).should == 'c'
7
+ s.send(@method).should == '5'
8
+ s.send(@method).should == '.'
9
+ end
10
+
11
+ it "is not multi-byte character sensitive" do
12
+ s = StringScanner.new("\244\242")
13
+ s.send(@method).should == "\244"
14
+ s.send(@method).should == "\242"
15
+ end
16
+
17
+ it "returns nil at the end of the string" do
18
+ # empty string case
19
+ s = StringScanner.new('')
20
+ s.send(@method).should == nil
21
+ s.send(@method).should == nil
22
+
23
+ # non-empty string case
24
+ s = StringScanner.new('a')
25
+ s.send(@method) # skip one
26
+ s.send(@method).should == nil
27
+ end
28
+ end
@@ -0,0 +1,21 @@
1
+ describe :strscan_matched_size, :shared => true do
2
+ before :each do
3
+ @s = StringScanner.new("This is a test")
4
+ end
5
+
6
+ it "returns the size of the most recent match" do
7
+ @s.check /This/
8
+ @s.send(@method).should == 4
9
+ @s.send(@method).should == 4
10
+ @s.scan //
11
+ @s.send(@method).should == 0
12
+ end
13
+
14
+ it "returns nil if there was no recent match" do
15
+ @s.send(@method).should == nil
16
+ @s.check /\d+/
17
+ @s.send(@method).should == nil
18
+ @s.terminate
19
+ @s.send(@method).should == nil
20
+ end
21
+ end
@@ -0,0 +1,44 @@
1
+ describe :strscan_peek, :shared => true do
2
+ before :each do
3
+ @s = StringScanner.new('This is a test')
4
+ end
5
+
6
+ it "returns at most the specified number of characters from the current position" do
7
+ @s.send(@method, 4).should == "This"
8
+ @s.pos.should == 0
9
+ @s.pos = 5
10
+ @s.send(@method, 2).should == "is"
11
+ @s.send(@method, 1000).should == "is a test"
12
+ end
13
+
14
+ it "returns an empty string when the passed argument is zero" do
15
+ @s.send(@method, 0).should == ""
16
+ end
17
+
18
+ it "raises a ArgumentError when the passed argument is negative" do
19
+ lambda { @s.send(@method, -2) }.should raise_error(ArgumentError)
20
+ end
21
+
22
+ it "raises a RangeError when the passed argument is a Bignum" do
23
+ lambda { @s.send(@method, bignum_value) }.should raise_error(RangeError)
24
+ end
25
+
26
+ it "returns an instance of String when passed a String subclass" do
27
+ cls = Class.new(String)
28
+ sub = cls.new("abc")
29
+
30
+ s = StringScanner.new(sub)
31
+
32
+ ch = s.send(@method, 1)
33
+ ch.should_not be_kind_of(cls)
34
+ ch.should be_an_instance_of(String)
35
+ end
36
+
37
+ it "taints the returned String if the input was tainted" do
38
+ str = 'abc'
39
+ str.taint
40
+
41
+ s = StringScanner.new(str)
42
+ s.send(@method, 1).tainted?.should be_true
43
+ end
44
+ end
@@ -0,0 +1,83 @@
1
+ # encoding: utf-8
2
+
3
+ describe :strscan_pos, :shared => true do
4
+ before :each do
5
+ @s = StringScanner.new("This is a test")
6
+ @m = StringScanner.new("cölorfül")
7
+ end
8
+
9
+ it "returns the position of the scan pointer" do
10
+ @s.send(@method).should == 0
11
+ @s.scan_until /This is/
12
+ @s.send(@method).should == 7
13
+ @s.get_byte
14
+ @s.send(@method).should == 8
15
+ @s.terminate
16
+ @s.send(@method).should == 14
17
+ end
18
+
19
+ it "returns the position of the scan pointer for multibyte string" do
20
+ @m.send(@method).should == 0
21
+ @m.scan_until /cö/
22
+ @m.send(@method).should == 3
23
+ @m.get_byte
24
+ @m.send(@method).should == 4
25
+ @m.terminate
26
+ @m.send(@method).should == 10
27
+ end
28
+
29
+ it "returns 0 in the reset position" do
30
+ @s.reset
31
+ @s.send(@method).should == 0
32
+ end
33
+
34
+ it "returns the length of the string in the terminate position" do
35
+ @s.terminate
36
+ @s.send(@method).should == @s.string.length
37
+ end
38
+
39
+ it "returns the `bytesize` for multibyte string in the terminate position" do
40
+ @m.terminate
41
+ @m.send(@method).should == @m.string.bytesize
42
+ @m.send(@method).should >= @m.string.length
43
+ end
44
+ end
45
+
46
+ describe :strscan_pos_set, :shared => true do
47
+ before :each do
48
+ @s = StringScanner.new("This is a test")
49
+ @m = StringScanner.new("cölorfül")
50
+ end
51
+
52
+ it "modify the scan pointer" do
53
+ @s.send(@method, 5)
54
+ @s.rest.should == "is a test"
55
+ end
56
+
57
+ it "can poin position that greater than string length for multibyte string" do
58
+ @m.send(@method, 9)
59
+ @m.rest.should == "l"
60
+ end
61
+
62
+ it "positions from the end if the argument is negative" do
63
+ @s.send(@method, -2)
64
+ @s.rest.should == "st"
65
+ @s.pos.should == 12
66
+ end
67
+
68
+ it "positions from the end if the argument is negative for multibyte string" do
69
+ @m.send(@method, -3)
70
+ @m.rest.should == "ül"
71
+ @m.pos.should == 7
72
+ end
73
+
74
+ it "raises a RangeError if position too far backward" do
75
+ lambda {
76
+ @s.send(@method, -20)
77
+ }.should raise_error(RangeError)
78
+ end
79
+
80
+ it "raises a RangeError when the passed argument is out of range" do
81
+ lambda { @s.send(@method, 20) }.should raise_error(RangeError)
82
+ end
83
+ end
@@ -0,0 +1,18 @@
1
+ describe :strscan_rest_size, :shared => true do
2
+ before :each do
3
+ @s = StringScanner.new('This is a test')
4
+ end
5
+
6
+ it "Returns the length of the rest of the string" do
7
+ @s.send(@method).should == 14
8
+ @s.scan(/This/)
9
+ @s.send(@method).should == 10
10
+ @s.terminate
11
+ @s.send(@method).should == 0
12
+ end
13
+
14
+ it "is equivalent to rest.size" do
15
+ @s.scan(/This/)
16
+ @s.send(@method).should == @s.rest.size
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ describe :strscan_terminate, :shared => true do
4
+ it "set the scan pointer to the end of the string and clear matching data." do
5
+ s = StringScanner.new('This is a test')
6
+ s.send(@method)
7
+ s.bol?.should be_false
8
+ s.eos?.should be_true
9
+ end
10
+
11
+ it "set the scan pointer to the end of the string and clear matching data for multibyte string." do
12
+ s = StringScanner.new('cölorfül')
13
+ s.send(@method)
14
+ s.bol?.should be_false
15
+ s.eos?.should be_true
16
+ end
17
+ end
data/spec/skip_spec.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'strscan'
2
+
3
+ describe "StringScanner#skip" do
4
+ before :each do
5
+ @s = StringScanner.new("This is a test")
6
+ end
7
+
8
+ it "returns length of the match" do
9
+ @s.skip(/\w+/).should == 4
10
+ @s.skip(/\s+\w+/).should == 3
11
+ end
12
+
13
+ it "returns nil if there's no match" do
14
+ @s.skip(/\s+/).should == nil
15
+ @s.skip(/\d+/).should == nil
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ require 'strscan'
2
+
3
+ describe "StringScanner#skip_until" do
4
+ before :each do
5
+ @s = StringScanner.new("This is a test")
6
+ end
7
+
8
+ it "returns the number of bytes advanced and advances the scan pointer until pattern is matched and consumed" do
9
+ @s.skip_until(/a/).should == 9
10
+ @s.pos.should == 9
11
+ @s.matched.should == "a"
12
+ end
13
+
14
+ it "returns nil if no match was found" do
15
+ @s.skip_until(/d+/).should == nil
16
+ end
17
+ end
@@ -0,0 +1,39 @@
1
+ require 'strscan'
2
+
3
+ describe "StringScanner#string" do
4
+ before :each do
5
+ @string = "This is a test"
6
+ @s = StringScanner.new(@string)
7
+ end
8
+
9
+ it "returns the string being scanned" do
10
+ @s.string.should == "This is a test"
11
+ @s << " case"
12
+ @s.string.should == "This is a test case"
13
+ end
14
+
15
+ it "returns the identical object passed in" do
16
+ @s.string.equal?(@string).should be_true
17
+ end
18
+ end
19
+
20
+ describe "StringScanner#string=" do
21
+ before :each do
22
+ @s = StringScanner.new("This is a test")
23
+ end
24
+
25
+ it "changes the string being scanned to the argument and resets the scanner" do
26
+ @s.string = "Hello world"
27
+ @s.string.should == "Hello world"
28
+ end
29
+
30
+ it "converts the argument into a string using #to_str" do
31
+ m = mock(:str)
32
+
33
+ s = "test"
34
+ m.should_receive(:to_str).and_return(s)
35
+
36
+ @s.string = m
37
+ @s.string.should == s
38
+ end
39
+ end
@@ -0,0 +1,6 @@
1
+ require File.expand_path('../shared/terminate.rb', __FILE__)
2
+ require 'strscan'
3
+
4
+ describe "StringScanner#terminate" do
5
+ it_behaves_like(:strscan_terminate, :terminate)
6
+ end
@@ -0,0 +1,28 @@
1
+ require 'strscan'
2
+
3
+ describe "StringScanner#unscan" do
4
+ before :each do
5
+ @s = StringScanner.new("This is a test")
6
+ end
7
+
8
+ it "set the scan pointer to the previous position" do
9
+ @s.scan(/This/)
10
+ @s.unscan
11
+ @s.matched.should == nil
12
+ @s.pos.should == 0
13
+ end
14
+
15
+ it "remember only one previous position" do
16
+ pos1 = @s.pos
17
+ @s.scan(/This/)
18
+ pos2 = @s.pos
19
+ @s.scan(/ is/)
20
+ @s.unscan
21
+ @s.pos.should == pos2
22
+ end
23
+
24
+ it "raises a ScanError when the previous match had failed" do
25
+ lambda { @s.unscan }.should raise_error(ScanError)
26
+ lambda { @s.scan(/\d/); @s.unscan }.should raise_error(ScanError)
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,201 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubysl-strscan
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Shirai
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ description: Ruby standard library strscan.
56
+ email:
57
+ - brixen@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .travis.yml
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - lib/rubysl/strscan.rb
69
+ - lib/rubysl/strscan/strscan.rb
70
+ - lib/rubysl/strscan/version.rb
71
+ - lib/strscan.rb
72
+ - rubysl-strscan.gemspec
73
+ - spec/append_spec.rb
74
+ - spec/beginning_of_line_spec.rb
75
+ - spec/bol_spec.rb
76
+ - spec/check_spec.rb
77
+ - spec/check_until_spec.rb
78
+ - spec/clear_spec.rb
79
+ - spec/concat_spec.rb
80
+ - spec/dup_spec.rb
81
+ - spec/element_reference_spec.rb
82
+ - spec/empty_spec.rb
83
+ - spec/eos_spec.rb
84
+ - spec/exist_spec.rb
85
+ - spec/get_byte_spec.rb
86
+ - spec/getbyte_spec.rb
87
+ - spec/getch_spec.rb
88
+ - spec/initialize_spec.rb
89
+ - spec/inspect_spec.rb
90
+ - spec/match_spec.rb
91
+ - spec/matched_size_spec.rb
92
+ - spec/matched_spec.rb
93
+ - spec/matchedsize_spec.rb
94
+ - spec/must_C_version_spec.rb
95
+ - spec/peek_spec.rb
96
+ - spec/peep_spec.rb
97
+ - spec/pointer_spec.rb
98
+ - spec/pos_spec.rb
99
+ - spec/post_match_spec.rb
100
+ - spec/pre_match_spec.rb
101
+ - spec/reset_spec.rb
102
+ - spec/rest_size_spec.rb
103
+ - spec/rest_spec.rb
104
+ - spec/restsize_spec.rb
105
+ - spec/scan_full_spec.rb
106
+ - spec/scan_spec.rb
107
+ - spec/scan_until_spec.rb
108
+ - spec/search_full_spec.rb
109
+ - spec/shared/bol.rb
110
+ - spec/shared/concat.rb
111
+ - spec/shared/eos.rb
112
+ - spec/shared/extract_range.rb
113
+ - spec/shared/extract_range_matched.rb
114
+ - spec/shared/get_byte.rb
115
+ - spec/shared/matched_size.rb
116
+ - spec/shared/peek.rb
117
+ - spec/shared/pos.rb
118
+ - spec/shared/rest_size.rb
119
+ - spec/shared/terminate.rb
120
+ - spec/skip_spec.rb
121
+ - spec/skip_until_spec.rb
122
+ - spec/string_spec.rb
123
+ - spec/terminate_spec.rb
124
+ - spec/unscan_spec.rb
125
+ homepage: https://github.com/rubysl/rubysl-strscan
126
+ licenses:
127
+ - BSD
128
+ metadata: {}
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 2.0.7
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: Ruby standard library strscan.
149
+ test_files:
150
+ - spec/append_spec.rb
151
+ - spec/beginning_of_line_spec.rb
152
+ - spec/bol_spec.rb
153
+ - spec/check_spec.rb
154
+ - spec/check_until_spec.rb
155
+ - spec/clear_spec.rb
156
+ - spec/concat_spec.rb
157
+ - spec/dup_spec.rb
158
+ - spec/element_reference_spec.rb
159
+ - spec/empty_spec.rb
160
+ - spec/eos_spec.rb
161
+ - spec/exist_spec.rb
162
+ - spec/get_byte_spec.rb
163
+ - spec/getbyte_spec.rb
164
+ - spec/getch_spec.rb
165
+ - spec/initialize_spec.rb
166
+ - spec/inspect_spec.rb
167
+ - spec/match_spec.rb
168
+ - spec/matched_size_spec.rb
169
+ - spec/matched_spec.rb
170
+ - spec/matchedsize_spec.rb
171
+ - spec/must_C_version_spec.rb
172
+ - spec/peek_spec.rb
173
+ - spec/peep_spec.rb
174
+ - spec/pointer_spec.rb
175
+ - spec/pos_spec.rb
176
+ - spec/post_match_spec.rb
177
+ - spec/pre_match_spec.rb
178
+ - spec/reset_spec.rb
179
+ - spec/rest_size_spec.rb
180
+ - spec/rest_spec.rb
181
+ - spec/restsize_spec.rb
182
+ - spec/scan_full_spec.rb
183
+ - spec/scan_spec.rb
184
+ - spec/scan_until_spec.rb
185
+ - spec/search_full_spec.rb
186
+ - spec/shared/bol.rb
187
+ - spec/shared/concat.rb
188
+ - spec/shared/eos.rb
189
+ - spec/shared/extract_range.rb
190
+ - spec/shared/extract_range_matched.rb
191
+ - spec/shared/get_byte.rb
192
+ - spec/shared/matched_size.rb
193
+ - spec/shared/peek.rb
194
+ - spec/shared/pos.rb
195
+ - spec/shared/rest_size.rb
196
+ - spec/shared/terminate.rb
197
+ - spec/skip_spec.rb
198
+ - spec/skip_until_spec.rb
199
+ - spec/string_spec.rb
200
+ - spec/terminate_spec.rb
201
+ - spec/unscan_spec.rb