GoPLS Viewer

Home|gopls/godoc/util/util.go
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.
4
5// Package util contains utility types and functions for godoc.
6package util // import "golang.org/x/tools/godoc/util"
7
8import (
9    pathpkg "path"
10    "sync"
11    "time"
12    "unicode/utf8"
13
14    "golang.org/x/tools/godoc/vfs"
15)
16
17// An RWValue wraps a value and permits mutually exclusive
18// access to it and records the time the value was last set.
19type RWValue struct {
20    mutex     sync.RWMutex
21    value     interface{}
22    timestamp time.Time // time of last set()
23}
24
25func (v *RWValueSet(value interface{}) {
26    v.mutex.Lock()
27    v.value = value
28    v.timestamp = time.Now()
29    v.mutex.Unlock()
30}
31
32func (v *RWValueGet() (interface{}, time.Time) {
33    v.mutex.RLock()
34    defer v.mutex.RUnlock()
35    return v.valuev.timestamp
36}
37
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.
40func IsText(s []bytebool {
41    const max = 1024 // at least utf8.UTFMax
42    if len(s) > max {
43        s = s[0:max]
44    }
45    for ic := range string(s) {
46        if i+utf8.UTFMax > len(s) {
47            // last char may be incomplete - ignore
48            break
49        }
50        if c == 0xFFFD || c < ' ' && c != '\n' && c != '\t' && c != '\f' {
51            // decoding error or control character - not a text file
52            return false
53        }
54    }
55    return true
56}
57
58// textExt[x] is true if the extension x indicates a text file, and false otherwise.
59var 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
63}
64
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-
68// readable text.
69func IsTextFile(fs vfs.Openerfilename stringbool {
70    // if the extension is known, use it for decision making
71    if isTextfound := textExt[pathpkg.Ext(filename)]; found {
72        return isText
73    }
74
75    // the extension is not known; read an initial chunk
76    // of the file and check if it looks like text
77    ferr := fs.Open(filename)
78    if err != nil {
79        return false
80    }
81    defer f.Close()
82
83    var buf [1024]byte
84    nerr := f.Read(buf[0:])
85    if err != nil {
86        return false
87    }
88
89    return IsText(buf[0:n])
90}
91
MembersX
IsText.RangeStmt_1103.c
pathpkg
utf8
RWValue.value
RWValue.Set.value
RWValue.Get
IsText
IsText.RangeStmt_1103.i
IsTextFile.buf
IsTextFile.n
RWValue.Set.v
IsText.s
IsTextFile
IsTextFile.err
RWValue
RWValue.Set
RWValue.Get.v
IsText.max
IsTextFile.f
sync
vfs
RWValue.mutex
RWValue.timestamp
IsTextFile.fs
IsTextFile.filename
Members
X