checksums.process: reimplement trim-hash and add tests
Use blank? to detect end of hash string.char-rename
parent
3cfc4c69ff
commit
817338360c
|
@ -1,13 +1,13 @@
|
|||
! Copyright (C) 2016 Alexander Ilin.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: checksums checksums.common destructors help.markup help.syntax
|
||||
kernel strings ;
|
||||
USING: ascii checksums checksums.common destructors help.markup
|
||||
help.syntax kernel strings ;
|
||||
IN: checksums.process
|
||||
|
||||
ABOUT: "checksums.process"
|
||||
|
||||
ARTICLE: "checksums.process" "Checksumming with External Utilities"
|
||||
"With the " { $vocab-link "checksums.process" } " vocabulary any console utility can be used to checksum data, provided it supports a certain interface: it should accept input data on STDIN and output result to STDOUT. The output should consist of the hexadecimal checksum string, terminated with \" *-\". For instance, all the checksums from the GNU CoreUtils package support this mode of operation as the default."
|
||||
"With the " { $vocab-link "checksums.process" } " vocabulary any console utility can be used to checksum data, provided it supports a certain interface: it should accept input data on STDIN and output result to STDOUT. The output should consist of the hexadecimal checksum string, terminated with a " { $link blank? } " character. For instance, all the checksums from the GNU CoreUtils package support this mode of operation as the default."
|
||||
$nl
|
||||
"The " { $link checksum-process } " tuple holds a launch descriptor (see " { $link "io.launcher.descriptors" } ") of the utility, e.g. \"sha1sum\". When the " { $link initialize-checksum-state } " method is called on it, a new instance of the checksum utility is started in the background. In Factor it is represented by the " { $link process-state } " tuple. You can then use " { $link add-checksum-bytes } " to stream data to it. When done, call " { $link get-checksum } " to read the resulting checksum and dispose of the tuple in one step. If you want to cancel the work without calling " { $link get-checksum } ", you must " { $link dispose } " of the tuple so that the underlying process is terminated."
|
||||
$nl
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
! Copyright (C) 2016 Alexander Ilin.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: tools.test checksums.process ;
|
||||
IN: checksums.process.tests
|
||||
|
||||
{ "" } [ "" trim-hash ] unit-test
|
||||
{ "" } [ " aoeu" trim-hash ] unit-test
|
||||
{ "aoeu" } [ "aoeu" trim-hash ] unit-test
|
||||
{ "aoeu" } [ "aoeu i" trim-hash ] unit-test
|
|
@ -1,8 +1,8 @@
|
|||
! Copyright (C) 2016 Alexander Ilin.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: accessors checksums checksums.common combinators destructors
|
||||
io io.encodings.binary io.launcher kernel math.parser sequences
|
||||
strings ;
|
||||
USING: accessors ascii checksums checksums.common combinators
|
||||
destructors io io.encodings.binary io.launcher kernel math.parser
|
||||
sequences strings ;
|
||||
IN: checksums.process
|
||||
|
||||
TUPLE: checksum-process launch-desc ;
|
||||
|
@ -20,14 +20,15 @@ M: process-state dispose* ( process-state -- )
|
|||
M: process-state add-checksum-bytes ( process-state bytes -- process-state' )
|
||||
over proc>> stream-write ;
|
||||
|
||||
: trim-hash ( str -- str' ) dup " *-" swap start head ;
|
||||
: trim-hash ( str -- str' )
|
||||
dup empty? [ dup [ blank? ] find drop [ head ] when* ] unless ;
|
||||
|
||||
M: process-state get-checksum ( checksum-state -- value )
|
||||
dup result>> [
|
||||
dup proc>> [
|
||||
[
|
||||
[ out>> dispose ] keep
|
||||
stream-contents >string trim-hash hex-string>bytes
|
||||
stream-contents trim-hash hex-string>bytes
|
||||
] with-disposal
|
||||
] [ B{ } clone ] if*
|
||||
[ >>result ] keep
|
||||
|
|
Loading…
Reference in New Issue