Bug 1023778 part 4 - Add JSONParser<Latin1Char>. r=Waldo

This commit is contained in:
Jan de Mooij 2014-06-13 20:51:07 +02:00
parent e261152579
commit e49fbcc777
3 changed files with 24 additions and 10 deletions

View File

@ -45,6 +45,7 @@ using mozilla::MinNumberValue;
using mozilla::NegativeInfinity;
using mozilla::PodCopy;
using mozilla::PositiveInfinity;
using mozilla::Range;
using mozilla::RangedPtr;
using JS::AutoCheckCannotGC;
@ -175,14 +176,15 @@ ComputeAccurateBinaryBaseInteger(const CharT *start, const CharT *end, int base)
return value;
}
template <typename CharT>
double
js::ParseDecimalNumber(const JS::TwoByteChars chars)
js::ParseDecimalNumber(const Range<const CharT> chars)
{
MOZ_ASSERT(chars.length() > 0);
uint64_t dec = 0;
RangedPtr<jschar> s = chars.start(), end = chars.end();
RangedPtr<const CharT> s = chars.start(), end = chars.end();
do {
jschar c = *s;
CharT c = *s;
MOZ_ASSERT('0' <= c && c <= '9');
uint8_t digit = c - '0';
uint64_t next = dec * 10 + digit;
@ -193,6 +195,12 @@ js::ParseDecimalNumber(const JS::TwoByteChars chars)
return static_cast<double>(dec);
}
template double
js::ParseDecimalNumber(const Range<const Latin1Char> chars);
template double
js::ParseDecimalNumber(const Range<const jschar> chars);
template <typename CharT>
bool
js::GetPrefixInteger(ThreadSafeContext *cx, const CharT *start, const CharT *end, int base,

View File

@ -8,6 +8,7 @@
#define jsnum_h
#include "mozilla/FloatingPoint.h"
#include "mozilla/Range.h"
#include "NamespaceImports.h"
@ -117,8 +118,9 @@ const double DOUBLE_INTEGRAL_PRECISION_LIMIT = uint64_t(1) << 53;
* the double type -- that is, the number will be smaller than
* DOUBLE_INTEGRAL_PRECISION_LIMIT
*/
template <typename CharT>
extern double
ParseDecimalNumber(const JS::TwoByteChars chars);
ParseDecimalNumber(const mozilla::Range<const CharT> chars);
/*
* Compute the positive integer of the given base described immediately at the

View File

@ -6,6 +6,7 @@
#include "jsonparser.h"
#include "mozilla/Range.h"
#include "mozilla/RangedPtr.h"
#include <ctype.h>
@ -21,6 +22,7 @@
using namespace js;
using mozilla::Range;
using mozilla::RangedPtr;
JSONParserBase::~JSONParserBase()
@ -126,7 +128,7 @@ JSONParser<CharT>::readString()
* Optimization: if the source contains no escaped characters, create the
* string directly from the source text.
*/
RangedPtr<const jschar> start = current;
CharPtr start = current;
for (; current < end; current++) {
if (*current == '"') {
size_t length = current - start;
@ -259,7 +261,7 @@ JSONParser<CharT>::readNumber()
return token(Error);
}
const RangedPtr<const jschar> digitStart = current;
const CharPtr digitStart = current;
/* 0|[1-9][0-9]+ */
if (!JS7_ISDEC(*current)) {
@ -275,7 +277,7 @@ JSONParser<CharT>::readNumber()
/* Fast path: no fractional or exponent part. */
if (current == end || (*current != '.' && *current != 'e' && *current != 'E')) {
TwoByteChars chars(digitStart.get(), current - digitStart);
Range<const CharT> chars(digitStart.get(), current - digitStart);
if (chars.length() < strlen("9007199254740992")) {
// If the decimal number is shorter than the length of 2**53, (the
// largest number a double can represent with integral precision),
@ -286,7 +288,7 @@ JSONParser<CharT>::readNumber()
}
double d;
const jschar *dummy;
const CharT *dummy;
if (!GetPrefixInteger(cx, digitStart.get(), current.get(), 10, &dummy, &d))
return token(OOM);
JS_ASSERT(current == dummy);
@ -332,7 +334,7 @@ JSONParser<CharT>::readNumber()
}
double d;
const jschar *finish;
const CharT *finish;
if (!js_strtod(cx, digitStart.get(), current.get(), &finish, &d))
return token(OOM);
JS_ASSERT(current == finish);
@ -452,8 +454,9 @@ JSONParser<CharT>::advanceAfterObjectOpen()
return token(Error);
}
template <typename CharT>
static inline void
AssertPastValue(const RangedPtr<const jschar> current)
AssertPastValue(const RangedPtr<const CharT> current)
{
/*
* We're past an arbitrary JSON value, so the previous character is
@ -827,4 +830,5 @@ JSONParser<CharT>::parse(MutableHandleValue vp)
return true;
}
template class js::JSONParser<Latin1Char>;
template class js::JSONParser<jschar>;