summaryrefslogtreecommitdiff
path: root/modules/language/python/bytes.scm
diff options
context:
space:
mode:
Diffstat (limited to 'modules/language/python/bytes.scm')
-rw-r--r--modules/language/python/bytes.scm78
1 files changed, 46 insertions, 32 deletions
diff --git a/modules/language/python/bytes.scm b/modules/language/python/bytes.scm
index 635358d..daf7adc 100644
--- a/modules/language/python/bytes.scm
+++ b/modules/language/python/bytes.scm
@@ -12,9 +12,13 @@
#:use-module (language python hash)
#:use-module (language python bool)
#:use-module (language python persist)
- #:export (<py-bytes> pybytes-listing bytes bytearray bytes->bytevector
+ #:export (<py-bytes> bv-scm pybytes-listing bytes bytearray bytes->bytevector
+ py-decode
<py-bytearray> pybytesarray-listing scm-bytevector))
+(define (bv-scm x)
+ (slot-ref (bytes x) 'bytes))
+
(define (scm-bytevector x)
(slot-ref (bytes x) 'bytes))
@@ -25,7 +29,8 @@
(define b-set! bytevector-u8-set!)
(define b-make make-bytevector)
(define b-len bytevector-length)
-
+(define (b->list x) (bytevector->u8-list (bv-scm x)))
+(define list->b u8-list->bytevector)
(define-class <py-bytes> () bytes)
(define-class <py-bytearray> () n vec)
@@ -193,6 +198,23 @@
(n (slot-ref o 'n)))
(apply g bytearray b n l)))))
+(define-syntax-rule (define-py** g (f m o nn . u) code ...)
+ (begin
+ (define (g m o nn . u) code ...)
+ (define-method (f (o <bytevector>) . l)
+ (apply g idd o (b-len o) l))
+ (define-method (f (o <py-bytes>) . l)
+ (let ((b (slot-ref o 'bytes)))
+ (apply g bytes b (b-len b) l)))
+ (define-method (f (o <py-bytearray>) . l)
+ (let ((b (slot-ref o 'vec))
+ (n (slot-ref o 'n)))
+ (apply g bytearray b n l)))
+ (define-method (f (o <p>) . l)
+ (aif it (ref o 'g)
+ (apply it l)
+ (next-method)))))
+
(define-py* -bool (bool m o nn) (if (= (len o) 0) #f o))
(define-method (write (b <py-bytes>) . l)
@@ -574,7 +596,12 @@
(lp (+ i 1) (+ j 1)))))
(bytes s)))
-;;;py-decode
+(define-py** decode (py-decode bytes o n . l)
+ (let lp ((i 0) (r '()))
+ (if (< i n)
+ (lp (+ i 1) (cons (b-ref o i) r))
+ (utf8->string (list->b (reverse r))))))
+
;;;py-encode
(define-py* endswith (py-endswith bytes o n suff . l)
@@ -1026,35 +1053,22 @@
(define-py (py-title title s)
(string-titlecase s))
-#;
-(define-py (py-split s . l)
- (define ws (f+ (f-reg "[ \t\n]")))
- (define r
- (f-or! (f-seq f-eof (f-out '()))
- (f-cons (f-seq (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)))))))
-
- (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-py* split (py-split bytes o n tag)
+ (let ((tag (b->list tag)))
+ (let lp ((i 0) (r '()))
+ (if (< i n)
+ (if (eq? (car tag) (b-ref o i))
+ (let lp2 ((j i) (tag tag))
+ (if (null? tag)
+ (cons (bytes (list->b (reverse r)))
+ (lp (+ i 1) '()))
+ (if (< j n)
+ (if (eq? (car tag) (b-ref o j))
+ (lp2 (+ j 1) (cdr tag))
+ (lp (+ i 1) (cons (b-ref o i) r)))
+ (lp (+ i 1) (cons (b-ref o i) r)))))
+ (lp (+ i 1) (cons (b-ref o i) r)))
+ '()))))
#;
(define-py (py-rsplit s . l)
(reverse