mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Separate mrb_str_buf_new and mrb_str_new_capa.
`mrb_str_buf_new` is an old function that ensures capacity size of `MRB_STR_BUF_MIN_SIZE` minimum. Usually one need to use `mrb_str_new_capa` instead.
This commit is contained in:
@@ -114,14 +114,14 @@ inspect_ary(mrb_state *mrb, mrb_value ary, mrb_value list)
|
||||
|
||||
mrb_ary_push(mrb, list, ary);
|
||||
|
||||
arystr = mrb_str_buf_new(mrb, 64);
|
||||
mrb_str_buf_cat(mrb, arystr, head, sizeof(head));
|
||||
arystr = mrb_str_new_capa(mrb, 64);
|
||||
mrb_str_cat(mrb, arystr, head, sizeof(head));
|
||||
|
||||
for(i=0; i<RARRAY_LEN(ary); i++) {
|
||||
int ai = mrb_gc_arena_save(mrb);
|
||||
|
||||
if (i > 0) {
|
||||
mrb_str_buf_cat(mrb, arystr, sep, sizeof(sep));
|
||||
mrb_str_cat(mrb, arystr, sep, sizeof(sep));
|
||||
}
|
||||
if (mrb_array_p(RARRAY_PTR(ary)[i])) {
|
||||
s = inspect_ary(mrb, RARRAY_PTR(ary)[i], list);
|
||||
@@ -129,11 +129,11 @@ inspect_ary(mrb_state *mrb, mrb_value ary, mrb_value list)
|
||||
else {
|
||||
s = mrb_inspect(mrb, RARRAY_PTR(ary)[i]);
|
||||
}
|
||||
mrb_str_buf_cat(mrb, arystr, RSTRING_PTR(s), RSTRING_LEN(s));
|
||||
mrb_str_cat(mrb, arystr, RSTRING_PTR(s), RSTRING_LEN(s));
|
||||
mrb_gc_arena_restore(mrb, ai);
|
||||
}
|
||||
|
||||
mrb_str_buf_cat(mrb, arystr, tail, sizeof(tail));
|
||||
mrb_str_cat(mrb, arystr, tail, sizeof(tail));
|
||||
mrb_ary_pop(mrb, list);
|
||||
|
||||
return arystr;
|
||||
|
||||
@@ -304,6 +304,7 @@ MRB_API mrb_value mrb_str_substr(mrb_state *mrb, mrb_value str, mrb_int beg, mrb
|
||||
MRB_API mrb_value mrb_string_type(mrb_state *mrb, mrb_value str);
|
||||
|
||||
MRB_API mrb_value mrb_check_string_type(mrb_state *mrb, mrb_value str);
|
||||
MRB_API mrb_value mrb_str_new_capa(mrb_state *mrb, size_t capa);
|
||||
MRB_API mrb_value mrb_str_buf_new(mrb_state *mrb, size_t capa);
|
||||
|
||||
MRB_API const char *mrb_string_value_cstr(mrb_state *mrb, mrb_value *ptr);
|
||||
|
||||
@@ -556,7 +556,7 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
|
||||
end = p + RSTRING_LEN(fmt);
|
||||
blen = 0;
|
||||
bsiz = 120;
|
||||
result = mrb_str_buf_new(mrb, bsiz);
|
||||
result = mrb_str_new_capa(mrb, bsiz);
|
||||
buf = RSTRING_PTR(result);
|
||||
memset(buf, 0, bsiz);
|
||||
|
||||
|
||||
+1
-1
@@ -1093,7 +1093,7 @@ join_ary(mrb_state *mrb, mrb_value ary, mrb_value sep, mrb_value list)
|
||||
|
||||
mrb_ary_push(mrb, list, ary);
|
||||
|
||||
result = mrb_str_buf_new(mrb, 64);
|
||||
result = mrb_str_new_capa(mrb, 64);
|
||||
|
||||
for (i=0; i<RARRAY_LEN(ary); i++) {
|
||||
if (i > 0 && !mrb_nil_p(sep)) {
|
||||
|
||||
+4
-4
@@ -1332,7 +1332,7 @@ mrb_mod_attr_reader(mrb_state *mrb, mrb_value mod)
|
||||
|
||||
method = to_sym(mrb, argv[i]);
|
||||
name = mrb_sym2str(mrb, method);
|
||||
str = mrb_str_buf_new(mrb, RSTRING_LEN(name)+1);
|
||||
str = mrb_str_new_capa(mrb, RSTRING_LEN(name)+1);
|
||||
mrb_str_cat_lit(mrb, str, "@");
|
||||
mrb_str_cat_str(mrb, str, name);
|
||||
sym = mrb_intern_str(mrb, str);
|
||||
@@ -1374,7 +1374,7 @@ mrb_mod_attr_writer(mrb_state *mrb, mrb_value mod)
|
||||
|
||||
/* prepare iv name (@name) */
|
||||
name = mrb_sym2str(mrb, method);
|
||||
str = mrb_str_buf_new(mrb, RSTRING_LEN(name)+1);
|
||||
str = mrb_str_new_capa(mrb, RSTRING_LEN(name)+1);
|
||||
mrb_str_cat_lit(mrb, str, "@");
|
||||
mrb_str_cat_str(mrb, str, name);
|
||||
sym = mrb_intern_str(mrb, str);
|
||||
@@ -1382,7 +1382,7 @@ mrb_mod_attr_writer(mrb_state *mrb, mrb_value mod)
|
||||
attr = mrb_symbol_value(sym);
|
||||
|
||||
/* prepare method name (name=) */
|
||||
str = mrb_str_buf_new(mrb, RSTRING_LEN(str));
|
||||
str = mrb_str_new_capa(mrb, RSTRING_LEN(str));
|
||||
mrb_str_cat_str(mrb, str, name);
|
||||
mrb_str_cat_lit(mrb, str, "=");
|
||||
method = mrb_intern_str(mrb, str);
|
||||
@@ -1766,7 +1766,7 @@ mrb_mod_to_s(mrb_state *mrb, mrb_value klass)
|
||||
struct RClass *c;
|
||||
mrb_value path;
|
||||
|
||||
str = mrb_str_buf_new(mrb, 32);
|
||||
str = mrb_str_new_capa(mrb, 32);
|
||||
c = mrb_class_ptr(klass);
|
||||
path = mrb_class_path(mrb, c);
|
||||
|
||||
|
||||
+1
-1
@@ -364,7 +364,7 @@ mrb_float_to_str(mrb_state *mrb, mrb_value flo, const char *fmt)
|
||||
struct fmt_args f;
|
||||
|
||||
f.mrb = mrb;
|
||||
f.str = mrb_str_buf_new(mrb, 24);
|
||||
f.str = mrb_str_new_capa(mrb, 24);
|
||||
if (fmt_core(&f, fmt, mrb_float(flo)) < 0) {
|
||||
mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid format string");
|
||||
}
|
||||
|
||||
+1
-1
@@ -438,7 +438,7 @@ mrb_check_type(mrb_state *mrb, mrb_value x, enum mrb_vtype t)
|
||||
MRB_API mrb_value
|
||||
mrb_any_to_s(mrb_state *mrb, mrb_value obj)
|
||||
{
|
||||
mrb_value str = mrb_str_buf_new(mrb, 20);
|
||||
mrb_value str = mrb_str_new_capa(mrb, 20);
|
||||
const char *cname = mrb_obj_classname(mrb, obj);
|
||||
|
||||
mrb_str_cat_lit(mrb, str, "#<");
|
||||
|
||||
+14
-8
@@ -94,12 +94,8 @@ mrb_str_new_empty(mrb_state *mrb, mrb_value str)
|
||||
return mrb_obj_value(s);
|
||||
}
|
||||
|
||||
#ifndef MRB_STR_BUF_MIN_SIZE
|
||||
# define MRB_STR_BUF_MIN_SIZE 128
|
||||
#endif
|
||||
|
||||
MRB_API mrb_value
|
||||
mrb_str_buf_new(mrb_state *mrb, size_t capa)
|
||||
mrb_str_new_capa(mrb_state *mrb, size_t capa)
|
||||
{
|
||||
struct RString *s;
|
||||
|
||||
@@ -108,9 +104,6 @@ mrb_str_buf_new(mrb_state *mrb, size_t capa)
|
||||
if (capa >= MRB_INT_MAX) {
|
||||
mrb_raise(mrb, E_ARGUMENT_ERROR, "string capacity size too big");
|
||||
}
|
||||
if (capa < MRB_STR_BUF_MIN_SIZE) {
|
||||
capa = MRB_STR_BUF_MIN_SIZE;
|
||||
}
|
||||
s->as.heap.len = 0;
|
||||
s->as.heap.aux.capa = (mrb_int)capa;
|
||||
s->as.heap.ptr = (char *)mrb_malloc(mrb, capa+1);
|
||||
@@ -119,6 +112,19 @@ mrb_str_buf_new(mrb_state *mrb, size_t capa)
|
||||
return mrb_obj_value(s);
|
||||
}
|
||||
|
||||
#ifndef MRB_STR_BUF_MIN_SIZE
|
||||
# define MRB_STR_BUF_MIN_SIZE 128
|
||||
#endif
|
||||
|
||||
MRB_API mrb_value
|
||||
mrb_str_buf_new(mrb_state *mrb, size_t capa)
|
||||
{
|
||||
if (capa < MRB_STR_BUF_MIN_SIZE) {
|
||||
capa = MRB_STR_BUF_MIN_SIZE;
|
||||
}
|
||||
return mrb_str_new_capa(mrb, capa);
|
||||
}
|
||||
|
||||
static void
|
||||
resize_capa(mrb_state *mrb, struct RString *s, size_t capacity)
|
||||
{
|
||||
|
||||
+2
-2
@@ -513,7 +513,7 @@ mrb_obj_iv_inspect(mrb_state *mrb, struct RObject *obj)
|
||||
|
||||
if (len > 0) {
|
||||
const char *cn = mrb_obj_classname(mrb, mrb_obj_value(obj));
|
||||
mrb_value str = mrb_str_buf_new(mrb, 30);
|
||||
mrb_value str = mrb_str_new_capa(mrb, 30);
|
||||
|
||||
mrb_str_cat_lit(mrb, str, "-<");
|
||||
mrb_str_cat_cstr(mrb, str, cn);
|
||||
@@ -1108,7 +1108,7 @@ mrb_class_find_path(mrb_state *mrb, struct RClass *c)
|
||||
name = find_class_sym(mrb, mrb_class_ptr(outer), c);
|
||||
if (name == 0) return mrb_nil_value();
|
||||
str = mrb_class_name(mrb, mrb_class_ptr(outer));
|
||||
path = mrb_str_buf_new(mrb, 0);
|
||||
path = mrb_str_new_capa(mrb, 0);
|
||||
mrb_str_cat_cstr(mrb, path, str);
|
||||
mrb_str_cat_cstr(mrb, path, "::");
|
||||
|
||||
|
||||
@@ -813,7 +813,7 @@ localjump_error(mrb_state *mrb, localjump_error_kind kind)
|
||||
mrb_value msg;
|
||||
mrb_value exc;
|
||||
|
||||
msg = mrb_str_buf_new(mrb, sizeof(lead) + 7);
|
||||
msg = mrb_str_new_capa(mrb, sizeof(lead) + 7);
|
||||
mrb_str_cat(mrb, msg, lead, sizeof(lead) - 1);
|
||||
mrb_str_cat(mrb, msg, kind_str[kind], kind_str_len[kind]);
|
||||
exc = mrb_exc_new_str(mrb, E_LOCALJUMP_ERROR, msg);
|
||||
|
||||
Reference in New Issue
Block a user