improve zh2tw.go

This commit is contained in:
chai2010 2015-12-11 17:26:52 +08:00
parent 643409dd27
commit aa31d6ed67

View File

@ -25,8 +25,11 @@ import (
"os"
"path/filepath"
"regexp"
"unicode/utf8"
)
const MaxFileSize = 8 << 20 // 8MB
const usage = `
Usage: zh2tw dir [nameFilter]
zh2tw -h
@ -79,9 +82,9 @@ func main() {
if mathed {
if changed := convertFile(path, method); changed {
fmt.Printf("%s\n", relpath)
}
total++
}
}
return nil
})
fmt.Printf("%s total %d\n", method, total)
@ -93,10 +96,21 @@ func convertFile(path, method string) (changed bool) {
log.Fatal("convertFile: filepath.Abs:", err)
}
fi, err := os.Lstat(abspath)
if err != nil {
log.Fatal("convertFile: os.Lstat:", err)
}
if fi.Size() > MaxFileSize {
return false
}
oldData, err := ioutil.ReadFile(abspath)
if err != nil {
log.Fatal("convertFile: ioutil.ReadFile:", err)
}
if !utf8.Valid(oldData) {
return false
}
newData := oldData
switch {