diff options
Diffstat (limited to 'modules/language/python/string.scm')
-rw-r--r-- | modules/language/python/string.scm | 85 |
1 files changed, 59 insertions, 26 deletions
diff --git a/modules/language/python/string.scm b/modules/language/python/string.scm index 9a58712..3069527 100644 --- a/modules/language/python/string.scm +++ b/modules/language/python/string.scm @@ -407,33 +407,66 @@ l)) (define-py (py-split split s . l) - (define ws (f+ (f-reg "[ \t\n]"))) - (define (r ws) - (f-or! (f-seq f-eof (f-out '())) - (f-cons (f-seq (f? ws) (mk-token (f* (f-reg! "."))) f-eof) - (f-out '())))) - (define (u ws) (mk-token (f+ (f-not! ws)))) - (define (tok ws i) - (if (= i 0) - (f-list (mk-token (f* (f-reg! ".")))) - (let ((e (mk-token (f* (f-not! ws))))) - (f-seq (f? ws) - (f-cons* e - (let lp ((i i)) - (if (> (- i 1) 0) - (f-or! (f-seq (f? ws) f-eof (f-out '())) - (f-cons (f-seq ws e) (Ds (lp (- i 1))))) - (r ws)))))))) + (define N 1000000000000) + + (define ws (list (list (char->integer #\space)) + (list (char->integer #\newline)) + (list (char->integer #\tab)))) + + (define (to-ch x) (string-ref (scm-str x) 0)) - (define N 1000000000000) - (let ((e (call-with-values - (lambda () - (match l - (() (values ws N)) - ((sep) (values (f-tag sep) N)) - ((sep n) (values (f-tag sep) n)))) - tok))) - (parse s e))) + (define (mksep sep) + (for ((x : sep)) ((l '())) + (cons + (cond + ((and (number? x) (integer? x)) + (integer->char x)) + ((char? x) + x) + (else + (to-ch x))) l) + #:final + (list (reverse l)))) + + (call-with-values + (lambda () + (match l + (() (values (list ws ) N)) + ((sep) (values (mksep sep) N)) + ((sep n) (values (mksep sep) n)))) + (lambda (sep n) + (let lp ((l (to-list s)) (i 0) (v '()) (r '())) + (if (= i n) + (reverse r) + (if (pair? l) + (let ((ch (to-ch (car l)))) + (let lp2 ((ss sep)) + (if (pair? ss) + (let lp3 ((sl (car ss)) (l3 l)) + (if (pair? sl) + (if (pair? l3) + (let ((s (car sl))) + (if (eqv? s ch) + (lp3 (cdr sl) (cdr l3)) + (lp2 (cdr ss)))) + (lp2 (cdr ss))) + (lp l3 (+ i 1) + '() + (cons + (list->string (reverse v)) + r)))) + (lp (cdr l) i (cons ch v) r)))) + (reverse (cons (list->string (reverse v)) r)))))))) + + + + + + + + + + (define-py (py-rsplit rsplit s . l) (reverse |