mes: Add string-trim, string-trim-right, string-trim-both.

* module/srfi/srfi-13.mes (string-trim, string-trimn-right,
  string-trim-both): New function.
* tests/srfi-13.test ("string-trim"): Test it.
  ("string-trim-right"):
  ("string-trim-both"):
This commit is contained in:
Jan Nieuwenhuizen 2018-04-29 18:24:29 +02:00
parent 3c9b5f433d
commit 59d4d90a90
No known key found for this signature in database
GPG key ID: F3C1A0D9C1D65273
2 changed files with 31 additions and 0 deletions

View file

@ -25,6 +25,7 @@
;;; Code:
(mes-use-module (srfi srfi-1))
(mes-use-module (srfi srfi-14))
(define (string-copy s)
(list->string (string->list s)))
@ -132,3 +133,24 @@
(or (match (cdr start) (cdr needle) (1+ n))
(loop (cdr string) (1+ i)))
(loop (cdr string) (1+ i))))))))))
(define (string-trim string . pred)
(list->string
(if (pair? pred) (error "string-trim: not supported: PRED=" pred)
(let loop ((lst (string->list string)))
(if (or (null? lst)
(not (char-whitespace? (car lst)))) lst
(loop (cdr lst)))))))
(define (string-trim-right string . pred)
(list->string
(reverse!
(if (pair? pred) (error "string-trim-right: not supported: PRED=" pred)
(let loop ((lst (reverse (string->list string))))
(if (or (null? lst)
(not (char-whitespace? (car lst)))) lst
(loop (cdr lst))))))))
(define (string-trim-both string . pred)
((compose string-trim string-trim-right) string))

View file

@ -68,4 +68,13 @@ exit $?
(pass-if-not "string-contains not"
(string-contains "fuba" "bar"))
(pass-if-equal "string-trim" "foo "
(string-trim " foo "))
(pass-if-equal "string-trim-right" " foo"
(string-trim-right " foo "))
(pass-if-equal "string-trim-both" "foo"
(string-trim-both " foo "))
(result 'report)