istring: Add rfc7613 casemapping

This commit is contained in:
Daniel Oaks 2017-06-13 17:18:10 -06:00
parent dcb717d58d
commit 7e427ba929
2 changed files with 38 additions and 2 deletions

@ -6,6 +6,8 @@ package ircmap
import ( import (
"strings" "strings"
"golang.org/x/text/secure/precis"
"github.com/DanielOaks/go-idn/idna2003/stringprep" "github.com/DanielOaks/go-idn/idna2003/stringprep"
) )
@ -22,9 +24,12 @@ const (
// RFC1459 represents the casemapping defined by "rfc1459" // RFC1459 represents the casemapping defined by "rfc1459"
RFC1459 RFC1459
// RFC3454 represents the UTF-8 nameprep casefolding as used by mammon-ircd // RFC3454 represents the UTF-8 nameprep casefolding as used by mammon-ircd.
// and specified by ircv3-harmony.
RFC3454 RFC3454
// RFC7613 represents the UTF-8 casefolding currently being drafted by me
// with the IRCv3 WG.
RFC7613
) )
var ( var (
@ -33,6 +38,7 @@ var (
"ascii": ASCII, "ascii": ASCII,
"rfc1459": RFC1459, "rfc1459": RFC1459,
"rfc3454": RFC3454, "rfc3454": RFC3454,
"rfc7613": RFC7613,
} }
) )
@ -62,6 +68,8 @@ func Casefold(mapping MappingType, input string) (string, error) {
} }
} else if mapping == RFC3454 { } else if mapping == RFC3454 {
out, err = stringprep.Nameprep(input) out, err = stringprep.Nameprep(input)
} else if mapping == RFC7613 {
out, err = precis.UsernameCaseMapped.CompareKey(input)
} }
return out, err return out, err

@ -22,6 +22,13 @@ var equalRFC1459Tests = []testcase{
var equalRFC3454Tests = []testcase{ var equalRFC3454Tests = []testcase{
{"#TeStChAn", "#testchan"}, {"#TeStChAn", "#testchan"},
{"#beßtchannEL", "#besstchannel"}, {"#beßtchannEL", "#besstchannel"},
{"3456", "34563456"},
}
var equalRFC7613Tests = []testcase{
{"#TeStChAn", "#testchan"},
{"#beßtchannEL", "#beßtchannel"},
{"3456", "34563456"},
} }
func TestASCII(t *testing.T) { func TestASCII(t *testing.T) {
@ -86,3 +93,24 @@ func TestRFC3454(t *testing.T) {
} }
} }
} }
func TestRFC7613(t *testing.T) {
for _, pair := range equalRFC7613Tests {
val, err := Casefold(RFC7613, pair.raw)
if err != nil {
t.Error(
"For", pair.raw,
"expected", pair.folded,
"but we got an error:", err.Error(),
)
}
if val != pair.folded {
t.Error(
"For", pair.raw,
"expected", pair.folded,
"got", val,
)
}
}
}