shithub: femtolisp

Download patch

ref: cf3f244661f70421b8c266b3670a8481835d604e
parent: 1cca028c935fb4b59ec19c570be7a4d1425f8cf1
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Sun Oct 27 15:17:56 EDT 2024

get rid of string.dec and string.inc

--- a/aliases.scm
+++ b/aliases.scm
@@ -86,18 +86,16 @@
 (define (make-string k (fill #\space))
   (string.rep fill k))
 
-(define (string-ref s i)
-  (string.char s (string.inc s 0 i)))
+(define string-ref string.char)
 
 (define (list->string l) (apply string l))
 (define (string->list s)
-  (do ((i (sizeof s) i)
+  (do ((i (string.count s) i)
        (l '() (cons (string.char s i) l)))
       ((= i 0) l)
-    (set! i (string.dec s i))))
+    (set! i (1- i))))
 
-(define (substring s start end)
-  (string.sub s (string.inc s 0 start) (string.inc s 0 end)))
+(define substring string.sub)
 
 (define (input-port? x) (iostream? x))
 (define (output-port? x) (iostream? x))
@@ -145,11 +143,10 @@
 
 (define (put-u8 port o) (io.write port (uint8 o)))
 (define (put-string port s (start 0) (count #f))
-  (let* ((start (string.inc s 0 start))
-	 (end (if count
-		  (string.inc s start count)
-		  (sizeof s))))
-    (io.write port s start (- end start))))
+  (let* ((end (if count
+		      (+ start count)
+		      (string.count s))))
+    (io.write port (string.sub s start (- end start)))))
 
 (define (io.skipws s)
   (let ((c (io.peekc s)))
--- a/compiler.lsp
+++ b/compiler.lsp
@@ -462,7 +462,7 @@
 (define (keyword->symbol k)
   (if (keyword? k)
       (symbol (let ((s (string k)))
-		(string.sub s 0 (string.dec s (length s)))))
+		(string.sub s 0 (1- (string.count s)))))
       k))
 
 (define (lambda-arg-names argl)
--- a/flisp.boot
+++ b/flisp.boot
@@ -56,7 +56,7 @@
   length=) 1arg-lambda?)
 	    <= #fn("7000n210L;IB0470051;380470151S:" #(nan?) <=) >
 	    #fn("7000n210L:" #() >) >= #fn("7000n201L;IB0470051;380470151S:" #(nan?) >=)
-	    Instructions #table(not 35  vargc 76  load1 27  = 60  setc.l 75  sub2 80  brne.l 85  largc 81  brnn 26  loadc.l 70  loadi8 66  < 28  nop 46  set-cdr! 30  loada 8  neg 37  bound? 42  / 58  brn.l 88  lvargc 82  brt 25  trycatch 77  null? 38  load0 21  jmp.l 48  loadv 2  seta 15  keyargs 91  * 57  function? 44  builtin? 43  aref 23  optargs 89  loadt 20  vector? 45  cdr 13  brf 3  loadc00 17  symbol? 34  cadr 36  pop 4  pair? 18  for 78  closure 14  loadf 31  compare 61  loadv.l 67  setg.l 72  brn 87  eqv? 51  aset! 64  atom? 24  eq? 33  boolean? 39  brt.l 50  tapply 79  dummy_nil 94  loada0 0  brbound 90  dup 11  loadc01 22  list 53  loadc 9  apply 54  dummy_t 93  setg 71  loada1 1  tcall.l 84  jmp 16  fixnum? 41  cons 32  loadg.l 68  tcall 6  dummy_eof 95  call 5  - 56  brf.l 49  + 55  dummy_f 92  add2 29  seta.l 73  loadnil 65  brnn.l 86  setc 74  set-car! 47  loadg 7  vector 63  loada.l 69  argc 62  div0 59  ret 10  car 12  number? 40  equal? 52  call.l 83  brne 19)
+	    Instructions #table(brne 19  vargc 76  load1 27  = 60  setc.l 75  sub2 80  brne.l 85  largc 81  brnn 26  loadc.l 70  loadi8 66  < 28  nop 46  set-cdr! 30  loada 8  neg 37  bound? 42  / 58  brn.l 88  lvargc 82  brt 25  trycatch 77  null? 38  load0 21  jmp.l 48  loadv 2  seta 15  keyargs 91  * 57  function? 44  builtin? 43  aref 23  optargs 89  loadt 20  vector? 45  cdr 13  brf 3  loadc00 17  symbol? 34  cadr 36  pop 4  pair? 18  for 78  closure 14  loadf 31  compare 61  loadv.l 67  setg.l 72  brn 87  eqv? 51  aset! 64  atom? 24  eq? 33  boolean? 39  brt.l 50  tapply 79  dummy_nil 94  loada0 0  brbound 90  dup 11  loadc01 22  list 53  loadc 9  apply 54  dummy_t 93  setg 71  loada1 1  tcall.l 84  jmp 16  fixnum? 41  cons 32  loadg.l 68  tcall 6  dummy_eof 95  call 5  - 56  brf.l 49  + 55  dummy_f 92  add2 29  seta.l 73  loadnil 65  brnn.l 86  setc 74  set-car! 47  loadg 7  vector 63  loada.l 69  argc 62  div0 59  ret 10  car 12  number? 40  equal? 52  call.l 83  not 35)
 	    __init_globals #fn("6000n020w1422w3474w5476w7478w9:" #("/"
 								   *directory-separator*
 								   "\n"
@@ -284,8 +284,8 @@
 	    iota #fn("8000n17071062:" #(map-int identity) iota) keyword->symbol
 	    #fn("9000n1200513@02122230515161:0:" #(#fn(keyword?)
 						   #fn(symbol)
-						   #fn("<000n1200E210220515263:" #(#fn(string.sub)
-  #fn(string.dec) #fn(length))) #fn(string)) keyword->symbol)
+						   #fn(";000n1200E71220515163:" #(#fn(string.sub)
+  1- #fn(string.count))) #fn(string)) keyword->symbol)
 	    keyword-arg? #fn("7000n10B;3904200<61:" #(#fn(keyword?)) keyword-arg?)
 	    lambda-arg-names #fn("9000n170217205162:" #(map! #fn("7000n10B390700<61:0:" #(keyword->symbol))
 							to-proper) lambda-arg-names)
@@ -417,23 +417,20 @@
   #fn(io.tostring!))) #fn(buffer)) string.join)
 	    string.lpad #fn(";000n3207182122051\x8052062:" #(#fn(string)
 							     string.rep #fn(string.count)) string.lpad)
-	    string.map #fn("9000n220>21502215162:" #(#fn("7000n220>E51421061:" #(#fn(";000n1D0FL3S0420A910219110525152422911052?0@\v/:" #(#fn(io.putc)
-  #fn(string.char) #fn(string.inc))) #fn(io.tostring!)))
+	    string.map #fn("9000n220>21502215162:" #(#fn("7000n220>E51421061:" #(#fn(";000n1D0FL3P0420A910219110525152472051?0@\x0e/:" #(#fn(io.putc)
+  #fn(string.char) 1+)) #fn(io.tostring!)))
 						     #fn(buffer)
-						     #fn(length)) string.map)
+						     #fn(string.count)) string.map)
 	    string.rep #fn(";000n21r4L3`0701E5235021:1Kl38022061:1r2l390220062:2200063:731513@02207401K\x805262:742200521r2j262:" #(<=
   "" #fn(string) odd? string.rep) string.rep)
 	    string.rpad #fn("<000n32007182122051\x805262:" #(#fn(string)
 							     string.rep #fn(string.count)) string.rpad)
-	    string.tail #fn(";000n2200210E15362:" #(#fn(string.sub)
-						    #fn(string.inc)) string.tail)
-	    string.trim #fn("8000n320>DD62:" #(#fn("8000n220>?0421>?1422>23A5161:" #(#fn(";000n48283L;3?042012108252523A0A0122082528364:82:" #(#fn(string.find)
-  #fn(string.char) #fn(string.inc)) trim-start)
-  #fn("<000n37082E52;3D04211220230825252523?0F01230825263:82:" #(> #fn(string.find)
-								 #fn(string.char)
-								 #fn(string.dec)) trim-end)
+	    string.tail #fn("8000n2200162:" #(#fn(string.sub)) string.tail)
+	    string.trim #fn("8000n320>DD62:" #(#fn("8000n220>?0421>?1422>23A5161:" #(#fn(":000n48283L;3?042012108252523@0A017282518364:82:" #(#fn(string.find)
+  #fn(string.char) 1+) trim-start) #fn(":000n37082E52;3?042112208252523>0F0173825163:82:" #(>
+  #fn(string.find) #fn(string.char) 1-) trim-end)
   #fn("<000n120910A910911E054F91091205363:" #(#fn(string.sub)))
-  #fn(length)))) string.trim)
+  #fn(string.count)))) string.trim)
 	    symbol-syntax #fn("9000n120710O63:" #(#fn(get)
 						  *syntax-environment*) symbol-syntax)
 	    table.clone #fn("7000n120>215061:" #(#fn("9000n12021>qA5340:" #(#fn(table.foldl)
