| 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 |
|
| 5 |
// Simple file i/o and string manipulation, to avoid |
| 6 |
// depending on strconv and bufio and strings. |
| 7 |
|
| 8 |
package net |
| 9 |
|
| 10 |
/////////////////////////////////////////////////////////////////////////////// |
| 11 |
// NOTE: This file was forked because it is used by other code that needed to |
| 12 |
// be forked, not because it is used on its own. |
| 13 |
/////////////////////////////////////////////////////////////////////////////// |
| 14 |
|
| 15 |
// Bigger than we need, not too big to worry about overflow |
| 16 |
const big = 0xFFFFFF |
| 17 |
|
| 18 |
// Decimal to integer. |
| 19 |
// Returns number, characters consumed, success. |
| 20 |
func dtoi(s string) (n int, i int, ok bool) { |
| 21 |
n = 0 |
| 22 |
for i = 0; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ { |
| 23 |
n = n*10 + int(s[i]-'0') |
| 24 |
if n >= big { |
| 25 |
return big, i, false |
| 26 |
} |
| 27 |
} |
| 28 |
if i == 0 { |
| 29 |
return 0, 0, false |
| 30 |
} |
| 31 |
return n, i, true |
| 32 |
} |
| 33 |
|
| 34 |
// Hexadecimal to integer. |
| 35 |
// Returns number, characters consumed, success. |
| 36 |
func xtoi(s string) (n int, i int, ok bool) { |
| 37 |
n = 0 |
| 38 |
for i = 0; i < len(s); i++ { |
| 39 |
if '0' <= s[i] && s[i] <= '9' { |
| 40 |
n *= 16 |
| 41 |
n += int(s[i] - '0') |
| 42 |
} else if 'a' <= s[i] && s[i] <= 'f' { |
| 43 |
n *= 16 |
| 44 |
n += int(s[i]-'a') + 10 |
| 45 |
} else if 'A' <= s[i] && s[i] <= 'F' { |
| 46 |
n *= 16 |
| 47 |
n += int(s[i]-'A') + 10 |
| 48 |
} else { |
| 49 |
break |
| 50 |
} |
| 51 |
if n >= big { |
| 52 |
return 0, i, false |
| 53 |
} |
| 54 |
} |
| 55 |
if i == 0 { |
| 56 |
return 0, i, false |
| 57 |
} |
| 58 |
return n, i, true |
| 59 |
} |