Skip to content

Commit 1f79200

Browse files
authored
This fixes how we count integers in jsonstats. (simdjson#878)
* This fixes how we count integers in jsonstats.
1 parent c009e4a commit 1f79200

1 file changed

Lines changed: 23 additions & 4 deletions

File tree

tools/jsonstats.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ size_t count_backslash(const uint8_t *input, size_t length) {
2020

2121
struct stat_s {
2222
size_t integer_count;
23+
size_t integer32_count;
24+
size_t unsigned_integer32_count;
25+
size_t unsigned_integer_count;
2326
size_t float_count;
2427
size_t string_count;
2528
size_t backslash_count;
@@ -97,10 +100,22 @@ void recurse(simdjson::dom::element element, stat_t &s, size_t depth) {
97100
}
98101
} else {
99102
simdjson::error_code error;
100-
if (element.is<double>()) {
103+
if (element.is<int64_t>()) {
104+
s.integer_count++; // because an int can be sometimes represented as a double, we
105+
// to check whether it is an integer first!!!
106+
int64_t v;
107+
element.get<int64_t>().tie(v,error);
108+
if((v >= std::numeric_limits<int32_t>::min()) and (v <= std::numeric_limits<int32_t>::max()) ) {
109+
s.integer32_count++;
110+
}
111+
if((v >= std::numeric_limits<uint32_t>::min()) and (v <= std::numeric_limits<uint32_t>::max()) ) {
112+
s.unsigned_integer32_count++;
113+
}
114+
}
115+
if(element.is<uint64_t>()) { // the else is intentionally missing
116+
s.unsigned_integer_count++;
117+
} else if (element.is<double>()) {
101118
s.float_count++;
102-
} else if (element.is<int64_t>()) {
103-
s.integer_count++;
104119
} else if (element.is<bool>()) {
105120
bool v;
106121
element.get<bool>().tie(v,error);
@@ -180,6 +195,9 @@ int main(int argc, char *argv[]) {
180195

181196
printf(R"({
182197
"integer_count" = %10zu,
198+
"integer32_count" = %10zu,
199+
"unsigned_integer32_count" = %10zu,
200+
"unsigned_integer_count" = %10zu,
183201
"float_count" = %10zu,
184202
"string_count" = %10zu,
185203
"ascii_string_count" = %10zu,
@@ -201,7 +219,8 @@ int main(int argc, char *argv[]) {
201219
"maximum_depth" = %10zu
202220
}
203221
)",
204-
s.integer_count, s.float_count, s.string_count, s.ascii_string_count,
222+
s.integer_count,s.integer32_count,s.unsigned_integer32_count,s.unsigned_integer_count,
223+
s.float_count, s.string_count, s.ascii_string_count,
205224
s.string_maximum_length, s.backslash_count, s.non_ascii_byte_count,
206225
s.object_count, s.maximum_object_size, s.array_count,
207226
s.maximum_array_size, s.null_count, s.true_count, s.false_count,

0 commit comments

Comments
 (0)