--- a/string.c
+++ b/string.c
@@ -346,45 +346,6 @@
 	return FL_F;
 }
 
-BUILTIN("string.inc", string_inc)
-{
-	if(nargs < 2 || nargs > 3)
-		argcount(nargs, 2);
-	char *s = tostring(args[0]);
-	size_t len = cv_len((cvalue_t*)ptr(args[0]));
-	size_t i = toulong(args[1]);
-	size_t cnt = 1;
-	if(nargs == 3)
-		cnt = toulong(args[2]);
-	while(cnt--){
-		if(i >= len)
-			bounds_error(args[0], args[1]);
-		(void)(isutf(s[++i]) || isutf(s[++i]) || isutf(s[++i]) || ++i);
-	}
-	return size_wrap(i);
-}
-
-BUILTIN("string.dec", string_dec)
-{
-	if(nargs < 2 || nargs > 3)
-		argcount(nargs, 2);
-	char *s = tostring(args[0]);
-	size_t len = cv_len((cvalue_t*)ptr(args[0]));
-	size_t i = toulong(args[1]);
-	size_t cnt = 1;
-	if(nargs == 3)
-		cnt = toulong(args[2]);
-	// note: i is allowed to start at index len
-	if(i > len)
-		bounds_error(args[0], args[1]);
-	while(cnt--){
-		if(i == 0)
-			bounds_error(args[0], args[1]);
-		(void)(isutf(s[--i]) || isutf(s[--i]) || isutf(s[--i]) || --i);
-	}
-	return size_wrap(i);
-}
-
 static unsigned long
 get_radix_arg(value_t arg)
 {
--- a/system.lsp
+++ b/system.lsp
@@ -676,7 +676,7 @@
 
 ; string functions ------------------------------------------------------------
 
-(define (string.tail s n) (string.sub s (string.inc s 0 n)))
+(define (string.tail s n) (string.sub s n))
 
 (define *whitespace*
   (string.encode #array(rune 9 10 11 12 13 32 133 160 5760 6158 8192
@@ -687,14 +687,14 @@
   (define (trim-start s chars i L)
     (if (and (< i L)
 	     (string.find chars (string.char s i)))
-	(trim-start s chars (string.inc s i) L)
+	(trim-start s chars (1+ i) L)
 	i))
   (define (trim-end s chars i)
     (if (and (> i 0)
-	     (string.find chars (string.char s (string.dec s i))))
-	(trim-end s chars (string.dec s i))
+	     (string.find chars (string.char s i)))
+	(trim-end s chars (1- i))
 	i))
-  (let ((L (length s)))
+  (let ((L (string.count s)))
     (string.sub s
 		(trim-start s at-start 0 L)
 		(trim-end   s at-end   L))))
@@ -701,11 +701,11 @@
 
 (define (string.map f s)
   (let ((b (buffer))
-	(n (length s)))
+	(n (string.count s)))
     (let ((i 0))
       (while (< i n)
 	     (begin (io.putc b (f (string.char s i)))
-		    (set! i (string.inc s i)))))
+		    (set! i (1+ i)))))
     (io.tostring! b)))
 
 (define (string.rep s k)
--- a/test/test.lsp
+++ b/test/test.lsp
@@ -207,14 +207,6 @@
                         (begin (set! lst (cdr lst))
                                (set! i (1+ i)))))))
 
-(define (string.findall haystack needle . offs)
-  (define (sub h n offs lst)
-    (let ((i (string.find h n offs)))
-      (if i
-	  (sub h n (string.inc h i) (cons i lst))
-	  (reverse! lst))))
-  (sub haystack needle (if (null? offs) 0 (car offs)) ()))
-
 (let ((*profiles* (table)))
   (set! profile
 	(lambda (s)