-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcj-string-flatten.scm
78 lines (65 loc) · 2.02 KB
/
cj-string-flatten.scm
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
;;; Copyright 2007 by Christian Jaeger <ch@christianjaeger.ch>
;;; This file is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License (GPL) as published
;;; by the Free Software Foundation, either version 2 of the License, or
;;; (at your option) any later version.
(require cj-env
(srfi-1 fold fold-right)
(cj-string string-copy! @string-copy!_end)
test
test-lib-1)
(export flat-append-strings
#!optional
flat-string-length)
; (define (flat-append-strings . strings-or-lists-of-strings)
; (apply string-append (flatten strings-or-lists-of-strings)))
; or, much lengthier but w/o intermediate list building:
(define (flat-string-length lists-of-strings
#!optional
(tot 0)
do! ;; (& #!void)
)
(let flat-string-length ((lists-of-strings lists-of-strings)
(tot tot))
(if (null? lists-of-strings)
tot
(fold (lambda (v tot)
(cond ((pair? v)
(flat-string-length v tot))
((null? v)
tot)
((string? v)
(when do! (do! v tot))
(+ tot (string-length v)))
(else
(error "flat-string-length: not a string or list:" v))))
tot
lists-of-strings))))
(TEST
> (flat-string-length '() 1)
1
> (flat-string-length '((())) 1)
1
> (flat-string-length '("he" "l" () "lo" ()) -1)
4
> (flat-string-length '(() ((("hel")) "l") "o") 0)
5)
(define (flat-append-strings . strings-or-lists-of-strings)
(let* ((len (flat-string-length strings-or-lists-of-strings 0))
(str (##make-string len)))
(flat-string-length
strings-or-lists-of-strings 0
(lambda (v pos)
;; (string-copy! str pos v 0 (string-length v))
(@string-copy!_end str pos v 0 (##string-length v))
))
str))
(TEST
> (flat-append-strings '("Hallo"))
"Hallo"
> (flat-append-strings '("Hallo"(() " W"("e")"lt")))
"Hallo Welt"
> (flat-append-strings '(()))
""
> (%try (flat-append-strings '(() #f)))
(exception text: "flat-string-length: not a string or list: #f\n"))