Apply minor readability fixes

This commit is contained in:
Kai Wolf 2019-02-23 17:28:20 +01:00
parent 527ee8eace
commit ff22e75f95
24 changed files with 219 additions and 166 deletions

View File

@ -88,3 +88,5 @@ private:
} }
}; };
#endif #endif
#endif

View File

@ -1,17 +1,17 @@
#include <assert.h> #include <cassert>
#include <ctype.h> #include <cctype>
#ifndef _MSC_VER #ifndef _MSC_VER
#include <dirent.h>
#include <unistd.h> #include <unistd.h>
#include <x86intrin.h> #include <x86intrin.h>
#include <dirent.h>
#else #else
#include <intrin.h> #include <intrin.h>
#endif #endif
#include <inttypes.h> #include <cinttypes>
#include <stdbool.h>
#include <stdio.h> #include <cstdio>
#include <stdlib.h> #include <cstdlib>
#include <string.h> #include <cstring>
#include <algorithm> #include <algorithm>
#include <chrono> #include <chrono>
@ -47,7 +47,7 @@ int main(int argc, char *argv[]) {
#ifndef _MSC_VER #ifndef _MSC_VER
int c; int c;
while ((c = getopt(argc, argv, "1vdt")) != -1) while ((c = getopt(argc, argv, "1vdt")) != -1) {
switch (c) { switch (c) {
case 't': case 't':
justdata = true; justdata = true;
@ -67,6 +67,7 @@ int main(int argc, char *argv[]) {
default: default:
abort(); abort();
} }
}
#else #else
int optind = 1; int optind = 1;
#endif #endif
@ -78,8 +79,9 @@ int main(int argc, char *argv[]) {
if (optind + 1 < argc) { if (optind + 1 < argc) {
cerr << "warning: ignoring everything after " << argv[optind + 1] << endl; cerr << "warning: ignoring everything after " << argv[optind + 1] << endl;
} }
if (verbose) if (verbose) {
cout << "[verbose] loading " << filename << endl; cout << "[verbose] loading " << filename << endl;
}
std::string_view p; std::string_view p;
try { try {
p = get_corpus(filename); p = get_corpus(filename);
@ -87,9 +89,10 @@ int main(int argc, char *argv[]) {
std::cout << "Could not load the file " << filename << std::endl; std::cout << "Could not load the file " << filename << std::endl;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (verbose) if (verbose) {
cout << "[verbose] loaded " << filename << " (" << p.size() << " bytes)" cout << "[verbose] loaded " << filename << " (" << p.size() << " bytes)"
<< endl; << endl;
}
#if defined(DEBUG) #if defined(DEBUG)
const uint32_t iterations = 1; const uint32_t iterations = 1;
#else #else
@ -125,8 +128,9 @@ int main(int argc, char *argv[]) {
bool isok = true; bool isok = true;
for (uint32_t i = 0; i < iterations; i++) { for (uint32_t i = 0; i < iterations; i++) {
if (verbose) if (verbose) {
cout << "[verbose] iteration # " << i << endl; cout << "[verbose] iteration # " << i << endl;
}
#ifndef SQUASH_COUNTERS #ifndef SQUASH_COUNTERS
unified.start(); unified.start();
#endif #endif
@ -144,8 +148,9 @@ int main(int argc, char *argv[]) {
cref0 += results[3]; cref0 += results[3];
cmis0 += results[4]; cmis0 += results[4];
#endif #endif
if (verbose) if (verbose) {
cout << "[verbose] allocated memory for parsed JSON " << endl; cout << "[verbose] allocated memory for parsed JSON " << endl;
}
auto start = std::chrono::steady_clock::now(); auto start = std::chrono::steady_clock::now();
#ifndef SQUASH_COUNTERS #ifndef SQUASH_COUNTERS
@ -248,10 +253,11 @@ int main(int argc, char *argv[]) {
} }
#endif #endif
double min_result = *min_element(res.begin(), res.end()); double min_result = *min_element(res.begin(), res.end());
if (!justdata) if (!justdata) {
cout << "Min: " << min_result << " bytes read: " << p.size() cout << "Min: " << min_result << " bytes read: " << p.size()
<< " Gigabytes/second: " << (p.size()) / (min_result * 1000000000.0) << " Gigabytes/second: " << (p.size()) / (min_result * 1000000000.0)
<< "\n"; << "\n";
}
if (jsonoutput) { if (jsonoutput) {
isok = isok && pj.printjson(std::cout); isok = isok && pj.printjson(std::cout);
} }

View File

@ -42,7 +42,7 @@ struct stat_s {
bool valid; bool valid;
}; };
typedef struct stat_s stat_t; using stat_t = struct stat_s;
stat_t simdjson_computestats(const std::string_view &p) { stat_t simdjson_computestats(const std::string_view &p) {
stat_t answer; stat_t answer;
@ -115,12 +115,13 @@ stat_t simdjson_computestats(const std::string_view &p) {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
#ifndef _MSC_VER #ifndef _MSC_VER
int c; int c;
while ((c = getopt(argc, argv, "")) != -1) while ((c = getopt(argc, argv, "")) != -1) {
switch (c) { switch (c) {
default: default:
abort(); abort();
} }
}
#else #else
int optind = 1; int optind = 1;
#endif #endif

View File

@ -56,4 +56,4 @@
#endif // MSC_VER #endif // MSC_VER
#endif // COMMON_DEFS_H #endif // SIMDJSON_COMMON_DEFS_H

View File

@ -104,7 +104,7 @@ inline size_t codepoint_to_utf8(uint32_t cp, uint8_t *c) {
if (cp <= 0x7F) { if (cp <= 0x7F) {
c[0] = cp; c[0] = cp;
return 1; // ascii return 1; // ascii
} else if (cp <= 0x7FF) { } if (cp <= 0x7FF) {
c[0] = (cp >> 6) + 192; c[0] = (cp >> 6) + 192;
c[1] = (cp & 63) + 128; c[1] = (cp & 63) + 128;
return 2; // universal plane return 2; // universal plane

View File

@ -1,12 +1,12 @@
#ifndef SIMDJSON_JSONFORMATUTILS_H #ifndef SIMDJSON_JSONFORMATUTILS_H
#define SIMDJSON_JSONFORMATUTILS_H #define SIMDJSON_JSONFORMATUTILS_H
#include <stdio.h> #include <cstdio>
#include <iostream>
#include <iomanip> #include <iomanip>
#include <iostream>
static inline void print_with_escapes(const unsigned char *src) { static inline void print_with_escapes(const unsigned char *src) {
while (*src) { while (*src != 0u) {
switch (*src) { switch (*src) {
case '\b': case '\b':
putchar('\\'); putchar('\\');
@ -39,15 +39,16 @@ static inline void print_with_escapes(const unsigned char *src) {
default: default:
if (*src <= 0x1F) { if (*src <= 0x1F) {
printf("\\u%04x", *src); printf("\\u%04x", *src);
} else } else {
putchar(*src); putchar(*src);
}
} }
src++; src++;
} }
} }
static inline void print_with_escapes(const unsigned char *src, std::ostream &os) { static inline void print_with_escapes(const unsigned char *src, std::ostream &os) {
while (*src) { while (*src != 0u) {
switch (*src) { switch (*src) {
case '\b': case '\b':
os << '\\'; os << '\\';
@ -82,8 +83,9 @@ static inline void print_with_escapes(const unsigned char *src, std::ostream &os
std::ios::fmtflags f(os.flags()); std::ios::fmtflags f(os.flags());
os << std::hex << std::setw(4) << std::setfill('0') << (int) *src; os << std::hex << std::setw(4) << std::setfill('0') << (int) *src;
os.flags(f); os.flags(f);
} else } else {
os << *src; os << *src;
}
} }
src++; src++;
} }

View File

@ -1,12 +1,12 @@
#ifndef SIMDJSON_JSONIOUTIL_H #ifndef SIMDJSON_JSONIOUTIL_H
#define SIMDJSON_JSONIOUTIL_H #define SIMDJSON_JSONIOUTIL_H
#include "simdjson/common_defs.h"
#include <exception> #include <exception>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include "simdjson/common_defs.h"
// low-level function to allocate memory with padding so we can read passed the "length" bytes // low-level function to allocate memory with padding so we can read passed the "length" bytes
@ -34,7 +34,7 @@ char * allocate_padded_buffer(size_t length);
// free((void*)p.data());//use aligned_free if you plan to use VisualStudio // free((void*)p.data());//use aligned_free if you plan to use VisualStudio
// std::cout << "Could not load the file " << filename << std::endl; // std::cout << "Could not load the file " << filename << std::endl;
// } // }
std::string_view get_corpus(std::string filename); std::string_view get_corpus(const std::string& filename);
#endif #endif

View File

@ -1,10 +1,10 @@
#ifndef SIMDJSON_NUMBERPARSING_H #ifndef SIMDJSON_NUMBERPARSING_H
#define SIMDJSON_NUMBERPARSING_H #define SIMDJSON_NUMBERPARSING_H
#include "simdjson/portability.h"
#include "simdjson/common_defs.h" #include "simdjson/common_defs.h"
#include "simdjson/jsoncharutils.h" #include "simdjson/jsoncharutils.h"
#include "simdjson/parsedjson.h" #include "simdjson/parsedjson.h"
#include "simdjson/portability.h"
static const double power_of_ten[] = { static const double power_of_ten[] = {
1e-308, 1e-307, 1e-306, 1e-305, 1e-304, 1e-303, 1e-302, 1e-301, 1e-300, 1e-308, 1e-307, 1e-306, 1e-305, 1e-304, 1e-303, 1e-302, 1e-301, 1e-300,
@ -257,7 +257,7 @@ parse_float(const uint8_t *const buf,
} }
i *= power_of_ten[308 + exponent]; i *= power_of_ten[308 + exponent];
} }
if(is_not_structural_or_whitespace(*p)) { if(is_not_structural_or_whitespace(*p) != 0u) {
return false; return false;
} }
double d = negative ? -i : i; double d = negative ? -i : i;
@ -265,7 +265,7 @@ parse_float(const uint8_t *const buf,
#ifdef JSON_TEST_NUMBERS // for unit testing #ifdef JSON_TEST_NUMBERS // for unit testing
foundFloat(d, buf + offset); foundFloat(d, buf + offset);
#endif #endif
return is_structural_or_whitespace(*p); return is_structural_or_whitespace(*p) != 0u;
} }
// called by parse_number when we know that the output is an integer, // called by parse_number when we know that the output is an integer,
@ -336,7 +336,7 @@ static never_inline bool parse_large_integer(const uint8_t *const buf,
#ifdef JSON_TEST_NUMBERS // for unit testing #ifdef JSON_TEST_NUMBERS // for unit testing
foundInteger(signed_answer, buf + offset); foundInteger(signed_answer, buf + offset);
#endif #endif
return is_structural_or_whitespace(*p); return is_structural_or_whitespace(*p) != 0u;
} }
@ -513,7 +513,7 @@ static really_inline bool parse_number(const uint8_t *const buf,
foundInteger(i, buf + offset); foundInteger(i, buf + offset);
#endif #endif
} }
return is_structural_or_whitespace(*p); return is_structural_or_whitespace(*p) != 0u;
#endif // SIMDJSON_SKIPNUMBERPARSING #endif // SIMDJSON_SKIPNUMBERPARSING
} }

View File

@ -1,15 +1,15 @@
#ifndef SIMDJSON_PARSEDJSON_H #ifndef SIMDJSON_PARSEDJSON_H
#define SIMDJSON_PARSEDJSON_H #define SIMDJSON_PARSEDJSON_H
#include <math.h> #include <cinttypes>
#include <inttypes.h> #include <cmath>
#include <string.h> #include <cstring>
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
#include "simdjson/portability.h"
#include "simdjson/jsonformatutils.h"
#include "simdjson/common_defs.h" #include "simdjson/common_defs.h"
#include "simdjson/jsonformatutils.h"
#include "simdjson/portability.h"
#define JSONVALUEMASK 0xFFFFFFFFFFFFFF #define JSONVALUEMASK 0xFFFFFFFFFFFFFF
@ -193,7 +193,7 @@ public:
private: private:
iterator& operator=(const iterator& other) ; iterator& operator=(const iterator& other) = delete ;
ParsedJson &pj; ParsedJson &pj;
size_t depth; size_t depth;
@ -204,13 +204,13 @@ private:
scopeindex_t *depthindex; scopeindex_t *depthindex;
}; };
size_t bytecapacity; // indicates how many bits are meant to be supported size_t bytecapacity{0}; // indicates how many bits are meant to be supported
size_t depthcapacity; // how deep we can go size_t depthcapacity{0}; // how deep we can go
size_t tapecapacity; size_t tapecapacity{0};
size_t stringcapacity; size_t stringcapacity{0};
uint32_t current_loc; uint32_t current_loc{0};
uint32_t n_structural_indexes; uint32_t n_structural_indexes{0};
uint32_t *structural_indexes; uint32_t *structural_indexes;
@ -224,28 +224,28 @@ private:
uint8_t *string_buf; // should be at least bytecapacity uint8_t *string_buf; // should be at least bytecapacity
uint8_t *current_string_buf_loc; uint8_t *current_string_buf_loc;
bool isvalid; bool isvalid{false};
private : private :
// we don't want the default constructor to be called // we don't want the default constructor to be called
ParsedJson(const ParsedJson & p); // we don't want the default constructor to be called ParsedJson(const ParsedJson & p) = delete; // we don't want the default constructor to be called
// we don't want the assignment to be called // we don't want the assignment to be called
ParsedJson & operator=(const ParsedJson&o); ParsedJson & operator=(const ParsedJson&o) = delete;
}; };
// dump bits low to high // dump bits low to high
inline void dumpbits_always(uint64_t v, const std::string &msg) { inline void dumpbits_always(uint64_t v, const std::string &msg) {
for (uint32_t i = 0; i < 64; i++) { for (uint32_t i = 0; i < 64; i++) {
std::cout << (((v >> (uint64_t)i) & 0x1ULL) ? "1" : "_"); std::cout << (((v >> (uint64_t)i) & 0x1ULL) != 0u ? "1" : "_");
} }
std::cout << " " << msg.c_str() << "\n"; std::cout << " " << msg.c_str() << "\n";
} }
inline void dumpbits32_always(uint32_t v, const std::string &msg) { inline void dumpbits32_always(uint32_t v, const std::string &msg) {
for (uint32_t i = 0; i < 32; i++) { for (uint32_t i = 0; i < 32; i++) {
std::cout << (((v >> (uint32_t)i) & 0x1ULL) ? "1" : "_"); std::cout << (((v >> (uint32_t)i) & 0x1ULL) != 0u ? "1" : "_");
} }
std::cout << " " << msg.c_str() << "\n"; std::cout << " " << msg.c_str() << "\n";
} }

View File

@ -42,8 +42,8 @@ static inline int hamming(uint64_t input_num) {
} }
#else #else
#include <x86intrin.h>
#include <cstdint> #include <cstdint>
#include <x86intrin.h>
static inline bool add_overflow(uint64_t value1, uint64_t value2, uint64_t *result) { static inline bool add_overflow(uint64_t value1, uint64_t value2, uint64_t *result) {
return __builtin_uaddll_overflow(value1, value2, (unsigned long long*)result); return __builtin_uaddll_overflow(value1, value2, (unsigned long long*)result);
@ -86,7 +86,8 @@ static inline void *aligned_malloc(size_t alignment, size_t size) {
#else #else
// somehow, if this is used before including "x86intrin.h", it creates an // somehow, if this is used before including "x86intrin.h", it creates an
// implicit defined warning. // implicit defined warning.
if (posix_memalign(&p, alignment, size) != 0) return NULL; if (posix_memalign(&p, alignment, size) != 0) { return nullptr;
}
#endif #endif
return p; return p;
} }
@ -114,7 +115,8 @@ static inline void _mm256_storeu2_m128i(__m128i *__addr_hi, __m128i *__addr_lo,
static inline void aligned_free(void *memblock) { static inline void aligned_free(void *memblock) {
if(memblock == NULL) return; if(memblock == nullptr) { return;
}
#ifdef _MSC_VER #ifdef _MSC_VER
_aligned_free(memblock); _aligned_free(memblock);
#elif defined(__MINGW32__) || defined(__MINGW64__) #elif defined(__MINGW32__) || defined(__MINGW64__)
@ -124,4 +126,4 @@ static inline void aligned_free(void *memblock) {
#endif #endif
} }
#endif /* end of include PORTABILITY_H */ #endif // SIMDJSON_PORTABILITY_H

View File

@ -34989,7 +34989,7 @@ static const unsigned char mask128_epi32[] = {
#ifdef __AVX2__ #ifdef __AVX2__
#include <stdint.h> #include <cstdint>
static const uint32_t mask256_epi32[] = { static const uint32_t mask256_epi32[] = {
0, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 7, 0, 2, 3, 4, 5, 6, 7, 7, 2, 0, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 7, 0, 2, 3, 4, 5, 6, 7, 7, 2,

View File

@ -2,7 +2,7 @@
#ifndef SIMDJSON_SIMDUTF8CHECK_H #ifndef SIMDJSON_SIMDUTF8CHECK_H
#define SIMDJSON_SIMDUTF8CHECK_H #define SIMDJSON_SIMDUTF8CHECK_H
#include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
@ -168,7 +168,7 @@ static struct avx_processed_utf_bytes
avxcheckUTF8Bytes(__m256i current_bytes, avxcheckUTF8Bytes(__m256i current_bytes,
struct avx_processed_utf_bytes *previous, struct avx_processed_utf_bytes *previous,
__m256i *has_error) { __m256i *has_error) {
struct avx_processed_utf_bytes pb; struct avx_processed_utf_bytes pb{};
avx_count_nibbles(current_bytes, &pb); avx_count_nibbles(current_bytes, &pb);
avxcheckSmallerThan0xF4(current_bytes, has_error); avxcheckSmallerThan0xF4(current_bytes, has_error);

View File

@ -1,5 +1,5 @@
#ifndef SIMDJSON_STAGE34_UNIFIED_H #ifndef SIMDJSON_STAGE2_BUILD_TAPE_H
#define SIMDJSON_STAGE34_UNIFIED_H #define SIMDJSON_STAGE2_BUILD_TAPE_H
#include "simdjson/common_defs.h" #include "simdjson/common_defs.h"
#include "simdjson/parsedjson.h" #include "simdjson/parsedjson.h"

View File

@ -2,8 +2,8 @@
#define SIMDJSON_STRINGPARSING_H #define SIMDJSON_STRINGPARSING_H
#include "simdjson/common_defs.h" #include "simdjson/common_defs.h"
#include "simdjson/parsedjson.h"
#include "simdjson/jsoncharutils.h" #include "simdjson/jsoncharutils.h"
#include "simdjson/parsedjson.h"
// begin copypasta // begin copypasta
@ -73,9 +73,9 @@ really_inline bool parse_string(const uint8_t *buf, UNUSED size_t len,
#endif #endif
while (1) { while (1) {
__m256i v = _mm256_loadu_si256((const __m256i *)(src)); __m256i v = _mm256_loadu_si256((const __m256i *)(src));
uint32_t bs_bits = auto bs_bits =
(uint32_t)_mm256_movemask_epi8(_mm256_cmpeq_epi8(v, _mm256_set1_epi8('\\'))); (uint32_t)_mm256_movemask_epi8(_mm256_cmpeq_epi8(v, _mm256_set1_epi8('\\')));
uint32_t quote_bits = auto quote_bits =
(uint32_t)_mm256_movemask_epi8(_mm256_cmpeq_epi8(v, _mm256_set1_epi8('"'))); (uint32_t)_mm256_movemask_epi8(_mm256_cmpeq_epi8(v, _mm256_set1_epi8('"')));
#define CHECKUNESCAPED #define CHECKUNESCAPED
// All Unicode characters may be placed within the // All Unicode characters may be placed within the
@ -102,7 +102,7 @@ really_inline bool parse_string(const uint8_t *buf, UNUSED size_t len,
pj.current_string_buf_loc = dst + quote_dist + 1; // the +1 is due to the 0 value pj.current_string_buf_loc = dst + quote_dist + 1; // the +1 is due to the 0 value
#ifdef CHECKUNESCAPED #ifdef CHECKUNESCAPED
// check that there is no unescaped char before the quote // check that there is no unescaped char before the quote
uint32_t unescaped_bits = (uint32_t)_mm256_movemask_epi8(unescaped_vec); auto unescaped_bits = (uint32_t)_mm256_movemask_epi8(unescaped_vec);
bool is_ok = ((quote_bits - 1) & (~ quote_bits) & unescaped_bits) == 0; bool is_ok = ((quote_bits - 1) & (~ quote_bits) & unescaped_bits) == 0;
#ifdef JSON_TEST_STRINGS // for unit testing #ifdef JSON_TEST_STRINGS // for unit testing
if(is_ok) foundString(buf + offset,start_of_string,pj.current_string_buf_loc - 1); if(is_ok) foundString(buf + offset,start_of_string,pj.current_string_buf_loc - 1);
@ -115,11 +115,11 @@ really_inline bool parse_string(const uint8_t *buf, UNUSED size_t len,
#endif // JSON_TEST_STRINGS #endif // JSON_TEST_STRINGS
return true; return true;
#endif //CHECKUNESCAPED #endif //CHECKUNESCAPED
} else if (quote_dist > bs_dist) { } if (quote_dist > bs_dist) {
uint8_t escape_char = src[bs_dist + 1]; uint8_t escape_char = src[bs_dist + 1];
#ifdef CHECKUNESCAPED #ifdef CHECKUNESCAPED
// we are going to need the unescaped_bits to check for unescaped chars // we are going to need the unescaped_bits to check for unescaped chars
uint32_t unescaped_bits = (uint32_t)_mm256_movemask_epi8(unescaped_vec); auto unescaped_bits = (uint32_t)_mm256_movemask_epi8(unescaped_vec);
if(((bs_bits - 1) & (~ bs_bits) & unescaped_bits) != 0) { if(((bs_bits - 1) & (~ bs_bits) & unescaped_bits) != 0) {
#ifdef JSON_TEST_STRINGS // for unit testing #ifdef JSON_TEST_STRINGS // for unit testing
foundBadString(buf + offset); foundBadString(buf + offset);
@ -145,7 +145,7 @@ really_inline bool parse_string(const uint8_t *buf, UNUSED size_t len,
// note this may reach beyond the part of the buffer we've actually // note this may reach beyond the part of the buffer we've actually
// seen. I think this is ok // seen. I think this is ok
uint8_t escape_result = escape_map[escape_char]; uint8_t escape_result = escape_map[escape_char];
if (!escape_result) { if (escape_result == 0u) {
#ifdef JSON_TEST_STRINGS // for unit testing #ifdef JSON_TEST_STRINGS // for unit testing
foundBadString(buf + offset); foundBadString(buf + offset);
#endif // JSON_TEST_STRINGS #endif // JSON_TEST_STRINGS

View File

@ -1,6 +1,6 @@
#include "simdjson/jsonioutil.h" #include "simdjson/jsonioutil.h"
#include <cstring> #include <cstring>
#include <stdlib.h> #include <cstdlib>
char * allocate_padded_buffer(size_t length) { char * allocate_padded_buffer(size_t length) {
// we could do a simple malloc // we could do a simple malloc
@ -13,18 +13,19 @@ char * allocate_padded_buffer(size_t length) {
#elif defined(__MINGW32__) || defined(__MINGW64__) #elif defined(__MINGW32__) || defined(__MINGW64__)
padded_buffer = __mingw_aligned_malloc(totalpaddedlength, 64); padded_buffer = __mingw_aligned_malloc(totalpaddedlength, 64);
#else #else
if (posix_memalign((void **)&padded_buffer, 64, totalpaddedlength) != 0) return NULL; if (posix_memalign((void **)&padded_buffer, 64, totalpaddedlength) != 0) { return nullptr;
}
#endif #endif
return padded_buffer; return padded_buffer;
} }
std::string_view get_corpus(std::string filename) { std::string_view get_corpus(const std::string& filename) {
std::FILE *fp = std::fopen(filename.c_str(), "rb"); std::FILE *fp = std::fopen(filename.c_str(), "rb");
if (fp) { if (fp != nullptr) {
std::fseek(fp, 0, SEEK_END); std::fseek(fp, 0, SEEK_END);
size_t len = std::ftell(fp); size_t len = std::ftell(fp);
char * buf = allocate_padded_buffer(len); char * buf = allocate_padded_buffer(len);
if(buf == NULL) { if(buf == nullptr) {
std::fclose(fp); std::fclose(fp);
throw std::runtime_error("could not allocate memory"); throw std::runtime_error("could not allocate memory");
} }

View File

@ -1,5 +1,5 @@
#include <cstdint>
#include "simdjson/portability.h" #include "simdjson/portability.h"
#include <cstdint>
#ifndef __AVX2__ #ifndef __AVX2__

View File

@ -7,10 +7,10 @@
#endif #endif
extern bool json_parse(const char * buf, size_t len, ParsedJson &pj, bool reallocifneeded);
extern bool json_parse(const std::string_view &s, ParsedJson &pj, bool reallocifneeded);
extern ParsedJson build_parsed_json(const char * buf, size_t len, bool reallocifneeded);
extern ParsedJson build_parsed_json(const std::string_view &s, bool reallocifneeded);
// parse a document found in buf, need to preallocate ParsedJson. // parse a document found in buf, need to preallocate ParsedJson.
@ -34,7 +34,8 @@ bool json_parse(const uint8_t *buf, size_t len, ParsedJson &pj, bool reallocifne
if ( (reinterpret_cast<uintptr_t>(buf + len - 1) % pagesize ) < SIMDJSON_PADDING ) { if ( (reinterpret_cast<uintptr_t>(buf + len - 1) % pagesize ) < SIMDJSON_PADDING ) {
const uint8_t *tmpbuf = buf; const uint8_t *tmpbuf = buf;
buf = (uint8_t *) allocate_padded_buffer(len); buf = (uint8_t *) allocate_padded_buffer(len);
if(buf == NULL) return false; if(buf == nullptr) { return false;
}
memcpy((void*)buf,tmpbuf,len); memcpy((void*)buf,tmpbuf,len);
reallocated = true; reallocated = true;
} }
@ -43,10 +44,12 @@ bool json_parse(const uint8_t *buf, size_t len, ParsedJson &pj, bool reallocifne
if (isok) { if (isok) {
isok = unified_machine(buf, len, pj); isok = unified_machine(buf, len, pj);
} else { } else {
if(reallocated) free((void*)buf); if(reallocated) { free((void*)buf);
}
return false; return false;
} }
if(reallocated) free((void*)buf); if(reallocated) { free((void*)buf);
}
return isok; return isok;
} }

View File

@ -1,34 +1,33 @@
#include "simdjson/parsedjson.h" #include "simdjson/parsedjson.h"
ParsedJson::ParsedJson() : bytecapacity(0), depthcapacity(0), tapecapacity(0), stringcapacity(0), ParsedJson::ParsedJson() :
current_loc(0), n_structural_indexes(0), structural_indexes(nullptr), tape(nullptr), containing_scope_offset(nullptr),
structural_indexes(NULL), tape(NULL), containing_scope_offset(NULL), ret_address(nullptr), string_buf(nullptr), current_string_buf_loc(nullptr) {}
ret_address(NULL), string_buf(NULL), current_string_buf_loc(NULL), isvalid(false) {}
ParsedJson::~ParsedJson() { ParsedJson::~ParsedJson() {
deallocate(); deallocate();
} }
ParsedJson::ParsedJson(ParsedJson && p) ParsedJson::ParsedJson(ParsedJson && p)
: bytecapacity(std::move(p.bytecapacity)), : bytecapacity(p.bytecapacity),
depthcapacity(std::move(p.depthcapacity)), depthcapacity(p.depthcapacity),
tapecapacity(std::move(p.tapecapacity)), tapecapacity(p.tapecapacity),
stringcapacity(std::move(p.stringcapacity)), stringcapacity(p.stringcapacity),
current_loc(std::move(p.current_loc)), current_loc(p.current_loc),
n_structural_indexes(std::move(p.n_structural_indexes)), n_structural_indexes(p.n_structural_indexes),
structural_indexes(std::move(p.structural_indexes)), structural_indexes(p.structural_indexes),
tape(std::move(p.tape)), tape(p.tape),
containing_scope_offset(std::move(p.containing_scope_offset)), containing_scope_offset(p.containing_scope_offset),
ret_address(std::move(p.ret_address)), ret_address(p.ret_address),
string_buf(std::move(p.string_buf)), string_buf(p.string_buf),
current_string_buf_loc(std::move(p.current_string_buf_loc)), current_string_buf_loc(p.current_string_buf_loc),
isvalid(std::move(p.isvalid)) { isvalid(p.isvalid) {
p.structural_indexes=NULL; p.structural_indexes=nullptr;
p.tape=NULL; p.tape=nullptr;
p.containing_scope_offset=NULL; p.containing_scope_offset=nullptr;
p.ret_address=NULL; p.ret_address=nullptr;
p.string_buf=NULL; p.string_buf=nullptr;
p.current_string_buf_loc=NULL; p.current_string_buf_loc=nullptr;
} }
@ -40,8 +39,9 @@ bool ParsedJson::allocateCapacity(size_t len, size_t maxdepth) {
return false; return false;
} }
if (len > 0) { if (len > 0) {
if ((len <= bytecapacity) && (depthcapacity < maxdepth)) if ((len <= bytecapacity) && (depthcapacity < maxdepth)) {
return true; return true;
}
deallocate(); deallocate();
} }
isvalid = false; isvalid = false;
@ -59,14 +59,19 @@ bool ParsedJson::allocateCapacity(size_t len, size_t maxdepth) {
#else #else
ret_address = new char[maxdepth]; ret_address = new char[maxdepth];
#endif #endif
if ((string_buf == NULL) || (tape == NULL) || if ((string_buf == nullptr) || (tape == nullptr) ||
(containing_scope_offset == NULL) || (ret_address == NULL) || (structural_indexes == NULL)) { (containing_scope_offset == nullptr) || (ret_address == nullptr) || (structural_indexes == nullptr)) {
std::cerr << "Could not allocate memory" << std::endl; std::cerr << "Could not allocate memory" << std::endl;
if(ret_address != NULL) delete[] ret_address; {delete[] ret_address;
if(containing_scope_offset != NULL) delete[] containing_scope_offset; }
if(tape != NULL) delete[] tape; {delete[] containing_scope_offset;
if(string_buf != NULL) delete[] string_buf; }
if(structural_indexes != NULL) delete[] structural_indexes; {delete[] tape;
}
{delete[] string_buf;
}
{delete[] structural_indexes;
}
return false; return false;
} }
@ -86,11 +91,16 @@ void ParsedJson::deallocate() {
depthcapacity = 0; depthcapacity = 0;
tapecapacity = 0; tapecapacity = 0;
stringcapacity = 0; stringcapacity = 0;
if(ret_address != NULL) delete[] ret_address; {delete[] ret_address;
if(containing_scope_offset != NULL) delete[] containing_scope_offset; }
if(tape != NULL) delete[] tape; {delete[] containing_scope_offset;
if(string_buf != NULL) delete[] string_buf; }
if(structural_indexes != NULL) delete[] structural_indexes; {delete[] tape;
}
{delete[] string_buf;
}
{delete[] structural_indexes;
}
isvalid = false; isvalid = false;
} }
@ -102,7 +112,8 @@ void ParsedJson::init() {
WARN_UNUSED WARN_UNUSED
bool ParsedJson::printjson(std::ostream &os) { bool ParsedJson::printjson(std::ostream &os) {
if(!isvalid) return false; if(!isvalid) { return false;
}
size_t tapeidx = 0; size_t tapeidx = 0;
uint64_t tape_val = tape[tapeidx]; uint64_t tape_val = tape[tapeidx];
uint8_t type = (tape_val >> 56); uint8_t type = (tape_val >> 56);
@ -120,7 +131,7 @@ bool ParsedJson::printjson(std::ostream &os) {
} }
tapeidx++; tapeidx++;
bool *inobject = new bool[depthcapacity]; bool *inobject = new bool[depthcapacity];
size_t *inobjectidx = new size_t[depthcapacity]; auto *inobjectidx = new size_t[depthcapacity];
int depth = 1; // only root at level 0 int depth = 1; // only root at level 0
inobjectidx[depth] = 0; inobjectidx[depth] = 0;
inobject[depth] = false; inobject[depth] = false;
@ -129,15 +140,18 @@ bool ParsedJson::printjson(std::ostream &os) {
uint64_t payload = tape_val & JSONVALUEMASK; uint64_t payload = tape_val & JSONVALUEMASK;
type = (tape_val >> 56); type = (tape_val >> 56);
if (!inobject[depth]) { if (!inobject[depth]) {
if ((inobjectidx[depth] > 0) && (type != ']')) if ((inobjectidx[depth] > 0) && (type != ']')) {
os << ","; os << ",";
}
inobjectidx[depth]++; inobjectidx[depth]++;
} else { // if (inobject) { } else { // if (inobject) {
if ((inobjectidx[depth] > 0) && ((inobjectidx[depth] & 1) == 0) && if ((inobjectidx[depth] > 0) && ((inobjectidx[depth] & 1) == 0) &&
(type != '}')) (type != '}')) {
os << ","; os << ",";
if (((inobjectidx[depth] & 1) == 1)) }
if (((inobjectidx[depth] & 1) == 1)) {
os << ":"; os << ":";
}
inobjectidx[depth]++; inobjectidx[depth]++;
} }
switch (type) { switch (type) {
@ -147,13 +161,15 @@ bool ParsedJson::printjson(std::ostream &os) {
os << '"'; os << '"';
break; break;
case 'l': // we have a long int case 'l': // we have a long int
if (tapeidx + 1 >= howmany) if (tapeidx + 1 >= howmany) {
return false; return false;
}
os << (int64_t)tape[++tapeidx]; os << (int64_t)tape[++tapeidx];
break; break;
case 'd': // we have a double case 'd': // we have a double
if (tapeidx + 1 >= howmany) if (tapeidx + 1 >= howmany) {
return false; return false;
}
double answer; double answer;
memcpy(&answer, &tape[++tapeidx], sizeof(answer)); memcpy(&answer, &tape[++tapeidx], sizeof(answer));
os << answer; os << answer;
@ -206,7 +222,8 @@ bool ParsedJson::printjson(std::ostream &os) {
WARN_UNUSED WARN_UNUSED
bool ParsedJson::dump_raw_tape(std::ostream &os) { bool ParsedJson::dump_raw_tape(std::ostream &os) {
if(!isvalid) return false; if(!isvalid) { return false;
}
size_t tapeidx = 0; size_t tapeidx = 0;
uint64_t tape_val = tape[tapeidx]; uint64_t tape_val = tape[tapeidx];
uint8_t type = (tape_val >> 56); uint8_t type = (tape_val >> 56);
@ -234,14 +251,16 @@ bool ParsedJson::dump_raw_tape(std::ostream &os) {
os << '\n'; os << '\n';
break; break;
case 'l': // we have a long int case 'l': // we have a long int
if (tapeidx + 1 >= howmany) if (tapeidx + 1 >= howmany) {
return false; return false;
}
os << "integer " << (int64_t)tape[++tapeidx] << "\n"; os << "integer " << (int64_t)tape[++tapeidx] << "\n";
break; break;
case 'd': // we have a double case 'd': // we have a double
os << "float "; os << "float ";
if (tapeidx + 1 >= howmany) if (tapeidx + 1 >= howmany) {
return false; return false;
}
double answer; double answer;
memcpy(&answer, &tape[++tapeidx], sizeof(answer)); memcpy(&answer, &tape[++tapeidx], sizeof(answer));
os << answer << '\n'; os << answer << '\n';

View File

@ -1,10 +1,11 @@
#include "simdjson/parsedjson.h" #include "simdjson/parsedjson.h"
#include "simdjson/common_defs.h" #include "simdjson/common_defs.h"
ParsedJson::iterator::iterator(ParsedJson &pj_) : pj(pj_), depth(0), location(0), tape_length(0), depthindex(NULL) { ParsedJson::iterator::iterator(ParsedJson &pj_) : pj(pj_), depth(0), location(0), tape_length(0), depthindex(nullptr) {
if(pj.isValid()) { if(pj.isValid()) {
depthindex = new scopeindex_t[pj.depthcapacity]; depthindex = new scopeindex_t[pj.depthcapacity];
if(depthindex == NULL) return; if(depthindex == nullptr) { return;
}
depthindex[0].start_of_scope = location; depthindex[0].start_of_scope = location;
current_val = pj.tape[location++]; current_val = pj.tape[location++];
current_type = (current_val >> 56); current_type = (current_val >> 56);
@ -29,9 +30,9 @@ ParsedJson::iterator::~iterator() {
ParsedJson::iterator::iterator(const iterator &o): ParsedJson::iterator::iterator(const iterator &o):
pj(o.pj), depth(o.depth), location(o.location), pj(o.pj), depth(o.depth), location(o.location),
tape_length(o.tape_length), current_type(o.current_type), tape_length(o.tape_length), current_type(o.current_type),
current_val(o.current_val), depthindex(NULL) { current_val(o.current_val), depthindex(nullptr) {
depthindex = new scopeindex_t[pj.depthcapacity]; depthindex = new scopeindex_t[pj.depthcapacity];
if(depthindex != NULL) { if(depthindex != nullptr) {
memcpy(o.depthindex, depthindex, pj.depthcapacity * sizeof(depthindex[0])); memcpy(o.depthindex, depthindex, pj.depthcapacity * sizeof(depthindex[0]));
} else { } else {
tape_length = 0; tape_length = 0;
@ -39,10 +40,10 @@ ParsedJson::iterator::iterator(const iterator &o):
} }
ParsedJson::iterator::iterator(iterator &&o): ParsedJson::iterator::iterator(iterator &&o):
pj(o.pj), depth(std::move(o.depth)), location(std::move(o.location)), pj(o.pj), depth(o.depth), location(o.location),
tape_length(std::move(o.tape_length)), current_type(std::move(o.current_type)), tape_length(o.tape_length), current_type(o.current_type),
current_val(std::move(o.current_val)), depthindex(std::move(o.depthindex)) { current_val(o.current_val), depthindex(o.depthindex) {
o.depthindex = NULL;// we take ownership o.depthindex = nullptr;// we take ownership
} }
WARN_UNUSED WARN_UNUSED
@ -106,12 +107,14 @@ uint8_t ParsedJson::iterator::get_type() const {
int64_t ParsedJson::iterator::get_integer() const { int64_t ParsedJson::iterator::get_integer() const {
if(location + 1 >= tape_length) return 0;// default value in case of error if(location + 1 >= tape_length) { return 0;// default value in case of error
}
return (int64_t) pj.tape[location + 1]; return (int64_t) pj.tape[location + 1];
} }
double ParsedJson::iterator::get_double() const { double ParsedJson::iterator::get_double() const {
if(location + 1 >= tape_length) return NAN;// default value in case of error if(location + 1 >= tape_length) { return NAN;// default value in case of error
}
double answer; double answer;
memcpy(&answer, & pj.tape[location + 1], sizeof(answer)); memcpy(&answer, & pj.tape[location + 1], sizeof(answer));
return answer; return answer;
@ -156,7 +159,8 @@ bool ParsedJson::iterator::move_to_key(const char * key) {
assert(is_string()); assert(is_string());
bool rightkey = (strcmp(get_string(),key)==0); bool rightkey = (strcmp(get_string(),key)==0);
next(); next();
if(rightkey) return true; if(rightkey) { return true;
}
} while(next()); } while(next());
assert(up());// not found assert(up());// not found
} }
@ -180,9 +184,10 @@ bool ParsedJson::iterator::move_to_key(const char * key) {
current_val = nextval; current_val = nextval;
current_type = nexttype; current_type = nexttype;
return true; return true;
} else { }
size_t increment = (current_type == 'd' || current_type == 'l') ? 2 : 1; size_t increment = (current_type == 'd' || current_type == 'l') ? 2 : 1;
if(location + increment >= tape_length) return false; if(location + increment >= tape_length) { return false;
}
uint64_t nextval = pj.tape[location + increment]; uint64_t nextval = pj.tape[location + increment];
uint8_t nexttype = (nextval >> 56); uint8_t nexttype = (nextval >> 56);
if((nexttype == ']') || (nexttype == '}')) { if((nexttype == ']') || (nexttype == '}')) {
@ -192,12 +197,13 @@ bool ParsedJson::iterator::move_to_key(const char * key) {
current_val = nextval; current_val = nextval;
current_type = nexttype; current_type = nexttype;
return true; return true;
}
} }
bool ParsedJson::iterator::prev() { bool ParsedJson::iterator::prev() {
if(location - 1 < depthindex[depth].start_of_scope) return false; if(location - 1 < depthindex[depth].start_of_scope) { return false;
}
location -= 1; location -= 1;
current_val = pj.tape[location]; current_val = pj.tape[location];
current_type = (current_val >> 56); current_type = (current_val >> 56);
@ -230,7 +236,8 @@ bool ParsedJson::iterator::move_to_key(const char * key) {
bool ParsedJson::iterator::down() { bool ParsedJson::iterator::down() {
if(location + 1 >= tape_length) return false; if(location + 1 >= tape_length) { return false;
}
if ((current_type == '[') || (current_type == '{')) { if ((current_type == '[') || (current_type == '{')) {
size_t npos = (current_val & JSONVALUEMASK); size_t npos = (current_val & JSONVALUEMASK);
if(npos == location + 2) { if(npos == location + 2) {
@ -254,7 +261,8 @@ void ParsedJson::iterator::to_start_scope() {
} }
bool ParsedJson::iterator::print(std::ostream &os, bool escape_strings) const { bool ParsedJson::iterator::print(std::ostream &os, bool escape_strings) const {
if(!isOk()) return false; if(!isOk()) { return false;
}
switch (current_type) { switch (current_type) {
case '"': // we have a string case '"': // we have a string
os << '"'; os << '"';

View File

@ -1,7 +1,7 @@
#include "simdjson/portability.h" #include "simdjson/portability.h"
#include <cassert>
#include "simdjson/common_defs.h" #include "simdjson/common_defs.h"
#include "simdjson/parsedjson.h" #include "simdjson/parsedjson.h"
#include <cassert>
#ifndef SIMDJSON_SKIPUTF8VALIDATION #ifndef SIMDJSON_SKIPUTF8VALIDATION
#define SIMDJSON_UTF8VALIDATE #define SIMDJSON_UTF8VALIDATE
@ -38,7 +38,7 @@ WARN_UNUSED
uint32_t base = 0; uint32_t base = 0;
#ifdef SIMDJSON_UTF8VALIDATE #ifdef SIMDJSON_UTF8VALIDATE
__m256i has_error = _mm256_setzero_si256(); __m256i has_error = _mm256_setzero_si256();
struct avx_processed_utf_bytes previous; struct avx_processed_utf_bytes previous{};
previous.rawbytes = _mm256_setzero_si256(); previous.rawbytes = _mm256_setzero_si256();
previous.high_nibbles = _mm256_setzero_si256(); previous.high_nibbles = _mm256_setzero_si256();
previous.carried_continuations = _mm256_setzero_si256(); previous.carried_continuations = _mm256_setzero_si256();
@ -130,7 +130,7 @@ WARN_UNUSED
uint32_t cnt = hamming(structurals); uint32_t cnt = hamming(structurals);
uint32_t next_base = base + cnt; uint32_t next_base = base + cnt;
while (structurals) { while (structurals != 0u) {
base_ptr[base + 0] = (uint32_t)idx - 64 + trailingzeroes(structurals); base_ptr[base + 0] = (uint32_t)idx - 64 + trailingzeroes(structurals);
structurals = structurals & (structurals - 1); structurals = structurals & (structurals - 1);
base_ptr[base + 1] = (uint32_t)idx - 64 + trailingzeroes(structurals); base_ptr[base + 1] = (uint32_t)idx - 64 + trailingzeroes(structurals);
@ -308,7 +308,7 @@ WARN_UNUSED
uint32_t cnt = hamming(structurals); uint32_t cnt = hamming(structurals);
uint32_t next_base = base + cnt; uint32_t next_base = base + cnt;
while (structurals) { while (structurals != 0u) {
base_ptr[base + 0] = (uint32_t)idx - 64 + trailingzeroes(structurals); base_ptr[base + 0] = (uint32_t)idx - 64 + trailingzeroes(structurals);
structurals = structurals & (structurals - 1); structurals = structurals & (structurals - 1);
base_ptr[base + 1] = (uint32_t)idx - 64 + trailingzeroes(structurals); base_ptr[base + 1] = (uint32_t)idx - 64 + trailingzeroes(structurals);
@ -412,7 +412,7 @@ WARN_UNUSED
} }
uint32_t cnt = hamming(structurals); uint32_t cnt = hamming(structurals);
uint32_t next_base = base + cnt; uint32_t next_base = base + cnt;
while (structurals) { while (structurals != 0u) {
base_ptr[base + 0] = (uint32_t)idx - 64 + trailingzeroes(structurals); base_ptr[base + 0] = (uint32_t)idx - 64 + trailingzeroes(structurals);
structurals = structurals & (structurals - 1); structurals = structurals & (structurals - 1);
base_ptr[base + 1] = (uint32_t)idx - 64 + trailingzeroes(structurals); base_ptr[base + 1] = (uint32_t)idx - 64 + trailingzeroes(structurals);
@ -435,7 +435,7 @@ WARN_UNUSED
pj.n_structural_indexes = base; pj.n_structural_indexes = base;
// a valid JSON file cannot have zero structural indexes - we should have found something // a valid JSON file cannot have zero structural indexes - we should have found something
if (!pj.n_structural_indexes) { if (pj.n_structural_indexes == 0u) {
return false; return false;
} }
if(base_ptr[pj.n_structural_indexes-1] > len) { if(base_ptr[pj.n_structural_indexes-1] > len) {
@ -449,7 +449,7 @@ WARN_UNUSED
base_ptr[pj.n_structural_indexes] = 0; // make it safe to dereference one beyond this array base_ptr[pj.n_structural_indexes] = 0; // make it safe to dereference one beyond this array
#ifdef SIMDJSON_UTF8VALIDATE #ifdef SIMDJSON_UTF8VALIDATE
return _mm256_testz_si256(has_error, has_error); return _mm256_testz_si256(has_error, has_error) != 0;
#else #else
return true; return true;
#endif #endif

View File

@ -142,7 +142,8 @@ bool unified_machine(const uint8_t *buf, size_t len, ParsedJson &pj) {
// this only applies to the JSON document made solely of the true value. // this only applies to the JSON document made solely of the true value.
// this will almost never be called in practice // this will almost never be called in practice
char * copy = (char *) malloc(len + SIMDJSON_PADDING); char * copy = (char *) malloc(len + SIMDJSON_PADDING);
if(copy == NULL) goto fail; if(copy == nullptr) { goto fail;
}
memcpy(copy, buf, len); memcpy(copy, buf, len);
copy[len] = '\0'; copy[len] = '\0';
if (!is_valid_true_atom((const uint8_t *)copy + idx)) { if (!is_valid_true_atom((const uint8_t *)copy + idx)) {
@ -158,7 +159,8 @@ bool unified_machine(const uint8_t *buf, size_t len, ParsedJson &pj) {
// this only applies to the JSON document made solely of the false value. // this only applies to the JSON document made solely of the false value.
// this will almost never be called in practice // this will almost never be called in practice
char * copy = (char *) malloc(len + SIMDJSON_PADDING); char * copy = (char *) malloc(len + SIMDJSON_PADDING);
if(copy == NULL) goto fail; if(copy == nullptr) { goto fail;
}
memcpy(copy, buf, len); memcpy(copy, buf, len);
copy[len] = '\0'; copy[len] = '\0';
if (!is_valid_false_atom((const uint8_t *)copy + idx)) { if (!is_valid_false_atom((const uint8_t *)copy + idx)) {
@ -174,7 +176,8 @@ bool unified_machine(const uint8_t *buf, size_t len, ParsedJson &pj) {
// this only applies to the JSON document made solely of the null value. // this only applies to the JSON document made solely of the null value.
// this will almost never be called in practice // this will almost never be called in practice
char * copy = (char *) malloc(len + SIMDJSON_PADDING); char * copy = (char *) malloc(len + SIMDJSON_PADDING);
if(copy == NULL) goto fail; if(copy == nullptr) { goto fail;
}
memcpy(copy, buf, len); memcpy(copy, buf, len);
copy[len] = '\0'; copy[len] = '\0';
if (!is_valid_null_atom((const uint8_t *)copy + idx)) { if (!is_valid_null_atom((const uint8_t *)copy + idx)) {
@ -199,7 +202,8 @@ bool unified_machine(const uint8_t *buf, size_t len, ParsedJson &pj) {
// this is done only for JSON documents made of a sole number // this is done only for JSON documents made of a sole number
// this will almost never be called in practice // this will almost never be called in practice
char * copy = (char *) malloc(len + SIMDJSON_PADDING); char * copy = (char *) malloc(len + SIMDJSON_PADDING);
if(copy == NULL) goto fail; if(copy == nullptr) { goto fail;
}
memcpy(copy, buf, len); memcpy(copy, buf, len);
copy[len] = '\0'; copy[len] = '\0';
if (!parse_number((const uint8_t *)copy, pj, idx, false)) { if (!parse_number((const uint8_t *)copy, pj, idx, false)) {
@ -214,7 +218,8 @@ bool unified_machine(const uint8_t *buf, size_t len, ParsedJson &pj) {
// this is done only for JSON documents made of a sole number // this is done only for JSON documents made of a sole number
// this will almost never be called in practice // this will almost never be called in practice
char * copy = (char *) malloc(len + SIMDJSON_PADDING); char * copy = (char *) malloc(len + SIMDJSON_PADDING);
if(copy == NULL) goto fail; if(copy == nullptr) { goto fail;
}
memcpy(copy, buf, len); memcpy(copy, buf, len);
copy[len] = '\0'; copy[len] = '\0';
if (!parse_number((const uint8_t *)copy, pj, idx, true)) { if (!parse_number((const uint8_t *)copy, pj, idx, true)) {

View File

@ -1,4 +1,4 @@
#include <assert.h> #include <cassert>
#include <cstring> #include <cstring>
#ifndef _MSC_VER #ifndef _MSC_VER
#include <dirent.h> #include <dirent.h>
@ -7,10 +7,10 @@
// Microsoft can't be bothered to provide standard utils. // Microsoft can't be bothered to provide standard utils.
#include <dirent_portable.h> #include <dirent_portable.h>
#endif #endif
#include <inttypes.h> #include <cinttypes>
#include <stdbool.h>
#include <stdio.h> #include <cstdio>
#include <stdlib.h> #include <cstdlib>
#include "simdjson/jsonparser.h" #include "simdjson/jsonparser.h"
@ -19,7 +19,7 @@
*/ */
static bool hasExtension(const char *filename, const char *extension) { static bool hasExtension(const char *filename, const char *extension) {
const char *ext = strrchr(filename, '.'); const char *ext = strrchr(filename, '.');
return (ext && !strcmp(ext, extension)); return ((ext != nullptr) && (strcmp(ext, extension) == 0));
} }
bool startsWith(const char *pre, const char *str) { bool startsWith(const char *pre, const char *str) {
@ -28,7 +28,7 @@ bool startsWith(const char *pre, const char *str) {
} }
bool contains(const char *pre, const char *str) { bool contains(const char *pre, const char *str) {
return (strstr(str, pre) != NULL); return (strstr(str, pre) != nullptr);
} }
@ -37,7 +37,7 @@ bool validate(const char *dirname) {
const char *extension = ".json"; const char *extension = ".json";
size_t dirlen = strlen(dirname); size_t dirlen = strlen(dirname);
struct dirent **entry_list; struct dirent **entry_list;
int c = scandir(dirname, &entry_list, 0, alphasort); int c = scandir(dirname, &entry_list, nullptr, alphasort);
if (c < 0) { if (c < 0) {
fprintf(stderr, "error accessing %s \n", dirname); fprintf(stderr, "error accessing %s \n", dirname);
return false; return false;
@ -47,14 +47,15 @@ bool validate(const char *dirname) {
return false; return false;
} }
bool * isfileasexpected = new bool[c]; bool * isfileasexpected = new bool[c];
for(int i = 0; i < c; i++) isfileasexpected[i] = true; for(int i = 0; i < c; i++) { isfileasexpected[i] = true;
}
size_t howmany = 0; size_t howmany = 0;
bool needsep = (strlen(dirname) > 1) && (dirname[strlen(dirname) - 1] != '/'); bool needsep = (strlen(dirname) > 1) && (dirname[strlen(dirname) - 1] != '/');
for (int i = 0; i < c; i++) { for (int i = 0; i < c; i++) {
const char *name = entry_list[i]->d_name; const char *name = entry_list[i]->d_name;
if (hasExtension(name, extension)) { if (hasExtension(name, extension)) {
printf("validating: file %s ", name); printf("validating: file %s ", name);
fflush(NULL); fflush(nullptr);
size_t filelen = strlen(name); size_t filelen = strlen(name);
char *fullpath = (char *)malloc(dirlen + filelen + 1 + 1); char *fullpath = (char *)malloc(dirlen + filelen + 1 + 1);
strcpy(fullpath, dirname); strcpy(fullpath, dirname);
@ -106,11 +107,13 @@ bool validate(const char *dirname) {
} else { } else {
fprintf(stderr, "There were problems! Consider reviewing the following files:\n"); fprintf(stderr, "There were problems! Consider reviewing the following files:\n");
for(int i = 0; i < c; i++) { for(int i = 0; i < c; i++) {
if(!isfileasexpected[i]) fprintf(stderr, "%s \n", entry_list[i]->d_name); if(!isfileasexpected[i]) { fprintf(stderr, "%s \n", entry_list[i]->d_name);
}
} }
} }
for (int i = 0; i < c; ++i) for (int i = 0; i < c; ++i) {
free(entry_list[i]); free(entry_list[i]);
}
free(entry_list); free(entry_list);
delete[] isfileasexpected; delete[] isfileasexpected;
return everythingfine; return everythingfine;

View File

@ -48,7 +48,7 @@ int main(int argc, char *argv[]) {
#ifndef _MSC_VER #ifndef _MSC_VER
int c; int c;
while ((c = getopt(argc, argv, "da")) != -1) while ((c = getopt(argc, argv, "da")) != -1) {
switch (c) { switch (c) {
case 'd': case 'd':
rawdump = true; rawdump = true;
@ -59,6 +59,7 @@ int main(int argc, char *argv[]) {
default: default:
abort(); abort();
} }
}
#else #else
int optind = 1; int optind = 1;
#endif #endif

View File

@ -39,7 +39,7 @@ struct stat_s {
bool valid; bool valid;
}; };
typedef struct stat_s stat_t; using stat_t = struct stat_s;