Skip to content

Commit 352dd5e

Browse files
atomheartotherlemire
authored andcommitted
Change parse_json return type from bool to int (simdjson#82)
* Added simdjerr namespace * Updated jsonparser files * updated stage1 and stage2 * removed stage2 inline function * Added forgotten return statements * Updated tools and benchmarks * Corrected parenthesis * Removed extra = * Accidentally undid reinterpret_cast * Better comments, undid a header name fuckup * Added an errorMsg method, updated readme * Removed useless header from stage2 * Updated single-header file * added simdjerr.cpp contents to simdjson.cpp * Made single header version work * Updated singleheader test, fixed simdjson.cpp * Renamed simdjerr namespace and files to simdjson * Updating the amalgamation.
1 parent 10b6b04 commit 352dd5e

21 files changed

+271
-252
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121

2222
- run:
2323
name: Running tests (gcc)
24-
command: make quiettest
24+
command: make quiettest amalgamate
2525

2626
- run:
2727
name: Building (gcc, cmake)
@@ -58,7 +58,7 @@ jobs:
5858

5959
- run:
6060
name: Running tests (clang)
61-
command: make quiettest
61+
command: make quiettest amalgamate
6262

6363
- run:
6464
name: Building (clang, cmake)

.drone.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ steps:
77
commands:
88
- make -j2
99
- make quiettest -j2
10+
- make amalgamate

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ COMPARISONEXECUTABLES=minifiercompetition parsingcompetition parseandstatcompeti
2828
SUPPLEMENTARYEXECUTABLES=parse_noutf8validation parse_nonumberparsing parse_nostringparsing
2929

3030
HEADERS= include/simdjson/simdutf8check.h include/simdjson/stringparsing.h include/simdjson/numberparsing.h include/simdjson/jsonparser.h include/simdjson/common_defs.h include/simdjson/jsonioutil.h benchmark/benchmark.h benchmark/linux/linux-perf-events.h include/simdjson/parsedjson.h include/simdjson/stage1_find_marks.h include/simdjson/stage2_build_tape.h include/simdjson/jsoncharutils.h include/simdjson/jsonformatutils.h
31-
LIBFILES=src/jsonioutil.cpp src/jsonparser.cpp src/stage1_find_marks.cpp src/stage2_build_tape.cpp src/parsedjson.cpp src/parsedjsoniterator.cpp
31+
LIBFILES=src/jsonioutil.cpp src/jsonparser.cpp src/simdjson.cpp src/stage1_find_marks.cpp src/stage2_build_tape.cpp src/parsedjson.cpp src/parsedjsoniterator.cpp
3232
MINIFIERHEADERS=include/simdjson/jsonminifier.h include/simdjson/simdprune_tables.h
3333
MINIFIERLIBFILES=src/jsonminifier.cpp
3434

@@ -116,11 +116,11 @@ jsoncheck:tests/jsoncheck.cpp $(HEADERS) $(LIBFILES)
116116
$(CXX) $(CXXFLAGS) -o jsoncheck $(LIBFILES) tests/jsoncheck.cpp -I. $(LIBFLAGS)
117117

118118
numberparsingcheck:tests/numberparsingcheck.cpp $(HEADERS) $(LIBFILES)
119-
$(CXX) $(CXXFLAGS) -o numberparsingcheck tests/numberparsingcheck.cpp src/jsonioutil.cpp src/jsonparser.cpp src/stage1_find_marks.cpp src/parsedjson.cpp -I. $(LIBFLAGS) -DJSON_TEST_NUMBERS
119+
$(CXX) $(CXXFLAGS) -o numberparsingcheck tests/numberparsingcheck.cpp src/jsonioutil.cpp src/jsonparser.cpp src/simdjson.cpp src/stage1_find_marks.cpp src/parsedjson.cpp -I. $(LIBFLAGS) -DJSON_TEST_NUMBERS
120120

121121

122122
stringparsingcheck:tests/stringparsingcheck.cpp $(HEADERS) $(LIBFILES)
123-
$(CXX) $(CXXFLAGS) -o stringparsingcheck tests/stringparsingcheck.cpp src/jsonioutil.cpp src/jsonparser.cpp src/stage1_find_marks.cpp src/parsedjson.cpp -I. $(LIBFLAGS) -DJSON_TEST_STRINGS
123+
$(CXX) $(CXXFLAGS) -o stringparsingcheck tests/stringparsingcheck.cpp src/jsonioutil.cpp src/jsonparser.cpp src/simdjson.cpp src/stage1_find_marks.cpp src/parsedjson.cpp -I. $(LIBFLAGS) -DJSON_TEST_STRINGS
124124

125125

126126
minifiercompetition: benchmark/minifiercompetition.cpp $(HEADERS) $(LIBS) $(MINIFIERHEADERS) $(LIBFILES) $(MINIFIERLIBFILES)

README.md

Lines changed: 52 additions & 62 deletions
Large diffs are not rendered by default.

amalgamation.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ $SCRIPTPATH/src/parsedjsoniterator.cpp
2525
# order matters
2626
ALLCHEADERS="
2727
$SCRIPTPATH/include/simdjson/simdjson_version.h
28+
$SCRIPTPATH/include/simdjson/simdjson.h
2829
$SCRIPTPATH/include/simdjson/portability.h
2930
$SCRIPTPATH/include/simdjson/common_defs.h
3031
$SCRIPTPATH/include/simdjson/jsoncharutils.h
@@ -54,10 +55,11 @@ function stripinc()
5455
}
5556
function dofile()
5657
{
57-
echo "/* begin file $1 */"
58+
RELFILE=${1#"$SCRIPTPATH/"}
59+
echo "/* begin file $RELFILE */"
5860
# echo "#line 8 \"$1\"" ## redefining the line/file is not nearly as useful as it sounds for debugging. It breaks IDEs.
5961
stripinc < $1
60-
echo "/* end file $1 */"
62+
echo "/* end file $RELFILE */"
6163
}
6264

6365
timestamp=$(date)

benchmark/parse.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ int main(int argc, char *argv[]) {
171171
unified.start();
172172
#endif
173173

174-
isok = isok && unified_machine(p.data(), p.size(), pj);
174+
isok = isok && !unified_machine(p.data(), p.size(), pj);
175175
#ifndef SQUASH_COUNTERS
176176
unified.end(results);
177177
cy2 += results[0];

include/simdjson/jsonparser.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@
66
#include "simdjson/parsedjson.h"
77
#include "simdjson/stage1_find_marks.h"
88
#include "simdjson/stage2_build_tape.h"
9-
10-
11-
9+
#include "simdjson/simdjson.h"
1210

1311
// Parse a document found in buf, need to preallocate ParsedJson.
14-
// Return false in case of a failure. You can also check validity
15-
// by calling pj.isValid(). The same ParsedJson can be reused for other documents.
12+
// Return 0 on success, an error code from simdjson/simdjson.h otherwise
13+
// You can also check validit by calling pj.isValid(). The same ParsedJson can be reused for other documents.
1614
//
1715
// If reallocifneeded is true (default) then a temporary buffer is created when needed during processing
1816
// (a copy of the input string is made).
1917
// The input buf should be readable up to buf + len + SIMDJSON_PADDING if reallocifneeded is false,
2018
// all bytes at and after buf + len are ignored (can be garbage).
2119
WARN_UNUSED
22-
bool json_parse(const uint8_t *buf, size_t len, ParsedJson &pj, bool reallocifneeded = true);
20+
int json_parse(const uint8_t *buf, size_t len, ParsedJson &pj, bool reallocifneeded = true);
2321

2422
// Parse a document found in buf, need to preallocate ParsedJson.
2523
// Return false in case of a failure. You can also check validity
@@ -30,7 +28,7 @@ bool json_parse(const uint8_t *buf, size_t len, ParsedJson &pj, bool reallocifne
3028
// The input buf should be readable up to buf + len + SIMDJSON_PADDING if reallocifneeded is false,
3129
// all bytes at and after buf + len are ignored (can be garbage).
3230
WARN_UNUSED
33-
inline bool json_parse(const char * buf, size_t len, ParsedJson &pj, bool reallocifneeded = true) {
31+
inline int json_parse(const char * buf, size_t len, ParsedJson &pj, bool reallocifneeded = true) {
3432
return json_parse(reinterpret_cast<const uint8_t *>(buf), len, pj, reallocifneeded);
3533
}
3634

@@ -43,7 +41,7 @@ inline bool json_parse(const char * buf, size_t len, ParsedJson &pj, bool reallo
4341
// the input s should be readable up to s.data() + s.size() + SIMDJSON_PADDING if reallocifneeded is false,
4442
// all bytes at and after s.data()+s.size() are ignored (can be garbage).
4543
WARN_UNUSED
46-
inline bool json_parse(const std::string_view &s, ParsedJson &pj, bool reallocifneeded = true) {
44+
inline int json_parse(const std::string_view &s, ParsedJson &pj, bool reallocifneeded = true) {
4745
return json_parse(s.data(), s.size(), pj, reallocifneeded);
4846
}
4947

include/simdjson/simdjson.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef SIMDJSON_ERR_H
2+
# define SIMDJSON_ERR_H
3+
4+
#include <string>
5+
6+
struct simdjson {
7+
enum errorValues {
8+
SUCCESS = 0,
9+
CAPACITY, // This ParsedJson can't support a document that big
10+
MEMALLOC, // Error allocating memory, most likely out of memory
11+
TAPE_ERROR, // Something went wrong while writing to the tape
12+
};
13+
static const std::string& errorMsg(const int);
14+
};
15+
16+
#endif

include/simdjson/stage1_find_marks.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
#define SIMDJSON_STAGE1_FIND_MARKS_H
33

44
#include "simdjson/common_defs.h"
5-
#include "simdjson/parsedjson.h"
5+
6+
struct ParsedJson;
67

78
WARN_UNUSED
89
bool find_structural_bits(const uint8_t *buf, size_t len, ParsedJson &pj);
910

1011
WARN_UNUSED
11-
static inline bool find_structural_bits(const char *buf, size_t len, ParsedJson &pj) {
12-
return find_structural_bits(reinterpret_cast<const uint8_t *>(buf), len, pj);
13-
}
12+
bool find_structural_bits(const char *buf, size_t len, ParsedJson &pj);
1413

1514
#endif

include/simdjson/stage2_build_tape.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22
#define SIMDJSON_STAGE2_BUILD_TAPE_H
33

44
#include "simdjson/common_defs.h"
5-
#include "simdjson/parsedjson.h"
5+
6+
struct ParsedJson;
67

78
void init_state_machine();
89

910
WARN_UNUSED
10-
bool unified_machine(const uint8_t *buf, size_t len, ParsedJson &pj);
11-
11+
int unified_machine(const uint8_t *buf, size_t len, ParsedJson &pj);
1212

1313
WARN_UNUSED
14-
static inline bool unified_machine(const char *buf, size_t len, ParsedJson &pj) {
15-
return unified_machine(reinterpret_cast<const uint8_t *>(buf),len,pj);
16-
}
14+
int unified_machine(const char *buf, size_t len, ParsedJson &pj);
1715

1816
#endif

0 commit comments

Comments
 (0)