Skip to content

Commit 222ad9a

Browse files
committed
Add remaining API methods for other PECLS
1 parent d94c391 commit 222ad9a

3 files changed

Lines changed: 36 additions & 17 deletions

File tree

php_simdjson.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ static simdjson_php_parser *simdjson_get_parser() {
9595
return parser;
9696
}
9797

98+
PHP_SIMDJSON_API struct simdjson_php_parser *php_simdjson_get_default_singleton_parser(void) {
99+
return simdjson_get_parser();
100+
}
101+
98102
// The simdjson parser accepts strings with at most 32-bit lengths, for now.
99103
#define SIMDJSON_MAX_DEPTH ((zend_long)((SIZE_MAX / 8) < (UINT32_MAX / 2) ? (SIZE_MAX / 8) : (UINT32_MAX / 2)))
100104

@@ -125,14 +129,18 @@ PHP_FUNCTION (simdjson_is_valid) {
125129
if (!simdjson_validate_depth(depth, "simdjson_is_valid", 2)) {
126130
RETURN_THROWS();
127131
}
128-
bool is_json = php_simdjson_is_valid(simdjson_get_parser(), ZSTR_VAL(json), ZSTR_LEN(json), depth);
129-
ZVAL_BOOL(return_value, is_json);
132+
simdjson_php_error_code error = php_simdjson_validate(simdjson_get_parser(), ZSTR_VAL(json), ZSTR_LEN(json), depth);
133+
ZVAL_BOOL(return_value, !error);
130134
}
131135

132136
PHP_SIMDJSON_API simdjson_php_error_code php_simdjson_parse_default(const char *json, size_t len, zval *return_value, bool associative, size_t depth) {
133137
return php_simdjson_parse(simdjson_get_parser(), json, len, return_value, associative, depth);
134138
}
135139

140+
PHP_SIMDJSON_API simdjson_php_error_code php_simdjson_validate_default(const char *json, size_t len, size_t depth) {
141+
return php_simdjson_validate(simdjson_get_parser(), json, len, depth);
142+
}
143+
136144
PHP_FUNCTION (simdjson_decode) {
137145
zend_bool associative = 0;
138146
zend_long depth = SIMDJSON_PARSE_DEFAULT_DEPTH;

php_simdjson.h

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,28 @@ typedef uint8_t simdjson_php_error_code;
115115

116116
/* NOTE: Callers should check if len is greater than 4GB - simdjson will always return a non zero error code for those */
117117

118+
/**
119+
* Parses the given string into a return code, using the default singleton parser.
120+
*
121+
* This must be called after simdjson's request initialization phase and before simdjson's request shutdown phase.
122+
* (e.g. PECLs should not use this during module or request initialization/shutdown)
123+
*/
124+
PHP_SIMDJSON_API simdjson_php_error_code php_simdjson_parse_default(const char *json, size_t len, zval *return_value, bool associative, size_t depth);
125+
126+
/**
127+
* Checks if the given JSON is valid, using the default singleton parser.
128+
* Returns 0 if it is valid.
129+
*/
130+
PHP_SIMDJSON_API simdjson_php_error_code php_simdjson_validate_default(const char *json, size_t len, size_t depth);
131+
132+
/**
133+
* Returns or creates the singleton parser used internally by simdjson (e.g. for the `php_simdjson_*_default()` methods).
134+
* (Thread-local in ZTS builds of PHP)
135+
*
136+
* Callers must NOT free this.
137+
*/
138+
PHP_SIMDJSON_API struct simdjson_php_parser *php_simdjson_get_default_singleton_parser(void);
139+
118140
/* FIXME add php_simdjson_get_default_singleton_parser api */
119141
/* FIXME add php_simdjson_decode_with_default_singleton_parser(return_value, json, len, bool associative) */
120142

@@ -142,16 +164,9 @@ PHP_SIMDJSON_API struct simdjson_php_parser* php_simdjson_create_parser(void);
142164
*/
143165
PHP_SIMDJSON_API void php_simdjson_free_parser(struct simdjson_php_parser* parser);
144166
/**
145-
* Returns true if the given json string is valid
167+
* Returns 0 if the given json string is valid
146168
*/
147-
PHP_SIMDJSON_API bool php_simdjson_is_valid(struct simdjson_php_parser* parser, const char *json, size_t len, size_t depth);
148-
/**
149-
* Parses the given string into a return code, using the default singleton parser.
150-
*
151-
* This must be called after simdjson's request initialization phase and before simdjson's request shutdown phase.
152-
* (e.g. PECLs should not use this during module or request initialization/shutdown)
153-
*/
154-
PHP_SIMDJSON_API simdjson_php_error_code php_simdjson_parse_default(const char *json, size_t len, zval *return_value, bool associative, size_t depth);
169+
PHP_SIMDJSON_API simdjson_php_error_code php_simdjson_validate(struct simdjson_php_parser* parser, const char *json, size_t len, size_t depth);
155170
/**
156171
* Parses the given string into a return code.
157172
*

src/simdjson_bindings.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,14 +337,10 @@ PHP_SIMDJSON_API void php_simdjson_free_parser(simdjson_php_parser* parser) /* {
337337
delete parser;
338338
}
339339

340-
PHP_SIMDJSON_API bool php_simdjson_is_valid(simdjson_php_parser* parser, const char *json, size_t len, size_t depth) /* {{{ */ {
340+
PHP_SIMDJSON_API simdjson_php_error_code php_simdjson_validate(simdjson_php_parser* parser, const char *json, size_t len, size_t depth) /* {{{ */ {
341341
simdjson::dom::element doc;
342342
/* The depth is passed in to ensure this behaves the same way for the same arguments */
343-
auto error = build_parsed_json_cust(parser, doc, json, len, true, depth);
344-
if (error) {
345-
return false;
346-
}
347-
return true;
343+
return build_parsed_json_cust(parser, doc, json, len, true, depth);
348344
}
349345

350346
/* }}} */

0 commit comments

Comments
 (0)