Skip to content

Commit

Permalink
Factor out advanceStringIndex
Browse files Browse the repository at this point in the history
Summary:
The advanceStringIndex operation is used to skip over surrogate pairs.
Factor the implementation into a global function so it can be shared.

Reviewed By: avp

Differential Revision: D17413829

fbshipit-source-id: da339c0d4edc0b90145dfbe2e23475ca92324ac0
  • Loading branch information
Peter Ammon authored and facebook-github-bot committed Oct 3, 2019
1 parent 02c422b commit ce53dc3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
7 changes: 7 additions & 0 deletions lib/VM/JSLib/JSLibInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,13 @@ CallResult<HermesValue> splitInternal(
Handle<> limit,
Handle<> separator);

/// ES6.0 21.2.5.2.3
/// If \p unicode is set and the character at \p index in \S is the start of a
/// surrogate pair, \return index + 2. Otherwise \return index + 1.
/// Note that this function does not allocate.
uint64_t
advanceStringIndex(const StringPrimitive *S, uint64_t index, bool unicode);

/// Create and initialize the global Function constructor. Populate the methods
/// of Function and Function.prototype.
/// \return the global Function constructor.
Expand Down
18 changes: 4 additions & 14 deletions lib/VM/JSLib/RegExp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,6 @@ static CallResult<HermesValue> regExpBuiltinExec(
Handle<JSRegExp> R,
Handle<StringPrimitive> S);

/// ES6.0 21.2.5.2.3
static uint64_t advanceStringIndex(
Runtime *runtime,
PseudoHandle<StringPrimitive> /* S */,
uint64_t index,
bool unicode);

/// @}

// Several RegExp accessors are defined to do particular things when passed the
Expand Down Expand Up @@ -567,11 +560,8 @@ static CallResult<HermesValue> regExpBuiltinExec(
}

/// ES6.0 21.2.5.2.3
static uint64_t advanceStringIndex(
Runtime *runtime,
PseudoHandle<StringPrimitive> S,
uint64_t index,
bool unicode) {
uint64_t
advanceStringIndex(const StringPrimitive *S, uint64_t index, bool unicode) {
if (unicode && index + 1 < S->getStringLength() &&
isHighSurrogate(S->at(index)) && isLowSurrogate(S->at(index + 1))) {
return index + 2;
Expand Down Expand Up @@ -1046,7 +1036,7 @@ regExpPrototypeSymbolMatch(void *, Runtime *runtime, NativeArgs args) {
}
// c. Let nextIndex be AdvanceStringIndex(S, thisIndex, fullUnicode).
double nextIndex = advanceStringIndex(
runtime, S, thisIndex->getNumberAs<uint64_t>(), fullUnicode);
S.get(), thisIndex->getNumberAs<uint64_t>(), fullUnicode);
// d. Let setStatus be Set(rx, "lastIndex", nextIndex, true).
auto setStatus = setLastIndex(rx, runtime, nextIndex);
// e. ReturnIfAbrupt(setStatus).
Expand Down Expand Up @@ -1391,7 +1381,7 @@ regExpPrototypeSymbolReplace(void *, Runtime *runtime, NativeArgs args) {
}
// c. Let nextIndex be AdvanceStringIndex(S, thisIndex, fullUnicode).
nextIndex = HermesValue::encodeDoubleValue(advanceStringIndex(
runtime, S, thisIndex->getNumberAs<uint64_t>(), fullUnicode));
S.get(), thisIndex->getNumberAs<uint64_t>(), fullUnicode));
// d. Let setStatus be Set(rx, "lastIndex", nextIndex, true).
auto setStatus = setLastIndex(rx, runtime, *nextIndex);
// e. ReturnIfAbrupt(setStatus).
Expand Down

0 comments on commit ce53dc3

Please sign in to comment.