Skip to content

Commit

Permalink
Implement StringScanner#bol? and StringScanner#terminate with specs
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Oct 25, 2013
1 parent 966c655 commit 04f480d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions spec/rubyspecs
Expand Up @@ -230,12 +230,14 @@ library/singleton/instantiate_spec
library/singleton/load_spec
library/singleton/new_spec

library/stringscanner/bol_spec
library/stringscanner/check_spec
library/stringscanner/element_reference_spec
library/stringscanner/get_byte_spec
library/stringscanner/pos_spec
library/stringscanner/scan_spec
library/stringscanner/skip_spec
library/stringscanner/terminate_spec

library/observer/add_observer_spec
library/observer/count_observers_spec
Expand Down
19 changes: 19 additions & 0 deletions stdlib/strscan.rb
Expand Up @@ -10,6 +10,10 @@ def initialize(string)
@match = []
end

def bol?
`#@pos === 0 || #@string.charAt(#@pos - 1) === "\\n"`
end

def scan(regex)
%x{
var regex = new RegExp('^' + regex.toString().substring(1, regex.toString().length - 1)),
Expand All @@ -19,6 +23,7 @@ def scan(regex)
return #{self}.matched = nil;
}
else if (typeof(result) === 'object') {
#@prev_pos = #@pos;
#@pos += result[0].length;
#@working = #@working.substring(result[0].length);
#@matched = result[0];
Expand Down Expand Up @@ -87,6 +92,7 @@ def skip(re)
var match_str = result[0];
var match_len = match_str.length;
#{self}.matched = match_str;
self.prev_pos = self.pos;
#{self}.pos += match_len;
#{self}.working = #{self}.working.substring(match_len);
return match_len;
Expand All @@ -98,6 +104,7 @@ def get_byte()
%x{
var result = nil;
if (#{self}.pos < #{self}.string.length) {
self.prev_pos = self.pos;
#{self}.pos += 1;
result = #{self}.matched = #{self}.working.substring(0, 1);
#{self}.working = #{self}.working.substring(1);
Expand Down Expand Up @@ -127,4 +134,16 @@ def pos=(pos)
def rest
@working
end

def terminate
@match = nil
self.pos = @string.length
end

def unscan
@pos = @prev_pos
@prev_pos = nil
@match = nil
self
end
end

0 comments on commit 04f480d

Please sign in to comment.