mes: Basic support for string-index.
authorJan Nieuwenhuizen <janneke@gnu.org>
Sat, 6 May 2017 21:00:27 +0000 (23:00 +0200)
committerJan Nieuwenhuizen <janneke@gnu.org>
Sat, 6 May 2017 21:00:27 +0000 (23:00 +0200)
* module/srfi/srfi-13.mes (string-index): New function.
* tests/srfi-13.test ("string-index"): Test it.

module/srfi/srfi-13.mes
tests/srfi-13.test

index caf31a00714a017bf5c9f1c0558d106788bd90c5..98b2d667be85ae868089c26a52963e4530be8456 100644 (file)
@@ -1,7 +1,7 @@
 ;;; -*-scheme-*-
 
 ;;; Mes --- Maxwell Equations of Software
-;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
+;;; Copyright © 2016,2017 Jan Nieuwenhuizen <janneke@gnu.org>
 ;;;
 ;;; This file is part of Mes.
 ;;;
   (let ((p (if (procedure? pred) pred
                (lambda (c) (not (eq? pred c))))))
     (list->string (filter p (string->list s)))))
+
+(define (string-index s pred . rest)
+  (let* ((start (and (pair? rest) (car rest)))
+         (end (and start (pair? (cdr rest)) (cadr rest))))
+    (if (not (char? pred)) (error "string-index: not supported: pred=" pred))
+    (if start (error "string-index: not supported: start=" start))
+    (if end (error "string-index: not supported: end=" end))
+    (let loop ((lst (string->list s)) (i 0))
+      (if (null? lst) #f
+          (if (eq? (car lst) pred) i
+              (loop (cdr lst) (1+ i)))))))
index e3bae7b6a420a67b6ae4f0825e44ea7290893f8b..14972d7a26b38fec6d3c6cb09294866b90accbb0 100755 (executable)
@@ -9,7 +9,7 @@ exit $?
 ;;; -*-scheme-*-
 
 ;;; Mes --- Maxwell Equations of Software
-;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
+;;; Copyright © 2016,2017 Jan Nieuwenhuizen <janneke@gnu.org>
 ;;;
 ;;; This file is part of Mes.
 ;;;
@@ -44,4 +44,8 @@ exit $?
                '("foo" "bar" "baz")
                (string-split "foo:bar:baz" #\:))
 
+(pass-if-equal "string-index"
+               3
+               (string-index "foo:bar" #\:))
+
 (result 'report)