All pastes #1670904 Raw Edit

Anonymous

public text v1 · immutable
#1670904 ·published 2009-11-14 07:25 UTC
rendered paste body
package main

import (
	"bufio";
	"fmt";
	"os";
)

type Tally struct {
	bytes int;
	words int;
	lines int;
}

func wc(input *bufio.Reader) (tally Tally, err os.Error) {
	tally.bytes = 0;
	tally.words = 0;
	tally.lines = 0;
	err = nil;

	for {
		var c uint8;
		c, err = input.ReadByte();
		if err != nil {
			break;
		}

		tally.bytes++;
		switch {
		case c == ' ' || c == '\t':
			tally.words++;
		case c == '\n':
			tally.words++;
			tally.lines++;
		}
	}

	return;
}

func main() {
	tally, err := wc(bufio.NewReader(os.Stdin));
	if err == os.EOF {
		fmt.Printf("%v\t%v\t%v\n", tally.bytes, tally.words,
			tally.lines);
	} else {
		fmt.Fprintf(os.Stderr, "%v\n", err);
		os.Exit(1);
	}
}