--- /dev/null
+USING: io.encodings.string io.encodings.ascii tools.test strings arrays ;
+IN: io.encodings.ascii.tests
+
+[ B{ CHAR: f CHAR: o CHAR: o } ] [ "foo" ascii encode ] unit-test
+[ { 128 } >string ascii encode ] must-fail
+[ B{ 127 } ] [ { 127 } ascii encode ] unit-test
+
+[ "bar" ] [ "bar" ascii decode ] unit-test
+[ { CHAR: b HEX: fffd CHAR: r } ] [ { CHAR: b 233 CHAR: r } ascii decode >array ] unit-test
USING: io io.encodings strings kernel math sequences byte-arrays io.encodings ;
IN: io.encodings.ascii
-: encode-check<= ( string stream max -- )
+: encode-check< ( string stream max -- )
[ pick <= [ encode-error ] [ stream-write1 ] if ] 2curry each ;
+: push-if< ( sbuf character max -- )
+ over <= [ drop HEX: fffd ] when swap push ;
+
TUPLE: ascii ;
M: ascii stream-write-encoded ( string stream encoding -- )
- drop 127 encode-check<= ;
+ drop 128 encode-check< ;
M: ascii decode-step
- drop dup 128 >= [ decode-error ] [ swap push ] if ;
+ drop 128 push-if< ;
--- /dev/null
+USING: io.encodings.string io.encodings.latin1 tools.test strings arrays ;
+IN: io.encodings.latin1.tests
+
+[ B{ CHAR: f CHAR: o CHAR: o } ] [ "foo" latin1 encode ] unit-test
+[ { 256 } >string latin1 encode ] must-fail
+[ B{ 255 } ] [ { 255 } latin1 encode ] unit-test
+
+[ "bar" ] [ "bar" latin1 decode ] unit-test
+[ { CHAR: b 233 CHAR: r } ] [ { CHAR: b 233 CHAR: r } latin1 decode >array ] unit-test
TUPLE: latin1 ;
M: latin1 stream-write-encoded
- drop 255 encode-check<= ;
-
-M: latin1 decode-step
- drop dup 256 >= [ decode-error ] [ swap push ] if ;
+ drop 256 encode-check< ;