Class: Mustermann::RegexpBased Abstract
- Extended by:
- Forwardable
- Defined in:
- lib/mustermann/regexp_based.rb
Overview
Superclass for patterns that internally compile to a regular expression.
Direct Known Subclasses
Constant Summary
Constants included from Mustermann
CompileError, DEFAULT_TYPE, Error, ExpandError, ParseError, TrieError
Instance Attribute Summary collapse
-
#regexp ⇒ Regexp
(also: #to_regexp)
readonly
Regular expression equivalent to the pattern.
Attributes inherited from Pattern
Instance Method Summary collapse
-
#initialize(string, **options) ⇒ Pattern
constructor
A new instance of Pattern.
-
#match(string) ⇒ Mustermann::Match?
The match object if the pattern matches.
-
#params(string = nil) ⇒ Hash{String: String, Array<String>}?
Extracts params directly from the regexp without allocating a Match object or populating the match cache — significant GC savings when called in hot loops.
-
#peek_match(string) ⇒ Mustermann::Match?
MatchData or similar object if the pattern matches.
-
#peek_size(string) ⇒ Integer?
The number of characters that match.
Methods inherited from Pattern
#+, #==, #===, #=~, #eql?, #expand, #hash, #identity_params?, #names, new, #peek, #peek_params, supported?, supported_options, #to_proc, #to_s, #to_templates, #|
Methods included from Mustermann
Constructor Details
#initialize(string, **options) ⇒ Pattern
Returns a new instance of Pattern.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mustermann/regexp_based.rb', line 20 def initialize(string, **) cache = .delete(:cache) { true } super regexp = compile(**) @peek_regexp = /\A#{regexp}/ @regexp = /\A#{regexp}\Z/ @simple_captures = @regexp.named_captures.none? { |name, positions| positions.size > 1 || always_array?(name) } cache_class = ObjectSpace::WeakKeyMap if defined?(ObjectSpace::WeakKeyMap) case cache when true @match_cache = cache_class&.new || false @peek_cache = cache_class&.new || false when false @match_cache = false @peek_cache = false when Hash @match_cache = cache[:match] || cache_class&.new || false @peek_cache = cache[:peek] || cache_class&.new || false else @match_cache = cache.new @peek_cache = cache.new end end |
Instance Attribute Details
#regexp ⇒ Regexp (readonly) Also known as: to_regexp
Returns regular expression equivalent to the pattern.
11 12 13 |
# File 'lib/mustermann/regexp_based.rb', line 11 def regexp @regexp end |
Instance Method Details
#match(string) ⇒ Mustermann::Match?
Returns the match object if the pattern matches.
63 |
# File 'lib/mustermann/regexp_based.rb', line 63 def match(string) = cache_match(@match_cache, @regexp, string) |
#params(string = nil) ⇒ Hash{String: String, Array<String>}?
Extracts params directly from the regexp without allocating a Match object or populating the match cache — significant GC savings when called in hot loops.
69 70 71 72 |
# File 'lib/mustermann/regexp_based.rb', line 69 def params(string = nil) return unless md = @regexp.match(string) build_params(md) end |
#peek_match(string) ⇒ Mustermann::Match?
Returns MatchData or similar object if the pattern matches.
58 |
# File 'lib/mustermann/regexp_based.rb', line 58 def peek_match(string) = cache_match(@peek_cache, @peek_regexp, string) |
#peek_size(string) ⇒ Integer?
Returns the number of characters that match.
50 51 52 53 |
# File 'lib/mustermann/regexp_based.rb', line 50 def peek_size(string) return unless match = peek_match(string) match.to_s.size end |