]> gitweb.factorcode.org Git - factor.git/commitdiff
checksums.process: reimplement trim-hash and add tests
authorAlexander Iljin <ajsoft@yandex.ru>
Mon, 19 Sep 2016 21:09:12 +0000 (00:09 +0300)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 20 Sep 2016 03:18:06 +0000 (20:18 -0700)
Use blank? to detect end of hash string.

extra/checksums/process/process-docs.factor
extra/checksums/process/process-tests.factor [new file with mode: 0644]
extra/checksums/process/process.factor

index b0c4fb22abdd282e859e07c190571f19bf583451..847a63771236c9a7f1cb29077feb12390c719627 100644 (file)
@@ -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
diff --git a/extra/checksums/process/process-tests.factor b/extra/checksums/process/process-tests.factor
new file mode 100644 (file)
index 0000000..7cf12cd
--- /dev/null
@@ -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
index 53cae08841ba421ab6a2cdb73824f7c08a4f42bf..381c90167425a1f5e5500e6e86a9c3362bcc7ac0 100644 (file)
@@ -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