Class: PCRE2::Regexp

Inherits:
Object
  • Object
show all
Defined in:
lib/pcre2/regexp.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, *options) ⇒ Regexp

Returns a new instance of Regexp.



4
5
6
7
# File 'lib/pcre2/regexp.rb', line 4

def initialize(pattern, *options)
  @source = pattern
  @pattern_ptr = PCRE2::Lib.compile_pattern(pattern, options)
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



2
3
4
# File 'lib/pcre2/regexp.rb', line 2

def source
  @source
end

Instance Method Details

#jit!Object

Compiles the Regexp into a JIT optimised version. Returns whether it was successful



10
11
12
13
14
# File 'lib/pcre2/regexp.rb', line 10

def jit!
  options = PCRE2::PCRE2_JIT_COMPLETE | PCRE2::PCRE2_JIT_PARTIAL_SOFT | PCRE2::PCRE2_JIT_PARTIAL_HARD

  PCRE2::Lib.pcre2_jit_compile_8(pattern_ptr, options) == 0
end

#match(str, pos = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pcre2/regexp.rb', line 16

def match(str, pos = nil)
  result_count, match_data_ptr = PCRE2::Lib.match(@pattern_ptr, str, position: pos)

  if result_count == 0
    nil
  else
    pairs = PCRE2::Lib.get_ovector_pairs(match_data_ptr, result_count)

    PCRE2::MatchData.new(self, str, pairs)
  end
end

#named_capturesObject



28
29
30
# File 'lib/pcre2/regexp.rb', line 28

def named_captures
  @named_captures ||= PCRE2::Lib.named_captures(pattern_ptr)
end

#namesObject



32
33
34
# File 'lib/pcre2/regexp.rb', line 32

def names
  named_captures.keys
end