2014-08-25 00:38:09 -04:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "picrin.h"
|
2016-02-18 10:14:50 -05:00
|
|
|
#include "picrin/object.h"
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2015-05-27 10:34:40 -04:00
|
|
|
struct pic_chunk {
|
2016-01-09 09:29:36 -05:00
|
|
|
char *str;
|
|
|
|
int refcnt;
|
|
|
|
size_t len;
|
|
|
|
char buf[1];
|
2015-05-27 10:34:40 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct pic_rope {
|
2016-01-09 09:29:36 -05:00
|
|
|
int refcnt;
|
|
|
|
size_t weight;
|
|
|
|
struct pic_chunk *chunk;
|
|
|
|
size_t offset;
|
|
|
|
struct pic_rope *left, *right;
|
2015-05-27 10:34:40 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#define CHUNK_INCREF(c) do { \
|
2016-01-09 09:29:36 -05:00
|
|
|
(c)->refcnt++; \
|
|
|
|
} while (0)
|
2015-05-27 10:34:40 -04:00
|
|
|
|
|
|
|
#define CHUNK_DECREF(c) do { \
|
2016-01-09 09:29:36 -05:00
|
|
|
struct pic_chunk *c_ = (c); \
|
|
|
|
if (! --c_->refcnt) { \
|
|
|
|
pic_free(pic, c_); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2015-05-27 10:34:40 -04:00
|
|
|
|
|
|
|
void
|
2015-05-28 04:06:41 -04:00
|
|
|
pic_rope_incref(pic_state PIC_UNUSED(*pic), struct pic_rope *x) {
|
2016-01-09 09:29:36 -05:00
|
|
|
x->refcnt++;
|
2015-05-27 10:34:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_rope_decref(pic_state *pic, struct pic_rope *x) {
|
2016-01-09 09:29:36 -05:00
|
|
|
if (! --x->refcnt) {
|
|
|
|
if (x->chunk) {
|
|
|
|
CHUNK_DECREF(x->chunk);
|
|
|
|
pic_free(pic, x);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pic_rope_decref(pic, x->left);
|
|
|
|
pic_rope_decref(pic, x->right);
|
|
|
|
pic_free(pic, x);
|
|
|
|
}
|
2015-05-27 10:34:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct pic_chunk *
|
|
|
|
pic_make_chunk(pic_state *pic, const char *str, size_t len)
|
|
|
|
{
|
2016-01-09 09:29:36 -05:00
|
|
|
struct pic_chunk *c;
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-02-07 11:17:33 -05:00
|
|
|
c = pic_malloc(pic, offsetof(struct pic_chunk, buf) + len + 1);
|
2016-01-09 09:29:36 -05:00
|
|
|
c->refcnt = 1;
|
|
|
|
c->str = c->buf;
|
|
|
|
c->len = len;
|
|
|
|
c->buf[len] = 0;
|
|
|
|
memcpy(c->buf, str, len);
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
return c;
|
2015-05-27 10:34:40 -04:00
|
|
|
}
|
|
|
|
|
2016-02-07 12:31:45 -05:00
|
|
|
static struct pic_chunk *
|
|
|
|
pic_make_chunk_lit(pic_state *pic, const char *str, size_t len)
|
|
|
|
{
|
|
|
|
struct pic_chunk *c;
|
|
|
|
|
|
|
|
c = pic_malloc(pic, sizeof(struct pic_chunk));
|
|
|
|
c->refcnt = 1;
|
|
|
|
c->str = (char *)str;
|
|
|
|
c->len = len;
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2015-05-27 10:34:40 -04:00
|
|
|
static struct pic_rope *
|
|
|
|
pic_make_rope(pic_state *pic, struct pic_chunk *c)
|
|
|
|
{
|
2016-01-09 09:29:36 -05:00
|
|
|
struct pic_rope *x;
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
x = pic_malloc(pic, sizeof(struct pic_rope));
|
|
|
|
x->refcnt = 1;
|
|
|
|
x->left = NULL;
|
|
|
|
x->right = NULL;
|
|
|
|
x->weight = c->len;
|
|
|
|
x->offset = 0;
|
|
|
|
x->chunk = c; /* delegate ownership */
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
return x;
|
2015-05-27 10:34:40 -04:00
|
|
|
}
|
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
static pic_value
|
|
|
|
pic_make_str(pic_state *pic, struct pic_rope *rope)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2016-02-14 10:20:49 -05:00
|
|
|
struct pic_string *str;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-18 09:25:45 -05:00
|
|
|
str = (struct pic_string *)pic_obj_alloc(pic, sizeof(struct pic_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
|
|
|
|
|
|
|
return pic_obj_value(str);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2015-05-27 10:34:40 -04:00
|
|
|
static size_t
|
|
|
|
rope_len(struct pic_rope *x)
|
|
|
|
{
|
2016-01-09 09:29:36 -05:00
|
|
|
return x->weight;
|
2015-05-27 10:34:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static char
|
|
|
|
rope_at(struct pic_rope *x, size_t i)
|
|
|
|
{
|
2016-01-09 09:29:36 -05:00
|
|
|
while (i < x->weight) {
|
|
|
|
if (x->chunk) {
|
|
|
|
return x->chunk->str[x->offset + i];
|
|
|
|
}
|
|
|
|
if (i < x->left->weight) {
|
|
|
|
x = x->left;
|
|
|
|
} else {
|
|
|
|
i -= x->left->weight;
|
2016-02-18 13:57:15 -05:00
|
|
|
x = x->right;
|
2016-01-09 09:29:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
2015-05-27 10:34:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct pic_rope *
|
|
|
|
rope_cat(pic_state *pic, struct pic_rope *x, struct pic_rope *y)
|
|
|
|
{
|
2016-01-09 09:29:36 -05:00
|
|
|
struct pic_rope *z;
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
z = pic_malloc(pic, sizeof(struct pic_rope));
|
|
|
|
z->refcnt = 1;
|
|
|
|
z->left = x;
|
|
|
|
z->right = y;
|
|
|
|
z->weight = x->weight + y->weight;
|
|
|
|
z->offset = 0;
|
|
|
|
z->chunk = NULL;
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
pic_rope_incref(pic, x);
|
|
|
|
pic_rope_incref(pic, y);
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
return z;
|
2015-05-27 10:34:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct pic_rope *
|
|
|
|
rope_sub(pic_state *pic, struct pic_rope *x, size_t i, size_t j)
|
|
|
|
{
|
2016-01-09 09:29:36 -05:00
|
|
|
assert(i <= j);
|
|
|
|
assert(j <= x->weight);
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
if (i == 0 && x->weight == j) {
|
|
|
|
pic_rope_incref(pic, x);
|
|
|
|
return x;
|
|
|
|
}
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
if (x->chunk) {
|
|
|
|
struct pic_rope *y;
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
y = pic_malloc(pic, sizeof(struct pic_rope));
|
|
|
|
y->refcnt = 1;
|
|
|
|
y->left = NULL;
|
|
|
|
y->right = NULL;
|
|
|
|
y->weight = j - i;
|
|
|
|
y->offset = x->offset + i;
|
|
|
|
y->chunk = x->chunk;
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
CHUNK_INCREF(x->chunk);
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
return y;
|
|
|
|
}
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
if (j <= x->left->weight) {
|
|
|
|
return rope_sub(pic, x->left, i, j);
|
|
|
|
}
|
|
|
|
else if (x->left->weight <= i) {
|
|
|
|
return rope_sub(pic, x->right, i - x->left->weight, j - x->left->weight);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
struct pic_rope *r, *l;
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
l = rope_sub(pic, x->left, i, x->left->weight);
|
|
|
|
r = rope_sub(pic, x->right, 0, j - x->left->weight);
|
|
|
|
x = rope_cat(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-01-09 09:29:36 -05:00
|
|
|
return x;
|
|
|
|
}
|
2015-05-27 10:34:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
flatten(pic_state *pic, struct pic_rope *x, struct pic_chunk *c, size_t offset)
|
|
|
|
{
|
2016-01-09 09:29:36 -05:00
|
|
|
if (x->chunk) {
|
|
|
|
memcpy(c->str + offset, x->chunk->str + x->offset, x->weight);
|
|
|
|
CHUNK_DECREF(x->chunk);
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
x->chunk = c;
|
|
|
|
x->offset = offset;
|
|
|
|
CHUNK_INCREF(c);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
flatten(pic, x->left, c, offset);
|
|
|
|
flatten(pic, x->right, c, offset + x->left->weight);
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
pic_rope_decref(pic, x->left);
|
|
|
|
pic_rope_decref(pic, x->right);
|
|
|
|
x->left = x->right = NULL;
|
|
|
|
x->chunk = c;
|
|
|
|
x->offset = offset;
|
|
|
|
CHUNK_INCREF(c);
|
2015-05-27 10:34:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
rope_cstr(pic_state *pic, struct pic_rope *x)
|
|
|
|
{
|
2016-01-09 09:29:36 -05:00
|
|
|
struct pic_chunk *c;
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
if (x->chunk && x->offset == 0 && x->weight == x->chunk->len) {
|
|
|
|
return x->chunk->str; /* reuse cached chunk */
|
|
|
|
}
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-02-07 12:31:45 -05:00
|
|
|
c = pic_malloc(pic, offsetof(struct pic_chunk, buf) + x->weight + 1);
|
2016-01-09 09:29:36 -05:00
|
|
|
c->refcnt = 1;
|
|
|
|
c->len = x->weight;
|
|
|
|
c->str = c->buf;
|
|
|
|
c->str[c->len] = '\0';
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
flatten(pic, x, c, 0);
|
2015-05-27 10:34:40 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
CHUNK_DECREF(c);
|
|
|
|
return c->str;
|
2015-05-27 10:34:40 -04:00
|
|
|
}
|
|
|
|
|
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-07 12:31:45 -05:00
|
|
|
struct pic_chunk *c;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-07 12:31:45 -05:00
|
|
|
if (len > 0) {
|
|
|
|
c = pic_make_chunk(pic, str, len);
|
|
|
|
} else {
|
|
|
|
if (len == 0) {
|
|
|
|
str = "";
|
|
|
|
}
|
|
|
|
c = pic_make_chunk_lit(pic, str, -len);
|
|
|
|
}
|
2016-02-19 13:26:52 -05:00
|
|
|
return pic_make_str(pic, pic_make_rope(pic, c));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2015-08-26 06:04:27 -04:00
|
|
|
int
|
2016-02-19 13:26:52 -05:00
|
|
|
pic_str_len(pic_state PIC_UNUSED(*pic), pic_value str)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2016-02-19 13:26:52 -05:00
|
|
|
return rope_len(pic_str_ptr(pic, str)->rope);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
char
|
2016-02-19 13:26:52 -05:00
|
|
|
pic_str_ref(pic_state *pic, pic_value str, int i)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2016-01-09 09:29:36 -05:00
|
|
|
int c;
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
c = rope_at(pic_str_ptr(pic, str)->rope, i);
|
2016-01-09 09:29:36 -05:00
|
|
|
if (c == -1) {
|
|
|
|
pic_errorf(pic, "index out of range %d", i);
|
|
|
|
}
|
|
|
|
return (char)c;
|
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-19 13:26:52 -05:00
|
|
|
return pic_make_str(pic, rope_cat(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-19 13:26:52 -05:00
|
|
|
return pic_make_str(pic, rope_sub(pic, pic_str_ptr(pic, str)->rope, s, e));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-02-19 13:26:52 -05:00
|
|
|
pic_str_cmp(pic_state *pic, pic_value str1, pic_value str2)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2016-02-18 09:49:16 -05:00
|
|
|
return strcmp(pic_str(pic, str1), pic_str(pic, str2));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2016-02-07 13:36:20 -05:00
|
|
|
int
|
2016-02-19 13:26:52 -05:00
|
|
|
pic_str_hash(pic_state *pic, pic_value str)
|
2016-02-07 13:36:20 -05:00
|
|
|
{
|
|
|
|
const char *s;
|
|
|
|
int h = 0;
|
|
|
|
|
2016-02-18 09:49:16 -05:00
|
|
|
s = pic_str(pic, str);
|
2016-02-07 13:36:20 -05:00
|
|
|
while (*s) {
|
|
|
|
h = (h << 5) - h + *s++;
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
const char *
|
2016-02-19 13:26:52 -05:00
|
|
|
pic_str(pic_state *pic, pic_value str)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2016-02-19 13:26:52 -05:00
|
|
|
return rope_cstr(pic, pic_str_ptr(pic, str)->rope);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2016-02-07 11:32:13 -05:00
|
|
|
static void
|
2016-02-18 15:54:50 -05:00
|
|
|
vfstrf(pic_state *pic, xFILE *file, const char *fmt, va_list ap)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2016-01-09 09:29:36 -05:00
|
|
|
char c;
|
|
|
|
|
2016-02-07 11:32:13 -05:00
|
|
|
while ((c = *fmt++) != '\0') {
|
2016-01-09 09:29:36 -05:00
|
|
|
switch (c) {
|
|
|
|
default:
|
|
|
|
xfputc(pic, c, file);
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
c = *fmt++;
|
|
|
|
if (! c)
|
|
|
|
goto exit;
|
|
|
|
switch (c) {
|
|
|
|
default:
|
|
|
|
xfputc(pic, c, file);
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
xfputc(pic, '%', file);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
xfprintf(pic, file, "%c", va_arg(ap, int));
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
xfprintf(pic, file, "%s", va_arg(ap, const char *));
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
xfprintf(pic, file, "%d", va_arg(ap, int));
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
xfprintf(pic, file, "%p", va_arg(ap, void *));
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
xfprintf(pic, file, "%f", va_arg(ap, double));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '~':
|
|
|
|
c = *fmt++;
|
|
|
|
if (! c)
|
|
|
|
goto exit;
|
|
|
|
switch (c) {
|
|
|
|
default:
|
|
|
|
xfputc(pic, c, file);
|
|
|
|
break;
|
|
|
|
case '~':
|
|
|
|
xfputc(pic, '~', file);
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
xfputc(pic, '\n', file);
|
|
|
|
break;
|
|
|
|
case 'a':
|
2016-02-07 11:32:13 -05:00
|
|
|
pic_fdisplay(pic, va_arg(ap, pic_value), file);
|
2016-01-09 09:29:36 -05:00
|
|
|
break;
|
|
|
|
case 's':
|
2016-02-07 11:32:13 -05:00
|
|
|
pic_fwrite(pic, va_arg(ap, pic_value), file);
|
2016-01-09 09:29:36 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-08-25 00:38:09 -04:00
|
|
|
exit:
|
2016-02-07 11:32:13 -05:00
|
|
|
return;
|
2014-08-26 00:30:08 -04:00
|
|
|
}
|
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
pic_value
|
2016-02-18 09:49:16 -05:00
|
|
|
pic_vstrf_value(pic_state *pic, const char *fmt, va_list ap)
|
2014-08-26 00:30:08 -04:00
|
|
|
{
|
2016-02-19 13:26:52 -05:00
|
|
|
pic_value str;
|
2016-02-18 15:54:50 -05:00
|
|
|
xFILE *file;
|
|
|
|
const char *buf;
|
|
|
|
int len;
|
2014-08-26 00:30:08 -04:00
|
|
|
|
2016-02-18 15:54:50 -05:00
|
|
|
file = xfopen_buf(pic, NULL, 0, "w");
|
2014-08-26 00:30:08 -04:00
|
|
|
|
2016-02-18 15:54:50 -05:00
|
|
|
vfstrf(pic, file, fmt, ap);
|
|
|
|
xfget_buf(pic, file, &buf, &len);
|
|
|
|
str = pic_str_value(pic, buf, len);
|
|
|
|
xfclose(pic, file);
|
2016-01-09 09:29:36 -05:00
|
|
|
return str;
|
2014-08-26 00:30:08 -04:00
|
|
|
}
|
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
pic_value
|
2016-02-18 09:49:16 -05:00
|
|
|
pic_strf_value(pic_state *pic, const char *fmt, ...)
|
2014-08-26 00:30:08 -04:00
|
|
|
{
|
2016-01-09 09:29:36 -05:00
|
|
|
va_list ap;
|
2016-02-19 13:26:52 -05:00
|
|
|
pic_value str;
|
2014-08-26 00:30:08 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
va_start(ap, fmt);
|
2016-02-18 09:49:16 -05:00
|
|
|
str = pic_vstrf_value(pic, fmt, ap);
|
2016-01-09 09:29:36 -05:00
|
|
|
va_end(ap);
|
2014-08-26 00:30:08 -04:00
|
|
|
|
2016-01-09 09:29:36 -05:00
|
|
|
return str;
|
2014-08-26 00:30:08 -04:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
pic_assert_type(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) {
|
|
|
|
pic_errorf(pic, "make-string: negative length given %d", len);
|
|
|
|
}
|
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);
|
|
|
|
|
2016-02-18 06:15:42 -05:00
|
|
|
return pic_char_value(pic, pic_str_ref(pic, str, k));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
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); \
|
|
|
|
} \
|
|
|
|
if (! (pic_str_cmp(pic, argv[i-1], argv[i]) op 0)) { \
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-19 13:26:52 -05:00
|
|
|
pic_assert_type(pic, argv[i], str);
|
|
|
|
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) {
|
|
|
|
pic_errorf(pic, "string-map: one or more strings expected, but got zero");
|
|
|
|
}
|
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
len = INT_MAX;
|
|
|
|
for (i = 0; i < argc; ++i) {
|
|
|
|
int l;
|
|
|
|
pic_assert_type(pic, argv[i], str);
|
|
|
|
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) {
|
|
|
|
pic_push(pic, pic_char_value(pic, pic_str_ref(pic, argv[j], i)), vals);
|
2016-01-09 09:29:36 -05:00
|
|
|
}
|
2016-02-19 13:26:52 -05:00
|
|
|
vals = pic_reverse(pic, vals);
|
|
|
|
val = pic_funcall(pic, "picrin.base", "apply", 2, proc, vals);
|
2016-01-09 09:29:36 -05:00
|
|
|
|
2016-02-19 13:26:52 -05:00
|
|
|
pic_assert_type(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) {
|
|
|
|
pic_errorf(pic, "string-map: one or more strings expected, but got zero");
|
|
|
|
}
|
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;
|
|
|
|
pic_assert_type(pic, argv[i], str);
|
|
|
|
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) {
|
2016-02-19 13:26:52 -05:00
|
|
|
pic_push(pic, pic_char_value(pic, pic_str_ref(pic, argv[j], i)), vals);
|
2016-01-09 09:29:36 -05:00
|
|
|
}
|
2016-02-19 13:26:52 -05:00
|
|
|
vals = pic_reverse(pic, vals);
|
2016-02-19 10:03:16 -05:00
|
|
|
pic_funcall(pic, "picrin.base", "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) {
|
|
|
|
pic_assert_type(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) {
|
2016-02-18 06:15:42 -05:00
|
|
|
pic_push(pic, pic_char_value(pic, pic_str_ref(pic, str, i)), list);
|
2016-01-09 09:29:36 -05:00
|
|
|
}
|
|
|
|
return pic_reverse(pic, list);
|
2014-09-10 07:09:32 -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);
|
|
|
|
pic_defun(pic, "string-copy", pic_str_string_copy);
|
|
|
|
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
|
|
|
}
|