picrin/extlib/benz/string.c

736 lines
16 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
struct pic_chunk {
2015-12-29 09:28:31 -05:00
char *str;
int refcnt;
size_t len;
char buf[1];
};
struct pic_rope {
2015-12-29 09:28:31 -05:00
int refcnt;
size_t weight;
struct pic_chunk *chunk;
size_t offset;
struct pic_rope *left, *right;
};
#define CHUNK_INCREF(c) do { \
2015-12-29 09:28:31 -05:00
(c)->refcnt++; \
} while (0)
#define CHUNK_DECREF(c) do { \
2015-12-29 09:28:31 -05:00
struct pic_chunk *c_ = (c); \
if (! --c_->refcnt) { \
if (c_->str != c_->buf) \
pic_free(pic, c_->str); \
pic_free(pic, c_); \
} \
} while (0)
void
2015-05-28 04:06:41 -04:00
pic_rope_incref(pic_state PIC_UNUSED(*pic), struct pic_rope *x) {
2015-12-29 09:28:31 -05:00
x->refcnt++;
}
void
pic_rope_decref(pic_state *pic, struct pic_rope *x) {
2015-12-29 09:28:31 -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);
}
}
static struct pic_chunk *
pic_make_chunk(pic_state *pic, const char *str, size_t len)
{
2015-12-29 09:28:31 -05:00
struct pic_chunk *c;
2015-12-29 09:28:31 -05:00
c = pic_malloc(pic, sizeof(struct pic_chunk) + len);
c->refcnt = 1;
c->str = c->buf;
c->len = len;
c->buf[len] = 0;
memcpy(c->buf, str, len);
2015-12-29 09:28:31 -05:00
return c;
}
static struct pic_rope *
pic_make_rope(pic_state *pic, struct pic_chunk *c)
{
2015-12-29 09:28:31 -05:00
struct pic_rope *x;
2015-12-29 09:28:31 -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-12-29 09:28:31 -05:00
return x;
}
2014-08-25 00:38:09 -04:00
static pic_str *
pic_make_string(pic_state *pic, struct pic_rope *rope)
2014-08-25 00:38:09 -04:00
{
2015-12-29 09:28:31 -05:00
pic_str *str;
2014-08-25 00:38:09 -04:00
2015-12-29 09:28:31 -05:00
str = (pic_str *)pic_obj_alloc(pic, sizeof(pic_str), PIC_TT_STRING);
str->rope = rope; /* delegate ownership */
return str;
2014-08-25 00:38:09 -04:00
}
static size_t
rope_len(struct pic_rope *x)
{
2015-12-29 09:28:31 -05:00
return x->weight;
}
static char
rope_at(struct pic_rope *x, size_t i)
{
2015-12-29 09:28:31 -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 {
x = x->right;
i -= x->left->weight;
}
}
return -1;
}
static struct pic_rope *
rope_cat(pic_state *pic, struct pic_rope *x, struct pic_rope *y)
{
2015-12-29 09:28:31 -05:00
struct pic_rope *z;
2015-12-29 09:28:31 -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-12-29 09:28:31 -05:00
pic_rope_incref(pic, x);
pic_rope_incref(pic, y);
2015-12-29 09:28:31 -05:00
return z;
}
static struct pic_rope *
rope_sub(pic_state *pic, struct pic_rope *x, size_t i, size_t j)
{
2015-12-29 09:28:31 -05:00
assert(i <= j);
assert(j <= x->weight);
2015-12-29 09:28:31 -05:00
if (i == 0 && x->weight == j) {
pic_rope_incref(pic, x);
return x;
}
2015-12-29 09:28:31 -05:00
if (x->chunk) {
struct pic_rope *y;
2015-12-29 09:28:31 -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-12-29 09:28:31 -05:00
CHUNK_INCREF(x->chunk);
2015-12-29 09:28:31 -05:00
return y;
}
2015-12-29 09:28:31 -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-12-29 09:28:31 -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-12-29 09:28:31 -05:00
pic_rope_decref(pic, l);
pic_rope_decref(pic, r);
2015-12-29 09:28:31 -05:00
return x;
}
}
static void
flatten(pic_state *pic, struct pic_rope *x, struct pic_chunk *c, size_t offset)
{
2015-12-29 09:28:31 -05:00
if (x->chunk) {
memcpy(c->str + offset, x->chunk->str + x->offset, x->weight);
CHUNK_DECREF(x->chunk);
2015-12-29 09:28:31 -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-12-29 09:28:31 -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);
}
static const char *
rope_cstr(pic_state *pic, struct pic_rope *x)
{
2015-12-29 09:28:31 -05:00
struct pic_chunk *c;
2015-12-29 09:28:31 -05:00
if (x->chunk && x->offset == 0 && x->weight == x->chunk->len) {
return x->chunk->str; /* reuse cached chunk */
}
2015-12-29 09:28:31 -05:00
c = pic_malloc(pic, sizeof(struct pic_chunk) + x->weight);
c->refcnt = 1;
c->len = x->weight;
c->str = c->buf;
c->str[c->len] = '\0';
2015-12-29 09:28:31 -05:00
flatten(pic, x, c, 0);
2015-12-29 09:28:31 -05:00
CHUNK_DECREF(c);
return c->str;
}
2014-08-25 00:38:09 -04:00
pic_str *
2015-08-26 06:04:27 -04:00
pic_make_str(pic_state *pic, const char *str, int len)
2014-08-25 00:38:09 -04:00
{
2015-12-29 09:28:31 -05:00
if (str == NULL && len > 0) {
pic_errorf(pic, "zero length specified against NULL ptr");
}
return pic_make_string(pic, pic_make_rope(pic, pic_make_chunk(pic, str, len)));
2014-08-25 00:38:09 -04:00
}
pic_str *
2014-09-12 06:52:49 -04:00
pic_make_str_cstr(pic_state *pic, const char *cstr)
2014-08-25 00:38:09 -04:00
{
2015-12-29 09:28:31 -05:00
return pic_make_str(pic, cstr, strlen(cstr));
2014-08-25 00:38:09 -04:00
}
2015-08-26 06:04:27 -04:00
int
pic_str_len(pic_str *str)
2014-08-25 00:38:09 -04:00
{
2015-12-29 09:28:31 -05:00
return rope_len(str->rope);
2014-08-25 00:38:09 -04:00
}
char
2015-08-26 06:04:27 -04:00
pic_str_ref(pic_state *pic, pic_str *str, int i)
2014-08-25 00:38:09 -04:00
{
2015-12-29 09:28:31 -05:00
int c;
2014-08-25 00:38:09 -04:00
2015-12-29 09:28:31 -05:00
c = rope_at(str->rope, i);
if (c == -1) {
pic_errorf(pic, "index out of range %d", i);
}
return (char)c;
2014-08-25 00:38:09 -04:00
}
pic_str *
pic_str_cat(pic_state *pic, pic_str *a, pic_str *b)
2014-08-25 00:38:09 -04:00
{
2015-12-29 09:28:31 -05:00
return pic_make_string(pic, rope_cat(pic, a->rope, b->rope));
2014-08-25 00:38:09 -04:00
}
pic_str *
2015-08-26 06:04:27 -04:00
pic_str_sub(pic_state *pic, pic_str *str, int s, int e)
2014-08-25 00:38:09 -04:00
{
2015-12-29 09:28:31 -05:00
return pic_make_string(pic, rope_sub(pic, str->rope, s, e));
2014-08-25 00:38:09 -04:00
}
int
pic_str_cmp(pic_state *pic, pic_str *str1, pic_str *str2)
2014-08-25 00:38:09 -04:00
{
2015-12-29 09:28:31 -05:00
return strcmp(pic_str_cstr(pic, str1), pic_str_cstr(pic, str2));
2014-08-25 00:38:09 -04:00
}
const char *
pic_str_cstr(pic_state *pic, pic_str *str)
2014-08-25 00:38:09 -04:00
{
2015-12-29 09:28:31 -05:00
return rope_cstr(pic, str->rope);
2014-08-25 00:38:09 -04:00
}
pic_value
2014-08-26 00:30:08 -04:00
pic_xvfformat(pic_state *pic, xFILE *file, const char *fmt, va_list ap)
2014-08-25 00:38:09 -04:00
{
2015-12-29 09:28:31 -05:00
char c;
pic_value irrs = pic_nil_value();
while ((c = *fmt++)) {
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':
irrs = pic_cons(pic, pic_fdisplay(pic, va_arg(ap, pic_value), file), irrs);
break;
case 's':
irrs = pic_cons(pic, pic_fwrite(pic, va_arg(ap, pic_value), file), irrs);
break;
}
break;
}
}
2014-08-25 00:38:09 -04:00
exit:
2015-12-29 09:28:31 -05:00
return pic_reverse(pic, irrs);
2014-08-25 00:38:09 -04:00
}
pic_value
2014-08-26 00:30:08 -04:00
pic_xvformat(pic_state *pic, const char *fmt, va_list ap)
2014-08-25 00:38:09 -04:00
{
2015-12-29 09:28:31 -05:00
struct pic_port *port;
pic_value irrs;
2014-08-25 00:38:09 -04:00
2015-12-29 09:28:31 -05:00
port = pic_open_output_string(pic);
2014-08-25 00:38:09 -04:00
2015-12-29 09:28:31 -05:00
irrs = pic_xvfformat(pic, port->file, fmt, ap);
irrs = pic_cons(pic, pic_obj_value(pic_get_output_string(pic, port)), irrs);
2014-08-25 00:38:09 -04:00
2015-12-29 09:28:31 -05:00
pic_close_port(pic, port);
return irrs;
2014-08-25 00:38:09 -04:00
}
pic_value
2014-08-26 00:30:08 -04:00
pic_xformat(pic_state *pic, const char *fmt, ...)
2014-08-25 00:38:09 -04:00
{
2015-12-29 09:28:31 -05:00
va_list ap;
pic_value objs;
2014-08-25 00:38:09 -04:00
2015-12-29 09:28:31 -05:00
va_start(ap, fmt);
objs = pic_xvformat(pic, fmt, ap);
va_end(ap);
2014-08-25 00:38:09 -04:00
2015-12-29 09:28:31 -05:00
return objs;
2014-08-25 00:38:09 -04:00
}
2014-08-26 00:30:08 -04:00
void
pic_vfformat(pic_state *pic, xFILE *file, const char *fmt, va_list ap)
{
2015-12-29 09:28:31 -05:00
pic_xvfformat(pic, file, fmt, ap);
2014-08-26 00:30:08 -04:00
}
pic_str *
pic_vformat(pic_state *pic, const char *fmt, va_list ap)
{
2015-12-29 09:28:31 -05:00
struct pic_port *port;
pic_str *str;
2014-08-26 00:30:08 -04:00
2015-12-29 09:28:31 -05:00
port = pic_open_output_string(pic);
2014-08-26 00:30:08 -04:00
2015-12-29 09:28:31 -05:00
pic_vfformat(pic, port->file, fmt, ap);
str = pic_get_output_string(pic, port);
2014-08-26 00:30:08 -04:00
2015-12-29 09:28:31 -05:00
pic_close_port(pic, port);
return str;
2014-08-26 00:30:08 -04:00
}
pic_str *
pic_format(pic_state *pic, const char *fmt, ...)
{
2015-12-29 09:28:31 -05:00
va_list ap;
pic_str *str;
2014-08-26 00:30:08 -04:00
2015-12-29 09:28:31 -05:00
va_start(ap, fmt);
str = pic_vformat(pic, fmt, ap);
va_end(ap);
2014-08-26 00:30:08 -04:00
2015-12-29 09:28:31 -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)
{
2015-12-29 09:28:31 -05:00
pic_value v;
2014-08-25 00:38:09 -04:00
2015-12-29 09:28:31 -05:00
pic_get_args(pic, "o", &v);
2014-08-25 00:38:09 -04:00
2015-12-29 09:28:31 -05:00
return pic_bool_value(pic_str_p(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)
{
2015-12-29 09:28:31 -05:00
int argc, i;
pic_value *argv;
pic_str *str;
char *buf;
2014-09-12 07:23:58 -04:00
2015-12-29 09:28:31 -05:00
pic_get_args(pic, "*", &argc, &argv);
2014-09-12 07:23:58 -04:00
2015-12-29 09:28:31 -05:00
buf = pic_malloc(pic, argc);
2014-09-12 07:23:58 -04:00
2015-12-29 09:28:31 -05:00
for (i = 0; i < argc; ++i) {
pic_assert_type(pic, argv[i], char);
buf[i] = pic_char(argv[i]);
}
2014-09-12 07:23:58 -04:00
2015-12-29 09:28:31 -05:00
str = pic_make_str(pic, buf, argc);
pic_free(pic, buf);
2014-09-12 07:23:58 -04:00
2015-12-29 09:28:31 -05:00
return pic_obj_value(str);
2014-09-12 07:23:58 -04:00
}
static pic_value
pic_str_make_string(pic_state *pic)
{
2015-12-29 09:28:31 -05:00
int len;
char c = ' ';
char *buf;
pic_value ret;
2015-12-29 09:28:31 -05:00
pic_get_args(pic, "i|c", &len, &c);
2015-12-29 09:28:31 -05:00
buf = pic_malloc(pic, len);
memset(buf, c, len);
2015-07-12 20:03:32 -04:00
2015-12-29 09:28:31 -05:00
ret = pic_obj_value(pic_make_str(pic, buf, len));
2015-07-12 20:03:32 -04:00
2015-12-29 09:28:31 -05:00
pic_free(pic, buf);
return ret;
}
2014-08-25 00:38:09 -04:00
static pic_value
pic_str_string_length(pic_state *pic)
{
2015-12-29 09:28:31 -05:00
pic_str *str;
2014-08-25 00:38:09 -04:00
2015-12-29 09:28:31 -05:00
pic_get_args(pic, "s", &str);
2014-08-25 00:38:09 -04:00
2015-12-29 09:28:31 -05:00
return pic_int_value(pic_str_len(str));
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_str_string_ref(pic_state *pic)
{
2015-12-29 09:28:31 -05:00
pic_str *str;
int k;
2014-08-25 00:38:09 -04:00
2015-12-29 09:28:31 -05:00
pic_get_args(pic, "si", &str, &k);
2014-08-25 00:38:09 -04:00
2015-12-29 09:28:31 -05:00
return pic_char_value(pic_str_ref(pic, str, k));
2014-08-25 00:38:09 -04:00
}
#define DEFINE_STRING_CMP(name, op) \
2015-12-29 09:28:31 -05:00
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(argv[0])) { \
return pic_false_value(); \
} \
\
for (i = 1; i < argc; ++i) { \
if (! pic_str_p(argv[i])) { \
return pic_false_value(); \
} \
if (! (pic_str_cmp(pic, pic_str_ptr(argv[i-1]), pic_str_ptr(argv[i])) op 0)) { \
return pic_false_value(); \
} \
} \
return pic_true_value(); \
}
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)
{
2015-12-29 09:28:31 -05:00
pic_str *str;
int n, start, end, len;
2014-08-25 00:38:09 -04:00
2015-12-29 09:28:31 -05:00
n = pic_get_args(pic, "s|ii", &str, &start, &end);
2014-08-25 00:38:09 -04:00
2015-12-29 09:28:31 -05:00
len = pic_str_len(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;
}
#if 0
#if 0
2015-12-29 09:28:31 -05:00
if (start < 0) start = 0; /* should an error be reported? */
if (end > len) end = len; /* should an error be reported? */
#else
2015-12-29 09:28:31 -05:00
if ((start < 0) || (end < 0) || (start > len) || (end > len))
pic_errorf(pic, "string-copy: invalid index");
#endif
2015-12-29 09:28:31 -05:00
if (end < start) /* surely this is an error!? */
pic_errorf(pic, "string-copy: start index > end index");
#else
2015-12-29 09:28:31 -05:00
/* simplest version to catch all cases as errors */
if ((start < 0) || (end > len) || (end < start))
pic_errorf(pic, "string-copy: invalid index");
#endif
2015-12-29 09:28:31 -05:00
return pic_obj_value(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)
{
2015-12-29 09:28:31 -05:00
int argc, i;
pic_value *argv;
pic_str *str;
2014-08-25 00:38:09 -04:00
2015-12-29 09:28:31 -05:00
pic_get_args(pic, "*", &argc, &argv);
2014-08-25 00:38:09 -04:00
2015-12-29 09:28:31 -05:00
str = pic_make_str(pic, NULL, 0);
for (i = 0; i < argc; ++i) {
if (! pic_str_p(argv[i])) {
pic_errorf(pic, "type error");
}
str = pic_str_cat(pic, str, pic_str_ptr(argv[i]));
}
return pic_obj_value(str);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_str_string_map(pic_state *pic)
{
2015-12-29 09:28:31 -05:00
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_str_string_for_each(pic_state *pic)
{
2015-12-29 09:28:31 -05:00
struct pic_proc *proc;
int argc, len, i, j;
pic_value *argv, vals;
2015-12-29 09:28:31 -05:00
pic_get_args(pic, "l*", &proc, &argc, &argv);
2015-12-29 09:28:31 -05:00
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);
2015-12-29 09:28:31 -05:00
len = len < pic_str_len(pic_str_ptr(argv[i]))
? len
: pic_str_len(pic_str_ptr(argv[i]));
}
2015-12-29 09:28:31 -05:00
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);
}
2015-12-29 09:28:31 -05:00
return pic_undef_value();
}
2014-09-10 07:09:32 -04:00
static pic_value
pic_str_list_to_string(pic_state *pic)
{
2015-12-29 09:28:31 -05:00
pic_str *str;
pic_value list, e, it;
int i;
char *buf;
2014-09-10 07:09:32 -04:00
2015-12-29 09:28:31 -05:00
pic_get_args(pic, "o", &list);
2014-09-10 07:09:32 -04:00
2015-12-29 09:28:31 -05:00
if (pic_length(pic, list) == 0) {
return pic_obj_value(pic_make_str(pic, NULL, 0));
}
2015-12-29 09:28:31 -05:00
buf = pic_malloc(pic, pic_length(pic, list));
2014-09-10 07:09:32 -04:00
2015-12-29 09:28:31 -05:00
pic_try {
i = 0;
pic_for_each (e, list, it) {
pic_assert_type(pic, e, char);
2014-09-10 07:09:32 -04:00
2015-12-29 09:28:31 -05:00
buf[i++] = pic_char(e);
}
2014-09-10 07:09:32 -04:00
2015-12-29 09:28:31 -05:00
str = pic_make_str(pic, buf, i);
}
pic_catch {
pic_free(pic, buf);
pic_raise(pic, pic->err);
}
pic_free(pic, buf);
2015-12-29 09:28:31 -05:00
return pic_obj_value(str);
2014-09-10 07:09:32 -04:00
}
static pic_value
pic_str_string_to_list(pic_state *pic)
{
2015-12-29 09:28:31 -05:00
pic_str *str;
pic_value list;
int n, start, end, i;
2014-09-10 07:09:32 -04:00
2015-12-29 09:28:31 -05:00
n = pic_get_args(pic, "s|ii", &str, &start, &end);
2014-09-10 07:09:32 -04:00
2015-12-29 09:28:31 -05:00
switch (n) {
case 1:
start = 0;
case 2:
end = pic_str_len(str);
}
2014-09-10 07:09:32 -04:00
2015-12-29 09:28:31 -05:00
list = pic_nil_value();
2014-09-10 07:09:32 -04:00
2015-12-29 09:28:31 -05:00
for (i = start; i < end; ++i) {
pic_push(pic, pic_char_value(pic_str_ref(pic, str, i)), list);
}
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)
{
2015-12-29 09:28:31 -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
}