1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package util contains utility types and functions for godoc.
6 package util // import "golang.org/x/tools/godoc/util"
14 "golang.org/x/tools/godoc/vfs"
17 // An RWValue wraps a value and permits mutually exclusive
18 // access to it and records the time the value was last set.
22 timestamp time.Time // time of last set()
25 func (v *RWValue) Set(value interface{}) {
28 v.timestamp = time.Now()
32 func (v *RWValue) Get() (interface{}, time.Time) {
34 defer v.mutex.RUnlock()
35 return v.value, v.timestamp
38 // IsText reports whether a significant prefix of s looks like correct UTF-8;
39 // that is, if it is likely that s is human-readable text.
40 func IsText(s []byte) bool {
41 const max = 1024 // at least utf8.UTFMax
45 for i, c := range string(s) {
46 if i+utf8.UTFMax > len(s) {
47 // last char may be incomplete - ignore
50 if c == 0xFFFD || c < ' ' && c != '\n' && c != '\t' && c != '\f' {
51 // decoding error or control character - not a text file
58 // textExt[x] is true if the extension x indicates a text file, and false otherwise.
59 var textExt = map[string]bool{
60 ".css": false, // must be served raw
61 ".js": false, // must be served raw
62 ".svg": false, // must be served raw
65 // IsTextFile reports whether the file has a known extension indicating
66 // a text file, or if a significant chunk of the specified file looks like
67 // correct UTF-8; that is, if it is likely that the file contains human-
69 func IsTextFile(fs vfs.Opener, filename string) bool {
70 // if the extension is known, use it for decision making
71 if isText, found := textExt[pathpkg.Ext(filename)]; found {
75 // the extension is not known; read an initial chunk
76 // of the file and check if it looks like text
77 f, err := fs.Open(filename)
84 n, err := f.Read(buf[0:])
89 return IsText(buf[0:n])