Make SplitLabels use Split

This commit is contained in:
Miek Gieben 2013-06-21 15:17:12 +00:00
parent 939abbfea7
commit 9640d43c82
2 changed files with 22 additions and 23 deletions

View File

@ -10,31 +10,24 @@ package dns
// www.miek.nl. returns []string{"www", "miek", "nl"} // www.miek.nl. returns []string{"www", "miek", "nl"}
// The root label (.) returns nil. // The root label (.) returns nil.
func SplitLabels(s string) []string { func SplitLabels(s string) []string {
// TODO(miek): make this use Split idx := Split(s)
if s == "." { switch len(idx) {
case 0:
return nil return nil
} case 1:
return []string{s}
k := 0 default:
labels := make([]string, 0) begin := 0
last := byte('.') end := 0
lastlast := byte('.') labels := make([]string, 0)
s = Fqdn(s) // Make fully qualified for i := 1; i < len(idx); i++ {
for i := 0; i < len(s); i++ { end = idx[i]
if s[i] == '.' { labels = append(labels, s[begin:end])
if last == '\\' { begin = end
if lastlast != '\\' {
// do nothing
continue
}
}
labels = append(labels, s[k:i])
k = i + 1 // + dot
} }
lastlast = last return labels
last = s[i]
} }
return labels panic("dns: not reached")
} }
// CompareLabels compares the names s1 and s2 and // CompareLabels compares the names s1 and s2 and

View File

@ -48,11 +48,17 @@ func TestSplit(t *testing.T) {
`www\.miek.nl.`: 2, `www\.miek.nl.`: 2,
`www\\.miek.nl.`: 3, `www\\.miek.nl.`: 3,
".": 0, ".": 0,
"nl.": 1,
"nl": 1,
"com.": 1,
".com.": 2,
} }
for s, i := range splitter { for s, i := range splitter {
if x := len(Split(s)); x != i { if x := len(Split(s)); x != i {
t.Logf("Labels should be %d, got %d: %s\n", i, x, s) t.Logf("Labels should be %d, got %d: %s %v\n", i, x, s, Split(s))
t.Fail() t.Fail()
} else {
t.Logf("%s %v\n", s, Split(s))
} }
} }
} }