byte-arrays: Add a byte-sequence protocol for byte-{array,vector}

Allow hexdumping strings as utf8 for convenience.
master
Doug Coleman 2020-01-11 17:31:32 -06:00
parent d593d3d953
commit ada064d9c3
4 changed files with 11 additions and 4 deletions

View File

@ -3,9 +3,9 @@
USING: accessors ascii byte-arrays byte-vectors combinators
command-line destructors fry io io.encodings io.encodings.binary
io.files io.streams.string kernel literals locals math
math.parser namespaces sequences sequences.private strings typed
;
io.encodings.string io.encodings.utf8 io.files io.streams.string
kernel literals locals math math.parser namespaces sequences
sequences.private strings typed ;
IN: tools.hexdump
@ -78,6 +78,9 @@ M: byte-array hexdump. all-bytes hexdump-bytes ;
M: byte-vector hexdump. all-bytes underlying>> hexdump-bytes ;
M: string hexdump. utf8 encode hexdump. ;
: hexdump ( byte-array -- str )
[ hexdump. ] with-string-writer ;

View File

@ -43,7 +43,7 @@ IN: urls.encoding
: (url-encode) ( str quot: ( ch -- ? ) -- encoded )
[
over byte-array? [
over byte-sequence? [
'[ dup @ [ , ] [ hex% ] if ] each
] [
[ present ] dip

View File

@ -7,6 +7,8 @@ IN: byte-arrays
BUILTIN: byte-array
{ length array-capacity read-only initial: 0 } ;
MIXIN: byte-sequence
PRIMITIVE: (byte-array) ( n -- byte-array )
PRIMITIVE: <byte-array> ( n -- byte-array )
PRIMITIVE: resize-byte-array ( n byte-array -- new-byte-array )
@ -27,6 +29,7 @@ M: byte-array resize
resize-byte-array ; inline
INSTANCE: byte-array sequence
INSTANCE: byte-array byte-sequence
: 1byte-array ( x -- byte-array ) B{ } 1sequence ; inline

View File

@ -46,3 +46,4 @@ M: byte-array new-resizable drop <byte-vector> ; inline
M: byte-vector new-resizable drop <byte-vector> ; inline
INSTANCE: byte-vector growable
INSTANCE: byte-vector byte-sequence