From aa31d6ed67deee12721e1e161f40cce7faa6dc84 Mon Sep 17 00:00:00 2001 From: chai2010 Date: Fri, 11 Dec 2015 17:26:52 +0800 Subject: [PATCH] improve zh2tw.go --- zh2tw.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/zh2tw.go b/zh2tw.go index 139652f..c1aed46 100644 --- a/zh2tw.go +++ b/zh2tw.go @@ -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,8 +82,8 @@ func main() { if mathed { if changed := convertFile(path, method); changed { fmt.Printf("%s\n", relpath) + total++ } - total++ } return nil }) @@ -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 {