From 6d417f4f71a9c38138edfdf39ba7686aa0595eae Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Thu, 7 Apr 2016 12:32:05 -0700 Subject: [PATCH] io.encodings: a bit faster and make ascii support slices. --- core/io/encodings/ascii/ascii.factor | 5 ++++- core/io/encodings/utf16/utf16.factor | 32 +++++++++++++++++++--------- core/io/encodings/utf8/utf8.factor | 16 +++++++++----- 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/core/io/encodings/ascii/ascii.factor b/core/io/encodings/ascii/ascii.factor index 2c7a475217..ddb0818c82 100644 --- a/core/io/encodings/ascii/ascii.factor +++ b/core/io/encodings/ascii/ascii.factor @@ -14,9 +14,12 @@ M: ascii encode-char GENERIC: ascii> ( string -- byte-array ) +M: object ascii> + [ dup 127 <= [ encode-error ] unless ] B{ } map-as ; inline + M: string ascii> dup aux>> - [ [ dup 127 <= [ encode-error ] unless ] B{ } map-as ] + [ call-next-method ] [ string>byte-array-fast ] if ; inline PRIVATE> diff --git a/core/io/encodings/utf16/utf16.factor b/core/io/encodings/utf16/utf16.factor index 24491a7a26..5eba481bd3 100644 --- a/core/io/encodings/utf16/utf16.factor +++ b/core/io/encodings/utf16/utf16.factor @@ -114,17 +114,29 @@ M: utf16le encode-char ( char stream encoding -- ) : ascii-string>utf16be ( string stream -- ) [ 1 swap ascii-string>utf16-byte-array ] dip stream-write ; inline -M: utf16le encode-string - drop - over dup string? [ aux>> ] [ drop t ] if - [ [ char>utf16le ] curry each ] - [ ascii-string>utf16le ] if ; +GENERIC# encode-string-utf16le 1 ( string stream -- ) -M: utf16be encode-string - drop - over dup string? [ aux>> ] [ drop t ] if - [ [ char>utf16be ] curry each ] - [ ascii-string>utf16be ] if ; +M: object encode-string-utf16le + [ char>utf16le ] curry each ; inline + +M: string encode-string-utf16le + over aux>> + [ call-next-method ] + [ ascii-string>utf16le ] if ; inline + +M: utf16le encode-string drop encode-string-utf16le ; + +GENERIC# encode-string-utf16be 1 ( string stream -- ) + +M: object encode-string-utf16be + [ char>utf16be ] curry each ; inline + +M: string encode-string-utf16be + over aux>> + [ call-next-method ] + [ ascii-string>utf16be ] if ; inline + +M: utf16be encode-string drop encode-string-utf16be ; M: utf16le guess-encoded-length drop 2 * ; inline M: utf16le guess-decoded-length drop 2 /i ; inline diff --git a/core/io/encodings/utf8/utf8.factor b/core/io/encodings/utf8/utf8.factor index d07893c0ef..4979daad15 100644 --- a/core/io/encodings/utf8/utf8.factor +++ b/core/io/encodings/utf8/utf8.factor @@ -84,11 +84,17 @@ M: utf8 decode-until (decode-until) ; M: utf8 encode-char drop char>utf8 ; -M: utf8 encode-string - drop - over dup string? [ aux>> ] [ drop t ] if - [ [ char>utf8 ] curry each ] - [ [ string>byte-array-fast ] dip stream-write ] if ; +GENERIC# encode-string-utf8 1 ( string stream -- ) + +M: object encode-string-utf8 + [ char>utf8 ] curry each ; inline + +M: string encode-string-utf8 + over aux>> + [ call-next-method ] + [ [ string>byte-array-fast ] dip stream-write ] if ; inline + +M: utf8 encode-string drop encode-string-utf8 ; PRIVATE>