blob: c72e1c2eaf0150e3ec8cced64bc698d091ae25d1 [file] [log] [blame]
Russ Cox83348f92008-12-18 15:42:39 -08001// Copyright 2009 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// Simple file i/o and string manipulation, to avoid
Russ Cox35ace1d2009-11-01 11:15:34 -08006// depending on strconv and bufio and strings.
Russ Cox83348f92008-12-18 15:42:39 -08007
8package net
9
10import (
Robert Griesemera3d10452009-12-15 15:35:38 -080011 "io"
12 "os"
Russ Cox83348f92008-12-18 15:42:39 -080013)
14
Russ Coxd8921c52009-02-15 14:18:39 -080015type file struct {
Christopher Wedgwoodbe9f6342010-04-29 11:01:21 -070016 file *os.File
17 data []byte
18 atEOF bool
Russ Cox83348f92008-12-18 15:42:39 -080019}
20
Robert Griesemera3d10452009-12-15 15:35:38 -080021func (f *file) close() { f.file.Close() }
Russ Cox83348f92008-12-18 15:42:39 -080022
Russ Coxd8921c52009-02-15 14:18:39 -080023func (f *file) getLineFromData() (s string, ok bool) {
Robert Griesemera3d10452009-12-15 15:35:38 -080024 data := f.data
Christopher Wedgwoodbe9f6342010-04-29 11:01:21 -070025 i := 0
26 for i = 0; i < len(data); i++ {
Russ Cox83348f92008-12-18 15:42:39 -080027 if data[i] == '\n' {
Robert Griesemera3d10452009-12-15 15:35:38 -080028 s = string(data[0:i])
29 ok = true
Russ Cox83348f92008-12-18 15:42:39 -080030 // move data
Robert Griesemera3d10452009-12-15 15:35:38 -080031 i++
32 n := len(data) - i
Christopher Wedgwoodbe9f6342010-04-29 11:01:21 -070033 copy(data[0:], data[i:])
Robert Griesemera3d10452009-12-15 15:35:38 -080034 f.data = data[0:n]
35 return
Russ Cox83348f92008-12-18 15:42:39 -080036 }
37 }
Christopher Wedgwoodbe9f6342010-04-29 11:01:21 -070038 if f.atEOF && len(f.data) > 0 {
39 // EOF, return all we have
Russ Cox7c77e452010-04-28 19:36:04 -070040 s = string(data)
Christopher Wedgwoodbe9f6342010-04-29 11:01:21 -070041 f.data = f.data[0:0]
Russ Cox7c77e452010-04-28 19:36:04 -070042 ok = true
43 }
Robert Griesemera3d10452009-12-15 15:35:38 -080044 return
Russ Cox83348f92008-12-18 15:42:39 -080045}
46
Russ Coxd8921c52009-02-15 14:18:39 -080047func (f *file) readLine() (s string, ok bool) {
48 if s, ok = f.getLineFromData(); ok {
Robert Griesemer40621d52009-11-09 12:07:39 -080049 return
Russ Cox83348f92008-12-18 15:42:39 -080050 }
51 if len(f.data) < cap(f.data) {
Robert Griesemera3d10452009-12-15 15:35:38 -080052 ln := len(f.data)
Christopher Wedgwoodbe9f6342010-04-29 11:01:21 -070053 n, err := io.ReadFull(f.file, f.data[ln:cap(f.data)])
Russ Cox83348f92008-12-18 15:42:39 -080054 if n >= 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -080055 f.data = f.data[0 : ln+n]
Russ Cox83348f92008-12-18 15:42:39 -080056 }
Josh Bleecher Snyder56447742013-10-28 19:31:25 -040057 if err == io.EOF || err == io.ErrUnexpectedEOF {
Christopher Wedgwoodbe9f6342010-04-29 11:01:21 -070058 f.atEOF = true
59 }
Russ Cox83348f92008-12-18 15:42:39 -080060 }
Robert Griesemera3d10452009-12-15 15:35:38 -080061 s, ok = f.getLineFromData()
62 return
Russ Cox83348f92008-12-18 15:42:39 -080063}
64
Russ Coxeb692922011-11-01 22:05:34 -040065func open(name string) (*file, error) {
Rob Pike8a90fd32011-04-04 23:42:14 -070066 fd, err := os.Open(name)
Russ Cox83348f92008-12-18 15:42:39 -080067 if err != nil {
Robert Griesemer40621d52009-11-09 12:07:39 -080068 return nil, err
Russ Cox83348f92008-12-18 15:42:39 -080069 }
Josh Bleecher Snyder563d0b62013-12-12 10:13:17 +040070 return &file{fd, make([]byte, 0, os.Getpagesize()), false}, nil
Russ Cox83348f92008-12-18 15:42:39 -080071}
72
Russ Coxd8921c52009-02-15 14:18:39 -080073func byteIndex(s string, c byte) int {
Russ Cox83348f92008-12-18 15:42:39 -080074 for i := 0; i < len(s); i++ {
75 if s[i] == c {
Robert Griesemer40621d52009-11-09 12:07:39 -080076 return i
Russ Cox83348f92008-12-18 15:42:39 -080077 }
78 }
Robert Griesemera3d10452009-12-15 15:35:38 -080079 return -1
Russ Cox83348f92008-12-18 15:42:39 -080080}
81
82// Count occurrences in s of any bytes in t.
Russ Coxd8921c52009-02-15 14:18:39 -080083func countAnyByte(s string, t string) int {
Robert Griesemera3d10452009-12-15 15:35:38 -080084 n := 0
Russ Cox83348f92008-12-18 15:42:39 -080085 for i := 0; i < len(s); i++ {
Russ Coxd8921c52009-02-15 14:18:39 -080086 if byteIndex(t, s[i]) >= 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -080087 n++
Russ Cox83348f92008-12-18 15:42:39 -080088 }
89 }
Robert Griesemera3d10452009-12-15 15:35:38 -080090 return n
Russ Cox83348f92008-12-18 15:42:39 -080091}
92
93// Split s at any bytes in t.
Russ Coxd8921c52009-02-15 14:18:39 -080094func splitAtBytes(s string, t string) []string {
Robert Griesemera3d10452009-12-15 15:35:38 -080095 a := make([]string, 1+countAnyByte(s, t))
96 n := 0
97 last := 0
Russ Cox83348f92008-12-18 15:42:39 -080098 for i := 0; i < len(s); i++ {
Russ Coxd8921c52009-02-15 14:18:39 -080099 if byteIndex(t, s[i]) >= 0 {
Russ Cox83348f92008-12-18 15:42:39 -0800100 if last < i {
Robert Griesemera3d10452009-12-15 15:35:38 -0800101 a[n] = string(s[last:i])
102 n++
Russ Cox83348f92008-12-18 15:42:39 -0800103 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800104 last = i + 1
Russ Cox83348f92008-12-18 15:42:39 -0800105 }
106 }
107 if last < len(s) {
Robert Griesemera3d10452009-12-15 15:35:38 -0800108 a[n] = string(s[last:])
109 n++
Russ Cox83348f92008-12-18 15:42:39 -0800110 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800111 return a[0:n]
Russ Cox83348f92008-12-18 15:42:39 -0800112}
113
Robert Griesemera3d10452009-12-15 15:35:38 -0800114func getFields(s string) []string { return splitAtBytes(s, " \r\t\n") }
Russ Cox83348f92008-12-18 15:42:39 -0800115
116// Bigger than we need, not too big to worry about overflow
Russ Coxd8921c52009-02-15 14:18:39 -0800117const big = 0xFFFFFF
Russ Cox83348f92008-12-18 15:42:39 -0800118
119// Decimal to integer starting at &s[i0].
120// Returns number, new offset, success.
Russ Coxd8921c52009-02-15 14:18:39 -0800121func dtoi(s string, i0 int) (n int, i int, ok bool) {
Robert Griesemera3d10452009-12-15 15:35:38 -0800122 n = 0
Russ Cox83348f92008-12-18 15:42:39 -0800123 for i = i0; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ {
Robert Griesemera3d10452009-12-15 15:35:38 -0800124 n = n*10 + int(s[i]-'0')
Russ Coxd8921c52009-02-15 14:18:39 -0800125 if n >= big {
Robert Griesemer40621d52009-11-09 12:07:39 -0800126 return 0, i, false
Russ Cox83348f92008-12-18 15:42:39 -0800127 }
128 }
129 if i == i0 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800130 return 0, i, false
Russ Cox83348f92008-12-18 15:42:39 -0800131 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800132 return n, i, true
Russ Cox83348f92008-12-18 15:42:39 -0800133}
134
135// Hexadecimal to integer starting at &s[i0].
136// Returns number, new offset, success.
Russ Coxd8921c52009-02-15 14:18:39 -0800137func xtoi(s string, i0 int) (n int, i int, ok bool) {
Robert Griesemera3d10452009-12-15 15:35:38 -0800138 n = 0
Russ Cox83348f92008-12-18 15:42:39 -0800139 for i = i0; i < len(s); i++ {
140 if '0' <= s[i] && s[i] <= '9' {
Robert Griesemera3d10452009-12-15 15:35:38 -0800141 n *= 16
142 n += int(s[i] - '0')
Russ Cox83348f92008-12-18 15:42:39 -0800143 } else if 'a' <= s[i] && s[i] <= 'f' {
Robert Griesemera3d10452009-12-15 15:35:38 -0800144 n *= 16
145 n += int(s[i]-'a') + 10
Russ Cox83348f92008-12-18 15:42:39 -0800146 } else if 'A' <= s[i] && s[i] <= 'F' {
Robert Griesemera3d10452009-12-15 15:35:38 -0800147 n *= 16
148 n += int(s[i]-'A') + 10
Russ Cox83348f92008-12-18 15:42:39 -0800149 } else {
Robert Griesemer40621d52009-11-09 12:07:39 -0800150 break
Russ Cox83348f92008-12-18 15:42:39 -0800151 }
Russ Coxd8921c52009-02-15 14:18:39 -0800152 if n >= big {
Robert Griesemer40621d52009-11-09 12:07:39 -0800153 return 0, i, false
Russ Cox83348f92008-12-18 15:42:39 -0800154 }
155 }
156 if i == i0 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800157 return 0, i, false
Russ Cox83348f92008-12-18 15:42:39 -0800158 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800159 return n, i, true
Russ Cox83348f92008-12-18 15:42:39 -0800160}
161
Mikio Hara825f8c12011-08-25 19:22:46 -0400162// xtoi2 converts the next two hex digits of s into a byte.
163// If s is longer than 2 bytes then the third byte must be e.
164// If the first two bytes of s are not hex digits or the third byte
165// does not match e, false is returned.
166func xtoi2(s string, e byte) (byte, bool) {
167 if len(s) > 2 && s[2] != e {
168 return 0, false
169 }
170 n, ei, ok := xtoi(s[:2], 0)
171 return byte(n), ok && ei == 2
172}
173
Martin Möhrmann494b4ce2014-12-31 18:45:05 +0100174// Convert integer to decimal string.
175func itoa(val int) string {
176 if val < 0 {
177 return "-" + uitoa(uint(-val))
Russ Cox35ace1d2009-11-01 11:15:34 -0800178 }
Martin Möhrmann494b4ce2014-12-31 18:45:05 +0100179 return uitoa(uint(val))
Russ Cox35ace1d2009-11-01 11:15:34 -0800180}
181
Martin Möhrmann494b4ce2014-12-31 18:45:05 +0100182// Convert unsigned integer to decimal string.
183func uitoa(val uint) string {
184 if val == 0 { // avoid string allocation
Mikio Hara825f8c12011-08-25 19:22:46 -0400185 return "0"
186 }
Martin Möhrmann494b4ce2014-12-31 18:45:05 +0100187 var buf [20]byte // big enough for 64bit value base 10
188 i := len(buf) - 1
189 for val >= 10 {
190 q := val / 10
191 buf[i] = byte('0' + val - q*10)
192 i--
193 val = q
Mikio Hara825f8c12011-08-25 19:22:46 -0400194 }
Martin Möhrmann494b4ce2014-12-31 18:45:05 +0100195 // val < 10
196 buf[i] = byte('0' + val)
197 return string(buf[i:])
Mikio Hara825f8c12011-08-25 19:22:46 -0400198}
199
Rui Ueyamaf7c99f32014-06-11 20:40:00 -0700200// Convert i to a hexadecimal string. Leading zeros are not printed.
201func appendHex(dst []byte, i uint32) []byte {
202 if i == 0 {
203 return append(dst, '0')
Mikio Hara825f8c12011-08-25 19:22:46 -0400204 }
Rui Ueyamaf7c99f32014-06-11 20:40:00 -0700205 for j := 7; j >= 0; j-- {
206 v := i >> uint(j*4)
207 if v > 0 {
208 dst = append(dst, hexDigit[v&0xf])
209 }
210 }
211 return dst
Mikio Hara825f8c12011-08-25 19:22:46 -0400212}
213
Russ Cox35ace1d2009-11-01 11:15:34 -0800214// Number of occurrences of b in s.
215func count(s string, b byte) int {
Robert Griesemera3d10452009-12-15 15:35:38 -0800216 n := 0
Russ Cox35ace1d2009-11-01 11:15:34 -0800217 for i := 0; i < len(s); i++ {
218 if s[i] == b {
Robert Griesemer40621d52009-11-09 12:07:39 -0800219 n++
Russ Cox35ace1d2009-11-01 11:15:34 -0800220 }
221 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800222 return n
Russ Cox35ace1d2009-11-01 11:15:34 -0800223}
224
225// Index of rightmost occurrence of b in s.
226func last(s string, b byte) int {
Robert Griesemera3d10452009-12-15 15:35:38 -0800227 i := len(s)
Russ Cox35ace1d2009-11-01 11:15:34 -0800228 for i--; i >= 0; i-- {
229 if s[i] == b {
Robert Griesemer40621d52009-11-09 12:07:39 -0800230 break
Russ Cox35ace1d2009-11-01 11:15:34 -0800231 }
232 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800233 return i
Russ Cox35ace1d2009-11-01 11:15:34 -0800234}
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -0700235
236// lowerASCIIBytes makes x ASCII lowercase in-place.
237func lowerASCIIBytes(x []byte) {
238 for i, b := range x {
239 if 'A' <= b && b <= 'Z' {
240 x[i] += 'a' - 'A'
241 }
242 }
243}
244
245// lowerASCII returns the ASCII lowercase version of b.
246func lowerASCII(b byte) byte {
247 if 'A' <= b && b <= 'Z' {
248 return b + ('a' - 'A')
249 }
250 return b
251}
252
253// trimSpace returns x without any leading or trailing ASCII whitespace.
254func trimSpace(x []byte) []byte {
255 for len(x) > 0 && isSpace(x[0]) {
256 x = x[1:]
257 }
258 for len(x) > 0 && isSpace(x[len(x)-1]) {
259 x = x[:len(x)-1]
260 }
261 return x
262}
263
264// isSpace reports whether b is an ASCII space character.
265func isSpace(b byte) bool {
266 return b == ' ' || b == '\t' || b == '\n' || b == '\r'
267}
268
269// removeComment returns line, removing any '#' byte and any following
270// bytes.
271func removeComment(line []byte) []byte {
272 if i := bytesIndexByte(line, '#'); i != -1 {
273 return line[:i]
274 }
275 return line
276}
277
278// foreachLine runs fn on each line of x.
279// Each line (except for possibly the last) ends in '\n'.
280// It returns the first non-nil error returned by fn.
281func foreachLine(x []byte, fn func(line []byte) error) error {
282 for len(x) > 0 {
283 nl := bytesIndexByte(x, '\n')
284 if nl == -1 {
285 return fn(x)
286 }
287 line := x[:nl+1]
288 x = x[nl+1:]
289 if err := fn(line); err != nil {
290 return err
291 }
292 }
293 return nil
294}
295
296// foreachField runs fn on each non-empty run of non-space bytes in x.
297// It returns the first non-nil error returned by fn.
298func foreachField(x []byte, fn func(field []byte) error) error {
299 x = trimSpace(x)
300 for len(x) > 0 {
301 sp := bytesIndexByte(x, ' ')
302 if sp == -1 {
303 return fn(x)
304 }
305 if field := trimSpace(x[:sp]); len(field) > 0 {
306 if err := fn(field); err != nil {
307 return err
308 }
309 }
310 x = trimSpace(x[sp+1:])
311 }
312 return nil
313}
314
315// bytesIndexByte is bytes.IndexByte. It returns the index of the
316// first instance of c in s, or -1 if c is not present in s.
317func bytesIndexByte(s []byte, c byte) int {
318 for i, b := range s {
319 if b == c {
320 return i
321 }
322 }
323 return -1
324}
325
326// stringsHasSuffix is strings.HasSuffix. It reports whether s ends in
327// suffix.
328func stringsHasSuffix(s, suffix string) bool {
329 return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix
330}
331
332// stringsHasSuffixFold reports whether s ends in suffix,
333// ASCII-case-insensitively.
334func stringsHasSuffixFold(s, suffix string) bool {
335 if len(suffix) > len(s) {
336 return false
337 }
338 for i := 0; i < len(suffix); i++ {
339 if lowerASCII(suffix[i]) != lowerASCII(s[len(s)-len(suffix)+i]) {
340 return false
341 }
342 }
343 return true
344}
345
346// stringsHasPrefix is strings.HasPrefix. It reports whether s begins with prefix.
347func stringsHasPrefix(s, prefix string) bool {
348 return len(s) >= len(prefix) && s[:len(prefix)] == prefix
349}
350
351func readFull(r io.Reader) (all []byte, err error) {
352 buf := make([]byte, 1024)
353 for {
354 n, err := r.Read(buf)
355 all = append(all, buf[:n]...)
356 if err == io.EOF {
357 return all, nil
358 }
359 if err != nil {
360 return nil, err
361 }
362 }
363}
Brad Fitzpatrickb615ad82015-06-25 12:52:54 +0200364
365// goDebugString returns the value of the named GODEBUG key.
366// GODEBUG is of the form "key=val,key2=val2"
367func goDebugString(key string) string {
368 s := os.Getenv("GODEBUG")
369 for i := 0; i < len(s)-len(key)-1; i++ {
370 if i > 0 && s[i-1] != ',' {
371 continue
372 }
373 afterKey := s[i+len(key):]
374 if afterKey[0] != '=' || s[i:i+len(key)] != key {
375 continue
376 }
377 val := afterKey[1:]
378 for i, b := range val {
379 if b == ',' {
380 return val[:i]
381 }
382 }
383 return val
384 }
385 return ""
386}