Implement quoted-printable conversion.

This commit is contained in:
Dominik Pantůček 2023-04-11 14:17:37 +02:00
parent 418468f46e
commit 65e0e3154e
2 changed files with 42 additions and 2 deletions

View file

@ -35,6 +35,7 @@
(import scheme (import scheme
(chicken base) (chicken base)
(chicken keyword) (chicken keyword)
(chicken string)
util-io util-io
util-string) util-string)
@ -43,8 +44,14 @@
;; Encodes given UTF-8 string as quoted-printable ;; Encodes given UTF-8 string as quoted-printable
(define (string->qp str) (define (string->qp str)
(let ((lst (string->list/utf8 str))) (let loop ((lst (string->list/utf8 str))
str)) (res '()))
(if (null? lst)
(string-intersperse (reverse res) "")
(loop (cdr lst)
(cons (let ((chs (car lst)))
chs)
res)))))
;; Ensures the subject has proper encoding ;; Ensures the subject has proper encoding
(define (encode-subject subj) (define (encode-subject subj)

View file

@ -33,6 +33,7 @@
string-utf8? string-utf8?
string-tests! string-tests!
string->list/utf8 string->list/utf8
string->qp
) )
(import scheme (import scheme
@ -71,6 +72,32 @@
(define (string->list/utf8 str) (define (string->list/utf8 str)
(irregex-extract (irregex "." 'u) str)) (irregex-extract (irregex "." 'u) str))
;; Encodes given UTF-8 string as quoted-printable
(define (string->qp str)
(let loop ((lst (string->list/utf8 str))
(res '()))
(if (null? lst)
(string-intersperse (reverse res) "")
(loop (cdr lst)
(cons (let* ((chs (car lst))
(ch1 (if (= (string-length chs) 1)
(string-ref chs 0)
(integer->char 31))))
(if (and (char>=? ch1 #\space)
(char<=? ch1 #\~))
chs
(string-intersperse
(map (lambda (ch)
(string-append "="
(substring
(number->string
(+ 256 (char->integer ch))
16)
1)))
(string->list chs))
"")))
res)))))
;; Performs utils module self-tests. ;; Performs utils module self-tests.
(define (string-tests!) (define (string-tests!)
(run-tests (run-tests
@ -92,6 +119,12 @@
'("asdf" . "")) '("asdf" . ""))
(test-true string-utf8? (string-utf8? "ěščř")) (test-true string-utf8? (string-utf8? "ěščř"))
(test-false string-utf8? (string-utf8? "Hello World!")) (test-false string-utf8? (string-utf8? "Hello World!"))
(test-equal? string->qp
(string->qp "asdf")
"asdf")
(test-equal? string->qp
(string->qp "asdfásdf")
"asdf=c3=a1sdf")
)) ))
) )