Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 452545a6aa92
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 50859280fc6a
Choose a head ref
  • 10 commits
  • 3 files changed
  • 1 contributor

Commits on Oct 23, 2013

  1. Copy the full SHA
    fcdda11 View commit details
  2. Make String#gsub compliant

    meh committed Oct 23, 2013
    Copy the full SHA
    0318b87 View commit details
  3. Cleanup String#upcase

    meh committed Oct 23, 2013
    Copy the full SHA
    0c3696d View commit details
  4. Cleanup MatchData#begin

    meh committed Oct 23, 2013
    Copy the full SHA
    71e4ef7 View commit details
  5. Cleanup MatchData#captures

    meh committed Oct 23, 2013
    Copy the full SHA
    0791112 View commit details
  6. Move MatchData to its own file

    meh committed Oct 23, 2013
    Copy the full SHA
    780d6c5 View commit details
  7. Cleanup MatchData#values_at

    meh committed Oct 23, 2013
    Copy the full SHA
    5765233 View commit details
  8. Cleanup MatchData#to_s

    meh committed Oct 23, 2013
    Copy the full SHA
    976c6c8 View commit details
  9. Cleanup MatchData#to_n

    meh committed Oct 23, 2013
    Copy the full SHA
    88cc19a View commit details
  10. Cleanup MatchData.new

    meh committed Oct 23, 2013
    Copy the full SHA
    5085928 View commit details
Showing with 100 additions and 87 deletions.
  1. +89 −0 corelib/match_data.rb
  2. +1 −0 corelib/opal.rb
  3. +10 −87 corelib/string.rb
89 changes: 89 additions & 0 deletions corelib/match_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
class MatchData < Array
attr_reader :post_match, :pre_match, :regexp, :string

def self.new(regexp, match_groups)
%x{
var instance = new Opal.MatchData._alloc;
for (var i = 0, length = match_groups.length; i < length; i++) {
var group = match_groups[i];
if (group == null) {
instance.push(nil);
}
else {
instance.push(group);
}
}
instance._begin = match_groups.index;
instance.regexp = regexp;
instance.string = match_groups.input;
instance.pre_match = #{$` = `instance.string.substr(0, regexp.lastIndex - instance[0].length)`};
instance.post_match = #{$' = `instance.string.substr(regexp.lastIndex)`};
return #{$~ = `instance`};
}
end

def begin(pos)
%x{
if (pos == 0 || pos == 1) {
return self._begin;
}
#{raise ArgumentError, 'MatchData#begin only supports 0th element'};
}
end

def captures
`self.slice(1)`
end

def inspect
%x{
var str = "#<MatchData " + #{`self[0]`.inspect};
for (var i = 1, length = self.length; i < length; i++) {
str += " " + i + ":" + #{`self[i]`.inspect};
}
return str + ">";
}
end

def to_s
`self[0]`
end

def to_n
`self.valueOf()`
end

def values_at(*indexes)
%x{
var values = [],
match_length = self.length;
for (var i = 0, length = indexes.length; i < length; i++) {
var pos = indexes[i];
if (pos >= 0) {
values.push(self[pos]);
}
else {
pos += match_length;
if (pos > 0) {
values.push(self[pos]);
}
else {
values.push(nil);
}
}
}
return values;
}
end
end
1 change: 1 addition & 0 deletions corelib/opal.rb
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
require 'array'
require 'hash'
require 'string'
require 'match_data'
require 'numeric'
require 'proc'
require 'range'
97 changes: 10 additions & 87 deletions corelib/string.rb
Original file line number Diff line number Diff line change
@@ -286,8 +286,12 @@ def getbyte(idx)
end

def gsub(pattern, replace = undefined, &block)
if pattern.is_a?(String)
pattern = /#{Regexp.escape(pattern)}/
if String === pattern || pattern.respond_to?(:to_str)
pattern = /#{Regexp.escape(pattern.to_str)}/
end

unless Regexp === pattern
raise TypeError, "wrong argument type #{pattern.class} (expected Regexp)"
end

%x{
@@ -391,7 +395,9 @@ def intern
self
end

alias lines each_line
def lines(separator = $/)
each_line(separator).to_a
end

def length
`self.length`
@@ -1006,7 +1012,7 @@ def tr_s(from, to)
end

def upcase
`#{self}.toUpperCase()`
`self.toUpperCase()`
end

def freeze
@@ -1019,86 +1025,3 @@ def frozen?
end

Symbol = String

class MatchData < Array
attr_reader :post_match, :pre_match, :regexp, :string

def self.new(regexp, match_groups)
%x{
var instance = new Opal.MatchData._alloc;
for (var i = 0, len = match_groups.length; i < len; i++) {
var group = match_groups[i];
if (group == undefined) {
instance.push(nil);
}
else {
instance.push(group);
}
}
instance._begin = match_groups.index;
instance.regexp = regexp;
instance.string = match_groups.input;
instance.pre_match = #{$` = `instance.string.substr(0, regexp.lastIndex - instance[0].length)`};
instance.post_match = #{$' = `instance.string.substr(regexp.lastIndex)`};
return #{$~ = `instance`};
}
end

def begin(pos)
%x{
if (pos == 0 || pos == 1) {
return #{self}._begin;
}
else {
#{raise ArgumentError, 'MatchData#begin only supports 0th element'};
}
}
end

def captures
`#{self}.slice(1)`
end

def inspect
%x{
var str = "<#MatchData " + #{self}[0].$inspect()
for (var i = 1, len = #{self}.length; i < len; i++) {
str += " " + i + ":" + #{self}[i].$inspect();
}
str += ">";
return str;
}
end

def to_s
`#{self}[0]`
end

def to_n
`#{self}.valueOf()`
end

def values_at(*indexes)
%x{
var vals = [];
var match_length = #{self}.length;
for (var i = 0, length = indexes.length; i < length; i++) {
var pos = indexes[i];
if (pos >= 0) {
vals.push(#{self}[pos]);
}
else {
pos = match_length + pos;
if (pos > 0) {
vals.push(#{self}[pos]);
}
else {
vals.push(nil);
}
}
}
return vals;
}
end
end