Class: JSONP3::Match

Inherits:
FunctionExtension show all
Defined in:
lib/json_p3/function_extensions/match.rb

Overview

The standard match function.

Constant Summary collapse

ARG_TYPES =
%i[value_expression value_expression].freeze
RETURN_TYPE =
:logical_expression

Instance Method Summary collapse

Constructor Details

#initialize(cache_size = 128, raise_errors: false) ⇒ Match

Returns a new instance of Match.

Parameters:

  • cache_size (Integer) (defaults to: 128)

    the maximum size of the regexp cache. Set it to zero or negative to disable the cache.

  • raise_errors (Boolean) (defaults to: false)

    if false (the default), return false when this function causes a RegexpError instead of raising the exception.



17
18
19
20
21
22
# File 'lib/json_p3/function_extensions/match.rb', line 17

def initialize(cache_size = 128, raise_errors: false)
  super()
  @cache_size = cache_size
  @raise_errors = raise_errors
  @cache = LRUCache.new(cache_size)
end

Instance Method Details

#call(value, pattern) ⇒ Object

Returns Boolean.

Parameters:

  • value (String)
  • pattern (String)

Returns:

  • Boolean



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/json_p3/function_extensions/match.rb', line 27

def call(value, pattern)
  return false unless pattern.is_a?(String) && value.is_a?(String)

  if @cache_size.positive?
    re = @cache[pattern] || Regexp.new(full_match(pattern))
  else
    re = Regexp.new(full_match(pattern))
    @cache[pattern] = re
  end

  re.match?(value)
rescue RegexpError
  raise if @raise_errors

  false
end