github.com/opentofu/opentofu@v1.7.1/internal/ipaddr/parse.go (about) 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 ipaddr 9 10 // Bigger than we need, not too big to worry about overflow 11 const big = 0xFFFFFF 12 13 // Decimal to integer. 14 // Returns number, characters consumed, success. 15 func dtoi(s string) (n int, i int, ok bool) { 16 n = 0 17 for i = 0; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ { 18 n = n*10 + int(s[i]-'0') 19 if n >= big { 20 return big, i, false 21 } 22 } 23 if i == 0 { 24 return 0, 0, false 25 } 26 return n, i, true 27 } 28 29 // Hexadecimal to integer. 30 // Returns number, characters consumed, success. 31 func xtoi(s string) (n int, i int, ok bool) { 32 n = 0 33 for i = 0; i < len(s); i++ { 34 if '0' <= s[i] && s[i] <= '9' { 35 n *= 16 36 n += int(s[i] - '0') 37 } else if 'a' <= s[i] && s[i] <= 'f' { 38 n *= 16 39 n += int(s[i]-'a') + 10 40 } else if 'A' <= s[i] && s[i] <= 'F' { 41 n *= 16 42 n += int(s[i]-'A') + 10 43 } else { 44 break 45 } 46 if n >= big { 47 return 0, i, false 48 } 49 } 50 if i == 0 { 51 return 0, i, false 52 } 53 return n, i, true 54 }