[279] | 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 |
|
---|
[439] | 18 | def self.last_match=(match)
|
---|
| 19 | @last_match = match
|
---|
| 20 | end
|
---|
| 21 |
|
---|
[279] | 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?
|
---|
[439] | 30 | rescue TypeError
|
---|
| 31 | false
|
---|
[279] | 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 |
|
---|
[439] | 54 | # ISO 15.2.10.5.27
|
---|
| 55 | def match(re, pos=0, &block)
|
---|
| 56 | re.match(self, pos, &block)
|
---|
| 57 | end
|
---|
| 58 |
|
---|
| 59 |
|
---|
[279] | 60 | # redefine methods with oniguruma regexp version
|
---|
[439] | 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}"
|
---|
[279] | 64 | end
|
---|
| 65 |
|
---|
[439] | 66 | alias_method :match?, :onig_regexp_match?
|
---|
| 67 |
|
---|
[279] | 68 | alias_method :old_slice, :slice
|
---|
| 69 | alias_method :old_square_brancket, :[]
|
---|
[439] | 70 | alias_method :old_square_brancket_equal, :[]=
|
---|
[279] | 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 |
|
---|
[439] | 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 | self[match.begin(0)...match.end(0)] = args[1]
|
---|
| 104 | when 3
|
---|
| 105 | match = args[0].match(self)
|
---|
| 106 | n = args[1]
|
---|
| 107 | self[match.begin(n)...match.end(n)] = args[2]
|
---|
| 108 | else
|
---|
| 109 | raise ArgumentError, "wrong number of arguments (#{n_args} for 2..3)"
|
---|
| 110 | end
|
---|
| 111 |
|
---|
| 112 | self
|
---|
| 113 | end
|
---|
| 114 |
|
---|
[279] | 115 | def slice!(*args)
|
---|
| 116 | if args.size < 2
|
---|
| 117 | result = slice(*args)
|
---|
| 118 | nth = args[0]
|
---|
| 119 |
|
---|
| 120 | if nth.class == Regexp
|
---|
| 121 | lm = Regexp.last_match
|
---|
| 122 | self[nth] = '' if result
|
---|
| 123 | Regexp.last_match = lm
|
---|
| 124 | else
|
---|
| 125 | self[nth] = '' if result
|
---|
| 126 | end
|
---|
| 127 | else
|
---|
| 128 | result = slice(*args)
|
---|
| 129 |
|
---|
| 130 | nth = args[0]
|
---|
| 131 | len = args[1]
|
---|
| 132 |
|
---|
| 133 | if nth.class == Regexp
|
---|
| 134 | lm = Regexp.last_match
|
---|
| 135 | self[nth, len] = '' if result
|
---|
| 136 | Regexp.last_match = lm
|
---|
| 137 | else
|
---|
| 138 | self[nth, len] = '' if result && nth != self.size
|
---|
| 139 | end
|
---|
| 140 | end
|
---|
| 141 |
|
---|
| 142 | result
|
---|
| 143 | end
|
---|
| 144 |
|
---|
| 145 | alias_method :old_index, :index
|
---|
| 146 |
|
---|
| 147 | def index(pattern, pos=0)
|
---|
| 148 | if pattern.class == Regexp
|
---|
| 149 | str = self[pos..-1]
|
---|
| 150 | if str
|
---|
| 151 | if num = (pattern =~ str)
|
---|
| 152 | if pos < 0
|
---|
| 153 | num += self.size
|
---|
| 154 | end
|
---|
| 155 | return num + pos
|
---|
| 156 | end
|
---|
| 157 | end
|
---|
| 158 | nil
|
---|
| 159 | else
|
---|
| 160 | self.old_index(pattern, pos)
|
---|
| 161 | end
|
---|
| 162 | end
|
---|
| 163 | end
|
---|
| 164 |
|
---|
| 165 | module Kernel
|
---|
| 166 | def =~(_)
|
---|
| 167 | nil
|
---|
| 168 | end
|
---|
| 169 | end
|
---|
| 170 |
|
---|
| 171 | Regexp = OnigRegexp unless Object.const_defined?(:Regexp)
|
---|
| 172 | MatchData = OnigMatchData unless Object.const_defined? :MatchData
|
---|
| 173 |
|
---|
| 174 | # This is based on https://github.com/masamitsu-murase/mruby-hs-regexp
|
---|