~lattis/muon

abfdf4483b093daea5546e684ddeb05c8f1bde1d — Stone Tickle 3 months ago 3145258
implement string endswith
3 files changed, 24 insertions(+), 0 deletions(-)

M include/lang/string.h
M src/functions/string.c
M src/lang/string.c
M include/lang/string.h => include/lang/string.h +1 -0
@@ 39,6 39,7 @@ str str_clone(struct workspace *wk_src, struct workspace *wk_dest, str val);
bool wk_streql(const struct str *ss1, const struct str *ss2);
bool wk_cstreql(const struct str *ss, const char *cstring);
bool wk_str_startswith(const struct str *ss, const struct str *pre);
bool wk_str_endswith(const struct str *ss, const struct str *suf);
str wk_strcat(struct workspace *wk, str s1, str s2);

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

M src/functions/string.c => src/functions/string.c +13 -0
@@ 484,11 484,24 @@ func_string_startswith(struct workspace *wk, obj rcvr, uint32_t args_node, obj *
	return true;
}

static bool
func_string_endswith(struct workspace *wk, obj rcvr, uint32_t args_node, obj *res)
{
	struct args_norm an[] = { { obj_string }, ARG_TYPE_NULL };
	if (!interp_args(wk, args_node, an, NULL, NULL)) {
		return false;
	}

	make_obj(wk, res, obj_bool)->dat.boolean = wk_str_endswith(get_str(wk, rcvr), get_str(wk, an[0].val));
	return true;
}

const struct func_impl_name impl_tbl_string[] = {
	{ "format", func_format },
	{ "join", func_join },
	{ "split", func_split },
	{ "startswith", func_string_startswith },
	{ "endswith", func_string_endswith },
	{ "strip", func_strip },
	{ "to_int", func_string_to_int },
	{ "to_upper", func_to_upper },

M src/lang/string.c => src/lang/string.c +10 -0
@@ 268,6 268,16 @@ wk_str_startswith(const struct str *ss, const struct str *pre)
}

bool
wk_str_endswith(const struct str *ss, const struct str *suf)
{
	if (ss->len < suf->len) {
		return false;
	}

	return memcmp(&ss->s[ss->len - suf->len], suf->s, suf->len) == 0;
}

bool
wk_cstreql(const struct str *ss, const char *cstring)
{
	return wk_streql(ss, &WKSTR(cstring));