tools.wc: adding "wc" tool.

char-rename
John Benediktsson 2017-01-22 14:40:37 -08:00
parent baaa06278c
commit c4f39e32a5
3 changed files with 63 additions and 0 deletions

View File

@ -0,0 +1 @@
John Benediktsson

View File

@ -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 }
}

46
extra/tools/wc/wc.factor Normal file
View File

@ -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