From c4f39e32a536ce9c7926397573a4df0ddb548df2 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Sun, 22 Jan 2017 14:40:37 -0800 Subject: [PATCH] tools.wc: adding "wc" tool. --- extra/tools/wc/authors.txt | 1 + extra/tools/wc/deploy.factor | 16 +++++++++++++ extra/tools/wc/wc.factor | 46 ++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 extra/tools/wc/authors.txt create mode 100644 extra/tools/wc/deploy.factor create mode 100644 extra/tools/wc/wc.factor diff --git a/extra/tools/wc/authors.txt b/extra/tools/wc/authors.txt new file mode 100644 index 0000000000..e091bb8164 --- /dev/null +++ b/extra/tools/wc/authors.txt @@ -0,0 +1 @@ +John Benediktsson diff --git a/extra/tools/wc/deploy.factor b/extra/tools/wc/deploy.factor new file mode 100644 index 0000000000..6790d42a0e --- /dev/null +++ b/extra/tools/wc/deploy.factor @@ -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 index 0000000000..c0535df1bd --- /dev/null +++ b/extra/tools/wc/wc.factor @@ -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 + +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 -- 2.34.1