gpt4 book ai didi

ruby - 相同的字符串但不同的字节码

转载 作者:数据小太阳 更新时间:2023-10-29 06:52:36 25 4
gpt4 key购买 nike

我有两个字符串:

a = 'hà nội'
b = 'hà nội'

当我将它们与 a == b 进行比较时,它返回 false

我检查了字节码:

a.bytes = [104, 97, 204, 128, 32, 110, 195, 180, 204, 163, 105]
b.bytes = [104, 195, 160, 32, 110, 225, 187, 153, 105]

这是什么原因?我该如何修复它以便 a == b 返回 true

最佳答案

这是 Unicode equivalence 的问题.

为了比较这些字符串,您需要对它们进行规范化,以便它们都对这些类型的字符使用相同的字节序列。

a.unicode_normalize == b.unicode_normalize

unicode_normalize(form=:nfc) [link]

Returns a normalized form of str, using Unicode normalizations NFC, NFD, NFKC, or NFKD. The normalization form used is determined by form, which is any of the four values :nfc, :nfd, :nfkc, or :nfkd. The default is :nfc.

If the string is not in a Unicode Encoding, then an Exception is raised. In this context, 'Unicode Encoding' means any of UTF-8, UTF-16BE/LE, and UTF-32BE/LE, as well as GB18030, UCS_2BE, and UCS_4BE. Anything else than UTF-8 is implemented by converting to UTF-8, which makes it slower than UTF-8.

关于ruby - 相同的字符串但不同的字节码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48472375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com