Plan9 wc makes an interesting use of a finite state machine to count lines, words and runes. The code is on GitHub (local cache).