Skip to content

Commit

Permalink
Showing 4 changed files with 82 additions and 21 deletions.
5 changes: 5 additions & 0 deletions spec/filters/bugs/inheritance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
opal_filter "bridged inheritance" do
fails "StringScanner#pre_match returns an instance of String when passed a String subclass"
fails "StringScanner#post_match returns an instance of String when passed a String subclass"
fails "StringScanner#rest returns an instance of String when passed a String subclass"
end
20 changes: 0 additions & 20 deletions spec/filters/bugs/strscan.rb
Original file line number Diff line number Diff line change
@@ -31,9 +31,6 @@
fails "StringScanner#initialize converts the argument into a string using #to_str"
fails "StringScanner#initialize is a private method"
fails "StringScanner#inspect returns a string that represents the StringScanner object"
fails "StringScanner#match? effects pre_match"
fails "StringScanner#match? returns nil if there's no match"
fails "StringScanner#match? returns the length of the match and the scan pointer is not advanced"
fails "StringScanner#matched returns an instance of String when passed a String subclass"
fails "StringScanner#matched returns nil if there's no match"
fails "StringScanner#matched returns the last matched string"
@@ -63,20 +60,6 @@
fails "StringScanner#pos returns the position of the scan pointer"
fails "StringScanner#pos= raises a RangeError if position too far backward"
fails "StringScanner#pos= raises a RangeError when the passed argument is out of range"
fails "StringScanner#post_match returns an instance of String when passed a String subclass"
fails "StringScanner#post_match returns nil if there's no match"
fails "StringScanner#post_match returns the post-match (in the regular expression sense) of the last scan"
fails "StringScanner#post_match taints the returned String if the input was tainted"
fails "StringScanner#pre_match is more than just the data from the last match"
fails "StringScanner#pre_match is not changed when the scanner's position changes"
fails "StringScanner#pre_match returns an instance of String when passed a String subclass"
fails "StringScanner#pre_match returns nil if there's no match"
fails "StringScanner#pre_match returns the pre-match (in the regular expression sense) of the last scan"
fails "StringScanner#pre_match taints the returned String if the input was tainted"
fails "StringScanner#rest returns an instance of String when passed a String subclass"
fails "StringScanner#rest taints the returned String if the input was tainted"
fails "StringScanner#rest_size Returns the length of the rest of the string"
fails "StringScanner#rest_size is equivalent to rest.size"
fails "StringScanner#restsize Returns the length of the rest of the string"
fails "StringScanner#restsize is equivalent to rest.size"
fails "StringScanner#restsize warns in verbose mode that the method is obsolete"
@@ -85,9 +68,6 @@
fails "StringScanner#scan_full returns the matched string if the third argument is true"
fails "StringScanner#scan_full returns the number of bytes advanced and advances the scan pointer if the second argument is true"
fails "StringScanner#scan_full returns the number of bytes advanced"
fails "StringScanner#scan_until can match anchors properly"
fails "StringScanner#scan_until returns nil if there's no match"
fails "StringScanner#scan_until returns the substring up to and including the end of the match"
fails "StringScanner#search_full returns the matched string if the third argument is true and advances the scan pointer if the second argument is true"
fails "StringScanner#search_full returns the matched string if the third argument is true"
fails "StringScanner#search_full returns the number of bytes advanced and advances the scan pointer if the second argument is true"
4 changes: 4 additions & 0 deletions spec/filters/unsupported/taint.rb
Original file line number Diff line number Diff line change
@@ -7,4 +7,8 @@
fails "String#% doesn't taint the result for %f when argument is tainted"
fails "String#% doesn't taint the result for %g when argument is tainted"
fails "String#% doesn't taint the result for %G when argument is tainted"

fails "StringScanner#pre_match taints the returned String if the input was tainted"
fails "StringScanner#post_match taints the returned String if the input was tainted"
fails "StringScanner#rest taints the returned String if the input was tainted"
end
74 changes: 73 additions & 1 deletion stdlib/strscan.rb
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ def scan(regex)
var result = regex.exec(#@working);
if (result == null) {
return #{self}.matched = nil;
return #@matched = nil;
}
else if (typeof(result) === 'object') {
#@prev_pos = #@pos;
@@ -47,6 +47,38 @@ def scan(regex)
}
end

def scan_until(regex)
%x{
regex = new RegExp('^' + regex.toString().substring(1, regex.toString().length - 1));
var pos = #@pos,
working = #@working,
result;
while (true) {
result = regex.exec(working);
pos += 1;
working = working.substring(1);
if (result == null) {
if (working.length === 0) {
return #@matched = nil;
}
continue;
}
#@matched = #@string.substr(#@pos, pos - #@pos);
#@prev_pos = pos - 1;
#@pos = pos;
#@working = working;
return #@matched;
}
}
end


def [](idx)
%x{
var match = #@match;
@@ -144,6 +176,22 @@ def get_byte()
# not exactly, but for now...
alias getch get_byte

def match?(regex)
%x{
regex = new RegExp('^' + regex.toString().substring(1, regex.toString().length - 1));
var result = regex.exec(#@working);
if (result == null) {
return nil;
}
else {
#@prev_pos = #@pos;
return result[0].length;
}
}
end

def pos=(pos)
%x{
if (pos < 0) {
@@ -155,6 +203,26 @@ def pos=(pos)
@working = `#{@string}.slice(pos)`
end

def post_match
%x{
if (#@matched === nil) {
return nil;
}
return #@string.substr(#@pos);
}
end

def pre_match
%x{
if (#@matched === nil) {
return nil;
}
return #@string.substr(0, #@prev_pos);
}
end

def reset
@working = @string
@matched = nil
@@ -169,6 +237,10 @@ def rest?
`#@working.length !== 0`
end

def rest_size
rest.size
end

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

0 comments on commit 51812b1

Please sign in to comment.