diff options
| author | Tomoyuki Sahara <[email protected]> | 2015-06-08 17:32:14 +0900 |
|---|---|---|
| committer | Tomoyuki Sahara <[email protected]> | 2015-06-08 17:32:14 +0900 |
| commit | 5dd2b8e16f33b84773cdebeec4b6a86a511e8e9c (patch) | |
| tree | a0377e98eeb10f3bdd5f98759addc7cbb819d307 /mrblib/string.rb | |
| parent | a04116267850afc5b85459940656346c7c380da5 (diff) | |
| download | mruby-5dd2b8e16f33b84773cdebeec4b6a86a511e8e9c.tar.gz mruby-5dd2b8e16f33b84773cdebeec4b6a86a511e8e9c.zip | |
gsub/sub supports back references in substitutes. fixes #2816.
This implementation is compatible with CRuby's String#gsub/sub
except \1 ... \9 and \+. They are useless without Regexp library.
Diffstat (limited to 'mrblib/string.rb')
| -rw-r--r-- | mrblib/string.rb | 39 |
1 files changed, 37 insertions, 2 deletions
diff --git a/mrblib/string.rb b/mrblib/string.rb index 90aad5d32..ee097ad6d 100644 --- a/mrblib/string.rb +++ b/mrblib/string.rb @@ -20,6 +20,30 @@ class String self end + # private method for gsub/sub + def __sub_replace(pre, m, post) + s = "" + i = 0 + while j = index("\\", i) + break if j == length-1 + t = case self[j+1] + when "\\" + "\\" + when "`" + pre + when "&", "0" + m + when "'" + post + else + self[j, 2] + end + s += self[i, j-i] + t + i = j + 2 + end + s + self[i, length-i] + end + ## # Replace all matches of +pattern+ with +replacement+. # Call block (if given) for each match and replace @@ -29,7 +53,17 @@ class String # ISO 15.2.10.5.18 def gsub(*args, &block) if args.size == 2 - split(args[0], -1).join(args[1]) + s = "" + i = 0 + while j = index(args[0], i) + seplen = args[0].length + k = j + seplen + pre = self[0, j] + post = self[k, length-k] + s += self[i, j-i] + args[1].__sub_replace(pre, args[0], post) + i = k + end + s + self[i, length-i] elsif args.size == 1 && block split(args[0], -1).join(block.call(args[0])) else @@ -76,7 +110,8 @@ class String # ISO 15.2.10.5.36 def sub(*args, &block) if args.size == 2 - split(args[0], 2).join(args[1]) + pre, post = split(args[0], 2) + pre + args[1].__sub_replace(pre, args[0], post) + post elsif args.size == 1 && block split(args[0], 2).join(block.call(args[0])) else |
