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.
5 // Simple file i/o and string manipulation, to avoid
6 // depending on strconv and bufio and strings.
21 func (f
*file
) close() { f
.file
.Close() }
23 func (f
*file
) getLineFromData() (s
string, ok
bool) {
26 for i
= 0; i
< len(data
); i
++ {
33 copy(data
[0:], data
[i
:])
38 if f
.atEOF
&& len(f
.data
) > 0 {
39 // EOF, return all we have
47 func (f
*file
) readLine() (s
string, ok
bool) {
48 if s
, ok
= f
.getLineFromData(); ok
{
51 if len(f
.data
) < cap(f
.data
) {
53 n
, err
:= io
.ReadFull(f
.file
, f
.data
[ln
:cap(f
.data
)])
55 f
.data
= f
.data
[0 : ln
+n
]
61 s
, ok
= f
.getLineFromData()
65 func open(name
string) (*file
, error
) {
66 fd
, err
:= os
.Open(name
)
70 return &file
{fd
, make([]byte, os
.Getpagesize())[0:0], false}, nil
73 func byteIndex(s
string, c
byte) int {
74 for i
:= 0; i
< len(s
); i
++ {
82 // Count occurrences in s of any bytes in t.
83 func countAnyByte(s
string, t
string) int {
85 for i
:= 0; i
< len(s
); i
++ {
86 if byteIndex(t
, s
[i
]) >= 0 {
93 // Split s at any bytes in t.
94 func splitAtBytes(s
string, t
string) []string {
95 a
:= make([]string, 1+countAnyByte(s
, t
))
98 for i
:= 0; i
< len(s
); i
++ {
99 if byteIndex(t
, s
[i
]) >= 0 {
101 a
[n
] = string(s
[last
:i
])
108 a
[n
] = string(s
[last
:])
114 func getFields(s
string) []string { return splitAtBytes(s
, " \r\t\n") }
116 // Bigger than we need, not too big to worry about overflow
119 // Decimal to integer starting at &s[i0].
120 // Returns number, new offset, success.
121 func dtoi(s
string, i0
int) (n
int, i
int, ok
bool) {
123 for i
= i0
; i
< len(s
) && '0' <= s
[i
] && s
[i
] <= '9'; i
++ {
124 n
= n
*10 + int(s
[i
]-'0')
135 // Hexadecimal to integer starting at &s[i0].
136 // Returns number, new offset, success.
137 func xtoi(s
string, i0
int) (n
int, i
int, ok
bool) {
139 for i
= i0
; i
< len(s
); i
++ {
140 if '0' <= s
[i
] && s
[i
] <= '9' {
143 } else if 'a' <= s
[i
] && s
[i
] <= 'f' {
145 n
+= int(s
[i
]-'a') + 10
146 } else if 'A' <= s
[i
] && s
[i
] <= 'F' {
148 n
+= int(s
[i
]-'A') + 10
162 // 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.
166 func xtoi2(s
string, e
byte) (byte, bool) {
167 if len(s
) > 2 && s
[2] != e
{
170 n
, ei
, ok
:= xtoi(s
[:2], 0)
171 return byte(n
), ok
&& ei
== 2
174 // Integer to decimal.
175 func itoa(i
int) string {
184 for ui
> 0 || n
== len(buf
) {
186 buf
[n
] = byte('0' + ui%10
)
193 return string(buf
[n
:])
196 // Convert i to decimal string.
197 func itod(i
uint) string {
202 // Assemble decimal in reverse order.
205 for ; i
> 0; i
/= 10 {
207 b
[bp
] = byte(i%10
) + '0'
210 return string(b
[bp
:])
213 // Convert i to hexadecimal string.
214 func itox(i
uint, min
int) string {
215 // Assemble hexadecimal in reverse order.
218 for ; i
> 0 || min
> 0; i
/= 16 {
220 b
[bp
] = "0123456789abcdef"[byte(i%16
)]
224 return string(b
[bp
:])
227 // Number of occurrences of b in s.
228 func count(s
string, b
byte) int {
230 for i
:= 0; i
< len(s
); i
++ {
238 // Index of rightmost occurrence of b in s.
239 func last(s
string, b
byte) int {
241 for i
--; i
>= 0; i
-- {