c68b9aeedc772a513cf641c4e45b9851fb00d8f9
[software/python-on-guile.git] / modules / language / python / string.scm
1 (define-module (language python string)
2 #:use-module (oop goops)
3 #:use-module (oop pf-objects)
4 #:use-module (ice-9 match)
5 #:use-module (parser stis-parser)
6 #:export (py-format))
7
8 (define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))
9
10 (define-syntax-rule (define-py (f n o . u) code ...)
11 (begin
12 (define-method (f (o <string>) . u) code ...)
13 (define-method (f (o <p>) . l) (apply (ref o 'n) l))))
14
15 #|
16 (define-py (py-capitalize capitalize o)
17 (string-capitalize o))
18
19 (define-py (py-center center o w . l)
20 (let* ((ws (if (pair? l)
21 (car (string->list (car l)))
22 #\space))
23 (n (string-length o))
24 (w (if (< w n) n w))
25 (d (- w n))
26 (e (floor-quotient (- w n) / 2))
27 (s (make-string w #\space)))
28 (let lp ((i 0) (j e))
29 (if (< i n)
30 (begin
31 (string-set! s j (string-ref o i))
32 (lp (+ i 1) (+ j 1)))))
33 s))
34
35
36
37 ;py-decode
38 ;py-encode
39 (define-py (py-endswith endswith o (suff <string>) . l)
40 (let ((n (string-length o))
41 (ns (string-length suff))
42 (f (lambda (x) (< x 0) (+ n x) x)))
43 (call-with-values (lambda ()
44 (match l
45 (() (values 0 n ))
46 ((x) (values (f x) n ))
47 ((x y) (values (f x) (f y)))))
48 (lambda (start end)
49 (string-suffix? o suff start end)))))
50
51 (define-py (py-expandtabs expandtabs s . l)
52 (let* ((tabsize (match l (() 8) ((x) x)))
53 (u (string->list (make-string tabsize #\space)))
54 (n (string-length s)))
55 (let lp ((l (string->list s)) (r '()))
56 (if (pair? l)
57 (let ((x (car l)))
58 (if (eq? x #\tab)
59 (lp (cdr l) (append u r))
60 (lp (cdr l) (cons x r))))
61 (list->string (reverse r))))))
62
63 (define-py (py-find find s sub . l)
64 (let ((f (lambda (x) (< x 0) (+ n x) x)))
65 (call-with-values (lambda ()
66 (match l
67 (() (values 0 n ))
68 ((x) (values (f x) n ))
69 ((x y) (values (f x) (f y)))))
70 (lambda (start end)
71 (aif it (string-contains s sub start end)
72 it
73 -1)))))
74 |#
75
76 (define i (f-list #:i (mk-token (f+ (f-reg! "[0-9]")))))
77 (define s (f-list #:s (mk-token (f+ (f-not! (f-tag "}"))))))
78 (define e (f-list #:e (f-and (f-tag "}") f-true)))
79 (define tagbody (f-or! e i s))
80
81 (define tag (f-seq "{" tagbody "}"))
82 (define nontag (f-list #:str (mk-token (f+ (f-or! (f-tag "{{")
83 (f-not! tag))))))
84 (define e (ff* (f-or! tag nontag)))
85
86 (define (compile x args kwargs)
87 (let lp ((l x) (r '()) (u '()) (i 0))
88 (match l
89 (((#:str x) . l)
90 (lp l (cons x r) u i))
91 (((#:i x) . l)
92 (lp l (cons "~a" r) (cons (list-ref args (string->number x)) u) i))
93 (((#:s x) . l)
94 (lp l (cons "~a" r) (cons (hash-ref kwargs x 'None) u) i))
95 (((#:e) . l)
96 (lp l (cons "~a" r) (cons (list-ref args i) u) (+ i 1)))
97 (()
98 (apply format #f (string-join (reverse r) "") (reverse u))))))
99
100 (define-py (py-format format s . l)
101 (call-with-values
102 (lambda ()
103 (let lp ((l l) (args '()) (kwargs (make-hash-table)))
104 (match l
105 (((? keyword? key) x . l)
106 (hash-set! kwargs (symbol->string (keyword->symbol key)) x)
107 (lp l args kwargs))
108 ((x . l)
109 (lp l (cons x args) kwargs))
110 (()
111 (values (reverse args) kwargs)))))
112 (lambda (args kwargs)
113 (compile (parse s e) args kwargs))))
114
115 #|
116 py-isalnum
117 py-isalpha
118 py-isdigit
119 py-islower
120 py-isspace
121 py-istitle
122 py-isupper
123 py-join
124 py-ljust
125 py-lower
126 py-lstrip
127 py-partition
128 py-replace
129 py-rfind
130 py-rindex
131 py-rjust
132 py-rpartition
133 py-rsplit
134 py-rstrip
135 py-split
136 py-splitlines
137 py-startswith
138 py-strip
139 py-swapcase
140 py-title
141 py-translate
142 py-upper
143 py-zfill
144 |#