diff options
Diffstat (limited to 'vendor/golang.org/x/text/internal')
-rw-r--r-- | vendor/golang.org/x/text/internal/gen/code.go | 1 | ||||
-rw-r--r-- | vendor/golang.org/x/text/internal/gen/gen.go | 92 | ||||
-rw-r--r-- | vendor/golang.org/x/text/internal/ucd/ucd.go | 19 |
3 files changed, 88 insertions, 24 deletions
diff --git a/vendor/golang.org/x/text/internal/gen/code.go b/vendor/golang.org/x/text/internal/gen/code.go index ca917d2..2202c9f 100644 --- a/vendor/golang.org/x/text/internal/gen/code.go +++ b/vendor/golang.org/x/text/internal/gen/code.go @@ -187,6 +187,7 @@ func (w *CodeWriter) writeValue(v reflect.Value) { // WriteString writes a string literal. func (w *CodeWriter) WriteString(s string) { + s = strings.Replace(s, `\`, `\\`, -1) io.WriteString(w.Hash, s) // content hash w.Size += len(s) diff --git a/vendor/golang.org/x/text/internal/gen/gen.go b/vendor/golang.org/x/text/internal/gen/gen.go index dfaa278..9eb2987 100644 --- a/vendor/golang.org/x/text/internal/gen/gen.go +++ b/vendor/golang.org/x/text/internal/gen/gen.go @@ -22,6 +22,7 @@ import ( "bytes" "flag" "fmt" + "go/build" "go/format" "io" "io/ioutil" @@ -30,6 +31,7 @@ import ( "os" "path" "path/filepath" + "sync" "unicode" "golang.org/x/text/unicode/cldr" @@ -48,12 +50,6 @@ var ( cldrVersion = flag.String("cldr", getEnv("CLDR_VERSION", cldr.Version), "cldr version to use") - // Allow an environment variable to specify the local directory. - // go generate doesn't allow specifying arguments; this is a useful - // alternative to specifying a local mirror. - localDir = flag.String("local", - os.Getenv("UNICODE_DIR"), - "directory containing local data files; for debugging only.") ) func getEnv(name, def string) string { @@ -87,9 +83,12 @@ func CLDRVersion() string { return *cldrVersion } -// IsLocal reports whether the user specified a local directory. +// IsLocal reports whether data files are available locally. func IsLocal() bool { - return *localDir != "" + if _, err := os.Stat(localReadmeFile()); err != nil { + return false + } + return true } // OpenUCDFile opens the requested UCD file. The file is specified relative to @@ -124,29 +123,78 @@ func OpenIANAFile(path string) io.ReadCloser { return Open(*iana, "iana", path) } +var ( + dirMutex sync.Mutex + localDir string +) + +const permissions = 0755 + +func localReadmeFile() string { + p, err := build.Import("golang.org/x/text", "", build.FindOnly) + if err != nil { + log.Fatalf("Could not locate package: %v", err) + } + return filepath.Join(p.Dir, "DATA", "README") +} + +func getLocalDir() string { + dirMutex.Lock() + defer dirMutex.Unlock() + + readme := localReadmeFile() + dir := filepath.Dir(readme) + if _, err := os.Stat(readme); err != nil { + if err := os.MkdirAll(dir, permissions); err != nil { + log.Fatalf("Could not create directory: %v", err) + } + ioutil.WriteFile(readme, []byte(readmeTxt), permissions) + } + return dir +} + +const readmeTxt = `Generated by golang.org/x/text/internal/gen. DO NOT EDIT. + +This directory contains downloaded files used to generate the various tables +in the golang.org/x/text subrepo. + +Note that the language subtag repo (iana/assignments/language-subtag-registry) +and all other times in the iana subdirectory are not versioned and will need +to be periodically manually updated. The easiest way to do this is to remove +the entire iana directory. This is mostly of concern when updating the language +package. +` + // Open opens subdir/path if a local directory is specified and the file exists, // where subdir is a directory relative to the local root, or fetches it from // urlRoot/path otherwise. It will call log.Fatal if there are any errors. func Open(urlRoot, subdir, path string) io.ReadCloser { - if *localDir != "" { - path = filepath.FromSlash(path) - if f, err := os.Open(filepath.Join(*localDir, subdir, path)); err == nil { - return f - } - } - return get(urlRoot, path) + file := filepath.Join(getLocalDir(), subdir, filepath.FromSlash(path)) + return open(file, urlRoot, path) } func openUnicode(path string) io.ReadCloser { - if *localDir != "" { - path = filepath.FromSlash(path) - f, err := os.Open(filepath.Join(*localDir, path)) - if err != nil { - log.Fatal(err) - } + file := filepath.Join(getLocalDir(), filepath.FromSlash(path)) + return open(file, *url, path) +} + +// TODO: automatically periodically update non-versioned files. + +func open(file, urlRoot, path string) io.ReadCloser { + if f, err := os.Open(file); err == nil { return f } - return get(*url, path) + r := get(urlRoot, path) + defer r.Close() + b, err := ioutil.ReadAll(r) + if err != nil { + log.Fatalf("Could not download file: %v", err) + } + os.MkdirAll(filepath.Dir(file), permissions) + if err := ioutil.WriteFile(file, b, permissions); err != nil { + log.Fatalf("Could not create file: %v", err) + } + return ioutil.NopCloser(bytes.NewReader(b)) } func get(root, path string) io.ReadCloser { diff --git a/vendor/golang.org/x/text/internal/ucd/ucd.go b/vendor/golang.org/x/text/internal/ucd/ucd.go index 2b0d1a1..60b27d5 100644 --- a/vendor/golang.org/x/text/internal/ucd/ucd.go +++ b/vendor/golang.org/x/text/internal/ucd/ucd.go @@ -78,6 +78,14 @@ func Part(f func(p *Parser)) Option { } } +// The CommentHandler option passes comments that are on a line by itself to +// a given handler. +func CommentHandler(f func(s string)) Option { + return func(p *Parser) { + p.commentHandler = f + } +} + // A Parser parses Unicode Character Database (UCD) files. type Parser struct { scanner *bufio.Scanner @@ -92,7 +100,8 @@ type Parser struct { parsedRange bool rangeStart, rangeEnd rune - partHandler func(p *Parser) + partHandler func(p *Parser) + commentHandler func(s string) } func (p *Parser) setError(err error) { @@ -138,7 +147,13 @@ func (p *Parser) Next() bool { for p.scanner.Scan() { b := p.scanner.Bytes() - if len(b) == 0 || b[0] == '#' { + if len(b) == 0 { + continue + } + if b[0] == '#' { + if p.commentHandler != nil { + p.commentHandler(strings.TrimSpace(string(b[1:]))) + } continue } |