]> gitweb.factorcode.org Git - factor.git/commitdiff
io.encodings.utf7: revert last patch, fixed and faster.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 2 Apr 2020 17:54:51 +0000 (10:54 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 2 Apr 2020 17:54:51 +0000 (10:54 -0700)
basis/io/encodings/utf7/utf7.factor

index 76c89d98851d965bde130b21128d2c56ea6d6fb6..921c8571819f9785ae4802e4be120f9059802804 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2013-2014 Björn Lindqvist
 ! See http://factorcode.org/license.txt for BSD license
-USING: accessors ascii assocs base64 fry io io.encodings
-io.encodings.string io.encodings.utf16 kernel math
+USING: accessors ascii base64 fry io io.encodings
+io.encodings.string io.encodings.utf16 kernel make math
 math.functions sequences splitting strings ;
 IN: io.encodings.utf7
 
@@ -32,10 +32,18 @@ TUPLE: utf7codec dialect buffer ;
     [ swap [ first ] [ concat ] bi replace nip ]
     [ >raw-base64 -rot [ first2 replace ] [ first2 surround ] bi* ] if ;
 
+: split-chunk ( str -- after before printable? )
+    dup first printable? [
+        dupd '[ printable? _ = not ] find drop
+        [ cut-slice ] [ f ] if* swap
+    ] keep ;
+
 : encode-utf7-string ( str codec -- bytes )
-    [ [ printable? ] collect-by ] dip dialect>> first2
-    '[ [ _ _ ] 2dip swap encode-chunk ] { } assoc>map
-    B{ } concat-as ;
+    dialect>> first2 rot '[
+        [ dup empty? ] [
+            split-chunk '[ 2dup _ _ encode-chunk % ] dip
+        ] until
+    ] B{ } make 3nip ;
 
 M: utf7codec encode-string ( str stream codec -- )
     swapd encode-utf7-string swap stream-write ;