mattn / mruby-onig-regexp

mrbgem of 鬼雲's Regular Expression
31 stars 34 forks source link

Add String#match #96

Closed kou closed 5 years ago

kou commented 5 years ago

Because mruby removes String#match at https://github.com/mruby/mruby/commit/fd37bc53deb2d52fe3134838eab002dfb9ac35ab .

mattn commented 5 years ago

Thanks! Does this fix https://github.com/mattn/mruby-onig-regexp/issues/95 ?

kou commented 5 years ago

Unfortunately, this is unrelated.

For #95, we need to implement String#[]= and Regexp.last_match= something like this:

diff --git a/mrblib/onig_regexp.rb b/mrblib/onig_regexp.rb
index 3efb7cd..cce556b 100644
--- a/mrblib/onig_regexp.rb
+++ b/mrblib/onig_regexp.rb
@@ -15,6 +15,10 @@ class OnigRegexp
     @last_match
   end

+  def self.last_match=(match)
+    @last_match = match
+  end
+
   # ISO 15.2.15.7.2
   def initialize_copy(other)
     initialize(other.source, other.options)
@@ -65,6 +69,7 @@ class String

   alias_method :old_slice, :slice
   alias_method :old_square_brancket, :[]
+  alias_method :old_square_brancket_equal, :[]=

   def [](*args)
     return old_square_brancket(*args) unless args[0].class == Regexp
@@ -90,6 +95,25 @@ class String

   alias_method :slice, :[]

+  def []=(*args)
+    return old_square_brancket_equal(*args) unless args[0].class == Regexp
+
+    n_args = args.size
+    case n_args
+    when 2
+      match = args[0].match(self)
+      self[match.begin(0)...match.end(0)] = args[1]
+    when 3
+      match = args[0].match(self)
+      n = args[1]
+      self[match.begin(n)...match.end(n)] = args[2]
+    else
+      raise ArgumentError, "wrong number of arguments (#{n_args} for 2..3)"
+    end
+
+    self
+  end
+
   def slice!(*args)
     if args.size < 2
       result = slice(*args)
mattn commented 5 years ago

Thank you. Could you update your pull-request to include above changes?

kou commented 5 years ago

Should we include a fix for #95 into this? I can create a new pull request for #95. I think that it's better that we use separated pull requests because they are unrelated.

mattn commented 5 years ago

If it does not consume your time, please do, thank you.

kou commented 5 years ago

OK. I'll create a new pull request. Could you merge this pull request as is?

kou commented 5 years ago

Done: #97

mattn commented 5 years ago

Thank you so much.