MT#55283 change semantics of str_uri_encode_len

Return a `str` object with the output string for convenience. Also avoid
an unneeded strlen().

Change-Id: I6de16391dd71433c605dcaa2c6b874daad449e61
pull/1855/head
Richard Fuchs 1 year ago
parent 085189bfa5
commit 348e4e27fe

@ -2310,33 +2310,33 @@ err:
#define JSON_ADD_LIST_STRING(f,...) do { \
int len = snprintf(tmp,sizeof(tmp), f, __VA_ARGS__); \
char enc[len * 3 + 1]; \
str_uri_encode_len(enc, tmp, len); \
parser->list_add_str_dup(inner, &STR_NC(enc)); \
str encstr = str_uri_encode_len(enc, tmp, len); \
parser->list_add_str_dup(inner, &encstr); \
} while (0)
#define JSON_SET_NSTRING(a,b,c,...) do { \
int len = snprintf(tmp,sizeof(tmp), c, __VA_ARGS__); \
char enc[len * 3 + 1]; \
str_uri_encode_len(enc, tmp, len); \
str encstr = str_uri_encode_len(enc, tmp, len); \
snprintf(tmp,sizeof(tmp), a,b); \
parser->dict_add_str_dup(inner, tmp, &STR_NC(enc)); \
parser->dict_add_str_dup(inner, tmp, &encstr); \
} while (0)
#define JSON_SET_NSTRING_CSTR(a,b,d) JSON_SET_NSTRING_LEN(a, b, strlen(d), d)
#define JSON_SET_NSTRING_LEN(a,b,l,d) do { \
char enc[l * 3 + 1]; \
str_uri_encode_len(enc, d, l); \
str encstr = str_uri_encode_len(enc, d, l); \
snprintf(tmp,sizeof(tmp), a,b); \
parser->dict_add_str_dup(inner, tmp, &STR_NC(enc)); \
parser->dict_add_str_dup(inner, tmp, &encstr); \
} while (0)
#define JSON_SET_SIMPLE(a,c,...) do { \
int len = snprintf(tmp,sizeof(tmp), c, __VA_ARGS__); \
char enc[len * 3 + 1]; \
str_uri_encode_len(enc, tmp, len); \
parser->dict_add_str_dup(inner, a, &STR_NC(enc)); \
str encstr = str_uri_encode_len(enc, tmp, len); \
parser->dict_add_str_dup(inner, a, &encstr); \
} while (0)
#define JSON_SET_SIMPLE_LEN(a,l,d) do { \
char enc[l * 3 + 1]; \
str_uri_encode_len(enc, d, l); \
parser->dict_add_str_dup(inner, a, &STR_NC(enc)); \
str encstr = str_uri_encode_len(enc, d, l); \
parser->dict_add_str_dup(inner, a, &encstr); \
} while (0)
#define JSON_SET_SIMPLE_CSTR(a,d) parser->dict_add_str_dup(inner, a, &STR(d))
#define JSON_SET_SIMPLE_STR(a,d) parser->dict_add_str_dup(inner, a, d)

@ -65,7 +65,7 @@ char *rand_hex_str(char *rand_str, int num_bytes) {
static const char *hex_chars = "0123456789abcdef";
size_t str_uri_encode_len(char *out, const char *in, size_t len) {
str str_uri_encode_len(char *out, const char *in, size_t len) {
const char *end = in + len;
char *ori_out = out;
@ -82,7 +82,7 @@ size_t str_uri_encode_len(char *out, const char *in, size_t len) {
}
*out = 0;
return out - ori_out;
return STR_LEN(ori_out, out - ori_out);
}
str *str_uri_decode_len(const char *in, size_t in_len) {

@ -182,7 +182,7 @@ INLINE str *str_slice_dup(const str *);
INLINE void str_slice_free(str *);
/* saves "in" into "out" pseudo-URI encoded. "out" point to a buffer with sufficient length. returns length */
size_t str_uri_encode_len(char *out, const char *in, size_t in_len);
str str_uri_encode_len(char *out, const char *in, size_t in_len);
/* reverse of the above. returns newly allocated str + buffer as per str_alloc (must be free'd) */
str *str_uri_decode_len(const char *in, size_t in_len);

Loading…
Cancel
Save