mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-31 20:02:09 +00:00
Remove hg dep
This commit is contained in:
parent
f8977f4847
commit
25d6ae69d1
36 changed files with 119822 additions and 8 deletions
92
modules/mahonia/mbcs.go
Normal file
92
modules/mahonia/mbcs.go
Normal file
|
@ -0,0 +1,92 @@
|
|||
package mahonia
|
||||
|
||||
// Generic converters for multibyte character sets.
|
||||
|
||||
// An mbcsTrie contains the data to convert from the character set to Unicode.
|
||||
// If a character would be encoded as "\x01\x02\x03", its unicode value would be found at t.children[1].children[2].children[3].rune
|
||||
// children either is nil or has 256 elements.
|
||||
type mbcsTrie struct {
|
||||
// For leaf nodes, the Unicode character that is represented.
|
||||
char rune
|
||||
|
||||
// For non-leaf nodes, the trie to decode the remainder of the character.
|
||||
children []mbcsTrie
|
||||
}
|
||||
|
||||
// A MBCSTable holds the data to convert to and from Unicode.
|
||||
type MBCSTable struct {
|
||||
toUnicode mbcsTrie
|
||||
fromUnicode map[rune]string
|
||||
}
|
||||
|
||||
// AddCharacter adds a character to the table. rune is its Unicode code point,
|
||||
// and bytes contains the bytes used to encode it in the character set.
|
||||
func (table *MBCSTable) AddCharacter(c rune, bytes string) {
|
||||
if table.fromUnicode == nil {
|
||||
table.fromUnicode = make(map[rune]string)
|
||||
}
|
||||
|
||||
table.fromUnicode[c] = bytes
|
||||
|
||||
trie := &table.toUnicode
|
||||
for i := 0; i < len(bytes); i++ {
|
||||
if trie.children == nil {
|
||||
trie.children = make([]mbcsTrie, 256)
|
||||
}
|
||||
|
||||
b := bytes[i]
|
||||
trie = &trie.children[b]
|
||||
}
|
||||
|
||||
trie.char = c
|
||||
}
|
||||
|
||||
func (table *MBCSTable) Decoder() Decoder {
|
||||
return func(p []byte) (c rune, size int, status Status) {
|
||||
if len(p) == 0 {
|
||||
status = NO_ROOM
|
||||
return
|
||||
}
|
||||
|
||||
if p[0] == 0 {
|
||||
return 0, 1, SUCCESS
|
||||
}
|
||||
|
||||
trie := &table.toUnicode
|
||||
for trie.char == 0 {
|
||||
if trie.children == nil {
|
||||
return 0xfffd, 1, INVALID_CHAR
|
||||
}
|
||||
if len(p) < size+1 {
|
||||
return 0, 0, NO_ROOM
|
||||
}
|
||||
|
||||
trie = &trie.children[p[size]]
|
||||
size++
|
||||
}
|
||||
|
||||
c = trie.char
|
||||
status = SUCCESS
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (table *MBCSTable) Encoder() Encoder {
|
||||
return func(p []byte, c rune) (size int, status Status) {
|
||||
bytes := table.fromUnicode[c]
|
||||
if bytes == "" {
|
||||
if len(p) > 0 {
|
||||
p[0] = '?'
|
||||
return 1, INVALID_CHAR
|
||||
} else {
|
||||
return 0, NO_ROOM
|
||||
}
|
||||
}
|
||||
|
||||
if len(p) < len(bytes) {
|
||||
return 0, NO_ROOM
|
||||
}
|
||||
|
||||
return copy(p, bytes), SUCCESS
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue