Sha256: 5a696f32d07da6b4eadb61eb152a9344650855b14d004e11c0038d51e6fba265

Contents?: true

Size: 882 Bytes

Versions: 1

Compression:

Stored size: 882 Bytes

Contents

class PCRE2::Regexp
  attr :source, :pattern_ptr

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

  # Compiles the Regexp into a JIT optimised version. Returns whether it was successful
  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

  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

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

  def names
    named_captures.keys
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pcre2-0.1.0 lib/pcre2/regexp.rb