Split out string utils.

This commit is contained in:
Dominik Pantůček 2023-04-08 20:51:53 +02:00
parent 746e4450a6
commit 4ac7fdbc6d
5 changed files with 98 additions and 45 deletions

View file

@ -29,16 +29,12 @@
utils
(
filter
string-repeat
string-first+rest
get-process-output-lines
utils-tests!
)
(import scheme
(chicken base)
(chicken string)
(chicken irregex)
(chicken io)
(chicken process)
testing)
@ -55,26 +51,6 @@
(loop (cdr lst)
res)))))
;; Repeats given string.
(define (string-repeat str rep)
(let loop ((rep rep)
(res '()))
(if (> rep 0)
(loop (sub1 rep)
(cons str res))
(string-intersperse res ""))))
;; Extracts first token and the rest as separate string
(define (string-first+rest str)
(let ((dm (irregex-search (irregex "[ \\t]" 'u) str)))
(if dm
(let* ((sep-idx (irregex-match-start-index dm))
(key-str (substring str 0 sep-idx))
(sep+val (substring str sep-idx))
(val (irregex-replace (irregex "^[ \\t]*" 'u) sep+val "")))
(cons key-str val))
(cons str ""))))
;; Very simple shell command wrapper that returns lines produced by
;; given command. Dangerous - performs no argument escaping!
(define (get-process-output-lines cmd)
@ -90,21 +66,6 @@
utils
(test-equal? filter (filter odd? '(1 2 3 4)) '(1 3))
(test-equal? filter (filter odd? '(2 4)) '())
(test-equal? string-repeat
(string-repeat "-" 4)
"----")
(test-equal? string-repeat
(string-repeat "š" 4)
"šššš")
(test-equal? string-first+rest
(string-first+rest "asdf rest")
'("asdf" . "rest"))
(test-equal? string-first+rest
(string-first+rest "asdf rest test rest")
'("asdf" . "rest test rest"))
(test-equal? string-first+rest
(string-first+rest "asdf")
'("asdf" . ""))
))
)