mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-28 09:09:07 +00:00
improve zh2tw.go
This commit is contained in:
parent
643409dd27
commit
aa31d6ed67
16
zh2tw.go
16
zh2tw.go
@ -25,8 +25,11 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const MaxFileSize = 8 << 20 // 8MB
|
||||||
|
|
||||||
const usage = `
|
const usage = `
|
||||||
Usage: zh2tw dir [nameFilter]
|
Usage: zh2tw dir [nameFilter]
|
||||||
zh2tw -h
|
zh2tw -h
|
||||||
@ -79,8 +82,8 @@ func main() {
|
|||||||
if mathed {
|
if mathed {
|
||||||
if changed := convertFile(path, method); changed {
|
if changed := convertFile(path, method); changed {
|
||||||
fmt.Printf("%s\n", relpath)
|
fmt.Printf("%s\n", relpath)
|
||||||
|
total++
|
||||||
}
|
}
|
||||||
total++
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@ -93,10 +96,21 @@ func convertFile(path, method string) (changed bool) {
|
|||||||
log.Fatal("convertFile: filepath.Abs:", err)
|
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)
|
oldData, err := ioutil.ReadFile(abspath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("convertFile: ioutil.ReadFile:", err)
|
log.Fatal("convertFile: ioutil.ReadFile:", err)
|
||||||
}
|
}
|
||||||
|
if !utf8.Valid(oldData) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
newData := oldData
|
newData := oldData
|
||||||
switch {
|
switch {
|
||||||
|
Loading…
Reference in New Issue
Block a user