File: string-parse.scm

package info (click to toggle)
scheme9 2025.08.12-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,080 kB
  • sloc: lisp: 16,752; ansic: 11,869; sh: 806; makefile: 237; sed: 6
file content (44 lines) | stat: -rw-r--r-- 1,474 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
; Scheme 9 from Empty Space, Function Library
; By Nils M Holm, 2009-2010
; Placed in the Public Domain
;
; (string-parse string1 string2)  ==>  list
;
; Split a string into substrings. STRING1 is a set of separators.
; Return a list containing all coherent sequences of non-separating
; characters contained in the given string.
;
; Example:   (string-parse " ?" " to be  or  not to be? ")
;              ==>  ("to" "be" "or" "not" "to" "be")

(define (string-parse c* s)
  (let ((c* (string->list c*)))
    (letrec
      ((skip-separators
         (lambda (i k)
           (cond ((= i k)
                   i)
                 ((memv (string-ref s i) c*)
                   (skip-separators (+ i 1) k))
                 (else
                   i))))
       (parse
         (lambda (i k tmp res)
           (cond ((= i k)
                   (if (string=? "" tmp)
                       res
                       (cons tmp res)))
                 ((memv (string-ref s i) c*)
                   (parse (skip-separators i k)
                           k
                           ""
                           (cons tmp res)))
                 (else
                   (parse (+ 1 i)
                          k
                          (string-append
                            tmp
                            (string (string-ref s i)))
                          res))))))
      (let ((k (string-length s)))
        (reverse! (parse (skip-separators 0 k) k "" '()))))))