Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass on string length to user on calls to toml_string_in() #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions toml.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ static toml_arritem_t *expand_arritem(toml_arritem_t *p, int n) {
}

static char *norm_lit_str(const char *src, int srclen, int multiline,
char *errbuf, int errbufsz) {
char *errbuf, int errbufsz, size_t *destlen) {
char *dst = 0; /* will write to dst[] and return it */
int max = 0; /* max size of dst[] */
int off = 0; /* cur offset in dst[] */
Expand Down Expand Up @@ -482,7 +482,9 @@ static char *norm_lit_str(const char *src, int srclen, int multiline,
dst[off++] = ch;
}

dst[off++] = 0;
dst[off] = 0;
if (destlen)
*destlen = off;
return dst;
}

Expand All @@ -491,7 +493,7 @@ static char *norm_lit_str(const char *src, int srclen, int multiline,
* Returns NULL if error with errmsg in errbuf.
*/
static char *norm_basic_str(const char *src, int srclen, int multiline,
char *errbuf, int errbufsz) {
char *errbuf, int errbufsz, size_t *destlen) {
char *dst = 0; /* will write to dst[] and return it */
int max = 0; /* max size of dst[] */
int off = 0; /* cur offset in dst[] */
Expand Down Expand Up @@ -618,7 +620,9 @@ static char *norm_basic_str(const char *src, int srclen, int multiline,
}

// Cap with NUL and return it.
dst[off++] = 0;
dst[off] = 0;
if (destlen)
*destlen = off;
return dst;
}

Expand Down Expand Up @@ -649,7 +653,7 @@ static char *normalize_key(context_t *ctx, token_t strtok) {
}
} else {
/* for double quote, we need to normalize */
ret = norm_basic_str(sp, sq - sp, multiline, ebuf, sizeof(ebuf));
ret = norm_basic_str(sp, sq - sp, multiline, ebuf, sizeof(ebuf), NULL);
if (!ret) {
e_syntax(ctx, lineno, ebuf);
return 0;
Expand Down Expand Up @@ -2207,7 +2211,7 @@ int toml_rtod(toml_raw_t src, double *ret_) {
return toml_rtod_ex(src, ret_, buf, sizeof(buf));
}

int toml_rtos(toml_raw_t src, char **ret) {
int toml_rtos(toml_raw_t src, char **ret, size_t *s_len) {
int multiline = 0;
const char *sp;
const char *sq;
Expand Down Expand Up @@ -2254,9 +2258,9 @@ int toml_rtos(toml_raw_t src, char **ret) {
// sq points to one char beyond last valid char.
// string len is (sq - sp).
if (qchar == '\'') {
*ret = norm_lit_str(sp, sq - sp, multiline, 0, 0);
*ret = norm_lit_str(sp, sq - sp, multiline, 0, 0, s_len);
} else {
*ret = norm_basic_str(sp, sq - sp, multiline, 0, 0);
*ret = norm_basic_str(sp, sq - sp, multiline, 0, 0, s_len);
}

return *ret ? 0 : -1;
Expand All @@ -2265,7 +2269,7 @@ int toml_rtos(toml_raw_t src, char **ret) {
toml_datum_t toml_string_at(const toml_array_t *arr, int idx) {
toml_datum_t ret;
memset(&ret, 0, sizeof(ret));
ret.ok = (0 == toml_rtos(toml_raw_at(arr, idx), &ret.u.s));
ret.ok = (0 == toml_rtos(toml_raw_at(arr, idx), &ret.u.s, &ret.s_len));
return ret;
}

Expand Down Expand Up @@ -2325,7 +2329,7 @@ toml_datum_t toml_string_in(const toml_table_t *arr, const char *key) {
memset(&ret, 0, sizeof(ret));
toml_raw_t raw = toml_raw_in(arr, key);
if (raw) {
ret.ok = (0 == toml_rtos(raw, &ret.u.s));
ret.ok = (0 == toml_rtos(raw, &ret.u.s, &ret.s_len));
}
return ret;
}
Expand Down
3 changes: 2 additions & 1 deletion toml.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ struct toml_datum_t {
int64_t i; /* int value */
double d; /* double value */
} u;
size_t s_len;
};

/* on arrays: */
Expand Down Expand Up @@ -165,7 +166,7 @@ TOML_EXTERN void toml_set_memutil(void *(*xxmalloc)(size_t),
typedef const char *toml_raw_t;
TOML_EXTERN toml_raw_t toml_raw_in(const toml_table_t *tab, const char *key);
TOML_EXTERN toml_raw_t toml_raw_at(const toml_array_t *arr, int idx);
TOML_EXTERN int toml_rtos(toml_raw_t s, char **ret);
TOML_EXTERN int toml_rtos(toml_raw_t s, char **ret, size_t *s_len);
TOML_EXTERN int toml_rtob(toml_raw_t s, int *ret);
TOML_EXTERN int toml_rtoi(toml_raw_t s, int64_t *ret);
TOML_EXTERN int toml_rtod(toml_raw_t s, double *ret);
Expand Down
2 changes: 1 addition & 1 deletion toml_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static void print_raw(const char *s) {
toml_timestamp_t ts;
char dbuf[100];

if (0 == toml_rtos(s, &sval)) {
if (0 == toml_rtos(s, &sval, NULL)) {
printf("{\"type\":\"string\",\"value\":\"");
print_escape_string(sval);
printf("\"}");
Expand Down