Skip to content

Commit

Permalink
Added an internal flag to enable caching.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhop committed Jun 5, 2024
1 parent e5b8978 commit 335aed2
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 26 deletions.
7 changes: 3 additions & 4 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
{
"target_name": "re2",
"sources": [
"lib/str-val.cc",
"lib/addon.cc",
"lib/accessors.cc",
"lib/str-val.cc",
"lib/util.cc",
"lib/new.cc",
"lib/exec.cc",
"lib/test.cc",
Expand All @@ -13,9 +15,6 @@
"lib/search.cc",
"lib/split.cc",
"lib/to_string.cc",
"lib/accessors.cc",
"lib/util.cc",
"lib/str-val.cc",
"vendor/re2/re2/bitmap256.cc",
"vendor/re2/re2/bitstate.cc",
"vendor/re2/re2/compile.cc",
Expand Down
30 changes: 29 additions & 1 deletion lib/accessors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@ NAN_GETTER(WrappedRE2::GetInternalSource)
info.GetReturnValue().Set(Nan::New(re2->regexp.pattern()).ToLocalChecked());
}

NAN_GETTER(WrappedRE2::GetEnabledCache)
{
if (!WrappedRE2::HasInstance(info.This()))
{
info.GetReturnValue().SetUndefined();
return;
}

auto re2 = Nan::ObjectWrap::Unwrap<WrappedRE2>(info.This());
info.GetReturnValue().Set(re2->enabledCache);
}

NAN_GETTER(WrappedRE2::GetIsCached)
{
if (!WrappedRE2::HasInstance(info.This()))
{
info.GetReturnValue().SetUndefined();
return;
}

auto re2 = Nan::ObjectWrap::Unwrap<WrappedRE2>(info.This());
info.GetReturnValue().Set(!!re2->lastStringValue);
}

NAN_GETTER(WrappedRE2::GetFlags)
{
if (!WrappedRE2::HasInstance(info.This()))
Expand All @@ -39,9 +63,13 @@ NAN_GETTER(WrappedRE2::GetFlags)
auto re2 = Nan::ObjectWrap::Unwrap<WrappedRE2>(info.This());

std::string flags;
if (re2->enabledCache)
{
flags += "\b";
}
if (re2->hasIndices)
{
flags = "d";
flags += "d";
}
if (re2->global)
{
Expand Down
15 changes: 2 additions & 13 deletions lib/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ v8::Local<v8::Function> WrappedRE2::Init()
Nan::SetAccessor(instanceTemplate, Nan::New("hasIndices").ToLocalChecked(), GetHasIndices);
Nan::SetAccessor(instanceTemplate, Nan::New("lastIndex").ToLocalChecked(), GetLastIndex, SetLastIndex);
Nan::SetAccessor(instanceTemplate, Nan::New("internalSource").ToLocalChecked(), GetInternalSource);
Nan::SetAccessor(instanceTemplate, Nan::New("enabledCache").ToLocalChecked(), GetEnabledCache);
Nan::SetAccessor(instanceTemplate, Nan::New("isCached").ToLocalChecked(), GetIsCached);

auto ctr = Nan::GetFunction(tpl).ToLocalChecked();

Expand Down Expand Up @@ -104,19 +106,6 @@ void WrappedRE2::dropLastString()
}
}

inline size_t countBytes(const char *data, size_t from, size_t n)
{
for (; n > 0; --n)
{
size_t s = getUtf8CharSize(data[from]);
from += s;
if (s == 4 && n >= 2)
--n; // this utf8 character will take two utf16 characters
// the decrement above is protected to avoid an overflow of an unsigned integer
}
return from;
}

void WrappedRE2::weakLastStringCallback(const Nan::WeakCallbackInfo<WrappedRE2> &data)
{
WrappedRE2* re2 = data.GetParameter();
Expand Down
2 changes: 1 addition & 1 deletion lib/exec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ NAN_METHOD(WrappedRE2::Exec)
return;
}

re2->prepareLastString(info[0]);
PrepareLastString prepare(re2, info[0]);
StrValBase &str = *re2->lastStringValue;
if (str.isBad) return; // throws an exception

Expand Down
2 changes: 1 addition & 1 deletion lib/match.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ NAN_METHOD(WrappedRE2::Match)
return;
}

re2->prepareLastString(info[0], re2->global);
PrepareLastString prepare(re2, info[0], re2->global);
StrValBase &str = *re2->lastStringValue;
if (str.isBad) return; // throws an exception

Expand Down
7 changes: 6 additions & 1 deletion lib/new.cc
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ NAN_METHOD(WrappedRE2::New)
bool unicode = false;
bool sticky = false;
bool hasIndices = false;
bool enabledCache = false;

