From 17001b40cdf93bf9b485682c796a6bbbf9ef5843 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 12 Jun 2008 03:49:29 -0500 Subject: [PATCH] Improve encoding API --- core/io/encodings/encodings.factor | 24 ++++++++++++++++----- extra/io/encodings/8-bit/8-bit-tests.factor | 6 +++++- extra/io/encodings/8-bit/8-bit.factor | 7 ++++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/core/io/encodings/encodings.factor b/core/io/encodings/encodings.factor index 3fe6f9d6aa..4a9f90cb32 100755 --- a/core/io/encodings/encodings.factor +++ b/core/io/encodings/encodings.factor @@ -14,7 +14,7 @@ GENERIC: encode-char ( char stream encoding -- ) GENERIC: ( stream encoding -- newstream ) -: replacement-char HEX: fffd ; +: replacement-char HEX: fffd ; inline TUPLE: decoder stream code cr ; @@ -121,14 +121,28 @@ M: encoder stream-flush encoder-stream stream-flush ; INSTANCE: encoder plain-writer PRIVATE> -: re-encode ( stream encoding -- newstream ) - over encoder? [ >r encoder-stream r> ] when ; +GENERIC# re-encode 1 ( stream encoding -- newstream ) + +M: object re-encode ; + +M: encoder re-encode [ stream>> ] dip re-encode ; : encode-output ( encoding -- ) output-stream [ swap re-encode ] change ; -: re-decode ( stream encoding -- newstream ) - over decoder? [ >r decoder-stream r> ] when ; +: with-encoded-output ( encoding quot -- ) + [ [ output-stream get ] dip re-encode ] dip + with-output-stream* ; inline + +GENERIC# re-decode 1 ( stream encoding -- newstream ) + +M: object re-decode ; + +M: decoder re-decode [ stream>> ] dip re-decode ; : decode-input ( encoding -- ) input-stream [ swap re-decode ] change ; + +: with-decoded-input ( encoding quot -- ) + [ [ input-stream get ] dip re-decode ] dip + with-input-stream* ; inline diff --git a/extra/io/encodings/8-bit/8-bit-tests.factor b/extra/io/encodings/8-bit/8-bit-tests.factor index 24cd4137d4..8b18e2a9af 100644 --- a/extra/io/encodings/8-bit/8-bit-tests.factor +++ b/extra/io/encodings/8-bit/8-bit-tests.factor @@ -1,4 +1,5 @@ -USING: io.encodings.string io.encodings.8-bit tools.test strings arrays ; +USING: io.encodings.string io.encodings.8-bit +io.encodings.8-bit.private tools.test strings arrays ; IN: io.encodings.8-bit.tests [ B{ CHAR: f CHAR: o CHAR: o } ] [ "foo" latin1 encode ] unit-test @@ -8,3 +9,6 @@ IN: io.encodings.8-bit.tests [ "bar" ] [ "bar" latin1 decode ] unit-test [ { CHAR: b 233 CHAR: r } ] [ { CHAR: b 233 CHAR: r } latin1 decode >array ] unit-test [ { HEX: fffd HEX: 20AC } ] [ { HEX: 81 HEX: 80 } windows-1252 decode >array ] unit-test + +[ t ] [ \ latin1 8-bit-encoding? ] unit-test +[ "bar" ] [ "bar" \ latin1 decode ] unit-test diff --git a/extra/io/encodings/8-bit/8-bit.factor b/extra/io/encodings/8-bit/8-bit.factor index d4e6122321..71c57ef68c 100755 --- a/extra/io/encodings/8-bit/8-bit.factor +++ b/extra/io/encodings/8-bit/8-bit.factor @@ -73,6 +73,13 @@ M: 8-bit decode-char : define-8-bit-encoding ( name stream -- ) >r in get create r> parse-file make-8-bit ; +PREDICATE: 8-bit-encoding < word + word-def dup length 1 = [ first 8-bit? ] [ drop f ] if ; + +M: 8-bit-encoding word-def first ; + +M: 8-bit-encoding word-def first ; + PRIVATE> [