1 Star 0 Fork 0

蚂蚁web/mahonia

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
convert_string.go 988 Bytes
一键复制 编辑 原始数据 按行查看 历史
haoqis 提交于 2012-05-22 09:13 . '.'
package mahonia
// ConvertString converts a string from UTF-8 to e's encoding.
func (e Encoder) ConvertString(s string) string {
dest := make([]byte, len(s)+10)
destPos := 0
for _, rune := range s {
retry:
size, status := e(dest[destPos:], rune)
if status == NO_ROOM {
newDest := make([]byte, len(dest)*2)
copy(newDest, dest)
dest = newDest
goto retry
}
if status == STATE_ONLY {
destPos += size
goto retry
}
destPos += size
}
return string(dest[:destPos])
}
// ConvertString converts a string from d's encoding to UTF-8.
func (d Decoder) ConvertString(s string) string {
bytes := []byte(s)
runes := make([]rune, len(s))
destPos := 0
for len(bytes) > 0 {
c, size, status := d(bytes)
if status == STATE_ONLY {
bytes = bytes[size:]
continue
}
if status == NO_ROOM {
c = 0xfffd
size = len(bytes)
status = INVALID_CHAR
}
bytes = bytes[size:]
runes[destPos] = c
destPos++
}
return string(runes[:destPos])
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mayiweb/mahonia.git
git@gitee.com:mayiweb/mahonia.git
mayiweb
mahonia
mahonia
master

搜索帮助