Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #46 from alexandrudima/onig-string
Browse files Browse the repository at this point in the history
Introduce OnigString as the underlying caching mechanism
  • Loading branch information
zcbenz committed Dec 21, 2015
2 parents 025c3fb + 56f42fe commit eb92048
Show file tree
Hide file tree
Showing 22 changed files with 256 additions and 370 deletions.
11 changes: 2 additions & 9 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,12 @@
],
'include_dirs': [ '<!(node -e "require(\'nan\')")' ],
'sources': [
'src/onig-cache.cc',
'src/onig-result.cc',
'src/onig-reg-exp.cc',
'src/onig-scanner.cc',
'src/onig-scanner-worker.cc',
'src/onig-searcher.cc',
'src/onig-string-context.cc',
'src/onig-string.cc'
],
'conditions': [
['OS=="mac"', {
Expand Down Expand Up @@ -128,13 +127,7 @@
'defines': [
'ONIG_EXTERN=extern',
],
'sources': [
'src/unicode-utils-win.cc'
],
}, {
'sources': [
'src/unicode-utils-posix.cc'
],

}],
['OS=="freebsd"', {
'cflags': [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"main": "./lib/oniguruma.js",
"name": "oniguruma",
"description": "oniguruma regular expression library",
"version": "5.1.2",
"version": "6.0.0",
"licenses": [
{
"type": "MIT",
Expand Down
47 changes: 0 additions & 47 deletions src/onig-cache.cc

This file was deleted.

42 changes: 0 additions & 42 deletions src/onig-cache.h

This file was deleted.

28 changes: 17 additions & 11 deletions src/onig-reg-exp.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#include "onig-reg-exp.h"
#include "onig-result.h"

using ::v8::Exception;
using ::v8::String;

OnigRegExp::OnigRegExp(const string& source, int indexInScanner)
OnigRegExp::OnigRegExp(const string& source)
: source_(source),
regex_(NULL),
indexInScanner(indexInScanner) {
regex_(NULL) {
lastSearchStrUniqueId = -1;
lastSearchPosition = -1;
lastSearchResult = NULL;

OnigErrorInfo error;
const UChar* sourceData = (const UChar*)source.data();
int status = onig_new(&regex_, sourceData, sourceData + source.length(),
Expand All @@ -25,13 +27,17 @@ OnigRegExp::~OnigRegExp() {
if (regex_) onig_free(regex_);
}

bool OnigRegExp::Contains(const string& value) {
return source_.find(value) != string::npos;
}
shared_ptr<OnigResult> OnigRegExp::Search(OnigString* str, int position) {
if (lastSearchStrUniqueId == str->uniqueId() && lastSearchPosition <= position) {
if (lastSearchResult == NULL || lastSearchResult->LocationAt(0) >= position) {
return lastSearchResult;
}
}

shared_ptr<OnigResult> OnigRegExp::Search(const string& searchString,
size_t position) {
return Search(searchString.data(), position, searchString.size());
lastSearchStrUniqueId = str->uniqueId();
lastSearchPosition = position;
lastSearchResult = Search(str->utf8_value(), position, str->utf8_length());
return lastSearchResult;
}

shared_ptr<OnigResult> OnigRegExp::Search(const char* data,
Expand All @@ -48,7 +54,7 @@ shared_ptr<OnigResult> OnigRegExp::Search(const char* data,
ONIG_OPTION_NONE);

if (status != ONIG_MISMATCH) {
return shared_ptr<OnigResult>(new OnigResult(region, indexInScanner));
return shared_ptr<OnigResult>(new OnigResult(region, -1));
} else {
onig_region_free(region, 1);
return shared_ptr<OnigResult>();
Expand Down
21 changes: 10 additions & 11 deletions src/onig-reg-exp.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,31 @@
#include <memory>
#include <string>

#include "nan.h"
#include "oniguruma.h"
#include "onig-result.h"
#include "onig-string.h"

using ::std::shared_ptr;
using ::std::string;

class OnigResult;

class OnigRegExp {
public:
explicit OnigRegExp(const string& source, int indexInScanner);
explicit OnigRegExp(const string& source);
~OnigRegExp();

bool Contains(const string& value);
int LocationAt(int index);
int Index() { return indexInScanner; }
shared_ptr<OnigResult> Search(const string &searchString, size_t position);
shared_ptr<OnigResult> Search(const char* data, size_t position, size_t end);
shared_ptr<OnigResult> Search(OnigString* str, int position);

private:
OnigRegExp(const OnigRegExp&); // Disallow copying
OnigRegExp &operator=(const OnigRegExp&); // Disallow copying

shared_ptr<OnigResult> Search(const char* data, size_t position, size_t end);

string source_;
regex_t* regex_;
int indexInScanner;

int lastSearchStrUniqueId;
int lastSearchPosition;
shared_ptr<OnigResult> lastSearchResult;
};

#endif // SRC_ONIG_REG_EXP_H_
2 changes: 0 additions & 2 deletions src/onig-result.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include "oniguruma.h"
#include "onig-result.h"
#include "unicode-utils.h"

OnigResult::OnigResult(OnigRegion* region, int indexInScanner)
: region_(region), indexInScanner(indexInScanner) {}
Expand Down
8 changes: 3 additions & 5 deletions src/onig-result.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#ifndef SRC_ONIG_RESULT_H_
#define SRC_ONIG_RESULT_H_

#include <string>

using ::std::string;

class OnigRegExp;
#include "nan.h"
#include "oniguruma.h"

class OnigResult {
public:
Expand All @@ -16,6 +13,7 @@ class OnigResult {
int LocationAt(int index);
int LengthAt(int index);
int Index() { return indexInScanner; }
void SetIndex(int newIndex) { indexInScanner = newIndex; }

private:
OnigResult(const OnigResult&); // Disallow copying
Expand Down
18 changes: 2 additions & 16 deletions src/onig-scanner-worker.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
#include "onig-scanner-worker.h"
#include "onig-string-context.h"
#include "unicode-utils.h"

using ::v8::Array;
using ::v8::Local;
using ::v8::Null;
using ::v8::Number;
using ::v8::Object;
using ::v8::String;
using ::v8::Value;

void OnigScannerWorker::Execute() {
Expand All @@ -17,23 +11,15 @@ void OnigScannerWorker::Execute() {
void OnigScannerWorker::HandleOKCallback() {
Nan::HandleScope scope;

// Try to reuse the cached results across async searches
cache->Reset(searcher->GetCache());

if (bestResult != NULL) {
Local<Object> result = Nan::New<Object>();
result->Set(Nan::New<String>("index").ToLocalChecked(), Nan::New<Number>(bestResult->Index()));

int resultCount = bestResult->Count();
Local<Array> captures = Nan::New<Array>(resultCount);
for (int index = 0; index < resultCount; index++) {
int captureLength = bestResult->LengthAt(index);
int captureStart = bestResult->LocationAt(index);

if (source->has_multibyte_characters()) {
captureLength = UnicodeUtils::characters_in_bytes(source->utf8_value() + captureStart, captureLength);
captureStart = UnicodeUtils::characters_in_bytes(source->utf8_value(), captureStart);
}
int captureStart = source->ConvertUtf8OffsetToUtf16(bestResult->LocationAt(index));
int captureLength = source->ConvertUnicodeLengthToUtf16(captureStart, bestResult->LengthAt(index));

Local<Object> capture = Nan::New<Object>();
capture->Set(Nan::New<String>("index").ToLocalChecked(), Nan::New<Number>(index));
Expand Down
24 changes: 10 additions & 14 deletions src/onig-scanner-worker.h
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
#ifndef SRC_ONIG_SCANNER_WORKER_H_
#define SRC_ONIG_SCANNER_WORKER_H_

#include <node.h>
#include <string>
#include <memory>
#include <vector>

#include "nan.h"
#include "onig-reg-exp.h"
#include "onig-result.h"
#include "onig-searcher.h"

using ::std::string;
using ::std::shared_ptr;
using ::std::vector;

class OnigScannerWorker : public Nan::AsyncWorker {
public:
OnigScannerWorker(Nan::Callback *callback,
vector<shared_ptr<OnigRegExp>> regExps,
shared_ptr<OnigStringContext> source,
int charOffset,
shared_ptr<OnigCache> cache)
Local<String> v8String,
int charOffset)
: Nan::AsyncWorker(callback),
source(source),
charOffset(charOffset),
cache(cache) {
searcher = shared_ptr<OnigSearcher>(new OnigSearcher(regExps, *cache.get()));
charOffset(charOffset) {
source = new OnigString(v8String);
searcher = shared_ptr<OnigSearcher>(new OnigSearcher(regExps));
}

~OnigScannerWorker() {}
~OnigScannerWorker() {
delete source;
}

void Execute();
void HandleOKCallback();

private:
shared_ptr<OnigStringContext> source;
OnigString* source;
int charOffset;
shared_ptr<OnigCache> cache;
shared_ptr<OnigSearcher> searcher;
shared_ptr<OnigResult> bestResult;
};
Expand Down
Loading

0 comments on commit eb92048

Please sign in to comment.