@@ -1157,22 +1157,22 @@ template<typename Encoding = UTF8<> >
1157
1157
struct IterativeParsingReaderHandler {
1158
1158
typedef typename Encoding::Ch Ch;
1159
1159
1160
- const static int LOG_NULL = - 1 ;
1161
- const static int LOG_BOOL = - 2 ;
1162
- const static int LOG_INT = - 3 ;
1163
- const static int LOG_UINT = - 4 ;
1164
- const static int LOG_INT64 = - 5 ;
1165
- const static int LOG_UINT64 = - 6 ;
1166
- const static int LOG_DOUBLE = - 7 ;
1167
- const static int LOG_STRING = - 8 ;
1168
- const static int LOG_STARTOBJECT = - 9 ;
1169
- const static int LOG_KEY = - 10 ;
1170
- const static int LOG_ENDOBJECT = - 11 ;
1171
- const static int LOG_STARTARRAY = - 12 ;
1172
- const static int LOG_ENDARRAY = - 13 ;
1160
+ const static uint32_t LOG_NULL = 0x10000000 ;
1161
+ const static uint32_t LOG_BOOL = 0x20000000 ;
1162
+ const static uint32_t LOG_INT = 0x30000000 ;
1163
+ const static uint32_t LOG_UINT = 0x40000000 ;
1164
+ const static uint32_t LOG_INT64 = 0x50000000 ;
1165
+ const static uint32_t LOG_UINT64 = 0x60000000 ;
1166
+ const static uint32_t LOG_DOUBLE = 0x70000000 ;
1167
+ const static uint32_t LOG_STRING = 0x80000000 ;
1168
+ const static uint32_t LOG_STARTOBJECT = 0x90000000 ;
1169
+ const static uint32_t LOG_KEY = 0xA0000000 ;
1170
+ const static uint32_t LOG_ENDOBJECT = 0xB0000000 ;
1171
+ const static uint32_t LOG_STARTARRAY = 0xC0000000 ;
1172
+ const static uint32_t LOG_ENDARRAY = 0xD0000000 ;
1173
1173
1174
1174
const static size_t LogCapacity = 256 ;
1175
- int Logs[LogCapacity];
1175
+ uint32_t Logs[LogCapacity];
1176
1176
size_t LogCount;
1177
1177
1178
1178
IterativeParsingReaderHandler () : LogCount(0 ) {
@@ -1202,17 +1202,17 @@ struct IterativeParsingReaderHandler {
1202
1202
1203
1203
bool EndObject (SizeType c) {
1204
1204
RAPIDJSON_ASSERT (LogCount < LogCapacity);
1205
- Logs[LogCount++] = LOG_ENDOBJECT ;
1206
- Logs[LogCount++] = static_cast <int >(c);
1205
+ RAPIDJSON_ASSERT (( static_cast < uint32_t >(c) & 0xF0000000 ) == 0 ) ;
1206
+ Logs[LogCount++] = LOG_ENDOBJECT | static_cast <uint32_t >(c);
1207
1207
return true ;
1208
1208
}
1209
1209
1210
1210
bool StartArray () { RAPIDJSON_ASSERT (LogCount < LogCapacity); Logs[LogCount++] = LOG_STARTARRAY; return true ; }
1211
1211
1212
1212
bool EndArray (SizeType c) {
1213
1213
RAPIDJSON_ASSERT (LogCount < LogCapacity);
1214
- Logs[LogCount++] = LOG_ENDARRAY ;
1215
- Logs[LogCount++] = static_cast <int >(c);
1214
+ RAPIDJSON_ASSERT (( static_cast < uint32_t >(c) & 0xF0000000 ) == 0 ) ;
1215
+ Logs[LogCount++] = LOG_ENDARRAY | static_cast <uint32_t >(c);
1216
1216
return true ;
1217
1217
}
1218
1218
};
@@ -1228,22 +1228,22 @@ TEST(Reader, IterativeParsing_General) {
1228
1228
EXPECT_FALSE (r.IsError ());
1229
1229
EXPECT_FALSE (reader.HasParseError ());
1230
1230
1231
- int e[] = {
1231
+ uint32_t e[] = {
1232
1232
handler.LOG_STARTARRAY ,
1233
1233
handler.LOG_INT ,
1234
1234
handler.LOG_STARTOBJECT ,
1235
1235
handler.LOG_KEY ,
1236
1236
handler.LOG_STARTARRAY ,
1237
1237
handler.LOG_INT ,
1238
1238
handler.LOG_INT ,
1239
- handler.LOG_ENDARRAY , 2 ,
1240
- handler.LOG_ENDOBJECT , 1 ,
1239
+ handler.LOG_ENDARRAY | 2 ,
1240
+ handler.LOG_ENDOBJECT | 1 ,
1241
1241
handler.LOG_NULL ,
1242
1242
handler.LOG_BOOL ,
1243
1243
handler.LOG_BOOL ,
1244
1244
handler.LOG_STRING ,
1245
1245
handler.LOG_DOUBLE ,
1246
- handler.LOG_ENDARRAY , 7
1246
+ handler.LOG_ENDARRAY | 7
1247
1247
};
1248
1248
1249
1249
EXPECT_EQ (sizeof (e) / sizeof (int ), handler.LogCount );
@@ -1265,20 +1265,20 @@ TEST(Reader, IterativeParsing_Count) {
1265
1265
EXPECT_FALSE (r.IsError ());
1266
1266
EXPECT_FALSE (reader.HasParseError ());
1267
1267
1268
- int e[] = {
1268
+ uint32_t e[] = {
1269
1269
handler.LOG_STARTARRAY ,
1270
1270
handler.LOG_STARTOBJECT ,
1271
- handler.LOG_ENDOBJECT , 0 ,
1271
+ handler.LOG_ENDOBJECT | 0 ,
1272
1272
handler.LOG_STARTOBJECT ,
1273
1273
handler.LOG_KEY ,
1274
1274
handler.LOG_INT ,
1275
- handler.LOG_ENDOBJECT , 1 ,
1275
+ handler.LOG_ENDOBJECT | 1 ,
1276
1276
handler.LOG_STARTARRAY ,
1277
1277
handler.LOG_INT ,
1278
- handler.LOG_ENDARRAY , 1 ,
1278
+ handler.LOG_ENDARRAY | 1 ,
1279
1279
handler.LOG_STARTARRAY ,
1280
- handler.LOG_ENDARRAY , 0 ,
1281
- handler.LOG_ENDARRAY , 4
1280
+ handler.LOG_ENDARRAY | 0 ,
1281
+ handler.LOG_ENDARRAY | 4
1282
1282
};
1283
1283
1284
1284
EXPECT_EQ (sizeof (e) / sizeof (int ), handler.LogCount );
@@ -1289,6 +1289,51 @@ TEST(Reader, IterativeParsing_Count) {
1289
1289
}
1290
1290
}
1291
1291
1292
+ TEST (Reader, IterativePullParsing_General) {
1293
+ {
1294
+ IterativeParsingReaderHandler<> handler;
1295
+ uint32_t e[] = {
1296
+ handler.LOG_STARTARRAY ,
1297
+ handler.LOG_INT ,
1298
+ handler.LOG_STARTOBJECT ,
1299
+ handler.LOG_KEY ,
1300
+ handler.LOG_STARTARRAY ,
1301
+ handler.LOG_INT ,
1302
+ handler.LOG_INT ,
1303
+ handler.LOG_ENDARRAY | 2 ,
1304
+ handler.LOG_ENDOBJECT | 1 ,
1305
+ handler.LOG_NULL ,
1306
+ handler.LOG_BOOL ,
1307
+ handler.LOG_BOOL ,
1308
+ handler.LOG_STRING ,
1309
+ handler.LOG_DOUBLE ,
1310
+ handler.LOG_ENDARRAY | 7
1311
+ };
1312
+
1313
+ StringStream is (" [1, {\" k\" : [1, 2]}, null, false, true, \" string\" , 1.2]" );
1314
+ Reader reader;
1315
+
1316
+ reader.IterativeParseInit ();
1317
+ while (!reader.IterativeParseComplete ()) {
1318
+ size_t oldLogCount = handler.LogCount ;
1319
+ EXPECT_TRUE (oldLogCount < sizeof (e) / sizeof (int )) << " overrun" ;
1320
+
1321
+ EXPECT_TRUE (reader.IterativeParseNext <kParseDefaultFlags >(is, handler)) << " parse fail" ;
1322
+ EXPECT_EQ (handler.LogCount , oldLogCount + 1 ) << " handler should be invoked exactly once each time" ;
1323
+ EXPECT_EQ (e[oldLogCount], handler.Logs [oldLogCount]) << " wrong event returned" ;
1324
+ }
1325
+
1326
+ EXPECT_FALSE (reader.HasParseError ());
1327
+ EXPECT_EQ (sizeof (e) / sizeof (int ), handler.LogCount ) << " handler invoked wrong number of times" ;
1328
+
1329
+ // The handler should not be invoked when the JSON has been fully read, but it should not fail
1330
+ size_t oldLogCount = handler.LogCount ;
1331
+ EXPECT_TRUE (reader.IterativeParseNext <kParseDefaultFlags >(is, handler)) << " parse-next past complete is allowed" ;
1332
+ EXPECT_EQ (handler.LogCount , oldLogCount) << " parse-next past complete should not invoke handler" ;
1333
+ EXPECT_FALSE (reader.HasParseError ()) << " parse-next past complete should not generate parse error" ;
1334
+ }
1335
+ }
1336
+
1292
1337
// Test iterative parsing on kParseErrorTermination.
1293
1338
struct HandlerTerminateAtStartObject : public IterativeParsingReaderHandler <> {
1294
1339
bool StartObject () { return false ; }
0 commit comments