remove variable-length arrays in string.c

This commit is contained in:
Yuichi Nishiwaki 2015-01-27 23:58:03 +09:00
parent 914c295177
commit e4248584b6
1 changed files with 33 additions and 10 deletions

View File

@ -6,6 +6,7 @@
#include "picrin/string.h"
#include "picrin/pair.h"
#include "picrin/port.h"
#include "picrin/error.h"
static pic_str *
make_str_rope(pic_state *pic, xrope *rope)
@ -36,14 +37,19 @@ pic_str *
pic_make_str_fill(pic_state *pic, size_t len, char fill)
{
size_t i;
char buf[len + 1];
char *buf = pic_malloc(pic, len);
pic_str *str;
for (i = 0; i < len; ++i) {
buf[i] = fill;
}
buf[i] = '\0';
return pic_make_str(pic, buf, len);
str = pic_make_str(pic, buf, len);
pic_free(pic, buf);
return str;
}
size_t
@ -357,6 +363,8 @@ pic_str_string_map(pic_state *pic)
struct pic_proc *proc;
pic_value *argv, vals, val;
size_t argc, i, len, j;
pic_str *str;
char *buf;
pic_get_args(pic, "l*", &proc, &argc, &argv);
@ -371,9 +379,9 @@ pic_str_string_map(pic_state *pic)
if (len == SIZE_MAX) {
pic_errorf(pic, "string-map: one or more strings expected, but got zero");
}
else {
char buf[len];
buf = pic_malloc(pic, len);
pic_try {
for (i = 0; i < len; ++i) {
vals = pic_nil_value();
for (j = 0; j < argc; ++j) {
@ -384,9 +392,16 @@ pic_str_string_map(pic_state *pic)
pic_assert_type(pic, val, char);
buf[i] = pic_char(val);
}
return pic_obj_value(pic_make_str(pic, buf, len));
str = pic_make_str(pic, buf, len);
}
pic_catch {
pic_free(pic, buf);
pic_raise(pic, pic->err);
}
pic_free(pic, buf);
return pic_obj_value(str);
}
static pic_value
@ -427,14 +442,17 @@ pic_str_list_to_string(pic_state *pic)
pic_str *str;
pic_value list, e, it;
size_t i = 0;
char *buf;
pic_get_args(pic, "o", &list);
if (pic_length(pic, list) == 0) {
return pic_obj_value(pic_make_str(pic, NULL, 0));
} else {
char buf[pic_length(pic, list)];
}
buf = pic_malloc(pic, pic_length(pic, list));
pic_try {
pic_for_each (e, list, it) {
pic_assert_type(pic, e, char);
@ -442,9 +460,14 @@ pic_str_list_to_string(pic_state *pic)
}
str = pic_make_str(pic, buf, i);
return pic_obj_value(str);
}
pic_catch {
pic_free(pic, buf);
pic_raise(pic, pic->err);
}
pic_free(pic, buf);
return pic_obj_value(str);
}
static pic_value