2014-08-25 00:38:09 -04:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "picrin.h"
|
2017-03-28 10:09:40 -04:00
|
|
|
#include "object.h"
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-21 06:32:00 -05:00
|
|
|
struct rope {
|
2016-01-09 09:29:36 -05:00
|
|
|
int refcnt;
|
2016-02-26 10:44:32 -05:00
|
|
|
int weight;
|
2016-02-26 11:38:07 -05:00
|
|
|
bool isleaf;
|
|
|
|
union {
|
|
|
|
struct {
|
2016-02-29 02:48:38 -05:00
|
|
|
struct rope *owner;
|
|
|
|
const char *str; /* always points to zero-term'd buf */
|
2016-02-26 11:38:07 -05:00
|
|
|
} leaf;
|
|
|
|
struct {
|
|
|
|
struct rope *left, *right;
|
|
|
|
} node;
|
|
|
|
} u;
|
2016-02-29 02:48:38 -05:00
|
|
|
char buf[1];
|
2015-05-27 10:34:40 -04:00
|
|
|
};
|
|
|
|
|
2016-02-26 11:38:07 -05:00
|
|
|
struct rope *
|
|
|
|
pic_rope_incref(struct rope *rope) {
|
|
|
|
rope->refcnt++;
|
|
|
|
return rope;
|
2015-05-27 10:34:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-02-26 11:38:07 -05:00
|
|
|
pic_rope_decref(pic_state *pic, struct rope *rope) {
|
|
|
|
if (! --rope->refcnt) {
|
|
|
|
if (rope->isleaf) {
|
2016-02-29 02:48:38 -05:00
|
|
|
if (rope->u.leaf.owner) {
|
|
|
|
pic_rope_decref(pic, rope->u.leaf.owner);
|
|
|
|
}
|
2016-02-26 11:38:07 -05:00
|
|
|
} else {
|
|
|
|
pic_rope_decref(pic, rope->u.node.left);
|
|
|
|
pic_rope_decref(pic, rope->u.node.right);
|
2016-01-09 09:29:36 -05:00
|
|
|
}
|
2016-02-26 11:38:07 -05:00
|
|
|
pic_free(pic, rope);
|
2016-01-09 09:29:36 -05:00
|
|
|
}
|
2015-05-27 10:34:40 -04:00
|
|
|
}
|
|
|
|
|
2016-02-29 02:48:38 -05:00
|
|
|
static struct rope *
|
|
|
|
make_rope_leaf(pic_state *pic, const char *str, int len)
|
2015-05-27 10:34:40 -04:00
|
|
|
{
|
2016-02-29 02:48:38 -05:00
|
|
|
struct rope *rope;
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-02-29 02:48:38 -05:00
|
|
|
rope = pic_malloc(pic, offsetof(struct rope, buf) + len + 1);
|
|
|
|
rope->refcnt = 1;
|
|
|
|
rope->weight = len;
|
|
|
|
rope->isleaf = true;
|
|
|
|
rope->u.leaf.owner = NULL;
|
|
|
|
rope->u.leaf.str = rope->buf;
|
|
|
|
rope->buf[len] = 0;
|
2016-02-26 11:38:07 -05:00
|
|
|
if (str) {
|
2016-02-29 02:48:38 -05:00
|
|
|
memcpy(rope->buf, str, len);
|
2016-02-26 11:38:07 -05:00
|
|
|
}
|
2016-02-29 02:48:38 -05:00
|
|
|
|
|
|
|
return rope;
|
2015-05-27 10:34:40 -04:00
|
|
|
}
|
|
|
|
|
2016-02-29 02:48:38 -05:00
|
|
|
static struct rope *
|
|
|
|
make_rope_lit(pic_state *pic, const char *str, int len)
|
2016-02-07 12:31:45 -05:00
|
|
|
{
|
2016-02-29 02:48:38 -05:00
|
|
|
struct rope *rope;
|
2016-02-07 12:31:45 -05:00
|
|
|
|
2016-02-29 02:48:38 -05:00
|
|
|
rope = pic_malloc(pic, offsetof(struct rope, buf));
|
|
|
|
rope->refcnt = 1;
|
|
|
|
rope->weight = len;
|
|
|
|
rope->isleaf = true;
|
|
|
|
rope->u.leaf.owner = NULL;
|
|
|
|
rope->u.leaf.str = str;
|
2016-02-07 12:31:45 -05:00
|
|
|
|
2016-02-29 02:48:38 -05:00
|
|
|
return rope;
|
2016-02-07 12:31:45 -05:00
|
|
|
}
|
|
|
|
|
2016-02-21 06:32:00 -05:00
|
|
|
static struct rope *
|
2016-02-29 02:48:38 -05:00
|
|
|
make_rope_slice(pic_state *pic, struct rope *owner, int i, int j)
|
2015-05-27 10:34:40 -04:00
|
|
|
{
|
2017-03-23 09:33:41 -04:00
|
|
|
struct rope *rope, *real_owner;
|
2016-02-26 11:38:07 -05:00
|
|
|
|
2016-02-29 02:48:38 -05:00
|
|
|
assert(owner->isleaf);
|
|
|
|
|
2017-03-23 09:33:41 -04:00
|
|
|
real_owner = owner->u.leaf.owner == NULL ? owner : owner->u.leaf.owner;
|
2016-02-29 02:48:38 -05:00
|
|
|
|
|
|
|
rope = pic_malloc(pic, offsetof(struct rope, buf));
|
2016-02-26 11:38:07 -05:00
|
|
|
rope->refcnt = 1;
|
2016-02-29 02:48:38 -05:00
|
|
|
rope->weight = j - i;
|
2016-02-26 11:38:07 -05:00
|
|
|
rope->isleaf = true;
|
2017-03-23 09:33:41 -04:00
|
|
|
rope->u.leaf.owner = real_owner;
|
2016-02-29 02:48:38 -05:00
|
|
|
rope->u.leaf.str = owner->u.leaf.str + i;
|
|
|
|
|
2017-03-23 09:33:41 -04:00
|
|
|
pic_rope_incref(real_owner);
|
2016-02-26 11:38:07 -05:00
|
|
|
|
|
|
|
return rope;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct rope *
|
|
|
|
make_rope_node(pic_state *pic, struct rope *left, struct rope *right)
|
|
|
|
{
|
|
|
|
struct rope *rope;
|
|
|
|
|
|
|
|
rope = pic_malloc(pic, sizeof(struct rope));
|
|
|
|
rope->refcnt = 1;
|
|
|
|
rope->weight = left->weight + right->weight;
|
|
|
|
rope->isleaf = false;
|
|
|
|
rope->u.node.left = pic_rope_incref(left);
|
|
|
|
rope->u.node.right = pic_rope_incref(right);
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-02-26 11:38:07 -05:00
|
|
|
return rope;
|
2015-05-27 10:34:40 -04:00
|
|
|
}
|
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
static pic_value
|
2016-02-26 10:44:32 -05:00
|
|
|
make_str(pic_state *pic, struct rope *rope)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2016-02-21 06:32:00 -05:00
|
|
|
struct string *str;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-21 06:32:00 -05:00
|
|
|
str = (struct string *)pic_obj_alloc(pic, sizeof(struct string), PIC_TYPE_STRING);
|
2016-01-09 09:29:36 -05:00
|
|
|
str->rope = rope; /* delegate ownership */
|
2016-02-19 13:26:52 -05:00
|
|
|
|
2017-03-28 19:32:28 -04:00
|
|
|
return obj_value(pic, str);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2016-02-21 06:32:00 -05:00
|
|
|
static struct rope *
|
2016-02-26 11:38:07 -05:00
|
|
|
merge(pic_state *pic, struct rope *left, struct rope *right)
|
2015-05-27 10:34:40 -04:00
|
|
|
{
|
2016-02-26 11:55:49 -05:00
|
|
|
if (left == 0)
|
|
|
|
return pic_rope_incref(right);
|
|
|
|
if (right == 0)
|
|
|
|
return pic_rope_incref(left);
|
|
|
|
|
2016-02-26 11:38:07 -05:00
|
|
|
return make_rope_node(pic, left, right);
|
2015-05-27 10:34:40 -04:00
|
|
|
}
|
|
|
|
|
2016-02-21 06:32:00 -05:00
|
|
|
static struct rope *
|
2016-02-26 11:38:07 -05:00
|
|
|
slice(pic_state *pic, struct rope *rope, int i, int j)
|
2015-05-27 10:34:40 -04:00
|
|
|
{
|
2016-02-29 02:48:38 -05:00
|
|
|
int lweight;
|
|
|
|
|
2016-02-26 11:38:07 -05:00
|
|
|
if (i == 0 && rope->weight == j) {
|
|
|
|
return pic_rope_incref(rope);
|
2016-01-09 09:29:36 -05:00
|
|
|
}
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-02-26 11:38:07 -05:00
|
|
|
if (rope->isleaf) {
|
2016-02-29 02:48:38 -05:00
|
|
|
return make_rope_slice(pic, rope, i, j);
|
2016-01-09 09:29:36 -05:00
|
|
|
}
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-02-29 02:48:38 -05:00
|
|
|
lweight = rope->u.node.left->weight;
|
|
|
|
|
|
|
|
if (j <= lweight) {
|
2016-02-26 11:38:07 -05:00
|
|
|
return slice(pic, rope->u.node.left, i, j);
|
2016-02-29 02:48:38 -05:00
|
|
|
} else if (lweight <= i) {
|
|
|
|
return slice(pic, rope->u.node.right, i - lweight, j - lweight);
|
2016-02-26 11:38:07 -05:00
|
|
|
} else {
|
2016-02-21 06:32:00 -05:00
|
|
|
struct rope *r, *l;
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-02-29 02:48:38 -05:00
|
|
|
l = slice(pic, rope->u.node.left, i, lweight);
|
|
|
|
r = slice(pic, rope->u.node.right, 0, j - lweight);
|
2016-02-26 11:38:07 -05:00
|
|
|
rope = merge(pic, l, r);
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
pic_rope_decref(pic, l);
|
|
|
|
pic_rope_decref(pic, r);
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-02-26 11:38:07 -05:00
|
|
|
return rope;
|
2016-01-09 09:29:36 -05:00
|
|
|
}
|
2015-05-27 10:34:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-02-29 02:48:38 -05:00
|
|
|
flatten(pic_state *pic, struct rope *rope, struct rope *owner, char *buf)
|
2015-05-27 10:34:40 -04:00
|
|
|
{
|
2016-02-26 11:38:07 -05:00
|
|
|
if (rope->isleaf) {
|
2016-02-26 11:55:49 -05:00
|
|
|
memcpy(buf, rope->u.leaf.str, rope->weight);
|
|
|
|
} else {
|
2016-02-29 02:48:38 -05:00
|
|
|
flatten(pic, rope->u.node.left, owner, buf);
|
|
|
|
flatten(pic, rope->u.node.right, owner, buf + rope->u.node.left->weight);
|
2016-02-26 11:55:49 -05:00
|
|
|
}
|
|
|
|
|
2016-02-29 02:48:38 -05:00
|
|
|
/* path compression */
|
|
|
|
|
|
|
|
if (! rope->isleaf) {
|
|
|
|
pic_rope_incref(owner);
|
2016-02-26 11:38:07 -05:00
|
|
|
pic_rope_decref(pic, rope->u.node.left);
|
|
|
|
pic_rope_decref(pic, rope->u.node.right);
|
|
|
|
rope->isleaf = true;
|
2016-02-29 02:48:38 -05:00
|
|
|
rope->u.leaf.owner = owner;
|
2016-02-26 11:55:49 -05:00
|
|
|
rope->u.leaf.str = buf;
|
2016-01-09 09:29:36 -05:00
|
|
|
}
|
2015-05-27 10:34:40 -04:00
|
|
|
}
|
|
|
|
|
2016-02-20 11:04:55 -05:00
|
|
|
static void
|
|
|
|
str_update(pic_state *pic, pic_value dst, pic_value src)
|
|
|
|
{
|
2016-02-26 11:38:07 -05:00
|
|
|
pic_rope_incref(pic_str_ptr(pic, src)->rope);
|
2016-02-20 11:04:55 -05:00
|
|
|
pic_rope_decref(pic, pic_str_ptr(pic, dst)->rope);
|
|
|
|
pic_str_ptr(pic, dst)->rope = pic_str_ptr(pic, src)->rope;
|
|
|
|
}
|
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
pic_value
|
2016-02-18 09:49:16 -05:00
|
|
|
pic_str_value(pic_state *pic, const char *str, int len)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2016-02-29 02:48:38 -05:00
|
|
|
struct rope *r;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-07 12:31:45 -05:00
|
|
|
if (len > 0) {
|
2016-02-29 02:48:38 -05:00
|
|
|
r = make_rope_leaf(pic, str, len);
|
2016-02-07 12:31:45 -05:00
|
|
|
} else {
|
|
|
|
if (len == 0) {
|
|
|
|
str = "";
|
|
|
|
}
|
2016-02-29 02:48:38 -05:00
|
|
|
r = make_rope_lit(pic, str, -len);
|
2016-02-07 12:31:45 -05:00
|
|
|
}
|
2016-02-29 02:48:38 -05:00
|
|
|
return make_str(pic, r);
|
2016-02-26 11:38:07 -05:00
|
|
|
}
|
|
|
|
|
2017-04-02 11:37:37 -04:00
|
|
|
pic_value
|
|
|
|
pic_cstr_value(pic_state *pic, const char *cstr)
|
|
|
|
{
|
|
|
|
return pic_str_value(pic, cstr, strlen(cstr));
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:38:07 -05:00
|
|
|
pic_value
|
|
|
|
pic_strf_value(pic_state *pic, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
pic_value str;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
str = pic_vstrf_value(pic, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
|
|
|
pic_vstrf_value(pic_state *pic, const char *fmt, va_list ap)
|
|
|
|
{
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_value str, port;
|
2016-02-26 11:38:07 -05:00
|
|
|
const char *buf;
|
|
|
|
int len;
|
|
|
|
|
2016-06-19 15:49:01 -04:00
|
|
|
port = pic_fmemopen(pic, NULL, 0, "w");
|
2016-02-26 11:38:07 -05:00
|
|
|
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_vfprintf(pic, port, fmt, ap);
|
|
|
|
pic_fgetbuf(pic, port, &buf, &len);
|
2016-02-26 11:38:07 -05:00
|
|
|
str = pic_str_value(pic, buf, len);
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fclose(pic, port);
|
2016-02-26 11:38:07 -05:00
|
|
|
return str;
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2015-08-26 06:04:27 -04:00
|
|
|
int
|
2016-02-21 05:19:35 -05:00
|
|
|
pic_str_len(pic_state *PIC_UNUSED(pic), pic_value str)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2016-02-26 10:44:32 -05:00
|
|
|
return pic_str_ptr(pic, str)->rope->weight;
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
pic_value
|
|
|
|
pic_str_cat(pic_state *pic, pic_value a, pic_value b)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2016-02-26 11:38:07 -05:00
|
|
|
return make_str(pic, merge(pic, pic_str_ptr(pic, a)->rope, pic_str_ptr(pic, b)->rope));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
pic_value
|
|
|
|
pic_str_sub(pic_state *pic, pic_value str, int s, int e)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2016-02-26 11:38:07 -05:00
|
|
|
return make_str(pic, slice(pic, pic_str_ptr(pic, str)->rope, s, e));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2017-03-28 10:31:15 -04:00
|
|
|
pic_str(pic_state *pic, pic_value str, int *len)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2016-02-29 02:48:38 -05:00
|
|
|
struct rope *rope = pic_str_ptr(pic, str)->rope, *r;
|
2016-02-26 11:55:49 -05:00
|
|
|
|
2017-03-28 10:31:15 -04:00
|
|
|
if (len) {
|
|
|
|
*len = rope->weight;
|
|
|
|
}
|
|
|
|
|
2016-02-29 02:48:38 -05:00
|
|
|
if (rope->isleaf && rope->u.leaf.str[rope->weight] == '\0') {
|
|
|
|
return rope->u.leaf.str;
|
2016-02-26 11:55:49 -05:00
|
|
|
}
|
|
|
|
|
2016-02-29 02:48:38 -05:00
|
|
|
r = make_rope_leaf(pic, 0, rope->weight);
|
2016-02-26 11:55:49 -05:00
|
|
|
|
2016-02-29 02:48:38 -05:00
|
|
|
flatten(pic, rope, r, r->buf);
|
2016-02-26 11:55:49 -05:00
|
|
|
|
2016-02-29 02:48:38 -05:00
|
|
|
return r->u.leaf.str;
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_str_string_p(pic_state *pic)
|
|
|
|
{
|
2016-01-09 09:29:36 -05:00
|
|
|
pic_value v;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
pic_get_args(pic, "o", &v);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_bool_value(pic, pic_str_p(pic, v));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2014-09-12 07:23:58 -04:00
|
|
|
static pic_value
|
|
|
|
pic_str_string(pic_state *pic)
|
|
|
|
{
|
2016-01-09 09:29:36 -05:00
|
|
|
int argc, i;
|
|
|
|
pic_value *argv;
|
|
|
|
char *buf;
|
2014-09-12 07:23:58 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
pic_get_args(pic, "*", &argc, &argv);
|
2014-09-12 07:23:58 -04:00
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
buf = pic_alloca(pic, argc);
|
2014-09-12 07:23:58 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
for (i = 0; i < argc; ++i) {
|
2016-02-23 08:53:20 -05:00
|
|
|
TYPE_CHECK(pic, argv[i], char);
|
2016-02-18 06:15:42 -05:00
|
|
|
buf[i] = pic_char(pic, argv[i]);
|
2016-01-09 09:29:36 -05:00
|
|
|
}
|
2014-09-12 07:23:58 -04:00
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
return pic_str_value(pic, buf, argc);
|
2014-09-12 07:23:58 -04:00
|
|
|
}
|
|
|
|
|
2014-09-08 12:12:52 -04:00
|
|
|
static pic_value
|
|
|
|
pic_str_make_string(pic_state *pic)
|
|
|
|
{
|
2016-01-09 09:29:36 -05:00
|
|
|
int len;
|
|
|
|
char c = ' ';
|
|
|
|
char *buf;
|
2014-09-08 12:12:52 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
pic_get_args(pic, "i|c", &len, &c);
|
2014-09-08 12:12:52 -04:00
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
if (len < 0) {
|
2016-02-22 14:03:42 -05:00
|
|
|
pic_error(pic, "make-string: negative length given", 1, pic_int_value(pic, len));
|
2016-02-19 13:26:52 -05:00
|
|
|
}
|
2015-07-12 20:03:32 -04:00
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
buf = pic_alloca(pic, len);
|
2015-07-12 20:03:32 -04:00
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
memset(buf, c, len);
|
|
|
|
|
|
|
|
return pic_str_value(pic, buf, len);
|
2014-09-08 12:12:52 -04:00
|
|
|
}
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
static pic_value
|
|
|
|
pic_str_string_length(pic_state *pic)
|
|
|
|
{
|
2016-02-19 13:26:52 -05:00
|
|
|
pic_value str;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
pic_get_args(pic, "s", &str);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_int_value(pic, pic_str_len(pic, str));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_str_string_ref(pic_state *pic)
|
|
|
|
{
|
2016-02-19 13:26:52 -05:00
|
|
|
pic_value str;
|
2016-01-09 09:29:36 -05:00
|
|
|
int k;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
pic_get_args(pic, "si", &str, &k);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
VALID_INDEX(pic, pic_str_len(pic, str), k);
|
|
|
|
|
2017-03-30 03:19:20 -04:00
|
|
|
return pic_char_value(pic, pic_str(pic, str, 0)[k]);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2016-02-20 11:04:55 -05:00
|
|
|
static pic_value
|
|
|
|
pic_str_string_set(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value str, x, y, z;
|
|
|
|
char c;
|
|
|
|
int k, len;
|
|
|
|
|
|
|
|
pic_get_args(pic, "sic", &str, &k, &c);
|
|
|
|
|
|
|
|
len = pic_str_len(pic, str);
|
|
|
|
|
|
|
|
VALID_INDEX(pic, len, k);
|
|
|
|
|
|
|
|
x = pic_str_sub(pic, str, 0, k);
|
|
|
|
y = pic_str_value(pic, &c, 1);
|
|
|
|
z = pic_str_sub(pic, str, k + 1, len);
|
|
|
|
|
|
|
|
str_update(pic, str, pic_str_cat(pic, x, pic_str_cat(pic, y, z)));
|
|
|
|
|
|
|
|
return pic_undef_value(pic);
|
|
|
|
}
|
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
#define DEFINE_STRING_CMP(name, op) \
|
|
|
|
static pic_value \
|
|
|
|
pic_str_string_##name(pic_state *pic) \
|
|
|
|
{ \
|
|
|
|
int argc, i; \
|
|
|
|
pic_value *argv; \
|
|
|
|
\
|
|
|
|
pic_get_args(pic, "*", &argc, &argv); \
|
|
|
|
\
|
|
|
|
if (argc < 1 || ! pic_str_p(pic, argv[0])) { \
|
|
|
|
return pic_false_value(pic); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
for (i = 1; i < argc; ++i) { \
|
|
|
|
if (! pic_str_p(pic, argv[i])) { \
|
|
|
|
return pic_false_value(pic); \
|
|
|
|
} \
|
2017-03-30 03:19:20 -04:00
|
|
|
if (! (strcmp(pic_str(pic, argv[i-1], NULL), pic_str(pic, argv[i], NULL)) op 0)) { \
|
2016-02-19 13:26:52 -05:00
|
|
|
return pic_false_value(pic); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
return pic_true_value(pic); \
|
2015-12-29 09:28:31 -05:00
|
|
|
}
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
DEFINE_STRING_CMP(eq, ==)
|
|
|
|
DEFINE_STRING_CMP(lt, <)
|
|
|
|
DEFINE_STRING_CMP(gt, >)
|
|
|
|
DEFINE_STRING_CMP(le, <=)
|
|
|
|
DEFINE_STRING_CMP(ge, >=)
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_str_string_copy(pic_state *pic)
|
|
|
|
{
|
2016-02-19 13:26:52 -05:00
|
|
|
pic_value str;
|
2015-12-29 09:28:31 -05:00
|
|
|
int n, start, end, len;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
n = pic_get_args(pic, "s|ii", &str, &start, &end);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-18 06:15:42 -05:00
|
|
|
len = pic_str_len(pic, str);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-12-29 09:28:31 -05:00
|
|
|
switch (n) {
|
|
|
|
case 1:
|
|
|
|
start = 0;
|
|
|
|
case 2:
|
|
|
|
end = len;
|
|
|
|
}
|
2015-12-27 23:47:29 -05:00
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
VALID_RANGE(pic, len, start, end);
|
2015-12-27 23:47:29 -05:00
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
return pic_str_sub(pic, str, start, end);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2016-02-20 11:04:55 -05:00
|
|
|
static pic_value
|
|
|
|
pic_str_string_copy_ip(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value to, from, x, y, z;
|
|
|
|
int n, at, start, end, tolen, fromlen;
|
|
|
|
|
|
|
|
n = pic_get_args(pic, "sis|ii", &to, &at, &from, &start, &end);
|
|
|
|
|
|
|
|
tolen = pic_str_len(pic, to);
|
|
|
|
fromlen = pic_str_len(pic, from);
|
|
|
|
|
|
|
|
switch (n) {
|
|
|
|
case 3:
|
|
|
|
start = 0;
|
|
|
|
case 4:
|
|
|
|
end = fromlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALID_ATRANGE(pic, tolen, at, fromlen, start, end);
|
|
|
|
|
|
|
|
x = pic_str_sub(pic, to, 0, at);
|
|
|
|
y = pic_str_sub(pic, from, start, end);
|
|
|
|
z = pic_str_sub(pic, to, at + end - start, tolen);
|
|
|
|
|
|
|
|
str_update(pic, to, pic_str_cat(pic, x, pic_str_cat(pic, y, z)));
|
|
|
|
|
|
|
|
return pic_undef_value(pic);
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_str_string_fill_ip(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value str, x, y, z;
|
|
|
|
char c, *buf;
|
|
|
|
int n, start, end, len;
|
|
|
|
|
|
|
|
n = pic_get_args(pic, "sc|ii", &str, &c, &start, &end);
|
|
|
|
|
|
|
|
len = pic_str_len(pic, str);
|
|
|
|
|
|
|
|
switch (n) {
|
|
|
|
case 2:
|
|
|
|
start = 0;
|
|
|
|
case 3:
|
|
|
|
end = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALID_RANGE(pic, len, start, end);
|
|
|
|
|
|
|
|
buf = pic_alloca(pic, end - start);
|
|
|
|
memset(buf, c, end - start);
|
|
|
|
|
|
|
|
x = pic_str_sub(pic, str, 0, start);
|
|
|
|
y = pic_str_value(pic, buf, end - start);
|
|
|
|
z = pic_str_sub(pic, str, end, len);
|
|
|
|
|
|
|
|
str_update(pic, str, pic_str_cat(pic, x, pic_str_cat(pic, y, z)));
|
|
|
|
|
|
|
|
return pic_undef_value(pic);
|
|
|
|
}
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
static pic_value
|
|
|
|
pic_str_string_append(pic_state *pic)
|
|
|
|
{
|
2016-01-09 09:29:36 -05:00
|
|
|
int argc, i;
|
|
|
|
pic_value *argv;
|
2016-02-19 13:26:52 -05:00
|
|
|
pic_value str = pic_lit_value(pic, "");
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
pic_get_args(pic, "*", &argc, &argv);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
for (i = 0; i < argc; ++i) {
|
2016-02-23 08:53:20 -05:00
|
|
|
TYPE_CHECK(pic, argv[i], str);
|
2016-02-19 13:26:52 -05:00
|
|
|
str = pic_str_cat(pic, str, argv[i]);
|
2016-01-09 09:29:36 -05:00
|
|
|
}
|
2016-02-19 13:26:52 -05:00
|
|
|
return str;
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2014-09-19 05:16:28 -04:00
|
|
|
static pic_value
|
|
|
|
pic_str_string_map(pic_state *pic)
|
|
|
|
{
|
2016-02-19 10:03:16 -05:00
|
|
|
pic_value proc, *argv, vals, val;
|
2016-01-09 09:29:36 -05:00
|
|
|
int argc, i, len, j;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
pic_get_args(pic, "l*", &proc, &argc, &argv);
|
|
|
|
|
|
|
|
if (argc == 0) {
|
2016-02-22 14:03:42 -05:00
|
|
|
pic_error(pic, "string-map: one or more strings expected, but got zero", 0);
|
2016-01-09 09:29:36 -05:00
|
|
|
}
|
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
len = INT_MAX;
|
|
|
|
for (i = 0; i < argc; ++i) {
|
|
|
|
int l;
|
2016-02-23 08:53:20 -05:00
|
|
|
TYPE_CHECK(pic, argv[i], str);
|
2016-02-19 13:26:52 -05:00
|
|
|
l = pic_str_len(pic, argv[i]);
|
|
|
|
len = len < l ? len : l;
|
2016-01-09 09:29:36 -05:00
|
|
|
}
|
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
buf = pic_alloca(pic, len);
|
2016-01-09 09:29:36 -05:00
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
vals = pic_nil_value(pic);
|
|
|
|
for (j = 0; j < argc; ++j) {
|
2017-03-30 03:19:20 -04:00
|
|
|
pic_push(pic, pic_char_value(pic, pic_str(pic, argv[j], 0)[i]), vals);
|
2016-01-09 09:29:36 -05:00
|
|
|
}
|
2016-02-19 13:26:52 -05:00
|
|
|
vals = pic_reverse(pic, vals);
|
2017-04-02 06:16:25 -04:00
|
|
|
val = pic_funcall(pic, "apply", 2, proc, vals);
|
2016-01-09 09:29:36 -05:00
|
|
|
|
2016-02-23 08:53:20 -05:00
|
|
|
TYPE_CHECK(pic, val, char);
|
2016-01-09 09:29:36 -05:00
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
buf[i] = pic_char(pic, val);
|
|
|
|
}
|
|
|
|
return pic_str_value(pic, buf, len);
|
2014-09-19 05:16:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_str_string_for_each(pic_state *pic)
|
|
|
|
{
|
2016-02-19 10:03:16 -05:00
|
|
|
pic_value proc, *argv, vals;
|
2016-02-19 13:26:52 -05:00
|
|
|
int argc, i, len, j;
|
2014-09-19 05:16:28 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
pic_get_args(pic, "l*", &proc, &argc, &argv);
|
2014-09-19 05:16:28 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
if (argc == 0) {
|
2016-02-22 14:03:42 -05:00
|
|
|
pic_error(pic, "string-map: one or more strings expected, but got zero", 0);
|
2016-01-09 09:29:36 -05:00
|
|
|
}
|
2014-09-19 05:16:28 -04:00
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
len = INT_MAX;
|
|
|
|
for (i = 0; i < argc; ++i) {
|
|
|
|
int l;
|
2016-02-23 08:53:20 -05:00
|
|
|
TYPE_CHECK(pic, argv[i], str);
|
2016-02-19 13:26:52 -05:00
|
|
|
l = pic_str_len(pic, argv[i]);
|
|
|
|
len = len < l ? len : l;
|
2016-01-09 09:29:36 -05:00
|
|
|
}
|
2014-09-19 05:16:28 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
for (i = 0; i < len; ++i) {
|
2016-02-18 06:15:42 -05:00
|
|
|
vals = pic_nil_value(pic);
|
2016-01-09 09:29:36 -05:00
|
|
|
for (j = 0; j < argc; ++j) {
|
2017-03-30 03:19:20 -04:00
|
|
|
pic_push(pic, pic_char_value(pic, pic_str(pic, argv[j], 0)[i]), vals);
|
2016-01-09 09:29:36 -05:00
|
|
|
}
|
2016-02-19 13:26:52 -05:00
|
|
|
vals = pic_reverse(pic, vals);
|
2017-04-02 06:16:25 -04:00
|
|
|
pic_funcall(pic, "apply", 2, proc, vals);
|
2016-01-09 09:29:36 -05:00
|
|
|
}
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_undef_value(pic);
|
2014-09-19 05:16:28 -04:00
|
|
|
}
|
|
|
|
|
2014-09-10 07:09:32 -04:00
|
|
|
static pic_value
|
|
|
|
pic_str_list_to_string(pic_state *pic)
|
|
|
|
{
|
2016-01-09 09:29:36 -05:00
|
|
|
pic_value list, e, it;
|
|
|
|
int i;
|
|
|
|
char *buf;
|
2014-09-10 07:09:32 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
pic_get_args(pic, "o", &list);
|
2014-09-10 07:09:32 -04:00
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
buf = pic_alloca(pic, pic_length(pic, list));
|
2014-09-10 07:09:32 -04:00
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
i = 0;
|
|
|
|
pic_for_each (e, list, it) {
|
2016-02-23 08:53:20 -05:00
|
|
|
TYPE_CHECK(pic, e, char);
|
2014-09-10 07:09:32 -04:00
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
buf[i++] = pic_char(pic, e);
|
2016-01-09 09:29:36 -05:00
|
|
|
}
|
2015-01-27 09:58:03 -05:00
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
return pic_str_value(pic, buf, i);
|
2014-09-10 07:09:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_str_string_to_list(pic_state *pic)
|
|
|
|
{
|
2016-02-19 13:26:52 -05:00
|
|
|
pic_value str, list;
|
|
|
|
int n, start, end, len, i;
|
2014-09-10 07:09:32 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
n = pic_get_args(pic, "s|ii", &str, &start, &end);
|
2014-09-10 07:09:32 -04:00
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
len = pic_str_len(pic, str);
|
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
switch (n) {
|
|
|
|
case 1:
|
|
|
|
start = 0;
|
|
|
|
case 2:
|
2016-02-19 13:26:52 -05:00
|
|
|
end = len;
|
2016-01-09 09:29:36 -05:00
|
|
|
}
|
2014-09-10 07:09:32 -04:00
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
VALID_RANGE(pic, len, start, end);
|
2014-09-10 07:09:32 -04:00
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
list = pic_nil_value(pic);
|
2016-01-09 09:29:36 -05:00
|
|
|
for (i = start; i < end; ++i) {
|
2017-03-30 03:19:20 -04:00
|
|
|
pic_push(pic, pic_char_value(pic, pic_str(pic, str, 0)[i]), list);
|
2016-01-09 09:29:36 -05:00
|
|
|
}
|
|
|
|
return pic_reverse(pic, list);
|
2014-09-10 07:09:32 -04:00
|
|
|
}
|
2017-03-28 10:31:15 -04:00
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
void
|
|
|
|
pic_init_str(pic_state *pic)
|
|
|
|
{
|
2016-01-09 09:29:36 -05:00
|
|
|
pic_defun(pic, "string?", pic_str_string_p);
|
|
|
|
pic_defun(pic, "string", pic_str_string);
|
|
|
|
pic_defun(pic, "make-string", pic_str_make_string);
|
|
|
|
pic_defun(pic, "string-length", pic_str_string_length);
|
|
|
|
pic_defun(pic, "string-ref", pic_str_string_ref);
|
2016-02-20 11:04:55 -05:00
|
|
|
pic_defun(pic, "string-set!", pic_str_string_set);
|
2016-01-09 09:29:36 -05:00
|
|
|
pic_defun(pic, "string-copy", pic_str_string_copy);
|
2016-02-20 11:04:55 -05:00
|
|
|
pic_defun(pic, "string-copy!", pic_str_string_copy_ip);
|
|
|
|
pic_defun(pic, "string-fill!", pic_str_string_fill_ip);
|
2016-01-09 09:29:36 -05:00
|
|
|
pic_defun(pic, "string-append", pic_str_string_append);
|
|
|
|
pic_defun(pic, "string-map", pic_str_string_map);
|
|
|
|
pic_defun(pic, "string-for-each", pic_str_string_for_each);
|
|
|
|
pic_defun(pic, "list->string", pic_str_list_to_string);
|
|
|
|
pic_defun(pic, "string->list", pic_str_string_to_list);
|
|
|
|
|
|
|
|
pic_defun(pic, "string=?", pic_str_string_eq);
|
|
|
|
pic_defun(pic, "string<?", pic_str_string_lt);
|
|
|
|
pic_defun(pic, "string>?", pic_str_string_gt);
|
|
|
|
pic_defun(pic, "string<=?", pic_str_string_le);
|
|
|
|
pic_defun(pic, "string>=?", pic_str_string_ge);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|