Write your own WC - Golang
Though, I might have used a million times, when implementing it I had a chance to sidestep and understand UTF8 encodings, multi-byte characters, encoding of emoj with zero with joiners, the unix definition of a POSIX text file
The solution is nothing fancy, very simple and haven't invested time to make it efficient, but as I move forward with the another exercises I might take up them (Why not benchmark, etc for fun)
The code is structured under wc and implementation is straightforward due Golang
./wc --help
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified.
Usage:
wc [flags]
Flags:
-c, --bytes bytes count output
-m, --chars char count output
-h, --help help for wc
-l, --lines line count output
-v, --verbose verbose output
-V, --version version output
-w, --words word count output
Checking the input text mentioned in the challenge step zero
The output from wc (GNU Coreutils) 8.32 on Ubuntu with (LC_NAME=sv_SE.UTF-8)
wc tests/testdata/test.txt
7145 58164 342147 tests/testdata/test.txt
The output from wc.py for the same test.txt,
./wc tests/testdata/test.txt
7145 58164 342147 tests/testdata/test.txt
A sample run for stdin,
echo -n "TwoLines\n\n" | ./wc
2 1 10 /dev/stdin
The output for the various options are same as that of the wc tool and it is verified using both Go tests and Functional tests
The module is called wc and it uses cobra package for command line parsing, the logic is implemented in the package named util
go build -o ./wc .
To run the tests,
go test -v ./...
Checkout the specific util package to see the various inputs it uses.
The testdata is used which to compare alignment with wc to run the test use test.sh.