123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- package mahonia
- import (
- "bytes"
- "unicode"
- )
- type Status int
- const (
-
- SUCCESS = Status(iota)
-
-
-
- INVALID_CHAR
-
-
-
- NO_ROOM
-
-
- STATE_ONLY
- )
- type Decoder func(p []byte) (c rune, size int, status Status)
- type Encoder func(p []byte, c rune) (size int, status Status)
- type Charset struct {
-
- Name string
-
- Aliases []string
-
- NewDecoder func() Decoder
-
- NewEncoder func() Encoder
- }
- var charsets = make(map[string]*Charset)
- var aliases = make(map[string]string)
- func simplifyName(name string) string {
- var buf bytes.Buffer
- for _, c := range name {
- switch {
- case unicode.IsDigit(c):
- buf.WriteRune(c)
- case unicode.IsLetter(c):
- buf.WriteRune(unicode.ToLower(c))
- default:
- }
- }
- return buf.String()
- }
- func RegisterCharset(cs *Charset) {
- name := cs.Name
- charsets[name] = cs
- aliases[simplifyName(name)] = name
- for _, alias := range cs.Aliases {
- aliases[simplifyName(alias)] = name
- }
- }
- func GetCharset(name string) *Charset {
- return charsets[aliases[simplifyName(name)]]
- }
- func NewDecoder(name string) Decoder {
- cs := GetCharset(name)
- if cs == nil {
- return nil
- }
- return cs.NewDecoder()
- }
- func NewEncoder(name string) Encoder {
- cs := GetCharset(name)
- if cs == nil {
- return nil
- }
- return cs.NewEncoder()
- }
|