Bug 1472031 - Add SourceUnits::matchHexDigits and remove SourceUnits::peekCodeUnits used for the same ends (but that also performed a needless copy). r=arai

--HG--
extra : rebase_source : bad3e97ead1b4d72dd39420bfc96614913464d62
This commit is contained in:
Jeff Walden 2018-06-28 16:08:53 -07:00
parent 76990b22f2
commit e249f558aa
2 changed files with 23 additions and 28 deletions

View File

@ -1008,17 +1008,10 @@ GeneralTokenStreamChars<CharT, AnyCharsAccess>::matchUnicodeEscape(uint32_t* cod
return 0;
}
CharT cp[3];
char16_t v;
unit = getCodeUnit();
if (JS7_ISHEX(unit) &&
sourceUnits.peekCodeUnits(3, cp) &&
JS7_ISHEX(cp[0]) && JS7_ISHEX(cp[1]) && JS7_ISHEX(cp[2]))
{
*codePoint = (JS7_UNHEX(unit) << 12) |
(JS7_UNHEX(cp[0]) << 8) |
(JS7_UNHEX(cp[1]) << 4) |
JS7_UNHEX(cp[2]);
sourceUnits.skipCodeUnits(3);
if (JS7_ISHEX(unit) && sourceUnits.matchHexDigits(3, &v)) {
*codePoint = (JS7_UNHEX(unit) << 12) | v;
return 5;
}
@ -2450,16 +2443,9 @@ TokenStreamSpecific<CharT, AnyCharsAccess>::getStringOrTemplateToken(char untilC
// If it isn't, this is usually an error -- but if this is a
// template literal, we must defer error reporting because
// malformed escapes are okay in *tagged* template literals.
CharT cp[3];
if (JS7_ISHEX(c2) &&
sourceUnits.peekCodeUnits(3, cp) &&
JS7_ISHEX(cp[0]) && JS7_ISHEX(cp[1]) && JS7_ISHEX(cp[2]))
{
unit = (JS7_UNHEX(c2) << 12) |
(JS7_UNHEX(cp[0]) << 8) |
(JS7_UNHEX(cp[1]) << 4) |
JS7_UNHEX(cp[2]);
sourceUnits.skipCodeUnits(3);
char16_t v;
if (JS7_ISHEX(c2) && sourceUnits.matchHexDigits(3, &v)) {
unit = (JS7_UNHEX(c2) << 12) | v;
} else {
// Beware: |c2| may not be an ASCII code point here!
ungetCodeUnit(c2);
@ -2477,12 +2463,9 @@ TokenStreamSpecific<CharT, AnyCharsAccess>::getStringOrTemplateToken(char untilC
// Hexadecimal character specification.
case 'x': {
CharT cp[2];
if (sourceUnits.peekCodeUnits(2, cp) &&
JS7_ISHEX(cp[0]) && JS7_ISHEX(cp[1]))
{
unit = (JS7_UNHEX(cp[0]) << 4) + JS7_UNHEX(cp[1]);
sourceUnits.skipCodeUnits(2);
char16_t v;
if (sourceUnits.matchHexDigits(2, &v)) {
unit = v;
} else {
uint32_t start = sourceUnits.offset() - 2;
if (parsingTemplate) {

View File

@ -183,6 +183,7 @@
#include "frontend/TokenKind.h"
#include "js/UniquePtr.h"
#include "js/Vector.h"
#include "util/Text.h"
#include "util/Unicode.h"
#include "vm/ErrorReporting.h"
#include "vm/JSContext.h"
@ -983,12 +984,23 @@ class SourceUnits
return *ptr; // this will nullptr-crash if poisoned
}
bool peekCodeUnits(uint8_t n, CharT* out) const {
/** Match |n| hexadecimal digits and store their value in |*out|. */
bool matchHexDigits(uint8_t n, char16_t* out) {
MOZ_ASSERT(ptr, "shouldn't peek into poisoned SourceUnits");
MOZ_ASSERT(n <= 4, "hexdigit value can't overflow char16_t");
if (n > remaining())
return false;
std::copy_n(ptr, n, out);
char16_t v = 0;
for (uint8_t i = 0; i < n; i++) {
if (!JS7_ISHEX(ptr[i]))
return false;
v = (v << 4) | JS7_UNHEX(ptr[i]);
}
*out = v;
ptr += n;
return true;
}