2008-08-05 00:34:14 -04:00
|
|
|
/*
|
|
|
|
string functions
|
|
|
|
*/
|
2019-08-09 12:00:17 -04:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2008-08-05 00:34:14 -04:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
2019-08-09 12:00:17 -04:00
|
|
|
#include <errno.h>
|
2019-08-13 11:51:43 -04:00
|
|
|
#include <math.h>
|
2019-08-09 12:00:17 -04:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdarg.h>
|
2019-08-09 16:25:20 -04:00
|
|
|
#include <stdint.h>
|
2019-08-09 12:00:17 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-05-31 18:09:26 -04:00
|
|
|
#include <wchar.h>
|
2009-03-24 17:27:38 -04:00
|
|
|
#include <wctype.h>
|
2019-08-09 12:00:17 -04:00
|
|
|
|
2019-08-26 15:12:15 -04:00
|
|
|
#include "scheme.h"
|
2019-08-09 15:08:44 -04:00
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
value_t fl_stringp(value_t *args, uint32_t nargs)
|
2008-08-05 00:34:14 -04:00
|
|
|
{
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
argcount("string?", nargs, 1);
|
2009-08-23 03:06:57 -04:00
|
|
|
return fl_isstring(args[0]) ? FL_T : FL_F;
|
2008-08-05 00:34:14 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
value_t fl_string_count(value_t *args, uint32_t nargs)
|
2008-08-05 00:34:14 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
char *str;
|
|
|
|
size_t start, len, stop;
|
|
|
|
|
|
|
|
start = 0;
|
2009-02-23 23:12:33 -05:00
|
|
|
if (nargs < 1 || nargs > 3)
|
|
|
|
argcount("string.count", nargs, 1);
|
2009-08-23 03:06:57 -04:00
|
|
|
if (!fl_isstring(args[0]))
|
2009-02-23 23:12:33 -05:00
|
|
|
type_error("string.count", "string", args[0]);
|
2019-08-18 18:14:09 -04:00
|
|
|
len = cv_len((struct cvalue *)ptr(args[0]));
|
|
|
|
stop = len;
|
2009-02-23 23:12:33 -05:00
|
|
|
if (nargs > 1) {
|
|
|
|
start = toulong(args[1], "string.count");
|
|
|
|
if (start > len)
|
|
|
|
bounds_error("string.count", args[0], args[1]);
|
|
|
|
if (nargs > 2) {
|
|
|
|
stop = toulong(args[2], "string.count");
|
|
|
|
if (stop > len)
|
|
|
|
bounds_error("string.count", args[0], args[2]);
|
|
|
|
if (stop <= start)
|
|
|
|
return fixnum(0);
|
|
|
|
}
|
|
|
|
}
|
2019-08-18 18:14:09 -04:00
|
|
|
str = cvalue_data(args[0]);
|
2019-08-09 07:02:02 -04:00
|
|
|
return size_wrap(u8_charnum(str + start, stop - start));
|
2008-08-05 00:34:14 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
value_t fl_string_width(value_t *args, uint32_t nargs)
|
2009-05-31 18:09:26 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
char *s;
|
|
|
|
|
2009-05-31 18:09:26 -04:00
|
|
|
argcount("string.width", nargs, 1);
|
|
|
|
if (iscprim(args[0])) {
|
2019-08-09 12:25:43 -04:00
|
|
|
struct cprim *cp = (struct cprim *)ptr(args[0]);
|
2009-05-31 18:09:26 -04:00
|
|
|
if (cp_class(cp) == wchartype) {
|
2019-08-09 07:02:02 -04:00
|
|
|
int w = wcwidth(*(uint32_t *)cp_data(cp));
|
2009-05-31 18:09:26 -04:00
|
|
|
if (w < 0)
|
|
|
|
return FL_F;
|
|
|
|
return fixnum(w);
|
|
|
|
}
|
|
|
|
}
|
2019-08-18 18:14:09 -04:00
|
|
|
s = tostring(args[0], "string.width");
|
2009-05-31 18:09:26 -04:00
|
|
|
return size_wrap(u8_strwidth(s));
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
value_t fl_string_reverse(value_t *args, uint32_t nargs)
|
2008-08-05 00:34:14 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
size_t len;
|
|
|
|
value_t ns;
|
|
|
|
|
2008-08-05 00:34:14 -04:00
|
|
|
argcount("string.reverse", nargs, 1);
|
2009-08-23 03:06:57 -04:00
|
|
|
if (!fl_isstring(args[0]))
|
2008-08-05 00:34:14 -04:00
|
|
|
type_error("string.reverse", "string", args[0]);
|
2019-08-18 18:14:09 -04:00
|
|
|
len = cv_len((struct cvalue *)ptr(args[0]));
|
|
|
|
ns = cvalue_string(len);
|
2008-08-05 00:34:14 -04:00
|
|
|
u8_reverse(cvalue_data(ns), cvalue_data(args[0]), len);
|
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
value_t fl_string_encode(value_t *args, uint32_t nargs)
|
2008-08-05 00:34:14 -04:00
|
|
|
{
|
|
|
|
argcount("string.encode", nargs, 1);
|
|
|
|
if (iscvalue(args[0])) {
|
2019-08-09 12:36:20 -04:00
|
|
|
struct cvalue *cv = (struct cvalue *)ptr(args[0]);
|
2019-08-09 12:33:04 -04:00
|
|
|
struct fltype *t = cv_class(cv);
|
2009-01-02 18:00:21 -05:00
|
|
|
if (t->eltype == wchartype) {
|
2008-08-05 00:34:14 -04:00
|
|
|
size_t nc = cv_len(cv) / sizeof(uint32_t);
|
2019-08-09 07:02:02 -04:00
|
|
|
uint32_t *ptr = (uint32_t *)cv_data(cv);
|
2008-08-05 00:34:14 -04:00
|
|
|
size_t nbytes = u8_codingsize(ptr, nc);
|
|
|
|
value_t str = cvalue_string(nbytes);
|
2019-08-09 12:36:20 -04:00
|
|
|
ptr =
|
|
|
|
cv_data((struct cvalue *)ptr(args[0])); // relocatable pointer
|
2008-08-05 00:34:14 -04:00
|
|
|
u8_toutf8(cvalue_data(str), nbytes, ptr, nc);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
}
|
2009-03-04 22:48:17 -05:00
|
|
|
type_error("string.encode", "wchar array", args[0]);
|
2019-08-18 18:14:09 -04:00
|
|
|
return FL_NIL; // TODO: remove
|
2008-08-05 00:34:14 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
value_t fl_string_decode(value_t *args, uint32_t nargs)
|
2008-08-05 00:34:14 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
int term;
|
|
|
|
struct cvalue *cv;
|
|
|
|
char *ptr;
|
|
|
|
size_t nb, nc, newsz;
|
|
|
|
value_t wcstr;
|
|
|
|
uint32_t *pwc;
|
|
|
|
|
|
|
|
term = 0;
|
2008-08-05 00:34:14 -04:00
|
|
|
if (nargs == 2) {
|
2009-05-18 22:54:56 -04:00
|
|
|
term = (args[1] != FL_F);
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2009-05-18 22:54:56 -04:00
|
|
|
argcount("string.decode", nargs, 1);
|
2008-08-05 00:34:14 -04:00
|
|
|
}
|
2009-08-23 03:06:57 -04:00
|
|
|
if (!fl_isstring(args[0]))
|
2008-08-05 00:34:14 -04:00
|
|
|
type_error("string.decode", "string", args[0]);
|
2019-08-18 18:14:09 -04:00
|
|
|
cv = (struct cvalue *)ptr(args[0]);
|
|
|
|
ptr = (char *)cv_data(cv);
|
|
|
|
nb = cv_len(cv);
|
|
|
|
nc = u8_charnum(ptr, nb);
|
|
|
|
newsz = nc * sizeof(uint32_t);
|
2019-08-09 07:02:02 -04:00
|
|
|
if (term)
|
|
|
|
newsz += sizeof(uint32_t);
|
2019-08-18 18:14:09 -04:00
|
|
|
wcstr = cvalue(wcstringtype, newsz);
|
2019-08-09 12:36:20 -04:00
|
|
|
ptr = cv_data((struct cvalue *)ptr(args[0])); // relocatable pointer
|
2019-08-18 18:14:09 -04:00
|
|
|
pwc = cvalue_data(wcstr);
|
2008-08-05 00:34:14 -04:00
|
|
|
u8_toucs(pwc, nc, ptr, nb);
|
2019-08-09 07:02:02 -04:00
|
|
|
if (term)
|
|
|
|
pwc[nc] = 0;
|
2008-08-05 00:34:14 -04:00
|
|
|
return wcstr;
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
extern value_t fl_buffer(value_t *args, uint32_t nargs);
|
2009-03-11 10:52:37 -04:00
|
|
|
extern value_t stream_to_string(value_t *ps);
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
value_t fl_string(value_t *args, uint32_t nargs)
|
2008-08-05 00:34:14 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
value_t arg, buf;
|
|
|
|
struct ios *s;
|
|
|
|
uint32_t i;
|
2019-08-25 15:39:35 -04:00
|
|
|
value_t outp;
|
2019-08-18 18:14:09 -04:00
|
|
|
|
2009-08-23 03:06:57 -04:00
|
|
|
if (nargs == 1 && fl_isstring(args[0]))
|
2009-03-11 10:52:37 -04:00
|
|
|
return args[0];
|
2019-08-18 18:14:09 -04:00
|
|
|
buf = fl_buffer(NULL, 0);
|
2017-07-12 09:10:19 -04:00
|
|
|
fl_gc_handle(&buf);
|
2019-08-18 18:14:09 -04:00
|
|
|
s = value2c(struct ios *, buf);
|
2019-08-25 15:39:35 -04:00
|
|
|
FOR_ARGS(i, 0, arg, args) { display_defaults(s, args[i]); }
|
2019-08-18 18:14:09 -04:00
|
|
|
outp = stream_to_string(&buf);
|
2009-05-18 22:54:56 -04:00
|
|
|
fl_free_gc_handles(1);
|
2009-03-11 10:52:37 -04:00
|
|
|
return outp;
|
2008-08-05 00:34:14 -04:00
|
|
|
}
|
|
|
|
|
2019-08-28 09:18:37 -04:00
|
|
|
value_t builtin_string_split(value_t *args, uint32_t nargs)
|
2008-08-05 00:34:14 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
char *s;
|
|
|
|
char *delim;
|
|
|
|
size_t len, dlen, ssz, tokend, tokstart, i, junk;
|
|
|
|
value_t first, c, last;
|
|
|
|
|
2019-08-28 09:18:37 -04:00
|
|
|
argcount("string-split", nargs, 2);
|
|
|
|
s = tostring(args[0], "string-split");
|
|
|
|
delim = tostring(args[1], "string-split");
|
2019-08-18 18:14:09 -04:00
|
|
|
len = cv_len((struct cvalue *)ptr(args[0]));
|
|
|
|
dlen = cv_len((struct cvalue *)ptr(args[1]));
|
|
|
|
tokend = tokstart = i = 0;
|
|
|
|
first = c = FL_NIL;
|
2009-05-18 22:54:56 -04:00
|
|
|
fl_gc_handle(&first);
|
|
|
|
fl_gc_handle(&last);
|
2008-08-05 00:34:14 -04:00
|
|
|
do {
|
|
|
|
// find and allocate next token
|
|
|
|
tokstart = tokend = i;
|
|
|
|
while (i < len &&
|
|
|
|
!u8_memchr(delim, u8_nextmemchar(s, &i), dlen, &junk))
|
|
|
|
tokend = i;
|
|
|
|
ssz = tokend - tokstart;
|
2009-05-18 22:54:56 -04:00
|
|
|
last = c; // save previous cons cell
|
2009-08-09 14:04:03 -04:00
|
|
|
c = fl_cons(cvalue_string(ssz), FL_NIL);
|
2008-08-05 00:34:14 -04:00
|
|
|
|
|
|
|
// we've done allocation; reload movable pointers
|
2019-08-09 12:36:20 -04:00
|
|
|
s = cv_data((struct cvalue *)ptr(args[0]));
|
|
|
|
delim = cv_data((struct cvalue *)ptr(args[1]));
|
2008-08-05 00:34:14 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
if (ssz)
|
2019-08-09 12:36:20 -04:00
|
|
|
memcpy(cv_data((struct cvalue *)ptr(car_(c))), &s[tokstart], ssz);
|
2008-08-05 00:34:14 -04:00
|
|
|
|
|
|
|
// link new cell
|
2009-08-09 14:04:03 -04:00
|
|
|
if (last == FL_NIL)
|
2019-08-09 07:02:02 -04:00
|
|
|
first = c; // first time, save first cons
|
2009-05-18 22:54:56 -04:00
|
|
|
else
|
2019-08-09 12:28:14 -04:00
|
|
|
((struct cons *)ptr(last))->cdr = c;
|
2008-08-05 00:34:14 -04:00
|
|
|
|
|
|
|
// note this tricky condition: if the string ends with a
|
|
|
|
// delimiter, we need to go around one more time to add an
|
|
|
|
// empty string. this happens when (i==len && tokend<i)
|
2019-08-09 07:02:02 -04:00
|
|
|
} while (i < len || (i == len && (tokend != i)));
|
2009-05-18 22:54:56 -04:00
|
|
|
fl_free_gc_handles(2);
|
|
|
|
return first;
|
2008-08-05 00:34:14 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
value_t fl_string_sub(value_t *args, uint32_t nargs)
|
2008-08-05 00:34:14 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
char *s;
|
|
|
|
size_t len, i1, i2;
|
|
|
|
value_t ns;
|
|
|
|
|
2009-05-06 22:10:52 -04:00
|
|
|
if (nargs != 2)
|
|
|
|
argcount("string.sub", nargs, 3);
|
2019-08-18 18:14:09 -04:00
|
|
|
s = tostring(args[0], "string.sub");
|
|
|
|
len = cv_len((struct cvalue *)ptr(args[0]));
|
2008-08-05 00:34:14 -04:00
|
|
|
i1 = toulong(args[1], "string.sub");
|
|
|
|
if (i1 > len)
|
|
|
|
bounds_error("string.sub", args[0], args[1]);
|
2009-05-06 22:10:52 -04:00
|
|
|
if (nargs == 3) {
|
|
|
|
i2 = toulong(args[2], "string.sub");
|
|
|
|
if (i2 > len)
|
|
|
|
bounds_error("string.sub", args[0], args[2]);
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2009-05-06 22:10:52 -04:00
|
|
|
i2 = len;
|
|
|
|
}
|
2008-08-05 00:34:14 -04:00
|
|
|
if (i2 <= i1)
|
|
|
|
return cvalue_string(0);
|
2019-08-18 18:14:09 -04:00
|
|
|
ns = cvalue_string(i2 - i1);
|
2019-08-09 12:36:20 -04:00
|
|
|
memcpy(cv_data((struct cvalue *)ptr(ns)), &s[i1], i2 - i1);
|
2008-08-05 00:34:14 -04:00
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
value_t fl_string_char(value_t *args, uint32_t nargs)
|
2008-08-05 00:34:14 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
char *s;
|
|
|
|
size_t len, i, sl;
|
|
|
|
|
2008-08-05 00:34:14 -04:00
|
|
|
argcount("string.char", nargs, 2);
|
2019-08-18 18:14:09 -04:00
|
|
|
s = tostring(args[0], "string.char");
|
|
|
|
len = cv_len((struct cvalue *)ptr(args[0]));
|
|
|
|
i = toulong(args[1], "string.char");
|
2008-08-05 00:34:14 -04:00
|
|
|
if (i >= len)
|
|
|
|
bounds_error("string.char", args[0], args[1]);
|
2019-08-18 18:14:09 -04:00
|
|
|
sl = u8_seqlen(&s[i]);
|
2019-08-09 07:02:02 -04:00
|
|
|
if (sl > len || i > len - sl)
|
2008-08-05 00:34:14 -04:00
|
|
|
bounds_error("string.char", args[0], args[1]);
|
2008-12-23 23:43:36 -05:00
|
|
|
return mk_wchar(u8_nextchar(s, &i));
|
|
|
|
}
|
|
|
|
|
2019-08-26 04:12:50 -04:00
|
|
|
value_t builtin_char_upcase(value_t *args, uint32_t nargs)
|
2009-03-24 17:27:38 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
struct cprim *cp;
|
|
|
|
|
2009-03-24 17:27:38 -04:00
|
|
|
argcount("char.upcase", nargs, 1);
|
2019-08-18 18:14:09 -04:00
|
|
|
cp = (struct cprim *)ptr(args[0]);
|
2009-03-24 17:27:38 -04:00
|
|
|
if (!iscprim(args[0]) || cp_class(cp) != wchartype)
|
2017-08-19 12:27:27 -04:00
|
|
|
type_error("char.upcase", "wchar", args[0]);
|
2019-08-09 07:02:02 -04:00
|
|
|
return mk_wchar(towupper(*(int32_t *)cp_data(cp)));
|
2009-03-24 17:27:38 -04:00
|
|
|
}
|
2019-08-26 04:12:50 -04:00
|
|
|
|
|
|
|
value_t builtin_char_downcase(value_t *args, uint32_t nargs)
|
2009-03-24 17:27:38 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
struct cprim *cp;
|
|
|
|
|
2009-03-24 17:27:38 -04:00
|
|
|
argcount("char.downcase", nargs, 1);
|
2019-08-18 18:14:09 -04:00
|
|
|
cp = (struct cprim *)ptr(args[0]);
|
2009-03-24 17:27:38 -04:00
|
|
|
if (!iscprim(args[0]) || cp_class(cp) != wchartype)
|
2017-08-19 12:27:27 -04:00
|
|
|
type_error("char.downcase", "wchar", args[0]);
|
2019-08-09 07:02:02 -04:00
|
|
|
return mk_wchar(towlower(*(int32_t *)cp_data(cp)));
|
2009-03-24 17:27:38 -04:00
|
|
|
}
|
|
|
|
|
2019-08-26 04:47:56 -04:00
|
|
|
value_t string_map_chars(int (*mapfun)(int), char *os)
|
|
|
|
{
|
|
|
|
char *ns;
|
|
|
|
value_t nv;
|
|
|
|
size_t n, i;
|
|
|
|
|
|
|
|
n = strlen(os);
|
|
|
|
nv = cvalue_string(n);
|
|
|
|
ns = cv_data((struct cvalue *)ptr(nv));
|
|
|
|
memcpy(ns, os, n);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
ns[i] = mapfun(ns[i]);
|
|
|
|
}
|
|
|
|
return nv;
|
|
|
|
}
|
|
|
|
|
|
|
|
value_t builtin_string_upcase(value_t *args, uint32_t nargs)
|
|
|
|
{
|
|
|
|
argcount("string-upcase", nargs, 1);
|
|
|
|
return string_map_chars(toupper, tostring(args[0], "string-upcase"));
|
|
|
|
}
|
|
|
|
|
|
|
|
value_t builtin_string_downcase(value_t *args, uint32_t nargs)
|
|
|
|
{
|
|
|
|
argcount("string-downcase", nargs, 1);
|
|
|
|
return string_map_chars(tolower, tostring(args[0], "string-downcase"));
|
|
|
|
}
|
|
|
|
|
2019-08-26 04:12:50 -04:00
|
|
|
value_t builtin_char_alphabetic(value_t *args, uint32_t nargs)
|
2017-08-19 14:19:23 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
struct cprim *cp;
|
|
|
|
|
2017-08-19 14:19:23 -04:00
|
|
|
argcount("char-alphabetic?", nargs, 1);
|
2019-08-18 18:14:09 -04:00
|
|
|
cp = (struct cprim *)ptr(args[0]);
|
2017-08-19 14:19:23 -04:00
|
|
|
if (!iscprim(args[0]) || cp_class(cp) != wchartype)
|
|
|
|
type_error("char-alphabetic?", "wchar", args[0]);
|
2019-08-09 07:02:02 -04:00
|
|
|
return iswalpha(*(int32_t *)cp_data(cp)) ? FL_T : FL_F;
|
2017-08-19 14:19:23 -04:00
|
|
|
}
|
|
|
|
|
2008-12-23 23:43:36 -05:00
|
|
|
static value_t mem_find_byte(char *s, char c, size_t start, size_t len)
|
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = memchr(s + start, c, len - start);
|
2008-12-23 23:43:36 -05:00
|
|
|
if (p == NULL)
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
return FL_F;
|
2008-12-23 23:43:36 -05:00
|
|
|
return size_wrap((size_t)(p - s));
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
value_t fl_string_find(value_t *args, uint32_t nargs)
|
2008-12-23 23:43:36 -05:00
|
|
|
{
|
|
|
|
char cbuf[8];
|
2019-08-18 18:14:09 -04:00
|
|
|
char *s;
|
|
|
|
char *needle;
|
|
|
|
struct cprim *cp;
|
|
|
|
value_t v;
|
|
|
|
size_t start, len, needlesz, i;
|
|
|
|
|
2008-12-23 23:43:36 -05:00
|
|
|
if (nargs == 3)
|
|
|
|
start = toulong(args[2], "string.find");
|
2019-08-18 18:14:09 -04:00
|
|
|
else {
|
2008-12-23 23:43:36 -05:00
|
|
|
argcount("string.find", nargs, 2);
|
2019-08-18 18:14:09 -04:00
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
s = tostring(args[0], "string.find");
|
|
|
|
len = cv_len((struct cvalue *)ptr(args[0]));
|
2008-12-23 23:43:36 -05:00
|
|
|
if (start > len)
|
|
|
|
bounds_error("string.find", args[0], args[2]);
|
2009-01-02 18:00:21 -05:00
|
|
|
|
2019-08-18 18:14:09 -04:00
|
|
|
v = args[1];
|
|
|
|
cp = (struct cprim *)ptr(v);
|
2009-01-02 18:00:21 -05:00
|
|
|
if (iscprim(v) && cp_class(cp) == wchartype) {
|
2019-08-09 07:02:02 -04:00
|
|
|
uint32_t c = *(uint32_t *)cp_data(cp);
|
2008-12-23 23:43:36 -05:00
|
|
|
if (c <= 0x7f)
|
|
|
|
return mem_find_byte(s, (char)c, start, len);
|
|
|
|
needlesz = u8_toutf8(cbuf, sizeof(cbuf), &c, 1);
|
|
|
|
needle = cbuf;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (iscprim(v) && cp_class(cp) == bytetype) {
|
|
|
|
return mem_find_byte(s, *(char *)cp_data(cp), start, len);
|
|
|
|
} else if (fl_isstring(v)) {
|
2019-08-09 12:36:20 -04:00
|
|
|
struct cvalue *cv = (struct cvalue *)ptr(v);
|
2008-12-24 00:02:58 -05:00
|
|
|
needlesz = cv_len(cv);
|
2019-08-09 07:02:02 -04:00
|
|
|
needle = (char *)cv_data(cv);
|
|
|
|
} else {
|
2008-12-24 00:02:58 -05:00
|
|
|
type_error("string.find", "string", args[1]);
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
if (needlesz > len - start)
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
return FL_F;
|
2008-12-24 00:02:58 -05:00
|
|
|
else if (needlesz == 1)
|
|
|
|
return mem_find_byte(s, needle[0], start, len);
|
|
|
|
else if (needlesz == 0)
|
|
|
|
return size_wrap(start);
|
2019-08-09 07:02:02 -04:00
|
|
|
for (i = start; i < len - needlesz + 1; i++) {
|
2008-12-23 23:43:36 -05:00
|
|
|
if (s[i] == needle[0]) {
|
2019-08-09 07:02:02 -04:00
|
|
|
if (!memcmp(&s[i + 1], needle + 1, needlesz - 1))
|
2008-12-23 23:43:36 -05:00
|
|
|
return size_wrap(i);
|
|
|
|
}
|
|
|
|
}
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
return FL_F;
|
2008-08-05 00:34:14 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
value_t fl_string_inc(value_t *args, uint32_t nargs)
|
2008-08-05 00:34:14 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
char *s;
|
|
|
|
size_t len, cnt, i;
|
|
|
|
|
2008-08-05 00:34:14 -04:00
|
|
|
if (nargs < 2 || nargs > 3)
|
|
|
|
argcount("string.inc", nargs, 2);
|
2019-08-18 18:14:09 -04:00
|
|
|
s = tostring(args[0], "string.inc");
|
|
|
|
len = cv_len((struct cvalue *)ptr(args[0]));
|
|
|
|
i = toulong(args[1], "string.inc");
|
|
|
|
cnt = 1;
|
2008-08-05 00:34:14 -04:00
|
|
|
if (nargs == 3)
|
|
|
|
cnt = toulong(args[2], "string.inc");
|
|
|
|
while (cnt--) {
|
|
|
|
if (i >= len)
|
|
|
|
bounds_error("string.inc", args[0], args[1]);
|
2009-03-16 23:29:17 -04:00
|
|
|
(void)(isutf(s[++i]) || isutf(s[++i]) || isutf(s[++i]) || ++i);
|
2008-08-05 00:34:14 -04:00
|
|
|
}
|
|
|
|
return size_wrap(i);
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
value_t fl_string_dec(value_t *args, uint32_t nargs)
|
2008-08-05 00:34:14 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
char *s;
|
|
|
|
size_t len, cnt, i;
|
|
|
|
|
2008-08-05 00:34:14 -04:00
|
|
|
if (nargs < 2 || nargs > 3)
|
|
|
|
argcount("string.dec", nargs, 2);
|
2019-08-18 18:14:09 -04:00
|
|
|
s = tostring(args[0], "string.dec");
|
|
|
|
len = cv_len((struct cvalue *)ptr(args[0]));
|
|
|
|
i = toulong(args[1], "string.dec");
|
|
|
|
cnt = 1;
|
2008-08-05 00:34:14 -04:00
|
|
|
if (nargs == 3)
|
|
|
|
cnt = toulong(args[2], "string.dec");
|
|
|
|
// note: i is allowed to start at index len
|
|
|
|
if (i > len)
|
|
|
|
bounds_error("string.dec", args[0], args[1]);
|
|
|
|
while (cnt--) {
|
|
|
|
if (i == 0)
|
|
|
|
bounds_error("string.dec", args[0], args[1]);
|
2009-03-16 23:29:17 -04:00
|
|
|
(void)(isutf(s[--i]) || isutf(s[--i]) || isutf(s[--i]) || --i);
|
2008-08-05 00:34:14 -04:00
|
|
|
}
|
|
|
|
return size_wrap(i);
|
|
|
|
}
|
|
|
|
|
2010-08-04 15:03:19 -04:00
|
|
|
static unsigned long get_radix_arg(value_t arg, char *fname)
|
2009-03-23 15:49:08 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
unsigned long radix;
|
|
|
|
|
|
|
|
radix = toulong(arg, fname);
|
2009-03-23 15:49:08 -04:00
|
|
|
if (radix < 2 || radix > 36)
|
2009-03-24 22:28:21 -04:00
|
|
|
lerrorf(ArgError, "%s: invalid radix", fname);
|
2009-03-23 15:49:08 -04:00
|
|
|
return radix;
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
value_t fl_numbertostring(value_t *args, uint32_t nargs)
|
2009-02-01 00:41:43 -05:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
char buf[128];
|
|
|
|
uint64_t num;
|
|
|
|
unsigned long radix;
|
|
|
|
value_t n;
|
|
|
|
char *str;
|
|
|
|
int neg;
|
|
|
|
|
2009-02-01 00:41:43 -05:00
|
|
|
if (nargs < 1 || nargs > 2)
|
|
|
|
argcount("number->string", nargs, 2);
|
2019-08-18 18:14:09 -04:00
|
|
|
n = args[0];
|
|
|
|
neg = 0;
|
2019-08-09 07:02:02 -04:00
|
|
|
if (isfixnum(n))
|
|
|
|
num = numval(n);
|
|
|
|
else if (!iscprim(n))
|
|
|
|
type_error("number->string", "integer", n);
|
|
|
|
else
|
2019-08-09 12:25:43 -04:00
|
|
|
num = conv_to_uint64(cp_data((struct cprim *)ptr(n)),
|
|
|
|
cp_numtype((struct cprim *)ptr(n)));
|
2019-08-09 07:02:02 -04:00
|
|
|
if (numval(fl_compare(args[0], fixnum(0))) < 0) {
|
2009-03-13 18:26:44 -04:00
|
|
|
num = -num;
|
|
|
|
neg = 1;
|
|
|
|
}
|
2019-08-18 18:14:09 -04:00
|
|
|
radix = 10;
|
2009-03-23 15:49:08 -04:00
|
|
|
if (nargs == 2)
|
|
|
|
radix = get_radix_arg(args[1], "number->string");
|
2019-08-18 18:14:09 -04:00
|
|
|
str = uint2str(buf, sizeof(buf), num, radix);
|
2009-03-13 18:26:44 -04:00
|
|
|
if (neg && str > &buf[0])
|
|
|
|
*(--str) = '-';
|
2009-02-01 00:41:43 -05:00
|
|
|
return string_from_cstr(str);
|
|
|
|
}
|
|
|
|
|
2009-03-23 15:49:08 -04:00
|
|
|
value_t fl_stringtonumber(value_t *args, uint32_t nargs)
|
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
char *str;
|
|
|
|
value_t n;
|
|
|
|
unsigned long radix;
|
|
|
|
|
2009-03-23 15:49:08 -04:00
|
|
|
if (nargs < 1 || nargs > 2)
|
|
|
|
argcount("string->number", nargs, 2);
|
2019-08-18 18:14:09 -04:00
|
|
|
str = tostring(args[0], "string->number");
|
|
|
|
radix = 0;
|
2009-03-23 15:49:08 -04:00
|
|
|
if (nargs == 2)
|
|
|
|
radix = get_radix_arg(args[1], "string->number");
|
|
|
|
if (!isnumtok_base(str, &n, (int)radix))
|
|
|
|
return FL_F;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
value_t fl_string_isutf8(value_t *args, uint32_t nargs)
|
2013-06-08 19:29:15 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
char *s;
|
|
|
|
size_t len;
|
|
|
|
|
2013-06-08 19:29:15 -04:00
|
|
|
argcount("string.isutf8", nargs, 1);
|
2019-08-18 18:14:09 -04:00
|
|
|
s = tostring(args[0], "string.isutf8");
|
|
|
|
len = cv_len((struct cvalue *)ptr(args[0]));
|
2013-06-08 19:29:15 -04:00
|
|
|
return u8_isvalid(s, len) ? FL_T : FL_F;
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:20:18 -04:00
|
|
|
static struct builtinspec stringfunc_info[] = {
|
2008-11-23 02:12:37 -05:00
|
|
|
{ "string", fl_string },
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
{ "string?", fl_stringp },
|
2009-02-23 23:12:33 -05:00
|
|
|
{ "string.count", fl_string_count },
|
2009-05-31 18:09:26 -04:00
|
|
|
{ "string.width", fl_string_width },
|
2008-11-23 02:12:37 -05:00
|
|
|
{ "string.sub", fl_string_sub },
|
2008-12-23 23:43:36 -05:00
|
|
|
{ "string.find", fl_string_find },
|
2008-11-23 02:12:37 -05:00
|
|
|
{ "string.char", fl_string_char },
|
|
|
|
{ "string.inc", fl_string_inc },
|
|
|
|
{ "string.dec", fl_string_dec },
|
|
|
|
{ "string.reverse", fl_string_reverse },
|
|
|
|
{ "string.encode", fl_string_encode },
|
|
|
|
{ "string.decode", fl_string_decode },
|
2013-06-08 19:29:15 -04:00
|
|
|
{ "string.isutf8", fl_string_isutf8 },
|
2009-02-01 00:41:43 -05:00
|
|
|
|
2019-08-26 04:12:50 -04:00
|
|
|
{ "char-upcase", builtin_char_upcase },
|
|
|
|
{ "char-downcase", builtin_char_downcase },
|
|
|
|
{ "char-alphabetic?", builtin_char_alphabetic },
|
|
|
|
|
2019-08-26 04:47:56 -04:00
|
|
|
{ "string-upcase", builtin_string_upcase },
|
|
|
|
{ "string-downcase", builtin_string_downcase },
|
|
|
|
|
2009-02-01 00:41:43 -05:00
|
|
|
{ "number->string", fl_numbertostring },
|
2009-03-23 15:49:08 -04:00
|
|
|
{ "string->number", fl_stringtonumber },
|
2009-02-01 00:41:43 -05:00
|
|
|
|
2008-11-23 02:12:37 -05:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
void stringfuncs_init(void) { assign_global_builtins(stringfunc_info); }
|