diff --git a/spec/onig-scanner-spec.js b/spec/onig-scanner-spec.js index 1187731..992f32f 100644 --- a/spec/onig-scanner-spec.js +++ b/spec/onig-scanner-spec.js @@ -1,6 +1,7 @@ 'use strict' const OnigScanner = require('..').OnigScanner +const OnigString = require('..').OnigString; describe('OnigScanner', () => { describe('::findNextMatchSync', () => { @@ -113,6 +114,18 @@ describe('OnigScanner', () => { }) ) + describe('when a regular expression contains \\G', () => + it('it does not get cached', () => { + let str = new OnigString('first-and-second'); + let scanner = new OnigScanner(['\\G-and']) + let match = scanner.findNextMatchSync(str, 0) + expect(match).toEqual(null) + + match = scanner.findNextMatchSync(str, 5) + expect(match.captureIndices).toEqual([{index: 0, start: 5, end: 9, length: 4}]) + }) + ) + describe('::findNextMatch', () => { let matchCallback diff --git a/src/onig-reg-exp.cc b/src/onig-reg-exp.cc index 78609eb..fe91a2a 100644 --- a/src/onig-reg-exp.cc +++ b/src/onig-reg-exp.cc @@ -10,6 +10,17 @@ OnigRegExp::OnigRegExp(const string& source) lastSearchPosition = -1; lastSearchResult = NULL; + hasGAnchor = false; + for (size_t pos = 0, len = source.size(); pos < len; pos++) { + if (source[pos] == '\\' && pos + 1 < len) { + if (source[pos + 1] == 'G') { + hasGAnchor = true; + break; + } + pos++; + } + } + OnigErrorInfo error; const UChar* sourceData = (const UChar*)source.data(); int status = onig_new(®ex_, sourceData, sourceData + source.length(), @@ -28,6 +39,12 @@ OnigRegExp::~OnigRegExp() { } shared_ptr OnigRegExp::Search(OnigString* str, int position) { + if (hasGAnchor) { + // Should not use caching, because the regular expression + // targets the current search position (\G) + return Search(str->utf8_value(), position, str->utf8_length()); + } + if (lastSearchStrUniqueId == str->uniqueId() && lastSearchPosition <= position) { if (lastSearchResult == NULL || lastSearchResult->LocationAt(0) >= position) { return lastSearchResult; diff --git a/src/onig-reg-exp.h b/src/onig-reg-exp.h index 0f707a4..2c83afb 100644 --- a/src/onig-reg-exp.h +++ b/src/onig-reg-exp.h @@ -26,6 +26,7 @@ class OnigRegExp { string source_; regex_t* regex_; + bool hasGAnchor; int lastSearchStrUniqueId; int lastSearchPosition; shared_ptr lastSearchResult;