Skip to content

Commit e8a836e

Browse files
committed
Expose JSON internal function to escape string
1 parent 1029537 commit e8a836e

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

ext/json/json.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,21 @@ static PHP_MINFO_FUNCTION(json)
142142
}
143143
/* }}} */
144144

145+
PHP_JSON_API zend_string *php_json_encode_string(const char *s, size_t len, int options)
146+
{
147+
smart_str buf = {0};
148+
php_json_encoder encoder;
149+
150+
php_json_encode_init(&encoder);
151+
152+
if (php_json_escape_string(&buf, s, len, options, &encoder) == FAILURE) {
153+
smart_str_free(&buf);
154+
return NULL;
155+
}
156+
157+
return smart_str_extract(&buf);
158+
}
159+
145160
PHP_JSON_API int php_json_encode_ex(smart_str *buf, zval *val, int options, zend_long depth) /* {{{ */
146161
{
147162
php_json_encoder encoder;

ext/json/json_encoder.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@
3131

3232
static const char digits[] = "0123456789abcdef";
3333

34-
static int php_json_escape_string(
35-
smart_str *buf, const char *s, size_t len,
36-
int options, php_json_encoder *encoder);
37-
3834
static int php_json_determine_array_type(zval *val) /* {{{ */
3935
{
4036
zend_array *myht = Z_ARRVAL_P(val);
@@ -312,7 +308,7 @@ static int php_json_encode_array(smart_str *buf, zval *val, int options, php_jso
312308
}
313309
/* }}} */
314310

315-
static int php_json_escape_string(
311+
int php_json_escape_string(
316312
smart_str *buf, const char *s, size_t len,
317313
int options, php_json_encoder *encoder) /* {{{ */
318314
{

ext/json/php_json.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ PHP_JSON_API ZEND_EXTERN_MODULE_GLOBALS(json)
9797
ZEND_TSRMLS_CACHE_EXTERN()
9898
#endif
9999

100+
PHP_JSON_API zend_string *php_json_encode_string(const char *s, size_t len, int options);
101+
100102
PHP_JSON_API int php_json_encode_ex(smart_str *buf, zval *val, int options, zend_long depth);
101103
PHP_JSON_API int php_json_encode(smart_str *buf, zval *val, int options);
102104
PHP_JSON_API int php_json_decode_ex(zval *return_value, const char *str, size_t str_len, zend_long options, zend_long depth);

ext/json/php_json_encoder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ static inline void php_json_encode_init(php_json_encoder *encoder)
3535

3636
int php_json_encode_zval(smart_str *buf, zval *val, int options, php_json_encoder *encoder);
3737

38+
int php_json_escape_string(smart_str *buf, const char *s, size_t len, int options, php_json_encoder *encoder);
39+
3840
#endif /* PHP_JSON_ENCODER_H */

0 commit comments

Comments
 (0)