Make toRadixName much faster

fix bug when a escape is escaped: \\. This last dot is now a real
domain label seperator dot.
This commit is contained in:
Miek Gieben 2012-10-10 12:39:45 +02:00
parent 36af583ccf
commit 82cfe822ff
2 changed files with 20 additions and 6 deletions

19
zone.go
View File

@ -109,17 +109,28 @@ func NewZoneData(s string) *ZoneData {
// we preserve the nsec ordering of the zone (this idea was stolen from NSD).
// Each label is also lowercased. The name given must be fully qualified.
func toRadixName(d string) string {
if d == "." {
if d == "" || d == "." {
return "."
}
s := ""
ld := len(d)
if d[ld-1] != '.' {
d = d + "."
ld++
}
var lastdot int
var lastbyte byte
var lastlastbyte byte
for i := 0; i < len(d); i++ {
if d[i] == '.' && lastbyte != '\\' && lastlastbyte != '\\' {
s = d[lastdot:i] + "." + s
lastdot = i+1
if d[i] == '.' {
switch {
case lastbyte != '\\':
fallthrough
case lastbyte == '\\' && lastlastbyte == '\\':
s = d[lastdot:i] + "." + s
lastdot = i + 1
continue
}
}
lastlastbyte = lastbyte
lastbyte = d[i]

View File

@ -7,9 +7,12 @@ import (
func TestRadixName(t *testing.T) {
tests := map[string]string{".": ".",
"www.miek.nl.": ".nl.miek.www",
"miek.nl.": ".nl.miek",
"mi\\.ek.nl.": ".nl.mi\\.ek",}
"miek.nl.": ".nl.miek",
"mi\\.ek.nl.": ".nl.mi\\.ek",
`mi\\.ek.nl.`: `.nl.ek.mi\\`,
"": "."}
for i, o := range tests {
t.Logf("%s %v\n", i, SplitLabels(i))
if x := toRadixName(i); x != o {
t.Logf("%s should convert to %s, not %s\n", i, o, x)
t.Fail()