]> gitweb.factorcode.org Git - factor.git/commitdiff
tools.wc: adding "wc" tool.
authorJohn Benediktsson <mrjbq7@gmail.com>
Sun, 22 Jan 2017 22:40:37 +0000 (14:40 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sun, 22 Jan 2017 22:40:37 +0000 (14:40 -0800)
extra/tools/wc/authors.txt [new file with mode: 0644]
extra/tools/wc/deploy.factor [new file with mode: 0644]
extra/tools/wc/wc.factor [new file with mode: 0644]

diff --git a/extra/tools/wc/authors.txt b/extra/tools/wc/authors.txt
new file mode 100644 (file)
index 0000000..e091bb8
--- /dev/null
@@ -0,0 +1 @@
+John Benediktsson
diff --git a/extra/tools/wc/deploy.factor b/extra/tools/wc/deploy.factor
new file mode 100644 (file)
index 0000000..6790d42
--- /dev/null
@@ -0,0 +1,16 @@
+USING: tools.deploy.config ;
+H{
+    { deploy-c-types? f }
+    { deploy-help? f }
+    { deploy-name "wc" }
+    { "stop-after-last-window?" t }
+    { deploy-unicode? f }
+    { deploy-console? t }
+    { deploy-io 3 }
+    { deploy-reflection 1 }
+    { deploy-ui? f }
+    { deploy-word-defs? f }
+    { deploy-threads? t }
+    { deploy-math? t }
+    { deploy-word-props? f }
+}
diff --git a/extra/tools/wc/wc.factor b/extra/tools/wc/wc.factor
new file mode 100644 (file)
index 0000000..c0535df
--- /dev/null
@@ -0,0 +1,46 @@
+! Copyright (C) 2016 John Benediktsson
+! See http://factorcode.org/license.txt for BSD license
+
+USING: accessors alien.data command-line formatting io
+io.encodings io.encodings.binary io.files kernel math
+math.bitwise math.vectors math.vectors.simd namespaces sequences
+specialized-arrays ;
+
+SPECIALIZED-ARRAY: uchar-16
+
+IN: tools.wc
+
+<PRIVATE
+
+: aligned-slices ( seq -- head tail )
+   dup length 0xf unmask cut-slice ; inline
+
+: count-characters ( -- n )
+    0 [ length + ] each-block-slice ; inline
+
+: count-lines ( -- n )
+    0 [
+       aligned-slices [
+           uchar-16 cast-array swap
+           [ CHAR: \n uchar-16-with v= vcount + >fixnum ] reduce
+       ] [ [ CHAR: \n = ] count + >fixnum ] bi*
+    ] each-block-slice ; inline
+
+: wc-stdin ( -- n )
+    input-stream get dup decoder? [ stream>> ] when
+    [ count-lines ] with-input-stream* ;
+
+PRIVATE>
+
+: wc ( path -- n )
+    binary [ count-lines ] with-file-reader ;
+
+: run-wc ( -- )
+    command-line get [
+        wc-stdin "%8d\n" printf
+    ] [
+        [ [ wc ] keep dupd "%8d %s\n" printf ] map
+        dup length 1 > [ sum "%8d total\n" printf ] [ drop ] if
+    ] if-empty ;
+
+MAIN: run-wc