summaryrefslogtreecommitdiff
path: root/modules/language
diff options
context:
space:
mode:
authorStefan Israelsson Tampe <stefan.itampe@gmail.com>2017-10-12 22:08:26 +0200
committerStefan Israelsson Tampe <stefan.itampe@gmail.com>2017-10-12 22:08:26 +0200
commit7852343a565da36a09340306a57cae9d3239337a (patch)
tree127033771a25e81e6680edb2d089a2985c40e0f1 /modules/language
parent5da3350872568366fc8da10310ce9249491875d3 (diff)
bytevectors
Diffstat (limited to 'modules/language')
-rw-r--r--modules/language/python/compile.scm6
-rw-r--r--modules/language/python/str.scm626
-rw-r--r--modules/language/python/string.scm4
3 files changed, 413 insertions, 223 deletions
diff --git a/modules/language/python/compile.scm b/modules/language/python/compile.scm
index b7a52b2..ab9b8f6 100644
--- a/modules/language/python/compile.scm
+++ b/modules/language/python/compile.scm
@@ -597,8 +597,10 @@
'(values)))
(#:string
- ((#:string #f x)
- x))
+ ((#:string p x)
+ (if (or (equal? p "r") (equal? p "R"))
+ `((@ (language python str) str) ,x)
+ x)))
(#:+
((_ . l)
diff --git a/modules/language/python/str.scm b/modules/language/python/str.scm
index 44d2803..e4e821b 100644
--- a/modules/language/python/str.scm
+++ b/modules/language/python/str.scm
@@ -5,9 +5,14 @@
#:use-module (rnrs bytevectors)
#:use-module (system foreign)
#:use-module (language python string)
- #:export (<py-str> chf ch-find str))
-
-
+ #:use-module (language python for)
+ #:use-module (language python try)
+ #:use-module (language python exceptions)
+ #:use-module (language python list)
+ #:use-module (language python hash)
+ #:export (<py-str> pystr-listing str str->bytevector))
+
+(define (str->bytevector x) (slot-ref x 'str))
(define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))
(define b-ref bytevector-u8-ref)
@@ -17,15 +22,48 @@
(define-class <py-str> () str)
+(define (b-char x)
+ (cond
+ ((char? x)
+ (ch-find x))
+ ((string? x)
+ (ch-find (string-ref x 0)))
+ (else
+ x)))
+
(define-python-class str (<py-str>)
(define __init__
(case-lambda
((self s)
(cond
+ ((is-a? s <string>)
+ (let* ((n (string-length s))
+ (str (b-make n)))
+ (let lp ((i 0))
+ (if (< i n)
+ (begin
+ (b-set! str i (ch-find (string-ref s i)))
+ (lp (+ i 1)))))
+ (slot-set! self 'str str)))
+ ((is-a? s <py-string>)
+ (__init__ self (slot-ref s 'str)))
((is-a? s <py-str>)
(slot-set! self 'str (slot-ref s 'str)))
((is-a? s <bytevector>)
- (slot-set! self 'str s)))))))
+ (slot-set! self 'str s))
+ (else
+ (for ((x : s)) ((r '()))
+ (cons (b-char x) r)
+
+ #:final
+ (let* ((n (length r))
+ (str (b-make n)))
+ (let lp ((i (- n 1)) (r r))
+ (if (>= i 0)
+ (begin
+ (b-set! str i (car r))
+ (lp (- i 1) (cdr r)))
+ (slot-set! self 'str str)))))))))))
(define-syntax-rule (define-py (f o . u) code ...)
(begin
@@ -111,17 +149,15 @@
(b-set! s j (b-ref o i))
(lp (+ i 1) (+ j 1)))))
(str s)))
-
-
;;;py-decode
;;;py-encode
-
-(define-py (py-endswith o (suff <bytevector>) . l)
- (let* ((n (b-len o))
- (ns (b-len suff))
- (f (lambda (x) (< x 0) (+ n x) x)))
+(define-py (py-endswith o suff . l)
+ (let* ((suff (slot-ref (str suff) 'str))
+ (n (b-len o))
+ (ns (b-len suff))
+ (f (lambda (x) (< x 0) (+ n x) x)))
(call-with-values (lambda ()
(match l
(() (values 0 n ))
@@ -137,9 +173,10 @@
(eq? (b-ref o i) (b-ref suff j))
(lp (+ i 1) (+ j 1))))))))))
-(define-py (py-startswith o (suff <bytevector>) . l)
- (let* ((n (b-len o))
- (ns (b-len suff))
+(define-py (py-startswith o pre . l)
+ (let* ((pre (slot-ref (str pre) 'str))
+ (n (b-len o))
+ (ns (b-len pre))
(f (lambda (x) (< x 0) (+ n x) x)))
(call-with-values (lambda ()
(match l
@@ -156,24 +193,45 @@
(lp (+ i 1)))
(else
(and
- (eq? (b-ref o i) (b-ref suff i))
+ (eq? (b-ref o i) (b-ref pre i))
(lp (+ i 1))))))))))
-#|
-(define-py (py-expandtabs expandtabs s . l)
+
+(define-py (py-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* ((n (string-length s))
+ (ct (ch-find #\tab))
+ (cs (ch-find #\space))
+ (n (b-len s)))
+ (let lp ((i 0) (r '()))
+ (if (< i n)
+ (let ((x (b-ref s i)))
+ (if (eq? x ct)
+ (let lp2 ((j 0) (r r))
+ (if (< j tabsize)
+ (lp2 (+ j 1) (cons cs r))
+ (lp (+ i 1) r)))
+ (lp (+ i 1) (cons x r))))
+ (str (reverse r))))))
+
+(define (b-contains s sub start end)
+ (define nsub (b-len sub))
+ (define (match i)
+ (let lp ((i i) (j 0))
+ (if (and (< j nsub) (< i end))
+ (if (eq? (b-ref s i) (b-ref sub j))
+ (lp (+ i 1) (+ j 1))
+ #f)
+ #t)))
+
+ (let lp ((i (max start 0)))
+ (if (< i end)
+ (if (match i)
+ i
+ (lp (+ i 1)))
+ #f)))
+
+(define-py (py-find s sub . l)
+ (let* ((n (b-len s))
(f (lambda (x) (< x 0) (+ n x) x)))
(call-with-values (lambda ()
(match l
@@ -181,14 +239,29 @@
((x) (values (f x) n ))
((x y) (values (f x) (f y)))))
(lambda (start end)
- (aif it (string-contains s sub start end)
+ (let ((sub (slot-ref (str sub) 'str)))
+ (aif it (b-contains s sub start end)
it
- -1)))))
-
-(define-py (py-rfind rfind s sub . l)
- (let* ((n (string-length s))
- (s (string-reverse s))
- (sub (string-reverse sub))
+ -1))))))
+
+(define (b-reverse s)
+ (if (is-a? s (<py-str>))
+ (b-reverse (slot-ref s 'str))
+ (let* ((n (b-len s))
+ (r (b-make n)))
+ (let lp ((i 0) (j (- n 1)))
+ (if (< i n)
+ (begin
+ (b-set! r j (b-ref s i))
+ (lp (+ i 1) (- j 1)))
+ r)))))
+
+
+(define-py (py-rfind s sub . l)
+ (let* ((sub (slot-ref (str sub) 'str))
+ (n (b-len s))
+ (s (b-reverse s))
+ (sub (b-reverse sub))
(f (lambda (x) (< x 0) (+ n x) x)))
(call-with-values (lambda ()
(match l
@@ -196,10 +269,11 @@
((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))
+ (aif it (b-contains s sub start end)
+ (- n it (b-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)))
@@ -238,30 +312,33 @@
(values (reverse args) kwargs)))))
(lambda (args kwargs)
(compile (parse s e) args kwargs))))
+|#
-(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)))
+(define-syntax-rule (mk-is py-isalnum x ...)
+ (define-py (py-isalnum s)
+ (let ((n (b-len s)))
+ (let lp ((i 0))
+ (if (< i n)
+ (let ((ch (chf (b-ref s i))))
+ (if (or (x ch) ...)
+ (lp (+ i 1))
+ #f))
+ #t)))))
+
+(mk-is py-isalnum char-alphabetic? char-numeric?)
+(mk-is py-isalpha char-alphabetic?)
+(mk-is py-isdigit char-numeric?)
+(mk-is py-islower char-lower-case?)
+(mk-is py-isspace char-whitespace?)
+(mk-is py-isupper char-upper-case?)
+
+
+(define-py (py-istitle s)
+ (let ((n (b-len s)))
(if ((> n 0))
(let lp ((i 0) (space? #t))
(if (< i n)
- (let ((ch (string-ref s i)))
+ (let ((ch (chf (b-ref s i))))
(if space?
(cond
((char-whitespace? ch)
@@ -282,74 +359,191 @@
#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))
+(define (b-join l s)
+ (let* ((ns (b-len s))
+ (l (pk (map (lambda (x) (slot-ref (str x) 'str)) l)))
+ (n (let lp ((l l) (n 0))
+ (if (pair? l)
+ (let ((x (car l))
+ (l (cdr l)))
+ (lp l (+ n (b-len x) (if (pair? l) ns 0))))
+ n)))
+ (r (b-make n)))
+ (let lp ((l l) (i 0))
+ (if (pair? l)
+ (let* ((x (car l))
+ (n (b-len x))
+ (l (cdr l)))
+ (let lp2 ((j 0) (i i))
+ (if (< j n)
+ (begin
+ (b-set! r i (b-ref x j))
+ (lp2 (+ j 1) (+ i 1)))
+ (if (pair? l)
+ (let lp3 ((j 0) (i i))
+ (if (< j ns)
+ (begin
+ (b-set! r i (b-ref s j))
+ (lp3 (+ j 1) (+ i 1)))
+ (lp l i)))
+ (lp l i)))))
+ (str r)))))
+
+(define-py (py-join s iterator)
+ (b-join (to-list iterator) s))
+
+(define-py (pylist-slice s n1 n2 n3)
+ (define N (b-len s))
+ (define (f n) (if (< n 0) (+ N n) n))
+
+ (let* ((n1 (f (if (eq? n1 None) 0 n1)))
+ (n2 (f (if (eq? n2 None) N n2)))
+ (n3 (f (if (eq? n3 None) 1 n3)))
+ (r (b-make (floor-quotient (+ 1 (abs (- n2 n1))) n3))))
+ (let lp ((i n1) (j 0))
+ (if (< i n3)
+ (begin
+ (b-set! r j (b-ref s i))
+ (lp (+ i n3) (+ j 1)))
+ (str r)))))
+
+
+(define-py (py-ljust s width . l)
+ (let* ((n (b-len s))
(ch (match l
- ((x . l)
- (if (string? x)
- (string-ref x 0)
- x))
- (()
- #\space))))
+ ((x)
+ (b-char x))
+ (()
+ (b-char #\space)))))
(if (< width n)
- (pylist-slice s 0 width)
- (let ((ret (make-string width ch)))
+ (pylist-slice s 0 width 1)
+ (let ((ret (b-make width ch)))
(let lp ((i 0))
(if (< i n)
- (string-set! ret i (string-ref s i))
- ret))))))
+ (begin
+ (b-set! ret i (b-ref s i))
+ (lp (+ i 1)))
+ (str ret)))))))
-(define-py (py-rjust rjust s width . l)
- (let* ((n (len s))
+(define-py (py-rjust s width . l)
+ (let* ((n (b-len s))
(ch (match l
- ((x . l)
- (if (string? x)
- (string-ref x 0)
- x))
+ ((x)
+ (b-char x))
(()
- #\space))))
+ (b-char #\space)))))
(if (< width n)
- (pylist-slice s (- width) (len s))
- (let ((ret (make-string width ch)))
+ (pylist-slice s (- width) (len s) 1)
+ (let ((ret (b-make width ch)))
(let lp ((i 0) (j (- width n)))
(if (< i n)
- (string-set! ret j (string-ref s i))
- ret))))))
+ (begin
+ (b-set! ret j (b-ref s i))
+ (lp (+ i 1) (+ j 1)))
+ (str ret)))))))
-(define-py (py-lower lower s)
- (string-downcase s))
-(define-py (py-upper upper s)
- (string-upcase s))
+(define-py (py-lower s)
+ (let* ((n (b-len s))
+ (r (b-make n)))
+ (let lp ((i 0))
+ (if (< i n)
+ (let* ((x (b-ref s i))
+ (ch (chf x)))
+ (b-set! r i (if (char-upper-case? ch)
+ (ch-find (char-downcase ch))
+ x))
+ (lp (+ i 1)))
+ (str r)))))
-(define-py (py-lstrip lstrip s . l)
+(define-py (py-upper s)
+ (let* ((n (b-len s))
+ (r (b-make n)))
+ (let lp ((i 0))
+ (if (< i n)
+ (let* ((x (b-ref s i))
+ (ch (chf x)))
+ (b-set! r i (if (char-lower-case? ch)
+ (ch-find (char-upcase ch))
+ x))
+ (lp (+ i 1)))
+ (str r)))))
+
+(define-py (py-swapcase s)
+ (let* ((n (b-len s))
+ (r (b-make n)))
+ (let lp ((i 0))
+ (if (< i n)
+ (let* ((x (b-ref s i))
+ (ch (chf x)))
+ (b-set! r i (cond
+ ((char-lower-case? ch)
+ (ch-find (char-upcase ch)))
+ ((char-upper-case? ch)
+ (ch-find (char-downcase ch)))
+ (else
+ x)))
+ (lp (+ i 1)))
+ (str r)))))
+
+(define b-trim
+ (case-lambda
+ ((s)
+ (b-trim s (lambda (ch x) (char-whitespace? ch))))
+ ((s p)
+ (let ((n (b-len s)))
+ (let lp ((i 0) (r '()) (first? #t))
+ (if (< i n)
+ (let ((x (b-ref s i)))
+ (if first?
+ (if (p (chf x) x)
+ (lp (+ i 1) r #t)
+ (lp (+ i 1) (cons x r) #f))
+ (lp (+ i 1) (cons x r) #f)))
+ (str (reverse r))))))))
+
+(define b-rtrim
+ (case-lambda
+ ((s)
+ (b-rtrim s (lambda (ch x) (char-whitespace? ch))))
+ ((s p)
+ (let ((n (b-len s)))
+ (let lp ((i (- n 1)) (r '()) (first? #t))
+ (if (>= i 0)
+ (let ((x (b-ref s i)))
+ (if first?
+ (if (p (chf x) x)
+ (lp (- i 1) r #t)
+ (lp (- i 1) (cons x r) #f))
+ (lp (- i 1) (cons x r) #f)))
+ (str r)))))))
+
+(define-py (py-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)))))))
+ (b-trim s))
+ ((x)
+ (let ((l (map b-char (to-list x))))
+ (b-trim s (lambda (ch x) (member x l)))))))
-(define-py (py-rstrip rstrip s . l)
+(define-py (py-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)))
+ (b-rtrim s))
+ ((x)
+ (let ((l (map b-char (to-list x))))
+ (b-rtrim s (lambda (ch x) (member x l)))))))
+
+
+(define-py (py-partition s sep)
+ (let* ((sep (slot-ref (str sep) 'str))
+ (n (b-len s))
+ (m (b-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))
+ (if (eq? (b-ref s i) (b-ref sep j))
(lp (+ i 1) (+ j 1))
#f)
#t)
@@ -361,16 +555,17 @@
(lp (+ i 1)))
(list s "" "")))))
-(define-py (py-rpartition rpartition ss (ssep <string>))
- (let* ((s (string-reverse ss))
- (sep (string-reverse ssep))
- (n (len s))
- (m (len sep)))
+(define-py (py-rpartition ss ssep)
+ (let* ((ssep (slot-ref (str ssep) 'str))
+ (s (b-reverse ss))
+ (sep (b-reverse ssep))
+ (n (b-len s))
+ (m (b-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))
+ (if (eq? (b-ref s i) (b-ref sep j))
(lp (+ i 1) (+ j 1))
#f)
#t)
@@ -378,17 +573,19 @@
(let lp ((i 0))
(if (< i n)
(if (test i)
- (list (string-reverse
- (pylist-slice s (+ i m) n))
+ (list (str
+ (b-reverse
+ (pylist-slice s (+ i m) n)))
ssep
- (string-reverse
- (pylist-slice s 0 i)))
+ (str
+ (b-reverse
+ (pylist-slice s 0 i))))
(lp (+ i 1)))
(list "" "" s)))))
-
-(define-py (py-replace replace s old new . l)
+
+(define-py (py-replace s old new . l)
(let ((n (match l (() #f) ((n . _) n))))
- (string-join
+ (b-join
(reverse
(let lp ((s s) (r '()))
(let ((l (py-partition s old)))
@@ -397,19 +594,43 @@
(lp (list-ref l 2) (cons (car l) r))))))
new)))
-(define-py (py-strip strip s . l)
+(define-py (py-strip s . l)
(apply py-rstrip (apply py-lstrip s l) l))
+(define-py (pylist-index o val . l)
+ (let* ((n (b-len o))
+ (vec o)
+ (f (lambda (m) (if (< m 0) (+ m n) m))))
+ (call-with-values
+ (lambda ()
+ (match l
+ (()
+ (values 0 n))
+ ((x)
+ (values (f x) n))
+ ((x y)
+ (values (f x) (f y)))))
+ (lambda (n1 n2)
+ (if (and (>= n1 0) (>= n2 0) (< n1 n) (<= n2 n))
+ (let lp ((i n1))
+ (if (< i n2)
+ (let ((r (b-ref vec i)))
+ (if (equal? r val)
+ i
+ (lp (+ i 1))))
+ (raise ValueError "could not find value in index fkn")))
+ (raise IndexError "index out of scop in index fkn"))))))
+
+(define-py (py-rindex s . l)
+ (let ((n (b-len s)))
+ (- n (apply pylist-index (b-reverse s) l) 1)))
+
+#;
(define-py (py-title title s)
(string-titlecase s))
-(define-py (py-rindex rindex s . l)
- (let ((n (len s)))
- (- n (apply pylist-index (string-reverse s) l) 1)))
-
-
-
-(define-py (py-split split s . l)
+#;
+(define-py (py-split s . l)
(define ws (f+ (f-reg "[ \t\n]")))
(define r
(f-or! (f-seq f-eof (f-out '()))
@@ -437,7 +658,8 @@
tok)))
(parse s e)))
-(define-py (py-rsplit rsplit s . l)
+#;
+(define-py (py-rsplit s . l)
(reverse
(map string-reverse
(apply py-split
@@ -447,7 +669,7 @@
((sep . l) (cons (string-reverse sep) l)))))))
-(define-py (py-splitlines splitlines s . l)
+(define-py (py-splitlines s . l)
(let ((n (len s))
(keep? (match l
((#:keepends v)
@@ -457,7 +679,8 @@
(_ #f))))
(let lp ((i 0) (r '()) (old 0))
(if (< i n)
- (let ((ch (string-ref s i)))
+ (let* ((x (b-ref s i))
+ (ch (chf x)))
(if (eq? ch #\newline)
(if keep?
(lp (+ i 1)
@@ -472,48 +695,33 @@
(+ i 1)))
(lp (+ i 1) r old)))
(reverse r)))))
-
-(define-py (py-swapcase swapcase s)
- (list->string
- (string-fold
- (lambda (ch s)
- (cons
- (cond
- ((char-upper-case? ch)
- (char-downcase ch))
- ((char-lower-case? ch)
- (char-upcase ch))
- (else ch))
- s))
- '()
- s)))
-(define-py (py-translate translate s table . l)
- (let* ((n (len s))
- (w (make-string n))
+(define-py (py-translate s table . l)
+ (let* ((table (slot-ref (str table) 'str))
+ (n (b-len s))
+ (w (b-make n))
(t (if (eq? table None) #f table))
- (d (match l (() #f) ((x) x))))
+ (d (match l (() #f) ((x) (map b-char (to-list x))))))
(define (tr ch)
(define (e)
(if t
- (let ((i (char->integer ch)))
- (if (< i (string-length t))
- (string-ref t i)
- ch))
+ (if (< ch (b-len t))
+ (b-ref t ch)
+ ch)
ch))
(if d
- (if (string-contains d (list->string (list ch)))
+ (if (member ch d)
#f
(e))
(e)))
(let lp ((i 0) (k 0))
(if (< i n)
- (let ((ch (tr (string-ref s i))))
+ (let ((ch (tr (b-ref s i))))
(if ch
(begin
- (string-set! w k ch)
+ (b-set! w k ch)
(lp (+ i 1) (+ k 1)))
(lp (+ i 1) k)))
(if (= k n)
@@ -524,115 +732,98 @@
(define-syntax-rule (mkop op)
(begin
- (define-method (op (s1 <string>) (s2 <py-string>))
+ (define-method (op (s1 <bytevector>) (s2 <py-str>))
(op s1 (slot-ref s2 'str)))
- (define-method (op (s2 <py-string>) (s1 <string>))
+ (define-method (op (s2 <py-str>) (s1 <bytevector>))
(op s1 (slot-ref s2 'str)))))
(mkop <)
(mkop <=)
(mkop >)
(mkop >=)
+(mkop py-equal?)
(mkop +)
(mkop *)
-(define-method (< (s1 <string>) (s2 <string>)) (string-ci< s1 s2))
-(define-method (<= (s1 <string>) (s2 <string>)) (string-ci<= s1 s2))
-(define-method (> (s1 <string>) (s2 <string>)) (string-ci> s1 s2))
-(define-method (>= (s1 <string>) (s2 <string>)) (string-ci>= s1 s2))
-
-(define-method (< (s1 <symbol>) (s2 <symbol>)) (a string-ci< s1 s2))
-(define-method (<= (s1 <symbol>) (s2 <symbol>)) (a string-ci<= s1 s2))
-(define-method (> (s1 <symbol>) (s2 <symbol>)) (a string-ci> s1 s2))
-(define-method (>= (s1 <symbol>) (s2 <symbol>)) (a string-ci>= s1 s2))
-
-
-(define-py (py-zfill zfill s width)
- (let* ((n (len s))
- (w (pk (pylist-slice s 0 n 1))))
+(define-syntax-rule (mkop2 op)
+ (define-method (< (s1 <bytevector>) (s2 <bytevector>))
+ (let* ((n1 (b-len s1))
+ (n2 (b-len s2))
+ (n (min n1 n2)))
+ (let lp ((i 0))
+ (if (< i n)
+ (let ((x1 (b-ref s1 i))
+ (x2 (b-ref s2 i)))
+ (if (= x1 x2)
+ (lp (+ i 1))
+ (op x1 x2)))
+ (op n1 n2))))))
+
+(mkop2 <)
+(mkop2 <=)
+(mkop2 >=)
+(mkop2 >)
+(mkop2 py-equal?)
+
+(define-py (py-zfill s width)
+ (let* ((n (b-len s))
+ (w (pylist-slice s 0 n 1)))
(let lp ((i 0))
(if (< i n)
- (let ((ch (string-ref s i)))
+ (let* ((x (b-ref s i))
+ (ch (chf x)))
(if (char-numeric? ch)
(let lp ((j (max 0 (- i width))))
- (pk i j)
(if (< j i)
(begin
- (string-set! w j #\0)
+ (b-set! w j (ch-find #\0))
(lp (+ j 1)))
w))
(lp (+ i 1))))
s))))
-(define-python-class string (<py-string>)
- (define __init__
- (case-lambda
- ((self s)
- (cond
- ((is-a? s <py-string>)
- (slot-set! self 'str (slot-ref s 'src)))
- ((is-a? s <string>)
- (slot-set! self 'str s)))))))
-
-(define pystring string)
-
-(define-method (py-class (o <string>)) string)
-(define-method (py-class (o <py-string>)) string)
-
-(define-method (pyhash (o <py-string>)) (hash (slot-ref o 'str) pyhash-N))
+(define-method (pyhash (o <py-str>)) (hash (slot-ref o 'str) pyhash-N))
-(define-method (py-equal? (o <py-string>) x)
- (equal? (slot-ref o 'str) x))
-(define-method (py-equal? x (o <py-string>))
- (equal? (slot-ref o 'str) x))
+(define-class <str-iter> (<py-str>) str i d)
-(define-class <string-iter> (<py-string>) str i d)
-
-(define-method (write (o <string-iter>) . l)
- (define port (if (null? l) #t (car l)))
- (for ((x : o)) ((l '()))
- (cons (string-ref x 0) l)
- #:final
- (format port "iter(~s)" (list->string (reverse l)))))
-
-(define-method (wrap-in (o <string-iter> ))
- (let ((out (make <string-iter>)))
+(define-method (wrap-in (o <str-iter> ))
+ (let ((out (make <str-iter>)))
(slot-set! out 'str (slot-ref o 'str))
(slot-set! out 'i (slot-ref o 'i))
(slot-set! out 'd (slot-ref o 'd))
out))
-(define-method (wrap-in (s <string>))
- (let ((out (make <string-iter>)))
+(define-method (wrap-in (s <bytevector>))
+ (let ((out (make <str-iter>)))
(slot-set! out 'str s)
(slot-set! out 'i 0)
(slot-set! out 'd 1)
out))
-(define-method (py-reversed (s <string>))
- (let ((out (make <string-iter>)))
+(define-method (py-reversed (s <bytevector>))
+ (let ((out (make <str-iter>)))
(slot-set! out 'str s)
- (slot-set! out 'i (- (string-length s) 1))
+ (slot-set! out 'i (- (b-len s) 1))
(slot-set! out 'd -1)
out))
-(define-method (next (o <string-iter>))
+(define-method (next (o <str-iter>))
(let ((i (slot-ref o 'i ))
(d (slot-ref o 'd))
(str (slot-ref o 'str)))
(if (> d 0)
- (if (< i (string-length str))
- (let ((ret (string-ref str i)))
+ (if (< i (b-len str))
+ (let ((ret (b-ref str i)))
(slot-set! o 'i (+ i d))
- (list->string (list ret)))
+ ret)
(throw StopIteration))
(if (>= i 0)
- (let ((ret (string-ref str i)))
+ (let ((ret (b-ref str i)))
(slot-set! o 'i (+ i d))
- (list->string (list ret)))
+ ret)
(throw StopIteration)))))
-(define (pystring-listing)
+(define (pystr-listing)
(let ((l (to-pylist
(map symbol->string
'(__add__ __class__ __contains__ __delattr__ __doc__
@@ -652,6 +843,3 @@
title translate upper zfill)))))
(pylist-sort! l)
l))
-
-
-|#
diff --git a/modules/language/python/string.scm b/modules/language/python/string.scm
index d0e9c91..2a8fbcc 100644
--- a/modules/language/python/string.scm
+++ b/modules/language/python/string.scm
@@ -11,6 +11,7 @@
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-rjust
py-rljust py-lower py-upper py-lstrip py-rstrip
py-partition py-replace py-strip py-title
py-rpartitio py-rindex py-split py-rsplit py-splitlines
@@ -481,13 +482,12 @@
(define-py (py-zfill zfill s width)
(let* ((n (len s))
- (w (pk (pylist-slice s 0 n 1))))
+ (w (pylist-slice s 0 n 1)))
(let lp ((i 0))
(if (< i n)
(let ((ch (string-ref s i)))
(if (char-numeric? ch)
(let lp ((j (max 0 (- i width))))
- (pk i j)
(if (< j i)
(begin
(string-set! w j #\0)