add some string utility functions

This commit is contained in:
Yuichi Nishiwaki 2014-02-21 18:24:20 +09:00
parent 131440b1d0
commit a8aa6b2c5e
2 changed files with 42 additions and 0 deletions

View File

@ -166,6 +166,9 @@ char *pic_strdup(pic_state *, const char *);
char *pic_strndup(pic_state *, const char *, size_t);
pic_str *pic_str_new(pic_state *, const char * /* nullable */, size_t);
pic_str *pic_str_new_cstr(pic_state *, const char *);
char pic_str_ref(pic_state *, pic_str *, size_t);
pic_str *pic_strcat(pic_state *, pic_str *, pic_str *);
pic_str *pic_substr(pic_state *, pic_str *, size_t, size_t);
pic_value pic_format(pic_state *, const char *, ...);
pic_value pic_vformat(pic_state *, const char *, va_list);

View File

@ -32,6 +32,45 @@ pic_str_new_cstr(pic_state *pic, const char *cstr)
return pic_str_new(pic, cstr, strlen(cstr));
}
char
pic_str_ref(pic_state *pic, pic_str *str, size_t n)
{
UNUSED(pic);
return str->str[n];
}
pic_str *
pic_strcat(pic_state *pic, pic_str *a, pic_str *b)
{
size_t len;
char *buf;
len = a->len + b->len;
buf = pic_alloc(pic, len + 1);
memcpy(buf, a->str, a->len);
memcpy(buf + a->len, b->str, b->len);
buf[len] = '\0';
return pic_str_new(pic, buf, len);
}
pic_str *
pic_substr(pic_state *pic, pic_str *str, size_t s, size_t e)
{
size_t len;
char *buf;
len = e - s;
buf = pic_alloc(pic, len + 1);
memcpy(buf, str->str + s, len);
buf[len] = '\0';
return pic_str_new(pic, buf, len);
}
pic_value
pic_vfformat(pic_state *pic, XFILE *file, const char *fmt, va_list ap)
{