Extract shared NormalizeAlnum into pkg/strutil
The alphanumeric-normalisation helper was duplicated verbatim in the common-third-party owned-domains resolver and the cookiebanner tracker mapping worker. Hoist it into a new dependency-free strutil package so both call sites share one implementation and one test. Signed-off-by: Émile Ré <emile@probo.com>
This commit is contained in:
@@ -18,6 +18,7 @@ import (
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"go.probo.inc/probo/pkg/strutil"
|
||||
"go.probo.inc/probo/pkg/uri"
|
||||
)
|
||||
|
||||
@@ -157,7 +158,7 @@ func vendorLabels(name, website string) []string {
|
||||
}
|
||||
|
||||
add(uri.DomainLabel(website))
|
||||
add(normalizeAlnum(name))
|
||||
add(strutil.NormalizeAlnum(name))
|
||||
|
||||
return labels
|
||||
}
|
||||
@@ -213,18 +214,3 @@ func normalizeToETLD1(raw string) string {
|
||||
|
||||
return uri.ExtractDomain(raw)
|
||||
}
|
||||
|
||||
// normalizeAlnum lowercases a string and drops every non-alphanumeric
|
||||
// rune, so "Dark Reader" and "DarkReader, Inc." both reduce to a
|
||||
// comparable label root.
|
||||
func normalizeAlnum(s string) string {
|
||||
var b strings.Builder
|
||||
|
||||
for _, r := range strings.ToLower(s) {
|
||||
if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') {
|
||||
b.WriteRune(r)
|
||||
}
|
||||
}
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user