add pic_str_new_fill

This commit is contained in:
Yuichi Nishiwaki 2014-02-23 11:25:39 +09:00
parent 349d7f1799
commit 9f240c191c
2 changed files with 20 additions and 10 deletions

View File

@ -20,6 +20,7 @@ struct pic_string {
pic_str *pic_str_new(pic_state *, const char * /* nullable */, size_t); pic_str *pic_str_new(pic_state *, const char * /* nullable */, size_t);
pic_str *pic_str_new_cstr(pic_state *, const char *); pic_str *pic_str_new_cstr(pic_state *, const char *);
pic_str *pic_str_new_fill(pic_state *, size_t, char);
char pic_str_ref(pic_state *, pic_str *, size_t); char pic_str_ref(pic_state *, pic_str *, size_t);
pic_str *pic_strcat(pic_state *, pic_str *, pic_str *); pic_str *pic_strcat(pic_state *, pic_str *, pic_str *);

View File

@ -33,6 +33,21 @@ pic_str_new_cstr(pic_state *pic, const char *cstr)
return pic_str_new(pic, cstr, strlen(cstr)); return pic_str_new(pic, cstr, strlen(cstr));
} }
pic_str *
pic_str_new_fill(pic_state *pic, size_t len, char fill)
{
size_t i;
char *cstr;
cstr = (char *)pic_alloc(pic, len + 1);
for (i = 0; i < len; ++i) {
cstr[i] = fill;
}
cstr[len] = '\0';
return pic_str_new(pic, cstr, len);
}
char char
pic_str_ref(pic_state *pic, pic_str *str, size_t n) pic_str_ref(pic_state *pic, pic_str *str, size_t n)
{ {
@ -178,18 +193,12 @@ pic_str_string_p(pic_state *pic)
static pic_value static pic_value
pic_str_make_string(pic_state *pic) pic_str_make_string(pic_state *pic)
{ {
int k, i; int len;
char c = ' ', *cstr; char c = ' ';
pic_get_args(pic, "i|c", &k, &c); pic_get_args(pic, "i|c", &len, &c);
cstr = (char *)pic_alloc(pic, k + 1); return pic_obj_value(pic_str_new_fill(pic, len, c));
for (i = 0; i < k; ++i) {
cstr[i] = c;
}
cstr[k] = '\0';
return pic_obj_value(pic_str_new(pic, cstr, k));
} }
static pic_value static pic_value