Skip to content

Commit f7df408

Browse files
authored
Merge pull request open-source-parsers#593 from AlB80/master
Optimize Value::isIntegral() method
2 parents 86ed860 + c442fd9 commit f7df408

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/lib_json/json_value.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,11 +1360,23 @@ bool Value::isUInt64() const {
13601360
}
13611361

13621362
bool Value::isIntegral() const {
1363+
switch (type_) {
1364+
case intValue:
1365+
case uintValue:
1366+
return true;
1367+
case realValue:
13631368
#if defined(JSON_HAS_INT64)
1364-
return isInt64() || isUInt64();
1369+
// Note that maxUInt64 (= 2^64 - 1) is not exactly representable as a
1370+
// double, so double(maxUInt64) will be rounded up to 2^64. Therefore we
1371+
// require the value to be strictly less than the limit.
1372+
return value_.real_ >= double(minInt64) && value_.real_ < maxUInt64AsDouble && IsIntegral(value_.real_);
13651373
#else
1366-
return isInt() || isUInt();
1367-
#endif
1374+
return value_.real_ >= minInt && value_.real_ <= maxUInt && IsIntegral(value_.real_);
1375+
#endif // JSON_HAS_INT64
1376+
default:
1377+
break;
1378+
}
1379+
return false;
13681380
}
13691381

13701382
bool Value::isDouble() const { return type_ == intValue || type_ == uintValue || type_ == realValue; }

0 commit comments

Comments
 (0)