[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 |
|
---|
| 18 | # ISO 15.2.15.7.2
|
---|
| 19 | def initialize_copy(other)
|
---|
| 20 | initialize(other.source, other.options)
|
---|
| 21 | end
|
---|
| 22 |
|
---|
| 23 | # ISO 15.2.15.7.4
|
---|
| 24 | def ===(str)
|
---|
| 25 | not self.match(str).nil?
|
---|
| 26 | end
|
---|
| 27 |
|
---|
| 28 | # ISO 15.2.15.7.5
|
---|
| 29 | def =~(str)
|
---|
| 30 | m = self.match(str)
|
---|
| 31 | m ? m.begin(0) : nil
|
---|
| 32 | end
|
---|
| 33 |
|
---|
| 34 | # ISO 15.2.15.7.8
|
---|
| 35 | attr_reader :source
|
---|
| 36 | end
|
---|
| 37 |
|
---|
| 38 | class String
|
---|
| 39 | # ISO 15.2.10.5.5
|
---|
| 40 | def =~(a)
|
---|
| 41 | begin
|
---|
| 42 | (a.class.to_s == 'String' ? Regexp.new(a.to_s) : a) =~ self
|
---|
| 43 | rescue
|
---|
| 44 | false
|
---|
| 45 | end
|
---|
| 46 | end
|
---|
| 47 |
|
---|
| 48 | # redefine methods with oniguruma regexp version
|
---|
| 49 | [:sub, :gsub, :split, :scan].each do |v|
|
---|
| 50 | alias_method "string_#{v}".to_sym, v
|
---|
| 51 | alias_method v, "onig_regexp_#{v}".to_sym
|
---|
| 52 | end
|
---|
| 53 |
|
---|
| 54 | alias_method :old_slice, :slice
|
---|
| 55 | alias_method :old_square_brancket, :[]
|
---|
| 56 |
|
---|
| 57 | def [](*args)
|
---|
| 58 | return old_square_brancket(*args) unless args[0].class == Regexp
|
---|
| 59 |
|
---|
| 60 | if args.size == 2
|
---|
| 61 | match = args[0].match(self)
|
---|
| 62 | if match
|
---|
| 63 | if args[1] == 0
|
---|
| 64 | str = match[0]
|
---|
| 65 | else
|
---|
| 66 | str = match.captures[args[1] - 1]
|
---|
| 67 | end
|
---|
| 68 | return str
|
---|
| 69 | end
|
---|
| 70 | end
|
---|
| 71 |
|
---|
| 72 | match_data = args[0].match(self)
|
---|
| 73 | if match_data
|
---|
| 74 | result = match_data.to_s
|
---|
| 75 | return result
|
---|
| 76 | end
|
---|
| 77 | end
|
---|
| 78 |
|
---|
| 79 | alias_method :slice, :[]
|
---|
| 80 |
|
---|
| 81 | def slice!(*args)
|
---|
| 82 | if args.size < 2
|
---|
| 83 | result = slice(*args)
|
---|
| 84 | nth = args[0]
|
---|
| 85 |
|
---|
| 86 | if nth.class == Regexp
|
---|
| 87 | lm = Regexp.last_match
|
---|
| 88 | self[nth] = '' if result
|
---|
| 89 | Regexp.last_match = lm
|
---|
| 90 | else
|
---|
| 91 | self[nth] = '' if result
|
---|
| 92 | end
|
---|
| 93 | else
|
---|
| 94 | result = slice(*args)
|
---|
| 95 |
|
---|
| 96 | nth = args[0]
|
---|
| 97 | len = args[1]
|
---|
| 98 |
|
---|
| 99 | if nth.class == Regexp
|
---|
| 100 | lm = Regexp.last_match
|
---|
| 101 | self[nth, len] = '' if result
|
---|
| 102 | Regexp.last_match = lm
|
---|
| 103 | else
|
---|
| 104 | self[nth, len] = '' if result && nth != self.size
|
---|
| 105 | end
|
---|
| 106 | end
|
---|
| 107 |
|
---|
| 108 | result
|
---|
| 109 | end
|
---|
| 110 |
|
---|
| 111 | alias_method :old_index, :index
|
---|
| 112 |
|
---|
| 113 | def index(pattern, pos=0)
|
---|
| 114 | if pattern.class == Regexp
|
---|
| 115 | str = self[pos..-1]
|
---|
| 116 | if str
|
---|
| 117 | if num = (pattern =~ str)
|
---|
| 118 | if pos < 0
|
---|
| 119 | num += self.size
|
---|
| 120 | end
|
---|
| 121 | return num + pos
|
---|
| 122 | end
|
---|
| 123 | end
|
---|
| 124 | nil
|
---|
| 125 | else
|
---|
| 126 | self.old_index(pattern, pos)
|
---|
| 127 | end
|
---|
| 128 | end
|
---|
| 129 | end
|
---|
| 130 |
|
---|
| 131 | module Kernel
|
---|
| 132 | def =~(_)
|
---|
| 133 | nil
|
---|
| 134 | end
|
---|
| 135 | end
|
---|
| 136 |
|
---|
| 137 | Regexp = OnigRegexp unless Object.const_defined?(:Regexp)
|
---|
| 138 | MatchData = OnigMatchData unless Object.const_defined? :MatchData
|
---|
| 139 |
|
---|
| 140 | # This is based on https://github.com/masamitsu-murase/mruby-hs-regexp
|
---|