2014-01-17 06:58:31 -05:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
2013-10-20 19:48:55 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "picrin.h"
|
2014-02-22 01:20:53 -05:00
|
|
|
#include "picrin/string.h"
|
2014-02-15 05:53:16 -05:00
|
|
|
#include "picrin/pair.h"
|
|
|
|
#include "picrin/port.h"
|
2013-10-20 19:48:55 -04:00
|
|
|
|
2014-02-21 04:05:59 -05:00
|
|
|
pic_str *
|
2013-10-20 22:12:34 -04:00
|
|
|
pic_str_new(pic_state *pic, const char *cstr, size_t len)
|
2013-10-20 19:48:55 -04:00
|
|
|
{
|
2014-02-21 04:05:59 -05:00
|
|
|
pic_str *str;
|
2014-02-08 13:11:08 -05:00
|
|
|
char *copy;
|
|
|
|
|
|
|
|
if (cstr) {
|
|
|
|
copy = pic_strdup(pic, cstr);
|
|
|
|
} else {
|
|
|
|
copy = (char *)pic_alloc(pic, len);
|
|
|
|
}
|
2013-10-20 19:48:55 -04:00
|
|
|
|
2014-02-21 04:05:59 -05:00
|
|
|
str = (pic_str *)pic_obj_alloc(pic, sizeof(pic_str), PIC_TT_STRING);
|
2013-10-20 19:48:55 -04:00
|
|
|
str->len = len;
|
2014-02-08 13:11:08 -05:00
|
|
|
str->str = copy;
|
2013-11-17 10:28:42 -05:00
|
|
|
return str;
|
2013-10-20 19:48:55 -04:00
|
|
|
}
|
2013-10-20 22:12:34 -04:00
|
|
|
|
2014-02-21 04:05:59 -05:00
|
|
|
pic_str *
|
|
|
|
pic_str_new_cstr(pic_state *pic, const char *cstr)
|
|
|
|
{
|
|
|
|
return pic_str_new(pic, cstr, strlen(cstr));
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:25:39 -05:00
|
|
|
pic_str *
|
|
|
|
pic_str_new_fill(pic_state *pic, size_t len, char fill)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
char *cstr;
|
|
|
|
|
|
|
|
cstr = (char *)pic_alloc(pic, len + 1);
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
cstr[i] = fill;
|
|
|
|
}
|
|
|
|
cstr[len] = '\0';
|
|
|
|
|
|
|
|
return pic_str_new(pic, cstr, len);
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:52:15 -05:00
|
|
|
size_t
|
|
|
|
pic_strlen(pic_state *pic, pic_str *str)
|
|
|
|
{
|
|
|
|
UNUSED(pic);
|
|
|
|
|
|
|
|
return str->len;
|
|
|
|
}
|
|
|
|
|
2014-02-21 04:24:20 -05:00
|
|
|
char
|
|
|
|
pic_str_ref(pic_state *pic, pic_str *str, size_t n)
|
|
|
|
{
|
|
|
|
UNUSED(pic);
|
|
|
|
|
|
|
|
return str->str[n];
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:52:15 -05:00
|
|
|
void
|
|
|
|
pic_str_set(pic_state *pic, pic_str *str, size_t i, char c)
|
|
|
|
{
|
|
|
|
UNUSED(pic);
|
|
|
|
|
|
|
|
str->str[i] = c;
|
|
|
|
}
|
|
|
|
|
2014-02-21 04:24:20 -05:00
|
|
|
pic_str *
|
|
|
|
pic_strcat(pic_state *pic, pic_str *a, pic_str *b)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
len = a->len + b->len;
|
|
|
|
buf = pic_alloc(pic, len + 1);
|
|
|
|
|
|
|
|
memcpy(buf, a->str, a->len);
|
|
|
|
memcpy(buf + a->len, b->str, b->len);
|
|
|
|
buf[len] = '\0';
|
|
|
|
|
|
|
|
return pic_str_new(pic, buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
pic_str *
|
|
|
|
pic_substr(pic_state *pic, pic_str *str, size_t s, size_t e)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
len = e - s;
|
|
|
|
buf = pic_alloc(pic, len + 1);
|
|
|
|
|
|
|
|
memcpy(buf, str->str + s, len);
|
|
|
|
buf[len] = '\0';
|
|
|
|
|
|
|
|
return pic_str_new(pic, buf, len);
|
|
|
|
}
|
|
|
|
|
2014-02-23 03:45:34 -05:00
|
|
|
int
|
|
|
|
pic_strcmp(pic_state *pic, pic_str *str1, pic_str *str2)
|
|
|
|
{
|
|
|
|
UNUSED(pic);
|
|
|
|
|
|
|
|
return strcmp(str1->str, str2->str);
|
|
|
|
}
|
|
|
|
|
2014-02-15 05:53:16 -05:00
|
|
|
pic_value
|
2014-02-25 07:07:32 -05:00
|
|
|
pic_vfformat(pic_state *pic, xFILE *file, const char *fmt, va_list ap)
|
2014-02-15 05:53:16 -05:00
|
|
|
{
|
|
|
|
char c;
|
|
|
|
pic_value irrs = pic_nil_value();
|
|
|
|
|
|
|
|
while ((c = *fmt++)) {
|
|
|
|
switch (c) {
|
|
|
|
default:
|
|
|
|
xfputc(c, file);
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
c = *fmt++;
|
|
|
|
if (! c)
|
|
|
|
goto exit;
|
|
|
|
switch (c) {
|
|
|
|
default:
|
|
|
|
xfputc(c, file);
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
xfputc('%', file);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
xfprintf(file, "%c", va_arg(ap, int));
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
xfprintf(file, "%s", va_arg(ap, const char *));
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
xfprintf(file, "%d", va_arg(ap, int));
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
xfprintf(file, "%p", va_arg(ap, void *));
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
xfprintf(file, "%f", va_arg(ap, double));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '~':
|
|
|
|
c = *fmt++;
|
|
|
|
if (! c)
|
|
|
|
goto exit;
|
|
|
|
switch (c) {
|
|
|
|
default:
|
|
|
|
xfputc(c, file);
|
|
|
|
break;
|
|
|
|
case '~':
|
|
|
|
xfputc('~', file);
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
xfputc('\n', file);
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
irrs = pic_cons(pic, pic_fdebug(pic, va_arg(ap, pic_value), file), irrs);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit:
|
|
|
|
|
|
|
|
return pic_reverse(pic, irrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
|
|
|
pic_vformat(pic_state *pic, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
struct pic_port *port;
|
|
|
|
pic_value irrs;
|
|
|
|
|
|
|
|
port = pic_open_output_string(pic);
|
|
|
|
|
|
|
|
irrs = pic_vfformat(pic, port->file, fmt, ap);
|
|
|
|
irrs = pic_cons(pic, pic_obj_value(pic_get_output_string(pic, port)), irrs);
|
|
|
|
|
|
|
|
pic_close_port(pic, port);
|
|
|
|
return irrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
|
|
|
pic_format(pic_state *pic, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
pic_value objs;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
objs = pic_vformat(pic, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return objs;
|
|
|
|
}
|
|
|
|
|
2013-11-17 03:42:52 -05:00
|
|
|
static pic_value
|
|
|
|
pic_str_string_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
|
|
|
|
return pic_bool_value(pic_str_p(v));
|
|
|
|
}
|
|
|
|
|
2013-11-17 11:09:54 -05:00
|
|
|
static pic_value
|
|
|
|
pic_str_make_string(pic_state *pic)
|
|
|
|
{
|
2014-02-22 21:25:39 -05:00
|
|
|
int len;
|
|
|
|
char c = ' ';
|
2013-11-17 11:09:54 -05:00
|
|
|
|
2014-02-22 21:25:39 -05:00
|
|
|
pic_get_args(pic, "i|c", &len, &c);
|
2013-11-17 11:09:54 -05:00
|
|
|
|
2014-02-22 21:25:39 -05:00
|
|
|
return pic_obj_value(pic_str_new_fill(pic, len, c));
|
2013-11-17 11:09:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_str_string_length(pic_state *pic)
|
|
|
|
{
|
2014-02-22 21:52:15 -05:00
|
|
|
pic_str *str;
|
2013-11-17 11:09:54 -05:00
|
|
|
|
2014-02-22 21:52:15 -05:00
|
|
|
pic_get_args(pic, "s", &str);
|
2013-11-17 11:09:54 -05:00
|
|
|
|
2014-02-22 21:52:15 -05:00
|
|
|
return pic_int_value(pic_strlen(pic, str));
|
2013-11-17 11:09:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_str_string_ref(pic_state *pic)
|
|
|
|
{
|
2014-02-22 21:52:15 -05:00
|
|
|
pic_str *str;
|
2013-11-17 11:09:54 -05:00
|
|
|
int k;
|
|
|
|
|
2014-02-22 21:52:15 -05:00
|
|
|
pic_get_args(pic, "si", &str, &k);
|
2013-11-17 11:09:54 -05:00
|
|
|
|
2014-02-22 21:52:15 -05:00
|
|
|
return pic_char_value(pic_str_ref(pic, str, k));
|
2013-11-17 11:09:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_str_string_set(pic_state *pic)
|
|
|
|
{
|
2014-02-22 21:52:15 -05:00
|
|
|
pic_str *str;
|
|
|
|
char c;
|
2013-11-17 11:09:54 -05:00
|
|
|
int k;
|
|
|
|
|
2014-02-22 21:52:15 -05:00
|
|
|
pic_get_args(pic, "sic", &str, &k, &c);
|
2013-11-17 11:09:54 -05:00
|
|
|
|
2014-02-22 21:52:15 -05:00
|
|
|
pic_str_set(pic, str, k, c);
|
2014-01-08 01:22:23 -05:00
|
|
|
return pic_none_value();
|
2013-11-17 11:09:54 -05:00
|
|
|
}
|
|
|
|
|
2014-02-23 03:45:34 -05:00
|
|
|
#define DEFINE_STRING_CMP(name, op) \
|
|
|
|
static pic_value \
|
|
|
|
pic_str_string_##name(pic_state *pic) \
|
|
|
|
{ \
|
|
|
|
size_t argc; \
|
|
|
|
pic_value *argv; \
|
|
|
|
size_t i; \
|
|
|
|
\
|
|
|
|
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_strcmp(pic, pic_str_ptr(argv[i-1]), pic_str_ptr(argv[i])) op 0)) { \
|
|
|
|
return pic_false_value(); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
return pic_true_value(); \
|
2013-11-17 11:09:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_STRING_CMP(eq, ==)
|
|
|
|
DEFINE_STRING_CMP(lt, <)
|
|
|
|
DEFINE_STRING_CMP(gt, >)
|
|
|
|
DEFINE_STRING_CMP(le, <=)
|
|
|
|
DEFINE_STRING_CMP(ge, >=)
|
|
|
|
|
2014-02-08 13:10:58 -05:00
|
|
|
static pic_value
|
|
|
|
pic_str_string_copy(pic_state *pic)
|
|
|
|
{
|
2014-02-22 21:52:15 -05:00
|
|
|
pic_str *str;
|
|
|
|
int n, start, end;
|
2014-02-08 13:10:58 -05:00
|
|
|
|
2014-02-22 21:52:15 -05:00
|
|
|
n = pic_get_args(pic, "s|ii", &str, &start, &end);
|
2014-02-08 13:10:58 -05:00
|
|
|
|
|
|
|
switch (n) {
|
|
|
|
case 1:
|
|
|
|
start = 0;
|
|
|
|
case 2:
|
2014-02-22 21:52:15 -05:00
|
|
|
end = pic_strlen(pic, str);
|
2014-02-08 13:10:58 -05:00
|
|
|
}
|
|
|
|
|
2014-02-22 21:52:15 -05:00
|
|
|
return pic_obj_value(pic_substr(pic, str, start, end));
|
2014-02-08 13:10:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_str_string_copy_ip(pic_state *pic)
|
|
|
|
{
|
2014-02-22 21:52:15 -05:00
|
|
|
pic_str *to, *from;
|
2014-02-22 20:51:21 -05:00
|
|
|
int n, at, start, end;
|
2014-02-08 13:10:58 -05:00
|
|
|
|
2014-02-22 21:52:15 -05:00
|
|
|
n = pic_get_args(pic, "sis|ii", &to, &at, &from, &start, &end);
|
2014-02-08 13:10:58 -05:00
|
|
|
|
|
|
|
switch (n) {
|
|
|
|
case 3:
|
|
|
|
start = 0;
|
|
|
|
case 4:
|
2014-02-22 21:52:15 -05:00
|
|
|
end = pic_strlen(pic, from);
|
2014-02-08 13:10:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
while (start < end) {
|
2014-02-22 21:52:15 -05:00
|
|
|
pic_str_set(pic, to, at++, pic_str_ref(pic, from, start++));
|
2014-02-08 13:10:58 -05:00
|
|
|
}
|
|
|
|
return pic_none_value();
|
|
|
|
}
|
|
|
|
|
2014-02-08 12:42:50 -05:00
|
|
|
static pic_value
|
|
|
|
pic_str_string_append(pic_state *pic)
|
|
|
|
{
|
2014-02-22 21:52:15 -05:00
|
|
|
size_t argc, i;
|
2014-02-08 12:42:50 -05:00
|
|
|
pic_value *argv;
|
2014-02-22 21:52:15 -05:00
|
|
|
pic_str *str;
|
2014-02-08 12:42:50 -05:00
|
|
|
|
|
|
|
pic_get_args(pic, "*", &argc, &argv);
|
|
|
|
|
2014-02-22 21:52:15 -05:00
|
|
|
str = pic_str_new(pic, NULL, 0);
|
2014-02-08 12:42:50 -05:00
|
|
|
for (i = 0; i < argc; ++i) {
|
|
|
|
if (! pic_str_p(argv[i])) {
|
|
|
|
pic_error(pic, "type error");
|
|
|
|
}
|
2014-02-22 21:52:15 -05:00
|
|
|
str = pic_strcat(pic, str, pic_str_ptr(argv[i]));
|
2014-02-08 12:42:50 -05:00
|
|
|
}
|
2014-02-22 21:52:15 -05:00
|
|
|
return pic_obj_value(str);
|
2014-02-08 12:42:50 -05:00
|
|
|
}
|
|
|
|
|
2014-02-08 13:10:58 -05:00
|
|
|
static pic_value
|
|
|
|
pic_str_string_fill_ip(pic_state *pic)
|
|
|
|
{
|
2014-02-22 21:52:15 -05:00
|
|
|
pic_str *str;
|
|
|
|
char c;
|
2014-02-22 20:51:21 -05:00
|
|
|
int n, start, end;
|
2014-02-08 13:10:58 -05:00
|
|
|
|
2014-02-22 21:52:15 -05:00
|
|
|
n = pic_get_args(pic, "sc|ii", &str, &c, &start, &end);
|
2014-02-08 13:10:58 -05:00
|
|
|
|
|
|
|
switch (n) {
|
|
|
|
case 1:
|
|
|
|
start = 0;
|
|
|
|
case 2:
|
2014-02-22 21:52:15 -05:00
|
|
|
end = pic_strlen(pic, str);
|
2014-02-08 13:10:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
while (start < end) {
|
2014-02-22 21:52:15 -05:00
|
|
|
pic_str_set(pic, str, start++, c);
|
2014-02-08 13:10:58 -05:00
|
|
|
}
|
|
|
|
return pic_none_value();
|
|
|
|
}
|
|
|
|
|
2013-11-17 03:42:52 -05:00
|
|
|
void
|
|
|
|
pic_init_str(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_defun(pic, "string?", pic_str_string_p);
|
2013-11-17 11:09:54 -05:00
|
|
|
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-set!", pic_str_string_set);
|
2014-02-08 13:10:58 -05:00
|
|
|
|
2013-11-17 11:09:54 -05:00
|
|
|
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-02-08 13:10:58 -05:00
|
|
|
|
|
|
|
pic_defun(pic, "string-copy", pic_str_string_copy);
|
|
|
|
pic_defun(pic, "string-copy!", pic_str_string_copy_ip);
|
2014-02-08 12:42:50 -05:00
|
|
|
pic_defun(pic, "string-append", pic_str_string_append);
|
2014-02-08 13:10:58 -05:00
|
|
|
pic_defun(pic, "string-fill!", pic_str_string_fill_ip);
|
2013-11-17 03:42:52 -05:00
|
|
|
}
|