Make SplitLabels faster

don't allocate a new string at all, it does not matter is the given
string is not fully qualified.
This commit is contained in:
Miek Gieben 2013-09-12 08:57:37 +01:00
parent b76ac5bde6
commit 08e7365dc2
2 changed files with 17 additions and 3 deletions

View File

@ -129,13 +129,13 @@ func LenLabels(s string) int {
}
// Split splits a name s into its label indexes.
// www.miek.nl. returns []int{0, 4, 9}. The root name (.) returns nil.
// www.miek.nl. returns []int{0, 4, 9}, www.miek.nl also returns []int{0, 4, 9}
// The root name (.) returns nil.
func Split(s string) []int {
if s == "." {
return nil
}
s = Fqdn(s) // Grrr!
idx := []int{0} // TODO(miek): could allocate more (10) and then extend when needed
idx := []int{0}
off := 0
end := false

View File

@ -64,6 +64,20 @@ func TestSplit(t *testing.T) {
}
}
func TestSplit2(t *testing.T) {
splitter := map[string][]int{
"www.miek.nl.": []int{0, 4, 9},
"www.miek.nl": []int{0, 4, 9},
}
for s, i := range splitter {
x := Split(s)
if x[0] != i[0] || x[1] != i[1] || x[2] != i[2] {
t.Logf("Labels should be %v, got %v: %s\n", i, x, s)
t.Fail()
}
}
}
func TestSplitDomainName(t *testing.T) {
labels := map[string][]string{
"miek.nl": []string{"miek", "nl"},