Style uniformization (#238)

* massive clang-format -style=LLVM

* naming harmonization

* adding commentary about sysinfoapi.h
This commit is contained in:
ioioioio
2019-07-30 17:18:10 -04:00
committed by Daniel Lemire
parent 065805d6e1
commit c2eea8abba
57 changed files with 3617 additions and 3389 deletions
+222 -217
View File
@@ -1,264 +1,269 @@
#include "simdjson/parsedjson.h"
#include "simdjson/common_defs.h"
#include "simdjson/parsedjson.h"
#include <iterator>
namespace simdjson {
ParsedJson::iterator::iterator(ParsedJson &pj_) : pj(pj_), depth(0), location(0), tape_length(0), depthindex(nullptr) {
if(!pj.isValid()) {
throw InvalidJSON();
}
depthindex = new scopeindex_t[pj.depthcapacity];
// memory allocation would throw
//if(depthindex == nullptr) {
// return;
//}
depthindex[0].start_of_scope = location;
current_val = pj.tape[location++];
current_type = (current_val >> 56);
depthindex[0].scope_type = current_type;
if (current_type == 'r') {
tape_length = current_val & JSONVALUEMASK;
if(location < tape_length) {
current_val = pj.tape[location];
current_type = (current_val >> 56);
depth++;
depthindex[depth].start_of_scope = location;
depthindex[depth].scope_type = current_type;
}
} else {
// should never happen
throw InvalidJSON();
}
}
ParsedJson::iterator::~iterator() {
delete[] depthindex;
}
ParsedJson::iterator::iterator(const iterator &o):
pj(o.pj), depth(o.depth), location(o.location),
tape_length(0), current_type(o.current_type),
current_val(o.current_val), depthindex(nullptr) {
depthindex = new scopeindex_t[pj.depthcapacity];
// allocation might throw
memcpy(depthindex, o.depthindex, pj.depthcapacity * sizeof(depthindex[0]));
tape_length = o.tape_length;
}
ParsedJson::iterator::iterator(iterator &&o):
pj(o.pj), depth(o.depth), location(o.location),
tape_length(o.tape_length), current_type(o.current_type),
current_val(o.current_val), depthindex(o.depthindex) {
o.depthindex = nullptr;// we take ownership
}
bool ParsedJson::iterator::print(std::ostream &os, bool escape_strings) const {
if(!isOk()) {
return false;
ParsedJson::Iterator::Iterator(ParsedJson &pj_)
: pj(pj_), depth(0), location(0), tape_length(0), depth_index(nullptr) {
if (!pj.is_valid()) {
throw InvalidJSON();
}
depth_index = new scopeindex_t[pj.depth_capacity];
// memory allocation would throw
// if(depth_index == nullptr) {
// return;
//}
depth_index[0].start_of_scope = location;
current_val = pj.tape[location++];
current_type = (current_val >> 56);
depth_index[0].scope_type = current_type;
if (current_type == 'r') {
tape_length = current_val & JSON_VALUE_MASK;
if (location < tape_length) {
current_val = pj.tape[location];
current_type = (current_val >> 56);
depth++;
depth_index[depth].start_of_scope = location;
depth_index[depth].scope_type = current_type;
}
switch (current_type) {
case '"': // we have a string
} else {
// should never happen
throw InvalidJSON();
}
}
ParsedJson::Iterator::~Iterator() { delete[] depth_index; }
ParsedJson::Iterator::Iterator(const Iterator &o)
: pj(o.pj), depth(o.depth), location(o.location), tape_length(0),
current_type(o.current_type), current_val(o.current_val),
depth_index(nullptr) {
depth_index = new scopeindex_t[pj.depth_capacity];
// allocation might throw
memcpy(depth_index, o.depth_index,
pj.depth_capacity * sizeof(depth_index[0]));
tape_length = o.tape_length;
}
ParsedJson::Iterator::Iterator(Iterator &&o)
: pj(o.pj), depth(o.depth), location(o.location),
tape_length(o.tape_length), current_type(o.current_type),
current_val(o.current_val), depth_index(o.depth_index) {
o.depth_index = nullptr; // we take ownership
}
bool ParsedJson::Iterator::print(std::ostream &os, bool escape_strings) const {
if (!is_ok()) {
return false;
}
switch (current_type) {
case '"': // we have a string
os << '"';
if(escape_strings) {
print_with_escapes(get_string(), os, get_string_length());
if (escape_strings) {
print_with_escapes(get_string(), os, get_string_length());
} else {
// was: os << get_string();, but given that we can include null chars, we have to do something crazier:
std::copy(get_string(), get_string() + get_string_length(), std::ostream_iterator<char>(os));
// was: os << get_string();, but given that we can include null chars, we
// have to do something crazier:
std::copy(get_string(), get_string() + get_string_length(),
std::ostream_iterator<char>(os));
}
os << '"';
break;
case 'l': // we have a long int
case 'l': // we have a long int
os << get_integer();
break;
case 'd':
case 'd':
os << get_double();
break;
case 'n': // we have a null
case 'n': // we have a null
os << "null";
break;
case 't': // we have a true
case 't': // we have a true
os << "true";
break;
case 'f': // we have a false
case 'f': // we have a false
os << "false";
break;
case '{': // we have an object
case '}': // we end an object
case '[': // we start an array
case ']': // we end an array
case '{': // we have an object
case '}': // we end an object
case '[': // we start an array
case ']': // we end an array
os << static_cast<char>(current_type);
break;
default:
default:
return false;
}
return true;
}
return true;
}
bool ParsedJson::iterator::move_to(const char * pointer, uint32_t length) {
char* new_pointer = nullptr;
if (pointer[0] == '#') {
// Converting fragment representation to string representation
new_pointer = new char[length];
uint32_t new_length = 0;
for (uint32_t i = 1; i < length; i++) {
if (pointer[i] == '%' && pointer[i+1] == 'x') {
try {
int fragment = std::stoi(std::string(&pointer[i+2], 2), nullptr, 16);
if (fragment == '\\' || fragment == '"' || (fragment <= 0x1F)) {
// escaping the character
new_pointer[new_length] = '\\';
new_length++;
}
new_pointer[new_length] = fragment;
i += 3;
}
catch(std::invalid_argument& e) {
delete[] new_pointer;
return false; // the fragment is invalid
bool ParsedJson::Iterator::move_to(const char *pointer, uint32_t length) {
char *new_pointer = nullptr;
if (pointer[0] == '#') {
// Converting fragment representation to string representation
new_pointer = new char[length];
uint32_t new_length = 0;
for (uint32_t i = 1; i < length; i++) {
if (pointer[i] == '%' && pointer[i + 1] == 'x') {
try {
int fragment =
std::stoi(std::string(&pointer[i + 2], 2), nullptr, 16);
if (fragment == '\\' || fragment == '"' || (fragment <= 0x1F)) {
// escaping the character
new_pointer[new_length] = '\\';
new_length++;
}
new_pointer[new_length] = fragment;
i += 3;
} catch (std::invalid_argument &e) {
delete[] new_pointer;
return false; // the fragment is invalid
}
else {
new_pointer[new_length] = pointer[i];
}
new_length++;
} else {
new_pointer[new_length] = pointer[i];
}
length = new_length;
pointer = new_pointer;
new_length++;
}
// saving the current state
size_t depth_s = depth;
size_t location_s = location;
uint8_t current_type_s = current_type;
uint64_t current_val_s = current_val;
scopeindex_t *depthindex_s = depthindex;
rewind(); // The json pointer is used from the root of the document.
length = new_length;
pointer = new_pointer;
}
bool found = relative_move_to(pointer, length);
delete[] new_pointer;
// saving the current state
size_t depth_s = depth;
size_t location_s = location;
uint8_t current_type_s = current_type;
uint64_t current_val_s = current_val;
scopeindex_t *depth_index_s = depth_index;
if (!found) {
// since the pointer has found nothing, we get back to the original position.
depth = depth_s;
location = location_s;
current_type = current_type_s;
current_val = current_val_s;
depthindex = depthindex_s;
}
rewind(); // The json pointer is used from the root of the document.
return found;
bool found = relative_move_to(pointer, length);
delete[] new_pointer;
if (!found) {
// since the pointer has found nothing, we get back to the original
// position.
depth = depth_s;
location = location_s;
current_type = current_type_s;
current_val = current_val_s;
depth_index = depth_index_s;
}
return found;
}
bool ParsedJson::iterator::relative_move_to(const char * pointer, uint32_t length) {
if (length == 0) {
// returns the whole document
return true;
}
bool ParsedJson::Iterator::relative_move_to(const char *pointer,
uint32_t length) {
if (length == 0) {
// returns the whole document
return true;
}
if (pointer[0] != '/') {
// '/' must be the first character
if (pointer[0] != '/') {
// '/' must be the first character
return false;
}
// finding the key in an object or the index in an array
std::string key_or_index;
uint32_t offset = 1;
// checking for the "-" case
if (is_array() && pointer[1] == '-') {
if (length != 2) {
// the pointer must be exactly "/-"
// there can't be anything more after '-' as an index
return false;
}
key_or_index = '-';
offset = length; // will skip the loop coming right after
}
// finding the key in an object or the index in an array
std::string key_or_index;
uint32_t offset = 1;
// We either transform the first reference token to a valid json key
// or we make sure it is a valid index in an array.
for (; offset < length; offset++) {
if (pointer[offset] == '/') {
// beginning of the next key or index
break;
}
if (is_array() && (pointer[offset] < '0' || pointer[offset] > '9')) {
// the index of an array must be an integer
// we also make sure std::stoi won't discard whitespaces later
return false;
}
if (pointer[offset] == '~') {
// "~1" represents "/"
if (pointer[offset + 1] == '1') {
key_or_index += '/';
offset++;
continue;
}
// "~0" represents "~"
if (pointer[offset + 1] == '0') {
key_or_index += '~';
offset++;
continue;
}
}
if (pointer[offset] == '\\') {
if (pointer[offset + 1] == '\\' || pointer[offset + 1] == '"' ||
(pointer[offset + 1] <= 0x1F)) {
key_or_index += pointer[offset + 1];
offset++;
continue;
}
return false; // invalid escaped character
}
if (pointer[offset] == '\"') {
// unescaped quote character. this is an invalid case.
// lets do nothing and assume most pointers will be valid.
// it won't find any corresponding json key anyway.
// return false;
}
key_or_index += pointer[offset];
}
// checking for the "-" case
if (is_array() && pointer[1] == '-') {
if (length != 2) {
// the pointer must be exactly "/-"
// there can't be anything more after '-' as an index
bool found = false;
if (is_object()) {
if (move_to_key(key_or_index.c_str(), key_or_index.length())) {
found = relative_move_to(pointer + offset, length - offset);
}
} else if (is_array()) {
if (key_or_index == "-") { // handling "-" case first
if (down()) {
while (next())
; // moving to the end of the array
// moving to the nonexistent value right after...
size_t npos;
if ((current_type == '[') || (current_type == '{')) {
// we need to jump
npos = (current_val & JSON_VALUE_MASK);
} else {
npos =
location + ((current_type == 'd' || current_type == 'l') ? 2 : 1);
}
location = npos;
current_val = pj.tape[npos];
current_type = (current_val >> 56);
return true; // how could it fail ?
}
} else { // regular numeric index
// The index can't have a leading '0'
if (key_or_index[0] == '0' && key_or_index.length() > 1) {
return false;
}
key_or_index = '-';
offset = length; // will skip the loop coming right after
}
// We either transform the first reference token to a valid json key
// or we make sure it is a valid index in an array.
for (; offset < length ; offset++) {
if (pointer[offset] == '/') {
// beginning of the next key or index
break;
}
if (is_array() && (pointer[offset] < '0' || pointer[offset] > '9')) {
// the index of an array must be an integer
// we also make sure std::stoi won't discard whitespaces later
// it cannot be empty
if (key_or_index.length() == 0) {
return false;
}
if (pointer[offset] == '~') {
// "~1" represents "/"
if (pointer[offset+1] == '1') {
key_or_index += '/';
offset++;
continue;
}
// "~0" represents "~"
if (pointer[offset+1] == '0') {
key_or_index += '~';
offset++;
continue;
}
// we already checked the index contains only valid digits
uint32_t index = std::stoi(key_or_index);
if (move_to_index(index)) {
found = relative_move_to(pointer + offset, length - offset);
}
if (pointer[offset] == '\\') {
if (pointer[offset+1] == '\\' || pointer[offset+1] == '"' || (pointer[offset+1] <= 0x1F)) {
key_or_index += pointer[offset+1];
offset++;
continue;
}
return false; // invalid escaped character
}
if (pointer[offset] == '\"') {
// unescaped quote character. this is an invalid case.
// lets do nothing and assume most pointers will be valid.
// it won't find any corresponding json key anyway.
// return false;
}
key_or_index += pointer[offset];
}
}
bool found = false;
if (is_object()) {
if (move_to_key(key_or_index.c_str(), key_or_index.length())) {
found = relative_move_to(pointer+offset, length-offset);
}
}
else if(is_array()) {
if (key_or_index == "-") { // handling "-" case first
if (down()) {
while(next()); // moving to the end of the array
// moving to the nonexistent value right after...
size_t npos;
if ((current_type == '[') || (current_type == '{')) {
// we need to jump
npos = ( current_val & JSONVALUEMASK);
} else {
npos = location + ((current_type == 'd' || current_type == 'l') ? 2 : 1);
}
location = npos;
current_val = pj.tape[npos];
current_type = (current_val >> 56);
return true; // how could it fail ?
}
} else { // regular numeric index
// The index can't have a leading '0'
if (key_or_index[0] == '0' && key_or_index.length() > 1) {
return false;
}
// it cannot be empty
if (key_or_index.length() == 0) {
return false;
}
// we already checked the index contains only valid digits
uint32_t index = std::stoi(key_or_index);
if (move_to_index(index)) {
found = relative_move_to(pointer+offset, length-offset);
}
}
}
return found;
}
return found;
}
} // namespace simdjson