summaryrefslogtreecommitdiff
path: root/modules/language/python/string.scm
blob: c68b9aeedc772a513cf641c4e45b9851fb00d8f9 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
(define-module (language python string)
  #:use-module (oop goops)
  #:use-module (oop pf-objects)
  #:use-module (ice-9 match)
  #:use-module (parser stis-parser)
  #:export (py-format))

(define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))

(define-syntax-rule (define-py (f n o . u) code ...)
  (begin
    (define-method (f (o <string>) . u) code ...)
    (define-method (f (o <p>) . l) (apply (ref o 'n) l))))

#|
(define-py (py-capitalize capitalize o)
  (string-capitalize o))

(define-py (py-center center o w . l)
  (let* ((ws (if (pair? l)
                 (car (string->list (car l)))
                 #\space))
         (n  (string-length o))
         (w  (if (< w n) n w))
         (d  (- w n))
         (e  (floor-quotient (- w n) / 2))
         (s  (make-string w #\space)))
    (let lp ((i 0) (j e))
      (if (< i n)
          (begin
            (string-set! s j (string-ref o i))
            (lp (+ i 1) (+ j 1)))))
    s))
    
    

;py-decode
;py-encode
(define-py (py-endswith endswith o (suff <string>) . l)
  (let ((n   (string-length o))
        (ns  (string-length suff))
        (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)
        (string-suffix? o suff start end)))))

(define-py (py-expandtabs expandtabs s . l)
  (let* ((tabsize (match l (() 8) ((x) x)))
         (u       (string->list (make-string tabsize #\space)))
         (n       (string-length s)))
    (let lp ((l (string->list s)) (r '()))
      (if (pair? l)
          (let ((x (car l)))
            (if (eq? x #\tab)
                (lp (cdr l) (append u r))
                (lp (cdr l) (cons x r))))
          (list->string (reverse r))))))
      
(define-py (py-find find s sub . l)
  (let ((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)
             it
             -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)))
(define tagbody (f-or! e i s))

(define tag     (f-seq "{" tagbody "}"))
(define nontag  (f-list #:str (mk-token (f+  (f-or! (f-tag "{{") 
                                                    (f-not! tag))))))
(define e       (ff* (f-or! tag nontag)))

(define (compile x args kwargs)
  (let lp ((l x) (r '()) (u '()) (i 0))
    (match l
      (((#:str x) . l)
       (lp l (cons x r) u i))
      (((#:i x)   . l)
       (lp l (cons "~a" r) (cons (list-ref args (string->number x)) u) i))
      (((#:s x)   . l)
       (lp l (cons "~a" r) (cons (hash-ref kwargs x 'None) u) i))
      (((#:e)     . l)
       (lp l (cons "~a" r) (cons (list-ref args i) u) (+ i 1)))
      (()
       (apply format #f (string-join (reverse r) "") (reverse u))))))

(define-py (py-format format s . l)
  (call-with-values
      (lambda ()
        (let lp ((l l) (args '()) (kwargs (make-hash-table)))
          (match l
            (((? keyword? key) x . l)
             (hash-set! kwargs (symbol->string (keyword->symbol key)) x)
             (lp l args kwargs))
            ((x . l)
             (lp l (cons x args) kwargs))
            (()
             (values (reverse args) kwargs)))))
    (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
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
|#