1 Star 0 Fork 0

蚂蚁web/mahonia

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
translate.go 922 Bytes
一键复制 编辑 原始数据 按行查看 历史
haoqis 提交于 2012-05-22 09:13 . '.'
package mahonia
import "unicode/utf8"
// Translate enables a Decoder to implement go-charset's Translator interface.
func (d Decoder) Translate(data []byte, eof bool) (n int, cdata []byte, err error) {
cdata = make([]byte, len(data)+1)
destPos := 0
for n < len(data) {
rune, size, status := d(data[n:])
switch status {
case STATE_ONLY:
n += size
continue
case NO_ROOM:
if !eof {
return n, cdata[:destPos], nil
}
rune = 0xfffd
n = len(data)
default:
n += size
}
if rune < 128 {
if destPos >= len(cdata) {
cdata = doubleLength(cdata)
}
cdata[destPos] = byte(rune)
destPos++
} else {
if destPos+utf8.RuneLen(rune) > len(cdata) {
cdata = doubleLength(cdata)
}
destPos += utf8.EncodeRune(cdata[destPos:], rune)
}
}
return n, cdata[:destPos], nil
}
func doubleLength(b []byte) []byte {
b2 := make([]byte, 2*len(b))
copy(b2, b)
return b2
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mayiweb/mahonia.git
git@gitee.com:mayiweb/mahonia.git
mayiweb
mahonia
mahonia
master

搜索帮助