opal-strscan 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1 +1,4 @@
1
1
  .DS_Store
2
+ *.gem
3
+ /build
4
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+
5
+ gem "opal"
6
+ gem "opal-spec"
7
+ gem "opal-dom"
data/README.md CHANGED
@@ -1 +1,5 @@
1
1
  StringScanner for Opal
2
+ ======================
3
+
4
+ This is a port of `strscan` from the ruby stdlib. It is designed to run
5
+ on opal.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler'
2
+ Bundler.setup
3
+
4
+ require 'opal'
5
+
6
+ Opal::BuilderTask.new do |t|
7
+ t.name = 'opal-strscan'
8
+ t.dependencies = %w(opal-spec opal-dom)
9
+ end
data/lib/opal/strscan.rb CHANGED
@@ -1,7 +1,10 @@
1
1
  class StringScanner
2
+ attr_reader :pos
3
+ attr_reader :matched
4
+
2
5
  def initialize(string)
3
6
  @string = string
4
- @at = 0
7
+ @pos = 0
5
8
  @matched = ''
6
9
  @working = string
7
10
  end
@@ -17,14 +20,14 @@ class StringScanner
17
20
  return nil;
18
21
  }
19
22
  else if (typeof(result) === 'object') {
20
- #@at += result[0].length;
23
+ #@pos += result[0].length;
21
24
  #@working = #@working.substring(result[0].length);
22
25
  #@matched = result[0];
23
26
 
24
27
  return result[0];
25
28
  }
26
29
  else if (typeof(result) === 'string') {
27
- #@at += result.length;
30
+ #@pos += result.length;
28
31
  #@working = #@working.substring(result.length);
29
32
 
30
33
  return result;
@@ -36,7 +39,16 @@ class StringScanner
36
39
  end
37
40
 
38
41
  def check(regex)
39
- `!!new RegExp('^' + regex.toString().substring(1, regex.toString().length - 1)).exec(#@working)`
42
+ %x{
43
+ var regexp = new RegExp('^' + regex.toString().substring(1, regex.toString().length - 1)),
44
+ result = regexp.exec(#@working);
45
+
46
+ if (result == null) {
47
+ return this.matched = nil;
48
+ }
49
+
50
+ return this.matched = result[0];
51
+ }
40
52
  end
41
53
 
42
54
  def peek(length)
@@ -46,8 +58,4 @@ class StringScanner
46
58
  def eos?
47
59
  `#@working.length === 0`
48
60
  end
49
-
50
- def matched
51
- @matched
52
- end
53
- end
61
+ end
@@ -0,0 +1,14 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'opal-strscan'
5
+ s.version = '0.1.1'
6
+ s.author = 'Adam Beynon'
7
+ s.email = 'adam@adambeynon.com'
8
+ s.homepage = 'http://opalrb.org'
9
+ s.summary = 'Opal version of strscan'
10
+ s.description = 'Opal version of strscan.'
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+ s.require_paths = ['lib']
14
+ end
@@ -0,0 +1,13 @@
1
+ describe "StringScanner#check" do
2
+ before :each do
3
+ @s = StringScanner.new("This is a test")
4
+ end
5
+
6
+ it "returns the value that scan would return, without advancing the scan pointer" do
7
+ @s.check(/This/).should == "This"
8
+ @s.matched.should == "This"
9
+ @s.pos.should == 0
10
+ @s.check(/is/).should == nil
11
+ @s.matched.should == nil
12
+ end
13
+ end
data/spec/index.html ADDED
@@ -0,0 +1,13 @@
1
+ <!doctype html>
2
+ <html>
3
+ <head>
4
+ <title>opal-strscan specs</title>
5
+ </head>
6
+ <body>
7
+ <script src="../build/opal.js"></script>
8
+ <script src="../build/opal-dom.js"></script>
9
+ <script src="../build/opal-spec.js"></script>
10
+ <script src="../build/opal-strscan.js"></script>
11
+ <script src="../build/opal-strscan.specs.js"></script>
12
+ </body>
13
+ </html>
data/spec/scan_spec.rb ADDED
@@ -0,0 +1,33 @@
1
+ describe "StringScanner#scan" do
2
+ before :each do
3
+ @s = StringScanner.new("This is a test")
4
+ end
5
+
6
+ it "returns the matched string" do
7
+ @s.scan(/\w+/).should == "This"
8
+ @s.scan(/.../).should == " is"
9
+ @s.scan(//).should == ""
10
+ @s.scan(/\s+/).should == " "
11
+ end
12
+
13
+ it "treats ^ as matching from the beginning of the current position" do
14
+ @s.scan(/\w+/).should == "This"
15
+ @s.scan(/^\d/).should be_nil
16
+ @s.scan(/^\s/).should == " "
17
+ end
18
+
19
+ it "returns nil if there is no match" do
20
+ @s.scan(/\d/).should == nil
21
+ end
22
+
23
+ it "returns nil when there is no more to scan" do
24
+ @s.scan(/[\w\s]+/).should == "This is a test"
25
+ @s.scan(/\w+/).should be_nil
26
+ end
27
+
28
+ it "returns an empty string when the pattern matches empty" do
29
+ @s.scan(/.*/).should == "This is a test"
30
+ @s.scan(/.*/).should == ""
31
+ @s.scan(/./).should be_nil
32
+ end
33
+ end
File without changes
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opal-strscan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,17 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-22 00:00:00.000000000Z
12
+ date: 2012-06-24 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Opal version of strscan
14
+ description: Opal version of strscan.
15
15
  email: adam@adambeynon.com
16
16
  executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - .gitignore
21
+ - Gemfile
21
22
  - README.md
23
+ - Rakefile
22
24
  - lib/opal/strscan.rb
25
+ - opal-strscan.gemspec
26
+ - spec/check_spec.rb
27
+ - spec/index.html
28
+ - spec/scan_spec.rb
29
+ - spec/spec_helper.rb
23
30
  homepage: http://opalrb.org
24
31
  licenses: []
25
32
  post_install_message:
@@ -40,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
47
  version: '0'
41
48
  requirements: []
42
49
  rubyforge_project:
43
- rubygems_version: 1.8.11
50
+ rubygems_version: 1.8.24
44
51
  signing_key:
45
52
  specification_version: 3
46
53
  summary: Opal version of strscan