Class: Mustermann::RegexpBased Abstract

Inherits:
Pattern
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/mustermann/regexp_based.rb

Overview

This class is abstract.

Superclass for patterns that internally compile to a regular expression.

See Also:

Direct Known Subclasses

AST::Pattern, Regular

Constant Summary

Constants included from Mustermann

CompileError, DEFAULT_TYPE, Error, ExpandError, ParseError, TrieError

Instance Attribute Summary collapse

Attributes inherited from Pattern

#uri_decode

Instance Method Summary collapse

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

[], new

Constructor Details

#initialize(string, **options) ⇒ Pattern

Returns a new instance of Pattern.

Parameters:

  • string (String)

    the string representation of the pattern

  • options (Hash)

    options for fine-tuning the pattern behavior

See Also:



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, **options)
  cache = options.delete(:cache) { true }

  super
  regexp           = compile(**options)
  @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

#regexpRegexp (readonly) Also known as: to_regexp

Returns regular expression equivalent to the pattern.

Returns:

  • (Regexp)

    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.

Parameters:

  • string (String)

    The string to match against

Returns:

See Also:



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.

Parameters:

  • string (String) (defaults to: nil)

    the string to match against

Returns:

  • (Hash{String: String, Array<String>}, nil)

    Sinatra style params if pattern matches.



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.

Parameters:

  • string (String)

    The string to match against

Returns:

See Also:



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.

Parameters:

  • string (String)

    The string to match against

Returns:

  • (Integer, nil)

    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