Files
zy a790a97c93 fix: 修复标题长度计算不准确的问题 (#410)
使用基于 UTF-16 编码的加权算法替换 go-runewidth,与小红书实际计算规则一致:
非ASCII字符算2字节,ASCII字符算1字节,向上取整除以2。

Closes #401

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-10 23:04:12 +08:00

18 lines
398 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package xhsutil
import "unicode/utf16"
// CalcTitleLength 计算小红书标题长度
// 规则非ASCII字符(中文、全角符号等)算2字节ASCII字符算1字节最终结果向上取整除以2
func CalcTitleLength(s string) int {
byteLen := 0
for _, c := range utf16.Encode([]rune(s)) {
if c > 127 {
byteLen += 2
} else {
byteLen += 1
}
}
return (byteLen + 1) / 2
}