Skip to content

Commit

Permalink
ignore leading/trailing whitespace in str.to_int()
Browse files Browse the repository at this point in the history
This is what meson does and seems reasonable.
  • Loading branch information
annacrombie committed Jan 7, 2024
1 parent 2b47a3c commit 0f22851
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 13 deletions.
4 changes: 3 additions & 1 deletion include/lang/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ bool str_startswithi(const struct str *ss, const struct str *pre);
bool str_endswith(const struct str *ss, const struct str *suf);
obj str_join(struct workspace *wk, obj s1, obj s2);

bool str_to_i(const struct str *ss, int64_t *res);
bool str_to_i(const struct str *ss, int64_t *res, bool strip);

obj str_split(struct workspace *wk, const struct str *ss, const struct str *split);
obj str_strip(struct workspace *wk, const struct str *ss, const struct str *strip);
obj str_split_strip(struct workspace *wk, const struct str *ss, const struct str *split, const struct str *strip);

bool is_whitespace(char c);
#endif
6 changes: 0 additions & 6 deletions src/formats/ini.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ struct ini_parse_ctx {
bool success;
};

static bool
is_whitespace(char c)
{
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}

static bool
line_is_whitespace(const char *c)
{
Expand Down
2 changes: 1 addition & 1 deletion src/formats/tap.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ tap_parse_line_cb(void *_ctx, char *line, size_t _)
if (str_startswith(&l, &WKSTR("1..")) && l.len > 3) {
struct str i = { .s = &l.s[3], l.len - 3 };
int64_t plan_count;
if (str_to_i(&i, &plan_count) && plan_count > 0) {
if (str_to_i(&i, &plan_count, false) && plan_count > 0) {
ctx->have_plan = true;
ctx->res->total = plan_count;
}
Expand Down
2 changes: 1 addition & 1 deletion src/functions/kernel/custom_target.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ prefix_plus_index(const struct str *ss, const char *prefix, int64_t *index)
return str_to_i(&(struct str) {
.s = &ss->s[len],
.len = ss->len - len
}, index);
}, index, false);
}

return false;
Expand Down
4 changes: 2 additions & 2 deletions src/functions/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func_format_cb(struct workspace *wk, uint32_t node, void *_ctx, const struct str
struct func_format_ctx *ctx = _ctx;
int64_t i;

if (!str_to_i(key, &i)) {
if (!str_to_i(key, &i, false)) {
return format_cb_skip;
}

Expand Down Expand Up @@ -384,7 +384,7 @@ func_string_to_int(struct workspace *wk, obj rcvr, uint32_t args_node, obj *res)
const struct str *ss = get_str(wk, rcvr);

int64_t n;
if (!str_to_i(ss, &n)) {
if (!str_to_i(ss, &n, true)) {
interp_error(wk, args_node, "unable to parse %o", rcvr);
return false;
}
Expand Down
25 changes: 23 additions & 2 deletions src/lang/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,31 @@ str_join(struct workspace *wk, obj s1, obj s2)
}

bool
str_to_i(const struct str *ss, int64_t *res)
is_whitespace(char c)
{
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}

bool
str_to_i(const struct str *ss, int64_t *res, bool strip)
{
char *endptr = NULL;
*res = strtol(ss->s, &endptr, 10);
const char *start = ss->s;

if (strip) {
while (is_whitespace(*start)) {
++start;
}
}

*res = strtol(start, &endptr, 10);

if (strip) {
while (is_whitespace(*endptr)) {
++endptr;
}
}

if ((uint32_t)(endptr - ss->s) != ss->len) {
return false;
}
Expand Down

0 comments on commit 0f22851

Please sign in to comment.