1 #ifndef RAPIDJSON_READER_H_
2 #define RAPIDJSON_READER_H_
17 #ifdef RAPIDJSON_SSE42
18 #include <nmmintrin.h>
19 #elif defined(RAPIDJSON_SSE2)
20 #include <emmintrin.h>
25 #pragma warning(disable : 4127)
28 #ifndef RAPIDJSON_PARSE_ERROR
29 #define RAPIDJSON_PARSE_ERROR(msg, offset) \
30 RAPIDJSON_MULTILINEMACRO_BEGIN \
32 errorOffset_ = offset; \
33 longjmp(jmpbuf_, 1); \
34 RAPIDJSON_MULTILINEMACRO_END
78 template<
typename Encoding = UTF8<> >
80 typedef typename Encoding::Ch
Ch;
104 template<
typename Stream>
107 while (
s.Peek() ==
' ' ||
s.Peek() ==
'\n' ||
s.Peek() ==
'\r' ||
s.Peek() ==
'\t')
112 #ifdef RAPIDJSON_SSE42
114 inline const char *SkipWhitespace_SIMD(
const char*
p) {
115 static const char whitespace[16] =
" \n\r\t";
116 __m128i
w = _mm_loadu_si128((
const __m128i *)&whitespace[0]);
119 __m128i
s = _mm_loadu_si128((
const __m128i *)
p);
120 unsigned r = _mm_cvtsi128_si32(_mm_cmpistrm(
w,
s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK | _SIDD_NEGATIVE_POLARITY));
125 unsigned long offset;
126 if (_BitScanForward(&offset,
r))
130 return p + __builtin_ffs(
r) - 1;
136 #elif defined(RAPIDJSON_SSE2)
139 inline const char *SkipWhitespace_SIMD(
const char*
p) {
140 static const char whitespaces[4][17] = {
142 "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n",
143 "\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r",
144 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"};
146 __m128i w0 = _mm_loadu_si128((
const __m128i *)&whitespaces[0][0]);
147 __m128i w1 = _mm_loadu_si128((
const __m128i *)&whitespaces[1][0]);
148 __m128i w2 = _mm_loadu_si128((
const __m128i *)&whitespaces[2][0]);
149 __m128i w3 = _mm_loadu_si128((
const __m128i *)&whitespaces[3][0]);
152 __m128i
s = _mm_loadu_si128((
const __m128i *)
p);
153 __m128i
x = _mm_cmpeq_epi8(
s, w0);
154 x = _mm_or_si128(
x, _mm_cmpeq_epi8(
s, w1));
155 x = _mm_or_si128(
x, _mm_cmpeq_epi8(
s, w2));
156 x = _mm_or_si128(
x, _mm_cmpeq_epi8(
s, w3));
157 unsigned short r = ~_mm_movemask_epi8(
x);
162 unsigned long offset;
163 if (_BitScanForward(&offset,
r))
167 return p + __builtin_ffs(
r) - 1;
175 #ifdef RAPIDJSON_SIMD
178 stream.src_ =
const_cast<char*
>(SkipWhitespace_SIMD(stream.src_));
183 stream.src_ = SkipWhitespace_SIMD(stream.src_);
205 template <
typename Encoding,
typename Allocator = MemoryPoolAllocator<> >
208 typedef typename Encoding::Ch
Ch;
224 template <
unsigned parseFlags,
typename Stream,
typename Handler>
230 #pragma warning(push)
231 #pragma warning(disable : 4611)
243 if (stream.Peek() ==
'\0')
246 switch (stream.Peek()) {
247 case '{': ParseObject<parseFlags>(stream, handler);
break;
248 case '[': ParseArray<parseFlags>(stream, handler);
break;
253 if (stream.Peek() !=
'\0' && stream.Peek() !=
static_cast<Ch>(std::char_traits<Ch>::eof()))
266 template<
unsigned parseFlags,
typename Stream,
typename Handler>
270 handler.StartObject();
273 if (stream.Peek() ==
'}') {
275 handler.EndObject(0);
280 if (stream.Peek() !=
'"') {
285 ParseString<parseFlags>(stream, handler);
288 if (stream.Take() !=
':') {
294 ParseValue<parseFlags>(stream, handler);
299 switch(stream.Take()) {
301 case '}': handler.EndObject(memberCount);
return;
308 template<
unsigned parseFlags,
typename Stream,
typename Handler>
312 handler.StartArray();
315 if (stream.Peek() ==
']') {
322 ParseValue<parseFlags>(stream, handler);
326 switch (stream.Take()) {
328 case ']': handler.EndArray(elementCount);
return;
335 template<
unsigned parseFlags,
typename Stream,
typename Handler>
340 if( stream.Peek() ==
'a' && stream.Take() ==
'a' && stream.Take() ==
'n' )
341 handler.Double( std::numeric_limits<double>::quiet_NaN() );
342 else if (stream.Take() ==
'u' && stream.Take() ==
'l' && stream.Take() ==
'l')
349 template<
unsigned parseFlags,
typename Stream,
typename Handler>
354 if (stream.Take() ==
'n' && stream.Take() ==
'f')
355 handler.Double( std::numeric_limits<double>::infinity() );
360 template<
unsigned parseFlags,
typename Stream,
typename Handler>
365 if (stream.Take() ==
'r' && stream.Take() ==
'u' && stream.Take() ==
'e')
371 template<
unsigned parseFlags,
typename Stream,
typename Handler>
376 if (stream.Take() ==
'a' && stream.Take() ==
'l' && stream.Take() ==
's' && stream.Take() ==
'e')
377 handler.Bool_(
false);
383 template<
typename Stream>
386 unsigned codepoint = 0;
387 for (
int i = 0;
i < 4;
i++) {
391 if (
c >=
'0' &&
c <=
'9')
393 else if (
c >=
'A' &&
c <=
'F')
394 codepoint -=
'A' - 10;
395 else if (
c >=
'a' &&
c <=
'f')
396 codepoint -=
'a' - 10;
413 bool characterOk<char>(
Ch )
437 template<
unsigned parseFlags,
typename Stream,
typename Handler>
439 #define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
441 Z16,
Z16, 0, 0,
'\"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
'/',
442 Z16,
Z16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
'\\', 0, 0, 0,
443 0, 0,
'\b', 0, 0, 0,
'\f', 0, 0, 0, 0, 0, 0, 0,
'\n', 0,
444 0, 0,
'\r', 0,
'\t', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
459 #define RAPIDJSON_PUT(x) \
461 if (parseFlags & kParseInsituFlag) \
464 *stack_.template Push<Ch>() = x; \
477 if (codepoint >= 0xD800 && codepoint <= 0xDBFF) {
478 if (
s.Take() !=
'\\' ||
s.Take() !=
'u') {
483 if (codepoint2 < 0xDC00 || codepoint2 > 0xDFFF) {
487 codepoint = (((codepoint - 0xD800) << 10) | (codepoint2 - 0xDC00)) + 0x10000;
491 SizeType count =
SizeType(Encoding::Encode(buffer, codepoint) - &buffer[0]);
497 memcpy(
stack_.template Push<Ch>(count), buffer, count *
sizeof(
Ch));
508 size_t length =
s.PutEnd(head);
511 handler.String(head,
SizeType(length),
false);
515 handler.String(
stack_.template Pop<Ch>(len), len - 1,
true);
520 else if (
c ==
'\0') {
524 else if ((
unsigned)
c < 0x20) {
534 template<
unsigned parseFlags,
typename Stream,
typename Handler>
540 if (
s.Peek() ==
'-') {
547 bool try64bit =
false;
548 if (
s.Peek() ==
'0') {
552 else if (
s.Peek() >=
'1' &&
s.Peek() <=
'9') {
556 while (
s.Peek() >=
'0' &&
s.Peek() <=
'9') {
557 if (
i >= 214748364) {
558 if (
i != 214748364 ||
s.Peek() >
'8') {
563 i =
i * 10 + (
s.Take() -
'0');
566 while (
s.Peek() >=
'0' &&
s.Peek() <=
'9') {
567 if (
i >= 429496729) {
568 if (
i != 429496729 ||
s.Peek() >
'5') {
573 i =
i * 10 + (
s.Take() -
'0');
583 bool useDouble =
false;
587 while (
s.Peek() >=
'0' &&
s.Peek() <=
'9') {
588 if (i64 >= 922337203685477580uLL)
589 if (i64 != 922337203685477580uLL ||
s.Peek() >
'8') {
593 i64 = i64 * 10 + (
s.Take() -
'0');
596 while (
s.Peek() >=
'0' &&
s.Peek() <=
'9') {
597 if (i64 >= 1844674407370955161uLL)
598 if (i64 != 1844674407370955161uLL ||
s.Peek() >
'5') {
602 i64 = i64 * 10 + (
s.Take() -
'0');
610 while (
s.Peek() >=
'0' &&
s.Peek() <=
'9') {
615 d = d * 10 + (
s.Take() -
'0');
621 if (
s.Peek() ==
'.') {
623 d = try64bit ? (
double)i64 : (
double)
i;
628 if (
s.Peek() >=
'0' &&
s.Peek() <=
'9') {
629 d = d * 10 + (
s.Take() -
'0');
637 while (
s.Peek() >=
'0' &&
s.Peek() <=
'9') {
639 d = d * 10 + (
s.Peek() -
'0');
648 if (
s.Peek() ==
'e' ||
s.Peek() ==
'E') {
650 d = try64bit ? (
double)i64 : (
double)
i;
655 bool expMinus =
false;
658 else if (
s.Peek() ==
'-') {
663 if (
s.Peek() >=
'0' &&
s.Peek() <=
'9') {
664 exp =
s.Take() -
'0';
665 while (
s.Peek() >=
'0' &&
s.Peek() <=
'9') {
666 exp =
exp * 10 + (
s.Take() -
'0');
669 std::stringstream ss;
670 ss.precision( std::numeric_limits<double>::max_digits10 );
676 if( std::fpclassify( dd ) == FP_SUBNORMAL )
677 handler.Double( dd );
697 handler.Double(minus ? -d : d);
708 handler.Int(-(
int)
i);
718 template<
unsigned parseFlags,
typename Stream,
typename Handler>
720 switch (stream.Peek()) {
721 case 'n': ParseNaNNull_ <parseFlags>(stream, handler);
break;
722 case 'i': ParseInfinity <parseFlags>(stream, handler);
break;
723 case 't': ParseTrue <parseFlags>(stream, handler);
break;
724 case 'f': ParseFalse <parseFlags>(stream, handler);
break;
725 case '"': ParseString <parseFlags>(stream, handler);
break;
726 case '{': ParseObject <parseFlags>(stream, handler);
break;
727 case '[': ParseArray <parseFlags>(stream, handler);
break;
728 default : ParseNumber <parseFlags>(stream, handler);
static void escape(void *p)
Definition: BenchTimer.h:30
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RowVector3d w
Definition: Matrix_resize_int.cpp:3
float * p
Definition: Tutorial_Map_using.cpp:9
Concept for allocating, resizing and freeing memory block.
SAX-style JSON parser. Use Reader for UTF8 encoding and default allocator.
Definition: reader.h:206
void ParseValue(Stream &stream, Handler &handler)
Definition: reader.h:719
void ParseObject(Stream &stream, Handler &handler)
Definition: reader.h:267
void ParseNumber(Stream &stream, Handler &handler)
Definition: reader.h:535
jmp_buf jmpbuf_
setjmp buffer for fast exit from nested parsing function calls.
Definition: reader.h:734
void ParseInfinity(Stream &stream, Handler &handler)
Definition: reader.h:350
bool Parse(Stream &stream, Handler &handler)
Parse JSON text.
Definition: reader.h:225
unsigned ParseHex4(Stream &stream)
Definition: reader.h:384
internal::Stack< Allocator > stack_
A stack for storing decoded string temporarily during non-destructive parsing.
Definition: reader.h:733
void ParseArray(Stream &stream, Handler &handler)
Definition: reader.h:309
Encoding::Ch Ch
Definition: reader.h:208
const char * parseError_
Definition: reader.h:735
void ParseString(Stream &stream, Handler &handler)
Definition: reader.h:438
void ParseTrue(Stream &stream, Handler &handler)
Definition: reader.h:361
void ParseNaNNull_(Stream &stream, Handler &handler)
Definition: reader.h:336
const char * GetParseError() const
Definition: reader.h:261
std::enable_if< to_int(std::numeric_limits< Ch >::max())< to_int(256), bool >::type characterOk(Ch) { return true;} template< class Ch > typename std::enable_if< to_int(std::numeric_limits< Ch >::max()) >=to_int(256), bool >::type characterOk(Ch c)
Definition: reader.h:432
static constexpr int to_int(T t)
Definition: reader.h:421
size_t errorOffset_
Definition: reader.h:736
void ParseFalse(Stream &stream, Handler &handler)
Definition: reader.h:372
GenericReader(Allocator *allocator=0, size_t stackCapacity=kDefaultStackCapacity)
Constructor.
Definition: reader.h:214
bool HasParseError() const
Definition: reader.h:260
static const size_t kDefaultStackCapacity
Default stack capacity in bytes for storing a single decoded string.
Definition: reader.h:732
size_t GetErrorOffset() const
Definition: reader.h:262
Concept for receiving events from GenericReader upon parsing.
Concept for reading and writing characters.
A type-unsafe stack for storing different types of data.
Definition: stack.h:14
#define max(a, b)
Definition: datatypes.h:23
RealScalar s
Definition: level1_cplx_impl.h:130
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 exp(const bfloat16 &a)
Definition: BFloat16.h:615
std::int64_t int64_t
Definition: Meta.h:43
std::uint64_t uint64_t
Definition: Meta.h:42
int c
Definition: calibrate.py:100
default
Definition: calibrate.py:45
type
Definition: compute_granudrum_aor.py:141
list x
Definition: plotDoE.py:28
t
Definition: plotPSD.py:36
double Pow10(int n)
Computes integer powers of 10 in double (10.0^n).
Definition: pow10.h:12
Definition: document.h:13
unsigned SizeType
Use 32-bit array/string indices even for 64-bit platform, instead of using size_t.
Definition: rapidjson.h:67
GenericInsituStringStream< UTF8<> > InsituStringStream
Definition: rapidjson.h:507
void SkipWhitespace(Stream &stream)
Skip the JSON white spaces in a stream.
Definition: reader.h:105
GenericReader< UTF8<> > Reader
Reader with UTF8 encoding and default allocator.
Definition: reader.h:740
ParseFlag
Combination of parseFlags.
Definition: reader.h:42
@ kParseInsituFlag
In-situ(destructive) parsing.
Definition: reader.h:44
@ kParseDefaultFlags
Default parse flags. Non-destructive parsing. Text strings are decoded into allocated buffer.
Definition: reader.h:43
GenericStringStream< UTF8<> > StringStream
Definition: rapidjson.h:477
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:80
#define RAPIDJSON_PARSE_ERROR(msg, offset)
Definition: reader.h:29
Default implementation of Handler.
Definition: reader.h:79
void Default()
Definition: reader.h:82
void Int(int)
Definition: reader.h:85
void StartObject()
Definition: reader.h:91
void Null_()
Definition: reader.h:83
void Bool_(bool)
Definition: reader.h:84
void EndObject(SizeType)
Definition: reader.h:92
void StartArray()
Definition: reader.h:93
void Double(double)
Definition: reader.h:89
void Uint(unsigned)
Definition: reader.h:86
void Int64(int64_t)
Definition: reader.h:87
void Uint64(uint64_t)
Definition: reader.h:88
void EndArray(SizeType)
Definition: reader.h:94
void String(const Ch *, SizeType, bool)
Definition: reader.h:90
Encoding::Ch Ch
Definition: reader.h:80