| // 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 |
| // depending on strconv and bufio and strings. |
| |
| package ipaddr |
| |
| // Bigger than we need, not too big to worry about overflow |
| const big = 0xFFFFFF |
| |
| // Decimal to integer. |
| // Returns number, characters consumed, success. |
| func dtoi(s string) (n int, i int, ok bool) { |
| n = 0 |
| for i = 0; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ { |
| n = n*10 + int(s[i]-'0') |
| if n >= big { |
| return big, i, false |
| } |
| } |
| if i == 0 { |
| return 0, 0, false |
| } |
| return n, i, true |
| } |
| |
| // Hexadecimal to integer. |
| // Returns number, characters consumed, success. |
| func xtoi(s string) (n int, i int, ok bool) { |
| n = 0 |
| for i = 0; i < len(s); i++ { |
| if '0' <= s[i] && s[i] <= '9' { |
| n *= 16 |
| n += int(s[i] - '0') |
| } else if 'a' <= s[i] && s[i] <= 'f' { |
| n *= 16 |
| n += int(s[i]-'a') + 10 |
| } else if 'A' <= s[i] && s[i] <= 'F' { |
| n *= 16 |
| n += int(s[i]-'A') + 10 |
| } else { |
| break |
| } |
| if n >= big { |
| return 0, i, false |
| } |
| } |
| if i == 0 { |
| return 0, i, false |
| } |
| return n, i, true |
| } |