Skip to content

Commit c6bbbae

Browse files
committed
FEAT: quoted-printable codec with special space/no-space options
1 parent b241076 commit c6bbbae

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

src/mezz/codec-quoted-printable.reb

+9-2
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,27 @@ register-codec [
1919
decode: function [
2020
"Decodes quoted-printable data"
2121
data [binary! any-string!]
22+
/space
2223
][
2324
output: either binary? data [ copy data ][ to binary! data ]
2425
; remove soft line breaks
2526
parse output [any [to #"=" remove [#"=" [LF | CR LF]] | skip] to end]
26-
to data dehex/escape output #"="
27+
to data either space [
28+
dehex/escape/uri output #"="
29+
][ dehex/escape output #"="]
2730
]
2831

2932
encode: function/with [
3033
"Encodes data using quoted-printable encoding"
3134
data [binary! any-string!]
35+
/no-space "Q-encoding - space may not be represented directly"
3236
][
33-
output: enhex/escape/except to binary! data #"=" :quoted-printable
3437
assert [number? :max-line-length]
3538

39+
output: either no-space [
40+
enhex/escape/except/uri to binary! data #"=" :quoted-printable
41+
][ enhex/escape/except to binary! data #"=" :quoted-printable]
42+
3643
if 0 < length: to integer! max-line-length - 1 [
3744
; limit line length to 76 chars
3845
parse output [any [

src/tests/units/codecs-test.r3

+20-10
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,8 @@ if find codecs 'html-entities [
580580
try [import 'quoted-printable]
581581
if find codecs 'quoted-printable [
582582
was-length: :codecs/quoted-printable/max-line-length
583+
qp-encode: :codecs/quoted-printable/encode
584+
qp-decode: :codecs/quoted-printable/decode
583585
===start-group=== "Quoted-Printable codec"
584586
--test-- "encode Quoted-Printable"
585587
--assert (encode 'quoted-printable "Holčička") == "Hol=C4=8Di=C4=8Dka"
@@ -603,21 +605,29 @@ s te fabriquent pour te la vendre une =C3=A2me vulgaire.^M
603605
text-expected: {J'interdis aux marchands de vanter trop leurs marchandises. Car ils se font vite pédagogues et t'enseignent comme but ce qui n'est par essence qu'un moyen, et te trompant ainsi sur la route à suivre les voilà bientôt qui te dégradent, car si leur musique est vulgaire ils te fabriquent pour te la vendre une âme vulgaire.^M
604606
— Antoine de Saint-Exupéry, Citadelle (1948)}
605607

606-
--assert text-expected == decode 'quoted-printable text-encoded
607-
--assert text-expected == decode 'quoted-printable encode 'quoted-printable text-expected
608-
608+
--assert text-expected == qp-decode text-encoded
609+
--assert text-expected == qp-decode qp-encode text-expected
610+
609611
--test-- "encode with line length limit"
610612
codecs/quoted-printable/max-line-length: 4
611-
--assert "a" = encode 'quoted-printable "a"
612-
--assert "ab" = encode 'quoted-printable "ab"
613-
--assert "abc" = encode 'quoted-printable "abc"
614-
--assert "abcd" = encode 'quoted-printable "abcd"
615-
--assert "abc=^M^/de" = encode 'quoted-printable "abcde"
616-
--assert "abc=^M^/def" = encode 'quoted-printable "abcdef"
617-
--assert "abc=^M^/defg" = encode 'quoted-printable "abcdefg"
613+
--assert "a" = qp-encode "a"
614+
--assert "ab" = qp-encode "ab"
615+
--assert "abc" = qp-encode "abc"
616+
--assert "abcd" = qp-encode "abcd"
617+
--assert "abc=^M^/de" = qp-encode "abcde"
618+
--assert "abc=^M^/def" = qp-encode "abcdef"
619+
--assert "abc=^M^/defg" = qp-encode "abcdefg"
620+
621+
--test-- "quoted-encode with spaces"
622+
--assert "a b" = qp-encode "a b"
623+
--assert "a_b" = qp-encode/no-space "a b"
624+
--assert "a_b" = qp-decode "a_b"
625+
--assert "a b" = qp-decode/space "a_b"
618626

619627
===end-group===
620628
codecs/quoted-printable/max-line-length: :was-length
629+
unset 'qp-encode
630+
unset 'qp-decode
621631
]
622632

623633

0 commit comments

Comments
 (0)