2008-12-18 16:42:39 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Simple file i/o and string manipulation, to avoid
|
2009-11-01 12:15:34 -07:00
|
|
|
// depending on strconv and bufio and strings.
|
2008-12-18 16:42:39 -07:00
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:35:38 -07:00
|
|
|
"io"
|
|
|
|
"os"
|
2016-01-04 19:04:01 -07:00
|
|
|
"time"
|
2015-10-15 09:59:01 -06:00
|
|
|
_ "unsafe" // For go:linkname
|
2008-12-18 16:42:39 -07:00
|
|
|
)
|
|
|
|
|
2009-02-15 15:18:39 -07:00
|
|
|
type file struct {
|
2010-04-29 12:01:21 -06:00
|
|
|
file *os.File
|
|
|
|
data []byte
|
|
|
|
atEOF bool
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (f *file) close() { f.file.Close() }
|
2008-12-18 16:42:39 -07:00
|
|
|
|
2009-02-15 15:18:39 -07:00
|
|
|
func (f *file) getLineFromData() (s string, ok bool) {
|
2009-12-15 16:35:38 -07:00
|
|
|
data := f.data
|
2010-04-29 12:01:21 -06:00
|
|
|
i := 0
|
|
|
|
for i = 0; i < len(data); i++ {
|
2008-12-18 16:42:39 -07:00
|
|
|
if data[i] == '\n' {
|
2009-12-15 16:35:38 -07:00
|
|
|
s = string(data[0:i])
|
|
|
|
ok = true
|
2008-12-18 16:42:39 -07:00
|
|
|
// move data
|
2009-12-15 16:35:38 -07:00
|
|
|
i++
|
|
|
|
n := len(data) - i
|
2010-04-29 12:01:21 -06:00
|
|
|
copy(data[0:], data[i:])
|
2009-12-15 16:35:38 -07:00
|
|
|
f.data = data[0:n]
|
|
|
|
return
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
}
|
2010-04-29 12:01:21 -06:00
|
|
|
if f.atEOF && len(f.data) > 0 {
|
|
|
|
// EOF, return all we have
|
2010-04-28 20:36:04 -06:00
|
|
|
s = string(data)
|
2010-04-29 12:01:21 -06:00
|
|
|
f.data = f.data[0:0]
|
2010-04-28 20:36:04 -06:00
|
|
|
ok = true
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
|
2009-02-15 15:18:39 -07:00
|
|
|
func (f *file) readLine() (s string, ok bool) {
|
|
|
|
if s, ok = f.getLineFromData(); ok {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
if len(f.data) < cap(f.data) {
|
2009-12-15 16:35:38 -07:00
|
|
|
ln := len(f.data)
|
2010-04-29 12:01:21 -06:00
|
|
|
n, err := io.ReadFull(f.file, f.data[ln:cap(f.data)])
|
2008-12-18 16:42:39 -07:00
|
|
|
if n >= 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
f.data = f.data[0 : ln+n]
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2013-10-28 17:31:25 -06:00
|
|
|
if err == io.EOF || err == io.ErrUnexpectedEOF {
|
2010-04-29 12:01:21 -06:00
|
|
|
f.atEOF = true
|
|
|
|
}
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
s, ok = f.getLineFromData()
|
|
|
|
return
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
|
2011-11-01 20:05:34 -06:00
|
|
|
func open(name string) (*file, error) {
|
2011-04-05 00:42:14 -06:00
|
|
|
fd, err := os.Open(name)
|
2008-12-18 16:42:39 -07:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, err
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2013-12-11 23:13:17 -07:00
|
|
|
return &file{fd, make([]byte, 0, os.Getpagesize()), false}, nil
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
|
2016-01-04 19:04:01 -07:00
|
|
|
func stat(name string) (mtime time.Time, size int64, err error) {
|
|
|
|
st, err := os.Stat(name)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, 0, err
|
|
|
|
}
|
|
|
|
return st.ModTime(), st.Size(), nil
|
|
|
|
}
|
|
|
|
|
2015-10-15 09:59:01 -06:00
|
|
|
// byteIndex is strings.IndexByte. It returns the index of the
|
|
|
|
// first instance of c in s, or -1 if c is not present in s.
|
|
|
|
// strings.IndexByte is implemented in runtime/asm_$GOARCH.s
|
|
|
|
//go:linkname byteIndex strings.IndexByte
|
|
|
|
func byteIndex(s string, c byte) int
|
2008-12-18 16:42:39 -07:00
|
|
|
|
|
|
|
// Count occurrences in s of any bytes in t.
|
2009-02-15 15:18:39 -07:00
|
|
|
func countAnyByte(s string, t string) int {
|
2009-12-15 16:35:38 -07:00
|
|
|
n := 0
|
2008-12-18 16:42:39 -07:00
|
|
|
for i := 0; i < len(s); i++ {
|
2009-02-15 15:18:39 -07:00
|
|
|
if byteIndex(t, s[i]) >= 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
n++
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return n
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Split s at any bytes in t.
|
2009-02-15 15:18:39 -07:00
|
|
|
func splitAtBytes(s string, t string) []string {
|
2009-12-15 16:35:38 -07:00
|
|
|
a := make([]string, 1+countAnyByte(s, t))
|
|
|
|
n := 0
|
|
|
|
last := 0
|
2008-12-18 16:42:39 -07:00
|
|
|
for i := 0; i < len(s); i++ {
|
2009-02-15 15:18:39 -07:00
|
|
|
if byteIndex(t, s[i]) >= 0 {
|
2008-12-18 16:42:39 -07:00
|
|
|
if last < i {
|
2016-04-14 20:09:36 -06:00
|
|
|
a[n] = s[last:i]
|
2009-12-15 16:35:38 -07:00
|
|
|
n++
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
last = i + 1
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if last < len(s) {
|
2016-04-14 20:09:36 -06:00
|
|
|
a[n] = s[last:]
|
2009-12-15 16:35:38 -07:00
|
|
|
n++
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return a[0:n]
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func getFields(s string) []string { return splitAtBytes(s, " \r\t\n") }
|
2008-12-18 16:42:39 -07:00
|
|
|
|
|
|
|
// Bigger than we need, not too big to worry about overflow
|
2009-02-15 15:18:39 -07:00
|
|
|
const big = 0xFFFFFF
|
2008-12-18 16:42:39 -07:00
|
|
|
|
2016-08-16 10:33:27 -06:00
|
|
|
// Decimal to integer.
|
|
|
|
// Returns number, characters consumed, success.
|
|
|
|
func dtoi(s string) (n int, i int, ok bool) {
|
2009-12-15 16:35:38 -07:00
|
|
|
n = 0
|
2016-08-16 10:33:27 -06:00
|
|
|
for i = 0; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ {
|
2009-12-15 16:35:38 -07:00
|
|
|
n = n*10 + int(s[i]-'0')
|
2009-02-15 15:18:39 -07:00
|
|
|
if n >= big {
|
2015-07-20 21:51:01 -06:00
|
|
|
return big, i, false
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
}
|
2016-08-16 10:33:27 -06:00
|
|
|
if i == 0 {
|
2016-09-02 02:14:57 -06:00
|
|
|
return 0, 0, false
|
2015-07-20 21:51:01 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return n, i, true
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
|
2016-08-16 10:33:27 -06:00
|
|
|
// Hexadecimal to integer.
|
|
|
|
// Returns number, characters consumed, success.
|
|
|
|
func xtoi(s string) (n int, i int, ok bool) {
|
2009-12-15 16:35:38 -07:00
|
|
|
n = 0
|
2016-08-16 10:33:27 -06:00
|
|
|
for i = 0; i < len(s); i++ {
|
2008-12-18 16:42:39 -07:00
|
|
|
if '0' <= s[i] && s[i] <= '9' {
|
2009-12-15 16:35:38 -07:00
|
|
|
n *= 16
|
|
|
|
n += int(s[i] - '0')
|
2008-12-18 16:42:39 -07:00
|
|
|
} else if 'a' <= s[i] && s[i] <= 'f' {
|
2009-12-15 16:35:38 -07:00
|
|
|
n *= 16
|
|
|
|
n += int(s[i]-'a') + 10
|
2008-12-18 16:42:39 -07:00
|
|
|
} else if 'A' <= s[i] && s[i] <= 'F' {
|
2009-12-15 16:35:38 -07:00
|
|
|
n *= 16
|
|
|
|
n += int(s[i]-'A') + 10
|
2008-12-18 16:42:39 -07:00
|
|
|
} else {
|
2009-11-09 13:07:39 -07:00
|
|
|
break
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2009-02-15 15:18:39 -07:00
|
|
|
if n >= big {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, i, false
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
}
|
2016-08-16 10:33:27 -06:00
|
|
|
if i == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, i, false
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return n, i, true
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
|
2011-08-25 17:22:46 -06:00
|
|
|
// xtoi2 converts the next two hex digits of s into a byte.
|
|
|
|
// If s is longer than 2 bytes then the third byte must be e.
|
|
|
|
// If the first two bytes of s are not hex digits or the third byte
|
|
|
|
// does not match e, false is returned.
|
|
|
|
func xtoi2(s string, e byte) (byte, bool) {
|
|
|
|
if len(s) > 2 && s[2] != e {
|
|
|
|
return 0, false
|
|
|
|
}
|
2016-08-16 10:33:27 -06:00
|
|
|
n, ei, ok := xtoi(s[:2])
|
2011-08-25 17:22:46 -06:00
|
|
|
return byte(n), ok && ei == 2
|
|
|
|
}
|
|
|
|
|
2014-12-31 10:45:05 -07:00
|
|
|
// Convert integer to decimal string.
|
|
|
|
func itoa(val int) string {
|
|
|
|
if val < 0 {
|
|
|
|
return "-" + uitoa(uint(-val))
|
|
|
|
}
|
|
|
|
return uitoa(uint(val))
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
2014-12-31 10:45:05 -07:00
|
|
|
// Convert unsigned integer to decimal string.
|
|
|
|
func uitoa(val uint) string {
|
|
|
|
if val == 0 { // avoid string allocation
|
2011-08-25 17:22:46 -06:00
|
|
|
return "0"
|
|
|
|
}
|
2014-12-31 10:45:05 -07:00
|
|
|
var buf [20]byte // big enough for 64bit value base 10
|
|
|
|
i := len(buf) - 1
|
|
|
|
for val >= 10 {
|
|
|
|
q := val / 10
|
|
|
|
buf[i] = byte('0' + val - q*10)
|
|
|
|
i--
|
|
|
|
val = q
|
|
|
|
}
|
|
|
|
// val < 10
|
|
|
|
buf[i] = byte('0' + val)
|
|
|
|
return string(buf[i:])
|
2011-08-25 17:22:46 -06:00
|
|
|
}
|
|
|
|
|
2014-06-11 21:40:00 -06:00
|
|
|
// Convert i to a hexadecimal string. Leading zeros are not printed.
|
|
|
|
func appendHex(dst []byte, i uint32) []byte {
|
|
|
|
if i == 0 {
|
|
|
|
return append(dst, '0')
|
2011-08-25 17:22:46 -06:00
|
|
|
}
|
2014-06-11 21:40:00 -06:00
|
|
|
for j := 7; j >= 0; j-- {
|
|
|
|
v := i >> uint(j*4)
|
|
|
|
if v > 0 {
|
|
|
|
dst = append(dst, hexDigit[v&0xf])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dst
|
2011-08-25 17:22:46 -06:00
|
|
|
}
|
|
|
|
|
2009-11-01 12:15:34 -07:00
|
|
|
// Number of occurrences of b in s.
|
|
|
|
func count(s string, b byte) int {
|
2009-12-15 16:35:38 -07:00
|
|
|
n := 0
|
2009-11-01 12:15:34 -07:00
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
if s[i] == b {
|
2009-11-09 13:07:39 -07:00
|
|
|
n++
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return n
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Index of rightmost occurrence of b in s.
|
|
|
|
func last(s string, b byte) int {
|
2009-12-15 16:35:38 -07:00
|
|
|
i := len(s)
|
2009-11-01 12:15:34 -07:00
|
|
|
for i--; i >= 0; i-- {
|
|
|
|
if s[i] == b {
|
2009-11-09 13:07:39 -07:00
|
|
|
break
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return i
|
2009-11-01 12:15:34 -07:00
|
|
|
}
|
2015-04-16 15:33:25 -06:00
|
|
|
|
|
|
|
// lowerASCIIBytes makes x ASCII lowercase in-place.
|
|
|
|
func lowerASCIIBytes(x []byte) {
|
|
|
|
for i, b := range x {
|
|
|
|
if 'A' <= b && b <= 'Z' {
|
|
|
|
x[i] += 'a' - 'A'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// lowerASCII returns the ASCII lowercase version of b.
|
|
|
|
func lowerASCII(b byte) byte {
|
|
|
|
if 'A' <= b && b <= 'Z' {
|
|
|
|
return b + ('a' - 'A')
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// trimSpace returns x without any leading or trailing ASCII whitespace.
|
|
|
|
func trimSpace(x []byte) []byte {
|
|
|
|
for len(x) > 0 && isSpace(x[0]) {
|
|
|
|
x = x[1:]
|
|
|
|
}
|
|
|
|
for len(x) > 0 && isSpace(x[len(x)-1]) {
|
|
|
|
x = x[:len(x)-1]
|
|
|
|
}
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
// isSpace reports whether b is an ASCII space character.
|
|
|
|
func isSpace(b byte) bool {
|
|
|
|
return b == ' ' || b == '\t' || b == '\n' || b == '\r'
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeComment returns line, removing any '#' byte and any following
|
|
|
|
// bytes.
|
|
|
|
func removeComment(line []byte) []byte {
|
|
|
|
if i := bytesIndexByte(line, '#'); i != -1 {
|
|
|
|
return line[:i]
|
|
|
|
}
|
|
|
|
return line
|
|
|
|
}
|
|
|
|
|
|
|
|
// foreachLine runs fn on each line of x.
|
|
|
|
// Each line (except for possibly the last) ends in '\n'.
|
|
|
|
// It returns the first non-nil error returned by fn.
|
|
|
|
func foreachLine(x []byte, fn func(line []byte) error) error {
|
|
|
|
for len(x) > 0 {
|
|
|
|
nl := bytesIndexByte(x, '\n')
|
|
|
|
if nl == -1 {
|
|
|
|
return fn(x)
|
|
|
|
}
|
|
|
|
line := x[:nl+1]
|
|
|
|
x = x[nl+1:]
|
|
|
|
if err := fn(line); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// foreachField runs fn on each non-empty run of non-space bytes in x.
|
|
|
|
// It returns the first non-nil error returned by fn.
|
|
|
|
func foreachField(x []byte, fn func(field []byte) error) error {
|
|
|
|
x = trimSpace(x)
|
|
|
|
for len(x) > 0 {
|
|
|
|
sp := bytesIndexByte(x, ' ')
|
|
|
|
if sp == -1 {
|
|
|
|
return fn(x)
|
|
|
|
}
|
|
|
|
if field := trimSpace(x[:sp]); len(field) > 0 {
|
|
|
|
if err := fn(field); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
x = trimSpace(x[sp+1:])
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// bytesIndexByte is bytes.IndexByte. It returns the index of the
|
|
|
|
// first instance of c in s, or -1 if c is not present in s.
|
2015-10-15 09:59:01 -06:00
|
|
|
// bytes.IndexByte is implemented in runtime/asm_$GOARCH.s
|
|
|
|
//go:linkname bytesIndexByte bytes.IndexByte
|
|
|
|
func bytesIndexByte(s []byte, c byte) int
|
2015-04-16 15:33:25 -06:00
|
|
|
|
|
|
|
// stringsHasSuffix is strings.HasSuffix. It reports whether s ends in
|
|
|
|
// suffix.
|
|
|
|
func stringsHasSuffix(s, suffix string) bool {
|
|
|
|
return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix
|
|
|
|
}
|
|
|
|
|
|
|
|
// stringsHasSuffixFold reports whether s ends in suffix,
|
|
|
|
// ASCII-case-insensitively.
|
|
|
|
func stringsHasSuffixFold(s, suffix string) bool {
|
2016-11-23 11:35:17 -07:00
|
|
|
return len(s) >= len(suffix) && stringsEqualFold(s[len(s)-len(suffix):], suffix)
|
|
|
|
}
|
|
|
|
|
|
|
|
// stringsHasPrefix is strings.HasPrefix. It reports whether s begins with prefix.
|
|
|
|
func stringsHasPrefix(s, prefix string) bool {
|
|
|
|
return len(s) >= len(prefix) && s[:len(prefix)] == prefix
|
|
|
|
}
|
|
|
|
|
|
|
|
// stringsEqualFold is strings.EqualFold, ASCII only. It reports whether s and t
|
|
|
|
// are equal, ASCII-case-insensitively.
|
|
|
|
func stringsEqualFold(s, t string) bool {
|
|
|
|
if len(s) != len(t) {
|
2015-04-16 15:33:25 -06:00
|
|
|
return false
|
|
|
|
}
|
2016-11-23 11:35:17 -07:00
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
if lowerASCII(s[i]) != lowerASCII(t[i]) {
|
2015-04-16 15:33:25 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func readFull(r io.Reader) (all []byte, err error) {
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
for {
|
|
|
|
n, err := r.Read(buf)
|
|
|
|
all = append(all, buf[:n]...)
|
|
|
|
if err == io.EOF {
|
|
|
|
return all, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-25 04:52:54 -06:00
|
|
|
|
|
|
|
// goDebugString returns the value of the named GODEBUG key.
|
|
|
|
// GODEBUG is of the form "key=val,key2=val2"
|
|
|
|
func goDebugString(key string) string {
|
|
|
|
s := os.Getenv("GODEBUG")
|
|
|
|
for i := 0; i < len(s)-len(key)-1; i++ {
|
|
|
|
if i > 0 && s[i-1] != ',' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
afterKey := s[i+len(key):]
|
|
|
|
if afterKey[0] != '=' || s[i:i+len(key)] != key {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
val := afterKey[1:]
|
|
|
|
for i, b := range val {
|
|
|
|
if b == ',' {
|
|
|
|
return val[:i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|