summaryrefslogtreecommitdiff
path: root/modules/language/python/string.scm
diff options
context:
space:
mode:
Diffstat (limited to 'modules/language/python/string.scm')
-rw-r--r--modules/language/python/string.scm35
1 files changed, 29 insertions, 6 deletions
diff --git a/modules/language/python/string.scm b/modules/language/python/string.scm
index a560801..c1d7d1b 100644
--- a/modules/language/python/string.scm
+++ b/modules/language/python/string.scm
@@ -4,16 +4,19 @@
#:use-module (oop pf-objects)
#:use-module (language python hash)
#:use-module (ice-9 match)
+ #:use-module (ice-9 iconv)
#:use-module (language python list)
#:use-module (language python exceptions)
#:use-module (language python for)
+ #:use-module (language python def)
#:use-module (language python bool)
#:use-module (language python persist)
+ #:use-module (rnrs bytevectors)
#:export (py-format py-capitalize py-center py-endswith
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-format-map
+ py-rjust py-format-map py-encode
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
@@ -100,10 +103,6 @@
-;;;py-decode
-;;;py-encode
-
-
(define-py (py-endswith endswith o (suff <string>) . l)
(let* ((n (string-length o))
(ns (string-length suff))
@@ -376,7 +375,31 @@
(let ((n (len s)))
(- n (apply pylist-index (string-reverse s) l) 1)))
-
+(define bytes #f)
+
+(define-py (py-encode encode s . l)
+ (apply (lam ((= encoding "UTF-8") (= errors "strict"))
+ (set! errors (py-lower (scm-str errors)))
+ (set! errors (cond
+ ((equal? errors "strict")
+ 'error)
+ ((equal? errors "replace")
+ 'substitute)
+ ((equal? errors "ignore")
+ (warn
+ (string-append
+ "not possible to use ignore "
+ "encodong error strategy "
+ "using replace in stead"))
+ 'substitute)
+ (else
+ (warn
+ "not a correct encodong error strategy")
+ 'error)))
+ (set! encoding (py-upper (scm-str encoding)))
+
+ (bytes (string->bytevector (scm-str s) encoding errors)))
+ l))
(define-py (py-split split s . l)
(define ws (f+ (f-reg "[ \t\n]")))