Class: MatchSkeleton
- Inherits:
-
Object
- Object
- MatchSkeleton
- Defined in:
- lib/match_skeleton.rb
Overview
Class MatchSkeleton
To represent MatchData with much less memory use
Instance Attribute Summary collapse
-
#pos_begin ⇒ Object
The position Regexp match has started.
-
#regexp ⇒ Object
readonly
The same as MatchData#regexp.
-
#string ⇒ Object
readonly
The same as MatchData#string but it is identical to the original string.
Instance Method Summary collapse
-
#==(obj) ⇒ Boolean
Comparable with MatchSkeleton and MatchData.
-
#[](i, j = nil) ⇒ String, ...
The same as MatchData#[].
-
#begin(n) ⇒ Integer
The same as MatchData#begin.
-
#captures ⇒ Array
The same as MatchData#captures.
-
#end(n) ⇒ Integer
The same as MatchData#end.
-
#eql?(obj) ⇒ Boolean
The same as #== but stricter comparison.
-
#initialize(md, string = nil, pos_begin: nil) ⇒ MatchSkeleton
constructor
A new instance of MatchSkeleton.
-
#inspect ⇒ String
Similar to MatchData#inspect.
-
#names ⇒ Array<String>
The same as MatchData#names and Regexp#names.
-
#offset(n) ⇒ Array<integer>
The same as MatchData#offset.
-
#post_match ⇒ String
The same as MatchData#post_match.
-
#pre_match ⇒ String
The same as MatchData#pre_match.
-
#size ⇒ Integer
(also: #length)
The same as MatchData#size.
-
#to_a ⇒ Array
The same as MatchData#to_a.
-
#to_s ⇒ String
The same as MatchData#to_s.
-
#values_at(*rest) ⇒ Array
The same as MatchData#values_at.
Constructor Details
#initialize(md, string = nil, pos_begin: nil) ⇒ MatchSkeleton
Returns a new instance of MatchSkeleton.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/match_skeleton.rb', line 30 def initialize(md, string=nil, pos_begin: nil) size_str = md.string.size if string && string.size != size_str raise ArgumentError, 'The first parameter is obligatory.' end @string = (string || md.string) @regexp = md.regexp @pre_match = (0...md.pre_match.size) # {Range} @post_match = ((size_str-md.post_match.size)...size_str) # {Range} # @offsets is Hash with the keys of both Integer and possibly Symbol # if names exist. names = md.names ar_off = (0..(md.size-1)).map do |n| ar = md.offset(n) (ar.first...ar.last) end @offsets = {} ar_off.each_with_index do |ev, ei| @offsets[ei] = ev ej = ei - 1 @offsets[names[ej]] = ev if (ej >= 0 && names[ej]) end @pos_begin = pos_begin end |
Instance Attribute Details
#pos_begin ⇒ Object
The position Regexp match has started. For example, both
/x/.match('0000x', 0)
/x/.match('0000x', 3)
give the same (equal) MatchData. This instance variable #pos_begin holds the position (0 or 3 in the cases above), if set explicitly.
24 25 26 |
# File 'lib/match_skeleton.rb', line 24 def pos_begin @pos_begin end |
#regexp ⇒ Object (readonly)
The same as MatchData#regexp.
16 17 18 |
# File 'lib/match_skeleton.rb', line 16 def regexp @regexp end |
#string ⇒ Object (readonly)
The same as MatchData#string but it is identical to the original string. If the original string is modified destructively, this too is modified.
13 14 15 |
# File 'lib/match_skeleton.rb', line 13 def string @string end |
Instance Method Details
#==(obj) ⇒ Boolean
Comparable with MatchSkeleton and MatchData
A difference in #pos_begin is not taken into account.
65 66 67 68 69 |
# File 'lib/match_skeleton.rb', line 65 def ==(obj) (string == obj.string) && (regexp == obj.regexp) && (pre_match == obj.pre_match) end |
#[](i, j = nil) ⇒ String, ...
The same as MatchData#[]
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/match_skeleton.rb', line 77 def [](i, j=nil) if j to_a[i, j] elsif defined?(i.to_sym) i = i.to_s raise IndexError, sprintf("undefined group name reference: %s", i) if !names.include?(i) offset2string(i) else to_a[i] end end |
#begin(n) ⇒ Integer
The same as MatchData#begin
93 94 95 |
# File 'lib/match_skeleton.rb', line 93 def begin(n) offset(n)[0] end |
#captures ⇒ Array
The same as MatchData#captures
100 101 102 |
# File 'lib/match_skeleton.rb', line 100 def captures to_a[1..-1] end |
#end(n) ⇒ Integer
The same as MatchData#end
108 109 110 |
# File 'lib/match_skeleton.rb', line 108 def end(n) offset(n)[1] end |
#eql?(obj) ⇒ Boolean
The same as #== but stricter comparison.
The comparison between MatchSkeleton and MatchData returns false and that between MatchSkeleton with different #pos_begin or those with strings of different Object-IDs also return false.
121 122 123 124 125 126 127 |
# File 'lib/match_skeleton.rb', line 121 def eql?(obj) return false if self != obj return false if !defined?(obj.pos_begin) return false if (string.object_id != obj.string.object_id) return false if pos_begin != obj.pos_begin return true end |
#inspect ⇒ String
Similar to MatchData#inspect
132 133 134 135 136 137 138 139 |
# File 'lib/match_skeleton.rb', line 132 def inspect core = '' ar = (names.empty? ? captures : names) ar.each_with_index do |ev, ei| core << sprintf(" %d:%s", ei, ev.inspect) end sprintf("#<%s %s%s>", self.class.to_s, self[0].inspect, core) end |
#names ⇒ Array<String>
The same as MatchData#names and Regexp#names
144 145 146 |
# File 'lib/match_skeleton.rb', line 144 def names regexp.names end |
#offset(n) ⇒ Array<integer>
The same as MatchData#offset
152 153 154 155 156 157 158 159 |
# File 'lib/match_skeleton.rb', line 152 def offset(n) if defined?(n.to_sym) n = n.to_s raise IndexError, sprintf("undefined group name reference: %s", n) if !names.include?(n) end [@offsets[n].first, @offsets[n].last] end |
#post_match ⇒ String
The same as MatchData#post_match
171 172 173 |
# File 'lib/match_skeleton.rb', line 171 def post_match @string[@post_match] end |
#pre_match ⇒ String
The same as MatchData#pre_match
164 165 166 |
# File 'lib/match_skeleton.rb', line 164 def pre_match @string[@pre_match] end |
#size ⇒ Integer Also known as: length
The same as MatchData#size
178 179 180 |
# File 'lib/match_skeleton.rb', line 178 def size to_a.size end |
#to_a ⇒ Array
The same as MatchData#to_a
186 187 188 189 190 |
# File 'lib/match_skeleton.rb', line 186 def to_a indices = @offsets.keys.sort indices.delete_if { |i| !defined?(i.divmod) } indices.map { |i| offset2string(i) } end |
#to_s ⇒ String
The same as MatchData#to_s
195 196 197 |
# File 'lib/match_skeleton.rb', line 195 def to_s self[0] end |
#values_at(*rest) ⇒ Array
The same as MatchData#values_at
203 204 205 206 207 208 |
# File 'lib/match_skeleton.rb', line 203 def values_at(*rest) locary = to_a rest.map do |i| locary[i] end end |