From e3b56be41ed3e204dbffcbc6d53f4cd6097a4f64 Mon Sep 17 00:00:00 2001 From: Daniel Oaks Date: Wed, 20 Jan 2016 12:27:11 +1000 Subject: [PATCH] ircmap: Add RFC3454 mappings --- ircmap/istring.go | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/ircmap/istring.go b/ircmap/istring.go index fbc8024..1392630 100644 --- a/ircmap/istring.go +++ b/ircmap/istring.go @@ -3,7 +3,11 @@ package ircmap -import "strings" +import ( + "strings" + + "github.com/DanielOaks/go-idn/idna2003/stringprep" +) // MappingType values represent the types of IRC casemapping we support. type MappingType int @@ -15,9 +19,18 @@ const ( // RFC1459 represents the casemapping defined by "rfc1459" RFC1459 - // RFC3454 represents the UTF-8 casefolding defined by mammon.io and - // ircv3-harmony. Not supported yet due to no appropriate libs to do it. - // RFC3454 + // RFC3454 represents the UTF-8 nameprep casefolding as used by mammon.io + // and ircv3-harmony. + RFC3454 +) + +var ( + // Mappings is a mapping of ISUPPORT CASEMAP value to our MappingTypes. + Mappings = map[string]MappingType{ + "ascii": ASCII, + "rfc1459": RFC1459, + "rfc3454": RFC3454, + } ) // rfc1459Fold casefolds only the special chars defined by RFC1459 -- the @@ -30,19 +43,23 @@ func rfc1459Fold(r rune) rune { } // Casefold returns a string, lowercased/casefolded according to the given -// mapping, as defined by this package. -func Casefold(mapping MappingType, in string) string { +// mapping as defined by this package (or an error if the given string is not +// valid in the chosen mapping). +func Casefold(mapping MappingType, input string) (string, error) { var out string + var err error if mapping == ASCII || mapping == RFC1459 { // strings.ToLower ONLY replaces a-z, no unicode stuff so we're safe // to use that here without any issues. - out = strings.ToLower(in) + out = strings.ToLower(input) if mapping == RFC1459 { out = strings.Map(rfc1459Fold, out) } + } else if mapping == RFC3454 { + out, err = stringprep.Nameprep(input) } - return out + return out, err }