auto context = Nan::GetCurrentContext();
bool needFlags = true;
Expand All @@ -256,6 +257,9 @@ NAN_METHOD(WrappedRE2::New)
{
switch (data[i])
{
case '\b':
enabledCache = true;
break;
case 'g':
global = true;
break;
Expand Down Expand Up @@ -339,6 +343,7 @@ NAN_METHOD(WrappedRE2::New)

if (needFlags)
{
enabledCache = re2->enabledCache;
global = re2->global;
ignoreCase = re2->ignoreCase;
multiline = re2->multiline;
Expand Down Expand Up @@ -401,7 +406,7 @@ NAN_METHOD(WrappedRE2::New)
options.set_dot_nl(dotAll);
options.set_log_errors(false); // inappropriate when embedding

std::unique_ptr<WrappedRE2> re2(new WrappedRE2(re2::StringPiece(data, size), options, source, global, ignoreCase, multiline, dotAll, sticky, hasIndices));
std::unique_ptr<WrappedRE2> re2(new WrappedRE2(re2::StringPiece(data, size), options, source, enabledCache, global, ignoreCase, multiline, dotAll, sticky, hasIndices));
if (!re2->regexp.ok())
{
return Nan::ThrowSyntaxError(re2->regexp.error().c_str());
Expand Down
2 changes: 1 addition & 1 deletion lib/replace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ NAN_METHOD(WrappedRE2::Replace)
return;
}

re2->prepareLastString(info[0]);
PrepareLastString prepare(re2, info[0]);
StrValBase &replacee = *re2->lastStringValue;
if (replacee.isBad) return; // throws an exception

Expand Down
2 changes: 1 addition & 1 deletion lib/search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ NAN_METHOD(WrappedRE2::Search)
return;
}

re2->prepareLastString(info[0], true);
PrepareLastString prepare(re2, info[0], true);
StrValBase &str = *re2->lastStringValue;
if (str.isBad) return; // throws an exception

Expand Down
2 changes: 1 addition & 1 deletion lib/split.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ NAN_METHOD(WrappedRE2::Split)
return;
}

re2->prepareLastString(info[0], true);
PrepareLastString prepare(re2, info[0], true);
StrValBase &str = *re2->lastStringValue;
if (str.isBad) return; // throws an exception

Expand Down
2 changes: 1 addition & 1 deletion lib/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ NAN_METHOD(WrappedRE2::Test)
return;
}

re2->prepareLastString(info[0]);
PrepareLastString prepare(re2, info[0]);
StrValBase &str = *re2->lastStringValue;
if (str.isBad) return; // throws an exception

Expand Down
4 changes: 4 additions & 0 deletions lib/to_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ NAN_METHOD(WrappedRE2::ToString)
buffer += re2->source;
buffer += "/";

if (re2->enabledCache)
{
buffer += "\b";
}
if (re2->global)
{
buffer += "g";
Expand Down
21 changes: 21 additions & 0 deletions lib/wrapped_re2.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ class WrappedRE2 : public Nan::ObjectWrap
const re2::StringPiece &pattern,
const re2::RE2::Options &options,
const std::string &src,
const bool &c,
const bool &g,
const bool &i,
const bool &m,
const bool &s,
const bool &y,
const bool &d) : regexp(pattern, options),
source(src),
enabledCache(c),
global(g),
ignoreCase(i),
multiline(m),
Expand All @@ -47,6 +49,8 @@ class WrappedRE2 : public Nan::ObjectWrap
static NAN_GETTER(GetLastIndex);
static NAN_SETTER(SetLastIndex);
static NAN_GETTER(GetInternalSource);
static NAN_GETTER(GetEnabledCache);
static NAN_GETTER(GetIsCached);

// RegExp methods
static NAN_METHOD(Exec);
Expand Down Expand Up @@ -89,6 +93,7 @@ class WrappedRE2 : public Nan::ObjectWrap

re2::RE2 regexp;
std::string source;
bool enabledCache;
bool global;
bool ignoreCase;
bool multiline;
Expand All @@ -97,6 +102,8 @@ class WrappedRE2 : public Nan::ObjectWrap
bool hasIndices;
size_t lastIndex;

friend class PrepareLastString;

private:
Nan::Persistent<v8::Value> lastString; // weak pointer
StrValBase *lastStringValue;
Expand All @@ -107,6 +114,20 @@ class WrappedRE2 : public Nan::ObjectWrap
void prepareLastString(const v8::Local<v8::Value> &arg, bool ignoreLastIndex = false);
};

struct PrepareLastString
{
PrepareLastString(WrappedRE2 *re2, const v8::Local<v8::Value> &arg, bool ignoreLastIndex = false) : re2(re2) {
re2->prepareLastString(arg, ignoreLastIndex);
}

~PrepareLastString() {
if (!re2->enabledCache || !(re2->global || re2->sticky))
re2->dropLastString();
}

WrappedRE2 *re2;
};

// utilities

inline size_t getUtf8Length(const uint16_t *from, const uint16_t *to)
Expand Down
2 changes: 1 addition & 1 deletion re2.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ if (typeof Symbol != 'undefined') {
if (!this.global) {
throw TypeError('String.prototype.matchAll called with a non-global RE2 argument');
}
const re = new RE2(this);
const re = new RE2(this, this.flags + '\b');
re.lastIndex = this.lastIndex;
for (;;) {
const result = re.exec(str);
Expand Down

0 comments on commit 335aed2

Please sign in to comment.