void pic_str_set(pic_state *pic, pic_str *str, int i, char c) { pic_str *x, *y, *z, *tmp; char buf[1]; if (pic_str_len(str) <= i) { pic_errorf(pic, "index out of range %d", i); } buf[0] = c; x = pic_str_sub(pic, str, 0, i); y = pic_make_str(pic, buf, 1); z = pic_str_sub(pic, str, i + 1, pic_str_len(str)); tmp = pic_str_cat(pic, x, pic_str_cat(pic, y, z)); pic_rope_incref(pic, tmp->rope); pic_rope_decref(pic, str->rope); str->rope = tmp->rope; }
static pic_value pic_str_string_copy(pic_state *pic) { pic_str *str; int n; size_t start, end; n = pic_get_args(pic, "s|kk", &str, &start, &end); switch (n) { case 1: start = 0; case 2: end = pic_str_len(str); } return pic_obj_value(pic_str_sub(pic, str, start, end)); }
static pic_value pic_str_string_copy(pic_state *pic) { pic_str *str; int n, start, end, len; n = pic_get_args(pic, "s|ii", &str, &start, &end); len = pic_str_len(str); switch (n) { case 1: start = 0; case 2: end = len; } if (start < 0 || end > len || end < start) pic_errorf(pic, "string-copy: invalid index"); return pic_obj_value(pic_str_sub(pic, str, start, end)); }
static pic_value pic_str_string_copy_ip(pic_state *pic) { pic_str *to, *from; int n, at, start, end; n = pic_get_args(pic, "sis|ii", &to, &at, &from, &start, &end); switch (n) { case 3: start = 0; case 4: end = pic_str_len(from); } if (to == from) { from = pic_str_sub(pic, from, 0, end); } while (start < end) { pic_str_set(pic, to, at++, pic_str_ref(pic, from, start++)); } return pic_undef_value(); }