Add LenLabels function - no allocations

This commit is contained in:
Miek Gieben 2012-07-16 08:51:39 +02:00
parent 6e31effe78
commit 393719d659
1 changed files with 27 additions and 0 deletions

View File

@ -38,6 +38,9 @@ func SplitLabels(s string) []string {
//
// www.miek.nl. and miek.nl. have two labels in common: miek and nl
// www.miek.nl. and www.bla.nl. have one label in common: nl
//
// Domain s1 is a subdomain of s2 if
// CompareLabels(s1, s2) == LenLabels(s1)
func CompareLabels(s1, s2 string) (n int) {
l1 := SplitLabels(s1)
l2 := SplitLabels(s2)
@ -58,3 +61,27 @@ func CompareLabels(s1, s2 string) (n int) {
}
return
}
// LenLabels returns the number of labels in a domain name
func LenLabels(s string) (labels int) {
if s == "." {
return
}
last := byte('.')
lastlast := byte('.')
s = Fqdn(s) // Make fully qualified
for i := 0; i < len(s); i++ {
if s[i] == '.' {
if last == '\\' {
if lastlast != '\\' {
// do nothing
continue
}
}
labels++
}
lastlast = last
last = s[i]
}
return
}