Skip to content

Commit

Permalink
Simplify CanonicalName (#1494)
Browse files Browse the repository at this point in the history
This function was previously changed from using strings.ToLower to a
custom loop to ensure it only lowercases ASCII characters. This was
more complicated than it needed to be and introduced more allocations
than is necessary.

Instead of that approach we call strings.Map with a simple ASCII
lowercase mapping function. Sadly we still use the nice ASCII-only fast
path that strings.ToLower had, but it's unlikely to be worth all the
extra code.
  • Loading branch information
tmthrgd authored Nov 2, 2023
1 parent 52f175a commit 8a3ef11
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net"
"strconv"
"strings"
"unicode"
)

const hexDigit = "0123456789abcdef"
Expand Down Expand Up @@ -330,19 +329,15 @@ func Fqdn(s string) string {
}

// CanonicalName returns the domain name in canonical form. A name in canonical
// form is lowercase and fully qualified. See Section 6.2 in RFC 4034.
// According to the RFC all uppercase US-ASCII letters in the owner name of the
// RR areeplaced by the corresponding lowercase US-ASCII letters.
// form is lowercase and fully qualified. Only US-ASCII letters are affected. See
// Section 6.2 in RFC 4034.
func CanonicalName(s string) string {
var result strings.Builder
for _, ch := range s {
if unicode.IsUpper(ch) && (ch >= 0x00 && ch <= 0x7F) {
result.WriteRune(unicode.ToLower(ch))
} else {
result.WriteRune(ch)
return strings.Map(func(r rune) rune {
if r >= 'A' && r <= 'Z' {
r += 'a' - 'A'
}
}
return Fqdn(result.String())
return r
}, Fqdn(s))
}

// Copied from the official Go code.
Expand Down

0 comments on commit 8a3ef11

Please sign in to comment.