-
Notifications
You must be signed in to change notification settings - Fork 164
/
charset.go
112 lines (91 loc) · 3.32 KB
/
charset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// This package is a character-set conversion library for Go.
package mahonia
import (
"bytes"
"unicode"
)
// Status is the type for the status return value from a Decoder or Encoder.
type Status int
const (
// SUCCESS means that the character was converted with no problems.
SUCCESS = Status(iota)
// INVALID_CHAR means that the source contained invalid bytes, or that the character
// could not be represented in the destination encoding.
// The Encoder or Decoder should have output a substitute character.
INVALID_CHAR
// NO_ROOM means there were not enough input bytes to form a complete character,
// or there was not enough room in the output buffer to write a complete character.
// No bytes were written, and no internal state was changed in the Encoder or Decoder.
NO_ROOM
// STATE_ONLY means that bytes were read or written indicating a state transition,
// but no actual character was processed. (Examples: byte order marks, ISO-2022 escape sequences)
STATE_ONLY
)
// A Decoder is a function that decodes a character set, one character at a time.
// It works much like utf8.DecodeRune, but has an aditional status return value.
type Decoder func(p []byte) (c rune, size int, status Status)
// An Encoder is a function that encodes a character set, one character at a time.
// It works much like utf8.EncodeRune, but has an additional status return value.
type Encoder func(p []byte, c rune) (size int, status Status)
// A Charset represents a character set that can be converted, and contains functions
// to create Converters to encode and decode strings in that character set.
type Charset struct {
// Name is the character set's canonical name.
Name string
// Aliases returns a list of alternate names.
Aliases []string
// NewDecoder returns a Decoder to convert from the charset to Unicode.
NewDecoder func() Decoder
// NewEncoder returns an Encoder to convert from Unicode to the charset.
NewEncoder func() Encoder
}
// The charsets are stored in charsets under their canonical names.
var charsets = make(map[string]*Charset)
// aliases maps their aliases to their canonical names.
var aliases = make(map[string]string)
// simplifyName converts a name to lower case and removes non-alphanumeric characters.
// This is how the names are used as keys to the maps.
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()
}
// RegisterCharset adds a charset to the charsetMap.
func RegisterCharset(cs *Charset) {
name := cs.Name
charsets[name] = cs
aliases[simplifyName(name)] = name
for _, alias := range cs.Aliases {
aliases[simplifyName(alias)] = name
}
}
// GetCharset fetches a charset by name.
// If the name is not found, it returns nil.
func GetCharset(name string) *Charset {
return charsets[aliases[simplifyName(name)]]
}
// NewDecoder returns a Decoder to decode the named charset.
// If the name is not found, it returns nil.
func NewDecoder(name string) Decoder {
cs := GetCharset(name)
if cs == nil {
return nil
}
return cs.NewDecoder()
}
// NewEncoder returns an Encoder to encode the named charset.
func NewEncoder(name string) Encoder {
cs := GetCharset(name)
if cs == nil {
return nil
}
return cs.NewEncoder()
}