ircmap: Add RFC3454 mappings

This commit is contained in:
Daniel Oaks 2016-01-20 12:27:11 +10:00
parent d1eef8f96b
commit e3b56be41e

@ -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
}