79 lines
1.3 KiB
Go
79 lines
1.3 KiB
Go
package main
|
|
|
|
import "bufio"
|
|
|
|
//import "fmt"
|
|
import "io"
|
|
import "log"
|
|
import "os"
|
|
import "strings"
|
|
|
|
func splitWord(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
|
if atEOF && len(data) > 0 {
|
|
//fmt.Print("EOF")
|
|
return len(data), data, nil
|
|
}
|
|
splitAt := -1
|
|
if len(data) > 0 {
|
|
for i, c := range data {
|
|
//fmt.Print(string(c))
|
|
switch string(c) {
|
|
case "\n":
|
|
fallthrough
|
|
case " ":
|
|
fallthrough
|
|
case "\t":
|
|
if splitAt < 0 {
|
|
splitAt = i
|
|
}
|
|
default:
|
|
if splitAt > 0 {
|
|
return i, data[:splitAt], nil
|
|
}
|
|
}
|
|
}
|
|
//fmt.Println()
|
|
}
|
|
return 0, nil, nil
|
|
}
|
|
|
|
func transform(in io.Reader, out io.Writer) {
|
|
bufw := bufio.NewWriter(out)
|
|
s := bufio.NewScanner(in)
|
|
//s.Split(bufio.ScanWords)
|
|
s.Split(splitWord)
|
|
for s.Scan() {
|
|
token := s.Text()
|
|
//fmt.Println(token)
|
|
if len(token) > 1 {
|
|
bufw.WriteString(strings.ToUpper(string(token[0])) + token[1:] + " ")
|
|
}
|
|
}
|
|
bufw.Flush()
|
|
}
|
|
|
|
func main() {
|
|
if len(os.Args) < 2 {
|
|
log.Fatalln("Usage upper-line <file>")
|
|
}
|
|
f, err := os.Open(os.Args[1])
|
|
if err != nil {
|
|
log.Fatalln("Error opening file", err)
|
|
}
|
|
defer f.Close()
|
|
|
|
var w io.Writer
|
|
if len(os.Args) >= 3 {
|
|
fout, err := os.Create(os.Args[2])
|
|
if err != nil {
|
|
log.Fatalln("Error creating file", err)
|
|
}
|
|
defer fout.Close()
|
|
w = fout
|
|
} else {
|
|
w = os.Stdout
|
|
}
|
|
|
|
transform(f, w)
|
|
}
|