Skip to content

Commit

Permalink
Moved the utility.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhop committed Jun 8, 2024
1 parent b3c4524 commit 675a4cd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
15 changes: 1 addition & 14 deletions lib/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,6 @@ const StrVal& WrappedRE2::prepareArgument(const v8::Local<v8::Value> &arg, bool

// StrVal

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 StrVal::setIndex(size_t newIndex)
{
isIndexValid = newIndex <= length;
Expand Down Expand Up @@ -213,7 +200,7 @@ void StrVal::setIndex(size_t newIndex)
return;
}

byteIndex = index < newIndex ? countBytes(data, byteIndex, newIndex - index) : countBytes(data, 0, newIndex);
byteIndex = index < newIndex ? getUtf16PositionByCounter(data, byteIndex, newIndex - index) : getUtf16PositionByCounter(data, 0, newIndex);
index = newIndex;
}

Expand Down
13 changes: 13 additions & 0 deletions lib/wrapped_re2.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,16 @@ inline size_t getUtf8CharSize(char ch)
{
return ((0xE5000000 >> ((ch >> 3) & 0x1E)) & 3) + 1;
}

inline size_t getUtf16PositionByCounter(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;
}

0 comments on commit 675a4cd

Please sign in to comment.