lib/pcre2/regexp.rb in pcre2-0.1.0 vs lib/pcre2/regexp.rb in pcre2-0.2.0
- old
+ new
@@ -1,35 +1,69 @@
-class PCRE2::Regexp
- attr :source, :pattern_ptr
+module PCRE2
+ class Regexp
+ attr :source, :pattern_ptr
- def initialize(pattern, *options)
- @source = pattern
- @pattern_ptr = PCRE2::Lib.compile_pattern(pattern, options)
- end
+ include StringUtils
- # 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
+ # Accepts a String, Regexp or another PCRE2::Regexp
+ def initialize(pattern, *options)
+ case pattern
+ when ::Regexp, PCRE2::Regexp
+ @source = pattern.source
+ else
+ @source = pattern
+ end
- PCRE2::Lib.pcre2_jit_compile_8(pattern_ptr, options) == 0
- end
+ @pattern_ptr = Lib.compile_pattern(source, options)
+ end
- def match(str, pos = nil)
- result_count, match_data_ptr = PCRE2::Lib.match(@pattern_ptr, str, position: pos)
+ # Compiles the Regexp into a JIT optimised version. Returns whether it was successful
+ def jit!
+ options = PCRE2_JIT_COMPLETE | PCRE2_JIT_PARTIAL_SOFT | PCRE2_JIT_PARTIAL_HARD
- if result_count == 0
- nil
- else
- pairs = PCRE2::Lib.get_ovector_pairs(match_data_ptr, result_count)
+ Lib.pcre2_jit_compile_8(pattern_ptr, options) == 0
+ end
- PCRE2::MatchData.new(self, str, pairs)
+ def match(str, pos = nil)
+ result_count, match_data_ptr = Lib.match(@pattern_ptr, str, position: pos)
+
+ if result_count == 0
+ nil
+ else
+ pairs = PCRE2::Lib.get_ovector_pairs(match_data_ptr, result_count)
+
+ MatchData.new(self, str, pairs)
+ end
end
- end
- def named_captures
- @named_captures ||= PCRE2::Lib.named_captures(pattern_ptr)
- end
+ def matches(str, pos = nil, &block)
+ return enum_for(:matches, str, pos) if !block_given?
- def names
- named_captures.keys
+ pos ||= 0
+ while pos < str.length
+ matchdata = self.match(str, pos)
+
+ if matchdata
+ yield matchdata
+
+ beginning, ending = matchdata.offset(0)
+
+ if pos == ending # Manually increment position if no change to avoid infinite loops
+ pos += 1
+ else
+ pos = ending
+ end
+ else
+ return
+ end
+ end
+ end
+
+ def named_captures
+ @named_captures ||= Lib.named_captures(pattern_ptr)
+ end
+
+ def names
+ named_captures.keys
+ end
end
end