将 UTF-8 字符前进到下一个字符
我想更改一个UTF-8字符(位于gchar数组中),因此它根据标准获取下一个字符的值。我正在使用 glib,但没有看到这样的函数。我正在考虑一个可能的解决方案,但它可能需要更多的努力,而且肯定不会是最有效的,因为我对编码不太了解。有没有图书馆可以做到这一点?谷歌搜索没有帮助。
I want to change a UTF-8 character (which is in a gchar array), so it gets the value of the next character according to the standard. I'm using glib and I don't see a function like that. I'm thinking of a possible solution, but it would take maybe more effort and surely it wouldn't be the most efficient, as I don't know too much about encodings. Is there any library that can do that? Googling didn't help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这本质上只是对 64 进行加进位模。将字符的字节视为“数字”。您递增最后一个字节,如果它溢出,请将其重置为尽可能小的值,并递增倒数第二个字节。
例如,一个简单的增量:
单进位增量:
和双进位增量:
当您增量超过给定大小的最后一个字符时,您需要转到下一个大小的第一个字符,这当然不能在字符串中间就地完成。
This is essentially just add-and-carry modulo 64. Consider the bytes of the character as "digits". You increment the last byte, and if it overflows, reset it to the smallest possible value, and increment the second-to-last byte.
For example, a simple increment:
An increment with single carry:
And an increment with double carry:
When you increment past the last character of a given size, you'll need to go to the first character of the next size, which of course can't be done in-place in the middle of a string.
如果你想避免直接的字节黑客攻击,你可以这样做(未经测试):
当然,手动编写它会给你更优化的代码。对上述内容的一个小优化可能会使用 g_utf8_next_char() 来获取下一个字符串位置,并从中计算 old_len,而不是独立计算 old_len。
If you want to avoid direct byte-hacking, you could do something like this (untested):
Of course writing it manually would give you more optimized code. A minor optimization to the above might use g_utf8_next_char() to get the next string position, and compute the old_len from that, instead of independently computing old_len.