Debian Codegraph @

pkg/scanner/is_text_file.go

raw symbols SCIP
1 // Copied & modified from https://cs.opensource.google/go/x/tools/+/refs/tags/v0.1.8:godoc/util/util.go
2 //
3 // See LICENSE (https://cs.opensource.google/go/x/tools/+/refs/tags/v0.1.8:LICENSE):
4 // ------------------------------------------------------------------------
5 // Copyright (c) 2009 The Go Authors. All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are
9 // met:
10 //
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 // * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 // ------------------------------------------------------------------------
33
34 package scanner
35
36 import (
37 "io"
38 "os"
39 "unicode/utf8"
40 )
41
42 const (
43 maxBytesUsedToDetermineText = 1024
44 )
45
46 // isTextFile reports whether the first kb of the file looks like correct UTF-8.
47 // If so it is likely that the file contains human-readable text.
48 func isTextFile(f *os.File) bool {
49 var buf [maxBytesUsedToDetermineText]byte
50 n, err := f.Read(buf[0:])
51 if err != nil {
52 return false
53 }
54
55 // rewind reader
56 if _, err = f.Seek(0, io.SeekStart); err != nil {
57 return false
58 }
59 return isTextString(string(buf[0:n])) // return if the readed string is a text
60 }
61
62 // isTextReader checks if the data in the provided reader looks like correct UTF-8.
63 // It will consume up to 1KB from the reader to do so.
64 // Since the reader might not be seekable, the second return value holds the read data and should be used to properly get the whole input stream.
65 func isTextReader(reader io.Reader) (bool, []byte, error) {
66 var buf [maxBytesUsedToDetermineText]byte
67 n, err := reader.Read(buf[0:])
68 if err != nil {
69 return false, nil, err
70 }
71
72 // Input might be <1KB
73 readData := buf[0:n]
74 return isTextString(string(readData)), readData, nil
75 }
76
77 func isTextString(s string) bool {
78 for i, c := range s {
79 if i+utf8.UTFMax > len(s) {
80 // last char may be incomplete - ignore
81 break
82 }
83 if c == 0xFFFD || c < ' ' && c != '\n' && c != '\t' && c != '\f' {
84 // decoding error or control character - not a text file
85 return false
86 }
87 }
88 return true
89 }