mirror of
https://github.com/justinethier/cyclone.git
synced 2025-05-24 04:25:06 +02:00
Added string split/join
This commit is contained in:
parent
73b166c65f
commit
e2e2232146
2 changed files with 38 additions and 1 deletions
|
@ -27,7 +27,6 @@
|
||||||
emits
|
emits
|
||||||
emits*
|
emits*
|
||||||
emit-newline
|
emit-newline
|
||||||
string-join
|
|
||||||
)
|
)
|
||||||
(inline
|
(inline
|
||||||
global-not-lambda?
|
global-not-lambda?
|
||||||
|
|
|
@ -71,6 +71,9 @@
|
||||||
;; Inlines (TBD, this may move)
|
;; Inlines (TBD, this may move)
|
||||||
define-c-inline?
|
define-c-inline?
|
||||||
define-c->inline-var
|
define-c->inline-var
|
||||||
|
;; String functions
|
||||||
|
string-join
|
||||||
|
string-split
|
||||||
;; Scheme library functions
|
;; Scheme library functions
|
||||||
gensym
|
gensym
|
||||||
delete
|
delete
|
||||||
|
@ -690,4 +693,39 @@
|
||||||
))
|
))
|
||||||
;; END name mangling section
|
;; END name mangling section
|
||||||
|
|
||||||
|
;; string-join :: [string] -> Maybe (string, char) -> string
|
||||||
|
(define (string-join lst delim)
|
||||||
|
(let ((delim* (if (char? delim) (string delim) delim)))
|
||||||
|
(cond
|
||||||
|
((null? lst)
|
||||||
|
"")
|
||||||
|
((= (length lst) 1)
|
||||||
|
(car lst))
|
||||||
|
(else
|
||||||
|
(string-append
|
||||||
|
(car lst)
|
||||||
|
delim*
|
||||||
|
(string-join (cdr lst) delim*))))))
|
||||||
|
|
||||||
|
;; string-split :: string -> char -> [string]
|
||||||
|
;; Based on code from: https://codereview.stackexchange.com/q/75172/6414
|
||||||
|
(define (string-split str delim)
|
||||||
|
(let ((add (lambda (current output)
|
||||||
|
(cons (list->string (reverse current)) output))))
|
||||||
|
(let loop ((input (string->list str))
|
||||||
|
(output '())
|
||||||
|
(current '()))
|
||||||
|
;(write `(DEBUG ,input ,output ,current))
|
||||||
|
(if (null? input)
|
||||||
|
(if (not (null? output))
|
||||||
|
(reverse (add current output))
|
||||||
|
'())
|
||||||
|
(let ((char (car input))
|
||||||
|
(input (cdr input)))
|
||||||
|
(if (char=? char delim)
|
||||||
|
(if (null? current)
|
||||||
|
(loop input output current) ;; Ignore delim by itself
|
||||||
|
(loop input (add current output) '()))
|
||||||
|
(loop input output (cons char current))))))))
|
||||||
|
|
||||||
))
|
))
|
||||||
|
|
Loading…
Add table
Reference in a new issue