Skip to content

Commit

Permalink
refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
vztpv authored Sep 28, 2023
1 parent 32ace9c commit 40a3c74
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
63 changes: 34 additions & 29 deletions claujson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,7 @@ namespace claujson {
}

inline bool ConvertNumber(claujson::Value& data, char* text, size_t len, bool isFirst) {

std::unique_ptr<uint8_t[]> copy;

uint64_t temp[2] = { 0 };
Expand All @@ -1414,7 +1415,7 @@ namespace claujson {
auto x = simdjson_imple->parse_number(value, temp);

if (x != _simdjson::SUCCESS) {
log << warn << std::string_view((char*)value, 20) << "\n";
log << warn << StringView((char*)value, 20) << "\n";
log << warn << "parse number error. " << x << "\n";
return false; //ERROR("parse number error. ");
//throw "Error in Convert to parse number";
Expand Down Expand Up @@ -1450,14 +1451,26 @@ namespace claujson {

//try {
data.clear();

switch (buf[buf_idx]) {
case '"':
if (ConvertString(data, &buf[buf_idx], next_buf_idx - buf_idx)) {}
else {
goto ERR;
}
break;
case '-':
case '0':
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
if (ConvertNumber(data, &buf[buf_idx], next_buf_idx - buf_idx, token_idx == 0)) {}
else {
goto ERR;
}

break;
}
case 't':
{
if (!simdjson_imple->is_valid_true_atom(reinterpret_cast<uint8_t*>(&buf[buf_idx]), next_buf_idx - buf_idx)) {
Expand All @@ -1481,18 +1494,7 @@ namespace claujson {

data.set_null();
break;
case '-':
case '0':
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
if (ConvertNumber(data, &buf[buf_idx], next_buf_idx - buf_idx, token_idx == 0)) {}
else {
goto ERR;
}

break;
}

default:
log << warn << "convert error : " << (int)buf[buf_idx] << " " << buf[buf_idx] << "\n";
goto ERR; //ERROR("convert Error");
Expand Down Expand Up @@ -3619,6 +3621,7 @@ namespace claujson {

// token_arr_len >= 1


class Structured* global = _global.get();

size_t braceNum = 0;
Expand Down Expand Up @@ -3710,28 +3713,30 @@ namespace claujson {

if (state == 0) {
//find {, [, }, ]
bool pass = false;
for (size_t x = i; x < token_arr_len; ++x) {
size_t x = i;
for (; x < token_arr_len; ++x) {
switch (buf[imple->structural_indexes[token_arr_start + x]]) {
case '{':
case '[':
case '}':
case ']':
pass = true;
goto found;
break;
}
if (pass) {
if (is_key) { // "abc" : 123, "def" : 456 }
// i x
size_t sz = (x - i) / 3;
nowUT->reserve_data_list(nowUT->get_data_size() + sz);
}
else { // 123, 456 ] or 123, 456, 789 ]
// i x i x
size_t sz = (x - i) / 2 + 1;
nowUT->reserve_data_list(nowUT->get_data_size() + sz);
}
break;

}

found:
{
if (is_key) { // "abc" : 123, "def" : 456 }
// i x
size_t sz = (x - i) / 3;
nowUT->reserve_data_list(nowUT->get_data_size() + sz);
}
else { // 123, 456 ] or 123, 456, 789 ]
// i x i x
size_t sz = (x - i) / 2 + 1;
nowUT->reserve_data_list(nowUT->get_data_size() + sz);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,11 @@ int main(int argc, char* argv[])
auto dur = std::chrono::duration_cast<std::chrono::milliseconds>(b - a);
std::cout << "total " << dur.count() << "ms\n";


//
claujson::clean(j);

return 0;

//
//claujson::save("test12.txt", j);
claujson::save_parallel("test34.json", j, thr_num);
std::cout << "save_parallel " <<
Expand Down

0 comments on commit 40a3c74

Please sign in to comment.