mirror of
https://github.com/opentffoundation/opentf.git
synced 2026-03-23 13:00:40 -04:00
The code in this package is all snapshot from the Go codebase in older versions, inlined here to allow OpenTofu's cidr-calculation-related functions to preserve their original behavior despite upstream changing the parsing rules in a breaking way. This code is intentionally modified as little as possible from the upstream code it was derived from. We are imposing on ourselves considerably stricter style conventions than the Go project follows and so we need to disable various linters for this package to allow this code to remain written in the Go idiomatic style, rather than in OpenTofu's stricter local style. In particular, we've chosen to prohibit ourselves from using named return values or package-global variables, despite those both being typical in the standard library and in other codebases. Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
// 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.
|
|
|
|
//nolint:nonamedreturns,mnd // This file is copied from the Go codebase and intended to remain close to the original in case we need to backport changes.
|
|
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
|
|
}
|