67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
|
package updateip
|
||
|
|
||
|
import (
|
||
|
"strings"
|
||
|
"unicode/utf8"
|
||
|
)
|
||
|
|
||
|
// CompressLabel returns the smallest possible subdomain label for the provided record against the domain.
|
||
|
func CompressLabel(domainName, recordName string) string {
|
||
|
domainName = UnFqdn(domainName)
|
||
|
recordName = UnFqdn(recordName)
|
||
|
if strings.EqualFold(domainName, recordName) {
|
||
|
return "@"
|
||
|
} else if i := indexFold(recordName, "."+domainName); i > -1 {
|
||
|
return recordName[:i]
|
||
|
}
|
||
|
return recordName
|
||
|
}
|
||
|
|
||
|
// ExapandLabel gives the full DNS label for the (abbreviated) record.
|
||
|
func ExapandLabel(domainName, recordName string) string {
|
||
|
if strings.EqualFold(domainName, recordName) {
|
||
|
return recordName
|
||
|
} else if IsFqdn(recordName) {
|
||
|
return recordName
|
||
|
} else if recordName == "@" {
|
||
|
return domainName
|
||
|
} else if i := strings.LastIndex(recordName, ".@"); i > -1 {
|
||
|
return recordName[:i] + "." + domainName
|
||
|
}
|
||
|
return recordName + "." + domainName
|
||
|
}
|
||
|
|
||
|
// indexFold returns the index of the first instance of substr in s (case insensitive), or -1 if substr is not present in s.
|
||
|
func indexFold(s, substr string) int {
|
||
|
ns := len(s)
|
||
|
nb := len(substr)
|
||
|
if ns < nb {
|
||
|
return -1
|
||
|
}
|
||
|
if nb == 0 {
|
||
|
return 0
|
||
|
}
|
||
|
if ns == nb {
|
||
|
if strings.EqualFold(s, substr) {
|
||
|
return 0
|
||
|
}
|
||
|
return -1
|
||
|
}
|
||
|
|
||
|
l := ns - nb
|
||
|
for i := 0; i <= l; {
|
||
|
src := s[i : i+nb]
|
||
|
if strings.EqualFold(src, substr) {
|
||
|
return i
|
||
|
}
|
||
|
_, z := utf8.DecodeRuneInString(src)
|
||
|
i += z
|
||
|
}
|
||
|
return -1
|
||
|
}
|
||
|
|
||
|
// hasSuffixFold Tests if the string s ends with the specified suffix (case insensitive).
|
||
|
func hasSuffixFold(s, suffix string) bool {
|
||
|
return len(s) >= len(suffix) && strings.EqualFold(s[len(s)-len(suffix):], suffix)
|
||
|
}
|