diff --git a/binding.gyp b/binding.gyp index bbc239c..ef0035c 100644 --- a/binding.gyp +++ b/binding.gyp @@ -5,7 +5,6 @@ "sources": [ "lib/addon.cc", "lib/accessors.cc", - "lib/str-val.cc", "lib/util.cc", "lib/new.cc", "lib/exec.cc", diff --git a/lib/addon.cc b/lib/addon.cc index bd0ab10..2d103d5 100644 --- a/lib/addon.cc +++ b/lib/addon.cc @@ -1,7 +1,5 @@ #include "./wrapped_re2.h" -#include "./str-val.h" - static NAN_METHOD(GetUtf8Length) { auto t = info[0]->ToString(Nan::GetCurrentContext()); diff --git a/lib/str-val.cc b/lib/str-val.cc deleted file mode 100644 index 91e6d75..0000000 --- a/lib/str-val.cc +++ /dev/null @@ -1,112 +0,0 @@ -#include "./str-val.h" - -StrValBuffer::StrValBuffer(const v8::Local &arg, size_t newIndex) : StrValBase() -{ - if (!node::Buffer::HasInstance(arg)) - return; - - isBuffer = true; - size = length = node::Buffer::Length(arg); - data = node::Buffer::Data(arg); - - byteIndex = index = newIndex; - isIndexValid = byteIndex < size; -} - -inline size_t getUtf8CharSize(char ch) -{ - return ((0xE5000000 >> ((ch >> 3) & 0x1E)) & 3) + 1; -} - -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; -} - -StrValString::StrValString(const v8::Local &arg, size_t newIndex) : StrValBase() -{ - if (node::Buffer::HasInstance(arg)) - return; - - auto t = arg->ToString(Nan::GetCurrentContext()); - if (t.IsEmpty()) - { - isBad = true; - return; - } - - auto s = t.ToLocalChecked(); - length = Nan::DecodeBytes(s); - size = Nan::DecodeBytes(s, Nan::UTF8); - buffer.resize(size + 1); - data = &buffer[0]; - Nan::DecodeWrite(data, size, s, Nan::UTF8); - buffer[size] = '\0'; - - index = newIndex; - isIndexValid = index <= length; - - if (!isIndexValid || !index) - return; - - if (index == length) - { - byteIndex = size; - return; - } - - byteIndex = countBytes(data, 0, index); -} - -void StrValBase::setIndex(size_t newIndex) -{ - isIndexValid = newIndex <= length; - if (!isIndexValid) - { - index = newIndex; - byteIndex = 0; - return; - } - - if (newIndex == index) - return; - - if (isBuffer) - { - byteIndex = index = newIndex; - return; - } - - // String - - if (!newIndex) - { - byteIndex = index = 0; - return; - } - - if (newIndex == length) - { - byteIndex = size; - index = length; - return; - } - - byteIndex = index < newIndex ? countBytes(data, byteIndex, newIndex - index) : countBytes(data, 0, newIndex); - index = newIndex; -} - -StrValBase *StrValBase::New(const v8::Local &arg, size_t newIndex) -{ - if (node::Buffer::HasInstance(arg)) - return new StrValBuffer(arg, newIndex); - return new StrValString(arg, newIndex); -} diff --git a/lib/str-val.h b/lib/str-val.h deleted file mode 100644 index 330c7c9..0000000 --- a/lib/str-val.h +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include -#include -#include - -struct StrValBase -{ - char *data; - size_t size, length; - size_t index, byteIndex; - bool isBuffer, isIndexValid, isBad; - - StrValBase() : data(NULL), size(0), length(0), index(0), byteIndex(0), isBuffer(false), isIndexValid(false), isBad(false) {} - virtual ~StrValBase() {} - - operator re2::StringPiece() const { return re2::StringPiece(data, size); } - - void setIndex(size_t newIndex = 0); - - static StrValBase *New(const v8::Local &arg, size_t newIndex = 0); -}; - -struct StrValBuffer : public StrValBase -{ - StrValBuffer(const v8::Local &arg, size_t newIndex = 0); -}; - -struct StrValString : public StrValBase -{ - StrValString(const v8::Local &arg, size_t newIndex = 0); - - std::vector buffer; -};