| 1 |
class OnigRegexp |
| 2 |
@memo = {} |
| 3 |
|
| 4 |
# ISO 15.2.15.6.1 |
| 5 |
def self.compile(*args) |
| 6 |
as = args.to_s |
| 7 |
unless @memo.key? as |
| 8 |
@memo[as] = self.new(*args) |
| 9 |
end |
| 10 |
@memo[as] |
| 11 |
end |
| 12 |
|
| 13 |
# ISO 15.2.15.6.3 |
| 14 |
def self.last_match |
| 15 |
@last_match |
| 16 |
end |
| 17 |
|
| 18 |
def self.last_match=(match) |
| 19 |
@last_match = match |
| 20 |
end |
| 21 |
|
| 22 |
# ISO 15.2.15.7.2 |
| 23 |
def initialize_copy(other) |
| 24 |
initialize(other.source, other.options) |
| 25 |
end |
| 26 |
|
| 27 |
# ISO 15.2.15.7.4 |
| 28 |
def ===(str) |
| 29 |
not self.match(str).nil? |
| 30 |
rescue TypeError |
| 31 |
false |
| 32 |
end |
| 33 |
|
| 34 |
# ISO 15.2.15.7.5 |
| 35 |
def =~(str) |
| 36 |
m = self.match(str) |
| 37 |
m ? m.begin(0) : nil |
| 38 |
end |
| 39 |
|
| 40 |
# ISO 15.2.15.7.8 |
| 41 |
attr_reader :source |
| 42 |
end |
| 43 |
|
| 44 |
class String |
| 45 |
# ISO 15.2.10.5.5 |
| 46 |
def =~(a) |
| 47 |
begin |
| 48 |
(a.class.to_s == 'String' ? Regexp.new(a.to_s) : a) =~ self |
| 49 |
rescue |
| 50 |
false |
| 51 |
end |
| 52 |
end |
| 53 |
|
| 54 |
# ISO 15.2.10.5.27 |
| 55 |
def match(re, pos=0, &block) |
| 56 |
re.match(self, pos, &block) |
| 57 |
end |
| 58 |
|
| 59 |
|
| 60 |
# redefine methods with oniguruma regexp version |
| 61 |
%i[sub gsub split scan].each do |v| |
| 62 |
alias_method :"string_#{v}", v if method_defined?(v) |
| 63 |
alias_method v, :"onig_regexp_#{v}" |
| 64 |
end |
| 65 |
|
| 66 |
alias_method :match?, :onig_regexp_match? |
| 67 |
|
| 68 |
alias_method :old_slice, :slice |
| 69 |
alias_method :old_square_brancket, :[] |
| 70 |
alias_method :old_square_brancket_equal, :[]= |
| 71 |
|
| 72 |
def [](*args) |
| 73 |
return old_square_brancket(*args) unless args[0].class == Regexp |
| 74 |
|
| 75 |
if args.size == 2 |
| 76 |
match = args[0].match(self) |
| 77 |
if match |
| 78 |
if args[1] == 0 |
| 79 |
str = match[0] |
| 80 |
else |
| 81 |
str = match.captures[args[1] - 1] |
| 82 |
end |
| 83 |
return str |
| 84 |
end |
| 85 |
end |
| 86 |
|
| 87 |
match_data = args[0].match(self) |
| 88 |
if match_data |
| 89 |
result = match_data.to_s |
| 90 |
return result |
| 91 |
end |
| 92 |
end |
| 93 |
|
| 94 |
alias_method :slice, :[] |
| 95 |
|
| 96 |
def []=(*args) |
| 97 |
return old_square_brancket_equal(*args) unless args[0].class == Regexp |
| 98 |
|
| 99 |
n_args = args.size |
| 100 |
case n_args |
| 101 |
when 2 |
| 102 |
match = args[0].match(self) |
| 103 |
beg = match.begin(0) |
| 104 |
self[beg, match.end(0) - beg] = args[1] |
| 105 |
when 3 |
| 106 |
match = args[0].match(self) |
| 107 |
n = args[1] |
| 108 |
beg = match.begin(n) |
| 109 |
self[beg, match.end(n) - beg] = args[2] |
| 110 |
else |
| 111 |
raise ArgumentError, "wrong number of arguments (#{n_args} for 2..3)" |
| 112 |
end |
| 113 |
|
| 114 |
self |
| 115 |
end |
| 116 |
|
| 117 |
def slice!(*args) |
| 118 |
if args.size < 2 |
| 119 |
result = slice(*args) |
| 120 |
nth = args[0] |
| 121 |
|
| 122 |
if nth.class == Regexp |
| 123 |
lm = Regexp.last_match |
| 124 |
self[nth] = '' if result |
| 125 |
Regexp.last_match = lm |
| 126 |
else |
| 127 |
self[nth] = '' if result |
| 128 |
end |
| 129 |
else |
| 130 |
result = slice(*args) |
| 131 |
|
| 132 |
nth = args[0] |
| 133 |
len = args[1] |
| 134 |
|
| 135 |
if nth.class == Regexp |
| 136 |
lm = Regexp.last_match |
| 137 |
self[nth, len] = '' if result |
| 138 |
Regexp.last_match = lm |
| 139 |
else |
| 140 |
self[nth, len] = '' if result && nth != self.size |
| 141 |
end |
| 142 |
end |
| 143 |
|
| 144 |
result |
| 145 |
end |
| 146 |
|
| 147 |
alias_method :old_index, :index |
| 148 |
|
| 149 |
def index(pattern, pos=0) |
| 150 |
if pattern.class == Regexp |
| 151 |
str = self[pos..-1] |
| 152 |
if str |
| 153 |
if num = (pattern =~ str) |
| 154 |
if pos < 0 |
| 155 |
num += self.size |
| 156 |
end |
| 157 |
return num + pos |
| 158 |
end |
| 159 |
end |
| 160 |
nil |
| 161 |
else |
| 162 |
self.old_index(pattern, pos) |
| 163 |
end |
| 164 |
end |
| 165 |
end |
| 166 |
|
| 167 |
module Kernel |
| 168 |
def =~(_) |
| 169 |
nil |
| 170 |
end |
| 171 |
end |
| 172 |
|
| 173 |
Regexp = OnigRegexp unless Object.const_defined?(:Regexp) |
| 174 |
MatchData = OnigMatchData unless Object.const_defined? :MatchData |
| 175 |
|
| 176 |
# This is based on https://github.com/masamitsu-murase/mruby-hs-regexp |