static pic_value pic_str_string_for_each(pic_state *pic) { struct pic_proc *proc; int argc, len, i, j; pic_value *argv, vals; pic_get_args(pic, "l*", &proc, &argc, &argv); if (argc == 0) { pic_errorf(pic, "string-map: one or more strings expected, but got zero"); } else { pic_assert_type(pic, argv[0], str); len = pic_str_len(pic_str_ptr(argv[0])); } for (i = 1; i < argc; ++i) { pic_assert_type(pic, argv[i], str); len = len < pic_str_len(pic_str_ptr(argv[i])) ? len : pic_str_len(pic_str_ptr(argv[i])); } for (i = 0; i < len; ++i) { vals = pic_nil_value(); for (j = 0; j < argc; ++j) { pic_push(pic, pic_char_value(pic_str_ref(pic, pic_str_ptr(argv[j]), i)), vals); } pic_apply_list(pic, proc, vals); } return pic_undef_value(); }
static pic_value pic_str_string_map(pic_state *pic) { struct pic_proc *proc; pic_value *argv, vals, val; int argc, i, len, j; pic_str *str; char *buf; pic_get_args(pic, "l*", &proc, &argc, &argv); if (argc == 0) { pic_errorf(pic, "string-map: one or more strings expected, but got zero"); } else { pic_assert_type(pic, argv[0], str); len = pic_str_len(pic_str_ptr(argv[0])); } for (i = 1; i < argc; ++i) { pic_assert_type(pic, argv[i], str); len = len < pic_str_len(pic_str_ptr(argv[i])) ? len : pic_str_len(pic_str_ptr(argv[i])); } buf = pic_malloc(pic, len); pic_try { for (i = 0; i < len; ++i) { vals = pic_nil_value(); for (j = 0; j < argc; ++j) { pic_push(pic, pic_char_value(pic_str_ref(pic, pic_str_ptr(argv[j]), i)), vals); } val = pic_apply_list(pic, proc, vals); pic_assert_type(pic, val, char); buf[i] = pic_char(val); } 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 pic_vec_string_to_vector(pic_state *pic) { pic_str *str; int n, start, end, i; pic_vec *vec; n = pic_get_args(pic, "s|ii", &str, &start, &end); switch (n) { case 1: start = 0; case 2: end = pic_str_len(str); } if (end < start) { pic_errorf(pic, "string->vector: end index must not be less than start index"); } vec = pic_make_vec(pic, end - start); for (i = 0; i < end - start; ++i) { vec->data[i] = pic_char_value(pic_str_ref(pic, str, i + start)); } return pic_obj_value(vec); }
static pic_value pic_str_string_length(pic_state *pic) { pic_str *str; pic_get_args(pic, "s", &str); return pic_int_value(pic_str_len(str)); }
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 void write_str(pic_state *pic, pic_value str, pic_value port, struct writer_control *p) { int i; const char *cstr = pic_str(pic, str); if (p->mode == DISPLAY_MODE) { pic_fprintf(pic, port, "%s", pic_str(pic, str)); return; } pic_fprintf(pic, port, "\""); for (i = 0; i < pic_str_len(pic, str); ++i) { if (cstr[i] == '"' || cstr[i] == '\\') { pic_fputc(pic, '\\', port); } pic_fputc(pic, cstr[i], port); } pic_fprintf(pic, port, "\""); }
static void write_str(pic_state *pic, pic_str *str, xFILE *file, int mode) { size_t i; const char *cstr = pic_str_cstr(pic, str); if (mode == DISPLAY_MODE) { xfprintf(pic, file, "%s", pic_str_cstr(pic, str)); return; } xfprintf(pic, file, "\""); for (i = 0; i < pic_str_len(str); ++i) { if (cstr[i] == '"' || cstr[i] == '\\') { xfputc(pic, '\\', file); } xfputc(pic, cstr[i], file); } xfprintf(pic, file, "\""); }
static pic_value pic_str_string_fill_ip(pic_state *pic) { pic_str *str; char c; int n, start, end; n = pic_get_args(pic, "sc|ii", &str, &c, &start, &end); switch (n) { case 2: start = 0; case 3: end = pic_str_len(str); } while (start < end) { pic_str_set(pic, str, start++, c); } return pic_undef_value(); }
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(); }