soju/xirc/casemapping.go
2023-03-01 13:35:03 +01:00

78 lines
1.8 KiB
Go

package xirc
func casemapNone(name string) string {
return name
}
// CasemapASCII of name is the canonical representation of name according to the
// ascii casemapping.
func casemapASCII(name string) string {
nameBytes := []byte(name)
for i, r := range nameBytes {
if 'A' <= r && r <= 'Z' {
nameBytes[i] = r + 'a' - 'A'
}
}
return string(nameBytes)
}
// casemapRFC1459 of name is the canonical representation of name according to the
// rfc1459 casemapping.
func casemapRFC1459(name string) string {
nameBytes := []byte(name)
for i, r := range nameBytes {
if 'A' <= r && r <= 'Z' {
nameBytes[i] = r + 'a' - 'A'
} else if r == '{' {
nameBytes[i] = '['
} else if r == '}' {
nameBytes[i] = ']'
} else if r == '\\' {
nameBytes[i] = '|'
} else if r == '~' {
nameBytes[i] = '^'
}
}
return string(nameBytes)
}
// casemapRFC1459Strict of name is the canonical representation of name
// according to the rfc1459-strict casemapping.
func casemapRFC1459Strict(name string) string {
nameBytes := []byte(name)
for i, r := range nameBytes {
if 'A' <= r && r <= 'Z' {
nameBytes[i] = r + 'a' - 'A'
} else if r == '{' {
nameBytes[i] = '['
} else if r == '}' {
nameBytes[i] = ']'
} else if r == '\\' {
nameBytes[i] = '|'
}
}
return string(nameBytes)
}
type CaseMapping func(string) string
var (
CaseMappingNone CaseMapping = casemapNone
CaseMappingASCII CaseMapping = casemapASCII
CaseMappingRFC1459 CaseMapping = casemapRFC1459
CaseMappingRFC1459Strict CaseMapping = casemapRFC1459Strict
)
func ParseCaseMapping(s string) CaseMapping {
var cm CaseMapping
switch s {
case "ascii":
cm = CaseMappingASCII
case "rfc1459":
cm = CaseMappingRFC1459
case "rfc1459-strict":
cm = CaseMappingRFC1459Strict
}
return cm
}