diff options
-rw-r--r-- | modules/language/python/compile.scm | 26 | ||||
-rw-r--r-- | modules/language/python/string.scm | 184 |
2 files changed, 186 insertions, 24 deletions
diff --git a/modules/language/python/compile.scm b/modules/language/python/compile.scm index 50376f6..c823ee0 100644 --- a/modules/language/python/compile.scm +++ b/modules/language/python/compile.scm @@ -188,12 +188,32 @@ ((sort) (L 'pylist-sort!)) ;; String - ((format) (S 'py-format)) + ((format) (S 'py-format )) ((capitalize) (S 'py-capitalize)) - ((center) (S 'py-center)) + ((center) (S 'py-center )) ((endswith) (S 'py-endswith)) ((expandtabs) (S 'py-expandtabs)) - ((find) (S 'py-find)) + ((find) (S 'py-find )) + ((rfind) (S 'py-rfind )) + ((isalnum) (S 'py-isalnum)) + ((isalpha) (S 'py-isalpha)) + ((isdigit) (S 'py-isdigit)) + ((islower) (S 'py-islower)) + ((isspace) (S 'py-isspace)) + ((isupper) (S 'py-isupper)) + ((istitle) (S 'py-istitle)) + ((join) (S 'py-join )) + ((ljust) (S 'py-join )) + ((rljust) (S 'py-rljust )) + ((lower) (S 'py-lower )) + ((upper) (S 'py-upper )) + ((lstrip) (S 'py-lstrip )) + ((rstrip) (S 'py-rstrip )) + ((partition) (S 'py-partiti)) + ((replace) (S 'py-replace)) + ((strip) (S 'py-strip )) + ((title) (S 'py-title )) + (else #f))) (define (get-kwarg vs arg) diff --git a/modules/language/python/string.scm b/modules/language/python/string.scm index 156cc43..96b97f3 100644 --- a/modules/language/python/string.scm +++ b/modules/language/python/string.scm @@ -2,9 +2,14 @@ #:use-module (oop goops) #:use-module (oop pf-objects) #:use-module (ice-9 match) + #:use-module (language python list) #:use-module (parser stis-parser) #:export (py-format py-capitalize py-center py-endswith - py-expandtabs py-find)) + py-expandtabs py-find py-rfind + py-isalnum py-isalpha py-isdigit py-islower + py-isspace py-isupper py-istitle py-join py-ljust + py-rljust py-lower py-upper py-lstrip py-rstrip + py-partition py-replace py-strip py-title)) (define-syntax-rule (aif it p x y) (let ((it p)) (if it x y))) @@ -75,6 +80,21 @@ it -1))))) +(define-py (py-rfind rfind s sub . l) + (let* ((n (string-length s)) + (s (string-reverse s)) + (sub (string-reverse sub)) + (f (lambda (x) (< x 0) (+ n x) x))) + (call-with-values (lambda () + (match l + (() (values 0 n )) + ((x) (values (f x) n )) + ((x y) (values (f x) (f y))))) + (lambda (start end) + (aif it (string-contains s sub start end) + (- n it (len sub)) + -1))))) + (define i (f-list #:i (mk-token (f+ (f-reg! "[0-9]"))))) (define s (f-list #:s (mk-token (f+ (f-not! (f-tag "}")))))) (define e (f-list #:e (f-and (f-tag "}") f-true))) @@ -114,33 +134,155 @@ (lambda (args kwargs) (compile (parse s e) args kwargs)))) -#| -py-isalnum -py-isalpha -py-isdigit -py-islower -py-isspace -py-istitle -py-isupper -py-join -py-ljust -py-lower -py-lstrip -py-partition -py-replace -py-rfind +(define-syntax-rule (mk-is py-isalnum isalnum x ...) + (define-py (py-isalnum isalnum s) + (and (> (len s) 0) + (string-fold + (lambda (ch s) + (if (or (x ch) ...) + s + #f)) + #t s)))) + +(mk-is py-isalnum isalnum char-alphabetic? char-numeric?) +(mk-is py-isalpha isalpha char-alphabetic?) +(mk-is py-isdigit isdigit char-numeric?) +(mk-is py-islower islower char-lower-case?) +(mk-is py-isspace isspace char-whitespace?) +(mk-is py-isupper isupper char-upper-case?) + +(define-py (py-istitle istitle s) + (let ((n (len s))) + (if ((> n 0)) + (let lp ((i 0) (space? #t)) + (if (< i n) + (let ((ch (string-ref s i))) + (if space? + (cond + ((char-whitespace? ch) + (lp (+ i 1) #t)) + ((char-upper-case? ch) + (lp (+ i 1) #f)) + (else + #f)) + (cond + ((char-whitespace? ch) + (lp (+ i 1) #t)) + ((char-upper-case? ch) + #f) + ((char-lower-case? ch) + (lp (+ i 1) #f)) + (else + #f)))) + #t)) + #f))) + + +(define-py (py-join join s iterator) + (string-join (to-list iterator) s)) + +(define-py (py-ljust ljust s width . l) + (let* ((n (len s)) + (ch (match l + ((x . l) + (if (string? x) + (string-ref x 0) + x)) + (() + #\space)))) + (if (< width n) + (pylist-slice s 0 width) + (let ((ret (make-string width ch))) + (let lp ((i 0)) + (if (< i n) + (string-set! ret i (string-ref s i)) + ret)))))) + +(define-py (py-rjust rjust s width . l) + (let* ((n (len s)) + (ch (match l + ((x . l) + (if (string? x) + (string-ref x 0) + x)) + (() + #\space)))) + (if (< width n) + (pylist-slice s (- width) (len s)) + (let ((ret (make-string width ch))) + (let lp ((i 0) (j (- width n))) + (if (< i n) + (string-set! ret j (string-ref s i)) + ret)))))) + +(define-py (py-lower lower s) + (string-downcase s)) + +(define-py (py-upper upper s) + (string-upcase s)) + +(define-py (py-lstrip lstrip s . l) + (match l + (() + (string-trim s)) + ((x . _) + (let ((l (map (lambda (x) (if (string? x) (string-ref x 0) x)) x))) + (string-trim s (lambda (ch) (member ch l))))))) + +(define-py (py-rstrip rstrip s . l) + (match l + (() + (string-trim-right s)) + ((x . _) + (let ((l (map (lambda (x) (if (string? x) (string-ref x 0) x)) x))) + (string-trim-right s (lambda (ch) (member ch l))))))) + +(define-py (py-partition partition s (sep <string>)) + (let ((n (len s)) + (m (len sep))) + (define (test i) + (let lp ((i i) (j 0)) + (if (< i n) + (if (< j m) + (if (eq? (string-ref s i) (string-ref sep j)) + (lp (+ i 1) (+ j 1)) + #f) + #t) + #f))) + (let lp ((i 0)) + (if (< i n) + (if (test i) + (list (pylist-slice s 0 i) sep (pylist-slice s (+ i m) n)) + (lp (+ i 1))) + (list s "" ""))))) + +(define-py (py-replace replace s old new . l) + (let ((n (match l (() #f) ((n . _) n)))) + (string-join + (reverse + (let lp ((s s) (r '())) + (let ((l (py-partition s old))) + (if (equal? (cadr l) "") + (cons s r) + (lp (list-ref l 2) (cons (car l) r)))))) + new))) + +(define-py (py-strip strip s . l) + (apply py-rstrip (apply py-lstrip s l) l)) + +(define-py (py-title title s) (string-titlecase s)) + +#| py-rindex -py-rjust py-rpartition + py-rsplit -py-rstrip py-split py-splitlines + py-startswith -py-strip + py-swapcase -py-title py-translate -py-upper py-zfill |# |