mes: string-join: Support optional delimiter, support grammar.

* module/srfi/srfi-13.mes (string-join): Support optional infix and
  grammar.
* tests/srfi-13.test ("string-join"): Test it.
This commit is contained in:
Jan Nieuwenhuizen 2018-05-16 23:31:39 +02:00
parent 04860cca6b
commit 7f0af1b46a
No known key found for this signature in database
GPG key ID: F3C1A0D9C1D65273
2 changed files with 28 additions and 0 deletions

View file

@ -27,6 +27,18 @@
(mes-use-module (srfi srfi-1)) (mes-use-module (srfi srfi-1))
(mes-use-module (srfi srfi-14)) (mes-use-module (srfi srfi-14))
(define (string-join lst . delimiter+grammar)
(let ((delimiter (or (and (pair? delimiter+grammar) (car delimiter+grammar))
" "))
(grammar (or (and (pair? delimiter+grammar) (pair? (cdr delimiter+grammar)) (cadr delimiter+grammar))
'infix)))
(if (null? lst) ""
(case grammar
((infix) (if (null? (cdr lst)) (car lst)
(string-append (car lst) delimiter (string-join (cdr lst) delimiter))))
((prefix) (string-append delimiter (car lst) (apply string-join (cdr lst) delimiter+grammar)))
((suffix) (string-append (car lst) delimiter (apply string-join (cdr lst) delimiter+grammar)))))))
(define (string-copy s) (define (string-copy s)
(list->string (string->list s))) (list->string (string->list s)))

View file

@ -31,6 +31,22 @@ exit $?
(pass-if "first dummy" #t) (pass-if "first dummy" #t)
(pass-if-not "second dummy" #f) (pass-if-not "second dummy" #f)
(pass-if-equal "string-join"
"foo bar"
(string-join '("foo" "bar")))
(pass-if-equal "string-join infix"
"foo+bar"
(string-join '("foo" "bar") "+"))
(pass-if-equal "string-join prefix"
",foo,bar"
(string-join '("foo" "bar") "," 'prefix))
(pass-if-equal "string-join suffix"
"foo,bar,"
(string-join '("foo" "bar") "," 'suffix))
(pass-if-equal "string-split" (pass-if-equal "string-split"
'("foo") '("foo")
(string-split "foo" #\:)) (string-split "foo" #\:))