Skip to content
Permalink
Browse files

Merge pull request #768 from kachick/implement-matchdata-string,regex…

…p,offset,captures

Implement MatchData#{string,regexp,offset,captures}
  • Loading branch information
alex committed Jun 25, 2013
2 parents 8760931 + e3ee45a commit 513537ecbd7cd1a9a5fe3bcb86d34159671ae370
@@ -19,6 +19,7 @@
load_bootstrap.call("integer.rb")
load_bootstrap.call("io.rb")
load_bootstrap.call("kernel.rb")
load_bootstrap.call("matchdata.rb")
load_bootstrap.call("numeric.rb")
load_bootstrap.call("process.rb")
load_bootstrap.call("range.rb")
@@ -0,0 +1,5 @@
class MatchData
def offset(n)
[self.begin(n), self.end(n)]
end
end

This file was deleted.

@@ -1,3 +1,2 @@
fails:MatchData#offset returns a two element array with the begin and end of the nth match
fails:MatchData#offset returns the offset for multi byte strings
fails:MatchData#offset returns the offset for multi byte strings with unicode regexp

This file was deleted.

This file was deleted.

@@ -150,9 +150,9 @@ def make_ctx(self, s, offset=0):
endpos = len(s)
return rsre_core.StrMatchContext(self.code, s, offset, endpos, self.flags)

def get_match_result(self, space, ctx, found):
def get_match_result(self, space, ctx, target, found):
if found:
w_match = W_MatchDataObject(space, self, ctx)
w_match = W_MatchDataObject(space, self, ctx, target)
else:
w_match = space.w_nil
space.globals.set(space, "$~", w_match)
@@ -210,7 +210,7 @@ def method_match_operator(self, space, w_s):
s = Coerce.str(space, w_s)
ctx = self.make_ctx(s)
matched = rsre_core.search_context(ctx)
self.get_match_result(space, ctx, matched)
self.get_match_result(space, ctx, s, matched)
if matched:
return space.newint(ctx.match_start)
else:
@@ -227,13 +227,13 @@ def method_match(self, space, w_s, w_offset=None):
offset = 0
ctx = self.make_ctx(s, offset)
matched = rsre_core.search_context(ctx)
return self.get_match_result(space, ctx, matched)
return self.get_match_result(space, ctx, s, matched)

@classdef.method("===", s="str")
def method_eqeqeq(self, space, s):
ctx = self.make_ctx(s)
matched = rsre_core.search_context(ctx)
self.get_match_result(space, ctx, matched)
self.get_match_result(space, ctx, s, matched)
return space.newbool(matched)

@classdef.method("casefold?")
@@ -256,10 +256,11 @@ def method_options(self, space):
class W_MatchDataObject(W_Object):
classdef = ClassDef("MatchData", W_Object.classdef)

def __init__(self, space, regexp, ctx):
def __init__(self, space, regexp, ctx, target):
W_Object.__init__(self, space)
self.regexp = regexp
self.ctx = ctx
self.target = target
self._flatten_cache = None

def size(self):
@@ -290,6 +291,16 @@ def get_span(self, n):
assert idx >= 0
return fmarks[idx], fmarks[idx + 1]

@classdef.method("regexp")
def method_regexp(self, space):
return self.regexp

@classdef.method("string")
def method_string(self, space):
res = space.newstr_fromstr(self.target)
space.send(res, "freeze")
return res

@classdef.method("[]", n="int")
def method_subscript(self, space, n):
if n == 0:
@@ -303,6 +314,13 @@ def method_subscript(self, space, n):
else:
return space.w_nil

@classdef.method("captures")
def method_captures(self, space):
res_w = []
for i in xrange(1, self.size()):
res_w.append(space.send(self, "[]", [space.newint(i)]))
return space.newarray(res_w)

@classdef.method("to_a")
def method_to_a(self, space):
res_w = []
@@ -668,7 +668,7 @@ def method_split(self, space, w_sep=None, limit=0):
last = 0
string = space.str_w(self)
ctx = w_sep.make_ctx(string)
w_match = w_sep.get_match_result(space, ctx, found=True)
w_match = w_sep.get_match_result(space, ctx, string, found=True)

while limit <= 0 or n + 1 < limit:
if not self.search_context(space, ctx):
@@ -929,7 +929,7 @@ def scan_regexp(self, space, w_pattern):
ctx = w_pattern.make_ctx(string)

while last < len(string) and self.search_context(space, ctx):
w_matchdata = w_pattern.get_match_result(space, ctx, found=True)
w_matchdata = w_pattern.get_match_result(space, ctx, string, found=True)
if w_matchdata.size() > 1:
matches_w = []
for num in xrange(1, w_matchdata.size(), 1):
@@ -1021,7 +1021,7 @@ def gsub_regexp(self, space, w_pattern, replacement, w_hash, block, first_only):
string = space.str_w(self)
ctx = w_pattern.make_ctx(string)

w_matchdata = w_pattern.get_match_result(space, ctx, found=True)
w_matchdata = w_pattern.get_match_result(space, ctx, string, found=True)
replacement_parts = None
if replacement is not None and "\\" in replacement:
replacement_parts = [s for s in replacement.split("\\") if s]

0 comments on commit 513537e

Please sign in to comment.
You can’t perform that action at this time.