parse.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // 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. // Simple file i/o and string manipulation, to avoid
  5. // depending on strconv and bufio and strings.
  6. package net
  7. import (
  8. "internal/bytealg"
  9. "io"
  10. "os"
  11. "time"
  12. )
  13. type file struct {
  14. file *os.File
  15. data []byte
  16. atEOF bool
  17. }
  18. func (f *file) close() { f.file.Close() }
  19. func (f *file) getLineFromData() (s string, ok bool) {
  20. data := f.data
  21. i := 0
  22. for i = 0; i < len(data); i++ {
  23. if data[i] == '\n' {
  24. s = string(data[0:i])
  25. ok = true
  26. // move data
  27. i++
  28. n := len(data) - i
  29. copy(data[0:], data[i:])
  30. f.data = data[0:n]
  31. return
  32. }
  33. }
  34. if f.atEOF && len(f.data) > 0 {
  35. // EOF, return all we have
  36. s = string(data)
  37. f.data = f.data[0:0]
  38. ok = true
  39. }
  40. return
  41. }
  42. func (f *file) readLine() (s string, ok bool) {
  43. if s, ok = f.getLineFromData(); ok {
  44. return
  45. }
  46. if len(f.data) < cap(f.data) {
  47. ln := len(f.data)
  48. n, err := io.ReadFull(f.file, f.data[ln:cap(f.data)])
  49. if n >= 0 {
  50. f.data = f.data[0 : ln+n]
  51. }
  52. if err == io.EOF || err == io.ErrUnexpectedEOF {
  53. f.atEOF = true
  54. }
  55. }
  56. s, ok = f.getLineFromData()
  57. return
  58. }
  59. func open(name string) (*file, error) {
  60. fd, err := os.Open(name)
  61. if err != nil {
  62. return nil, err
  63. }
  64. return &file{fd, make([]byte, 0, 64*1024), false}, nil
  65. }
  66. func stat(name string) (mtime time.Time, size int64, err error) {
  67. st, err := os.Stat(name)
  68. if err != nil {
  69. return time.Time{}, 0, err
  70. }
  71. return st.ModTime(), st.Size(), nil
  72. }
  73. // Count occurrences in s of any bytes in t.
  74. func countAnyByte(s string, t string) int {
  75. n := 0
  76. for i := 0; i < len(s); i++ {
  77. if bytealg.IndexByteString(t, s[i]) >= 0 {
  78. n++
  79. }
  80. }
  81. return n
  82. }
  83. // Split s at any bytes in t.
  84. func splitAtBytes(s string, t string) []string {
  85. a := make([]string, 1+countAnyByte(s, t))
  86. n := 0
  87. last := 0
  88. for i := 0; i < len(s); i++ {
  89. if bytealg.IndexByteString(t, s[i]) >= 0 {
  90. if last < i {
  91. a[n] = s[last:i]
  92. n++
  93. }
  94. last = i + 1
  95. }
  96. }
  97. if last < len(s) {
  98. a[n] = s[last:]
  99. n++
  100. }
  101. return a[0:n]
  102. }
  103. func getFields(s string) []string { return splitAtBytes(s, " \r\t\n") }
  104. // Bigger than we need, not too big to worry about overflow
  105. const big = 0xFFFFFF
  106. // Decimal to integer.
  107. // Returns number, characters consumed, success.
  108. func dtoi(s string) (n int, i int, ok bool) {
  109. n = 0
  110. for i = 0; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ {
  111. n = n*10 + int(s[i]-'0')
  112. if n >= big {
  113. return big, i, false
  114. }
  115. }
  116. if i == 0 {
  117. return 0, 0, false
  118. }
  119. return n, i, true
  120. }
  121. // Hexadecimal to integer.
  122. // Returns number, characters consumed, success.
  123. func xtoi(s string) (n int, i int, ok bool) {
  124. n = 0
  125. for i = 0; i < len(s); i++ {
  126. if '0' <= s[i] && s[i] <= '9' {
  127. n *= 16
  128. n += int(s[i] - '0')
  129. } else if 'a' <= s[i] && s[i] <= 'f' {
  130. n *= 16
  131. n += int(s[i]-'a') + 10
  132. } else if 'A' <= s[i] && s[i] <= 'F' {
  133. n *= 16
  134. n += int(s[i]-'A') + 10
  135. } else {
  136. break
  137. }
  138. if n >= big {
  139. return 0, i, false
  140. }
  141. }
  142. if i == 0 {
  143. return 0, i, false
  144. }
  145. return n, i, true
  146. }
  147. // xtoi2 converts the next two hex digits of s into a byte.
  148. // If s is longer than 2 bytes then the third byte must be e.
  149. // If the first two bytes of s are not hex digits or the third byte
  150. // does not match e, false is returned.
  151. func xtoi2(s string, e byte) (byte, bool) {
  152. if len(s) > 2 && s[2] != e {
  153. return 0, false
  154. }
  155. n, ei, ok := xtoi(s[:2])
  156. return byte(n), ok && ei == 2
  157. }
  158. // Convert i to a hexadecimal string. Leading zeros are not printed.
  159. func appendHex(dst []byte, i uint32) []byte {
  160. if i == 0 {
  161. return append(dst, '0')
  162. }
  163. for j := 7; j >= 0; j-- {
  164. v := i >> uint(j*4)
  165. if v > 0 {
  166. dst = append(dst, hexDigit[v&0xf])
  167. }
  168. }
  169. return dst
  170. }
  171. // Number of occurrences of b in s.
  172. func count(s string, b byte) int {
  173. n := 0
  174. for i := 0; i < len(s); i++ {
  175. if s[i] == b {
  176. n++
  177. }
  178. }
  179. return n
  180. }
  181. // Index of rightmost occurrence of b in s.
  182. func last(s string, b byte) int {
  183. i := len(s)
  184. for i--; i >= 0; i-- {
  185. if s[i] == b {
  186. break
  187. }
  188. }
  189. return i
  190. }
  191. // hasUpperCase tells whether the given string contains at least one upper-case.
  192. func hasUpperCase(s string) bool {
  193. for i := range s {
  194. if 'A' <= s[i] && s[i] <= 'Z' {
  195. return true
  196. }
  197. }
  198. return false
  199. }
  200. // lowerASCIIBytes makes x ASCII lowercase in-place.
  201. func lowerASCIIBytes(x []byte) {
  202. for i, b := range x {
  203. if 'A' <= b && b <= 'Z' {
  204. x[i] += 'a' - 'A'
  205. }
  206. }
  207. }
  208. // lowerASCII returns the ASCII lowercase version of b.
  209. func lowerASCII(b byte) byte {
  210. if 'A' <= b && b <= 'Z' {
  211. return b + ('a' - 'A')
  212. }
  213. return b
  214. }
  215. // trimSpace returns x without any leading or trailing ASCII whitespace.
  216. func trimSpace(x []byte) []byte {
  217. for len(x) > 0 && isSpace(x[0]) {
  218. x = x[1:]
  219. }
  220. for len(x) > 0 && isSpace(x[len(x)-1]) {
  221. x = x[:len(x)-1]
  222. }
  223. return x
  224. }
  225. // isSpace reports whether b is an ASCII space character.
  226. func isSpace(b byte) bool {
  227. return b == ' ' || b == '\t' || b == '\n' || b == '\r'
  228. }
  229. // removeComment returns line, removing any '#' byte and any following
  230. // bytes.
  231. func removeComment(line []byte) []byte {
  232. if i := bytealg.IndexByte(line, '#'); i != -1 {
  233. return line[:i]
  234. }
  235. return line
  236. }
  237. // foreachLine runs fn on each line of x.
  238. // Each line (except for possibly the last) ends in '\n'.
  239. // It returns the first non-nil error returned by fn.
  240. func foreachLine(x []byte, fn func(line []byte) error) error {
  241. for len(x) > 0 {
  242. nl := bytealg.IndexByte(x, '\n')
  243. if nl == -1 {
  244. return fn(x)
  245. }
  246. line := x[:nl+1]
  247. x = x[nl+1:]
  248. if err := fn(line); err != nil {
  249. return err
  250. }
  251. }
  252. return nil
  253. }
  254. // foreachField runs fn on each non-empty run of non-space bytes in x.
  255. // It returns the first non-nil error returned by fn.
  256. func foreachField(x []byte, fn func(field []byte) error) error {
  257. x = trimSpace(x)
  258. for len(x) > 0 {
  259. sp := bytealg.IndexByte(x, ' ')
  260. if sp == -1 {
  261. return fn(x)
  262. }
  263. if field := trimSpace(x[:sp]); len(field) > 0 {
  264. if err := fn(field); err != nil {
  265. return err
  266. }
  267. }
  268. x = trimSpace(x[sp+1:])
  269. }
  270. return nil
  271. }
  272. // stringsHasSuffix is strings.HasSuffix. It reports whether s ends in
  273. // suffix.
  274. func stringsHasSuffix(s, suffix string) bool {
  275. return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix
  276. }
  277. // stringsHasSuffixFold reports whether s ends in suffix,
  278. // ASCII-case-insensitively.
  279. func stringsHasSuffixFold(s, suffix string) bool {
  280. return len(s) >= len(suffix) && stringsEqualFold(s[len(s)-len(suffix):], suffix)
  281. }
  282. // stringsHasPrefix is strings.HasPrefix. It reports whether s begins with prefix.
  283. func stringsHasPrefix(s, prefix string) bool {
  284. return len(s) >= len(prefix) && s[:len(prefix)] == prefix
  285. }
  286. // stringsEqualFold is strings.EqualFold, ASCII only. It reports whether s and t
  287. // are equal, ASCII-case-insensitively.
  288. func stringsEqualFold(s, t string) bool {
  289. if len(s) != len(t) {
  290. return false
  291. }
  292. for i := 0; i < len(s); i++ {
  293. if lowerASCII(s[i]) != lowerASCII(t[i]) {
  294. return false
  295. }
  296. }
  297. return true
  298. }
  299. func readFull(r io.Reader) (all []byte, err error) {
  300. buf := make([]byte, 1024)
  301. for {
  302. n, err := r.Read(buf)
  303. all = append(all, buf[:n]...)
  304. if err == io.EOF {
  305. return all, nil
  306. }
  307. if err != nil {
  308. return nil, err
  309. }
  310. }
  311. }