gpt4 book ai didi

swift - 将 Swift 字符串编码为转义的 unicode?

转载 作者:行者123 更新时间:2023-11-28 05:43:53 27 4
gpt4 key购买 nike

API数据字段只支持ASCII编码——但我需要支持Unicode(emoji、外来字符等)

我想将用户的文本输入编码为转义的 unicode 字符串:

let textContainingUnicode = """
Let's go 🏊 in the 🌊.
And some new lines.
"""

let result = textContainingUnicode.unicodeScalars.map { $0.escaped(asASCII: true)}
.joined(separator: "")
.replacingOccurrences(
of: "\\\\u\\{(.+?(?=\\}))\\}", <- converting swift format \\u{****}
with: "\\\\U$1", <- into format python expects
options: .regularExpression)

result 这里是“Let\'s go\U0001F3CA in the\U0001F30A.\n And some new lines.”

然后在服务器上用 python 解码:

codecs.decode("Let\\'s go\\U0001F3CA in the\\U0001F30A.\\n And some new lines.\n", 'unicode_escape')

但这听起来很有趣——我真的需要在 swift 中进行如此多的字符串操作才能获得转义的 unicode 吗?这些格式不是跨语言标准化的吗?

最佳答案

您可以在您的集合中使用 reduce 并检查每个字符是否为 ASCII,如果为真则返回该字符,否则将特殊字符转换为 unicode:

swift 5.1 • Xcode 11

extension Unicode.Scalar {
var hexa: String { .init(value, radix: 16, uppercase: true) }
}

extension Character {
var hexaValues: [String] {
unicodeScalars
.map(\.hexa)
.map { #"\\U"# + repeatElement("0", count: 8-$0.count) + $0 }
}
}

extension StringProtocol where Self: RangeReplaceableCollection {
var asciiRepresentation: String { map { $0.isASCII ? .init($0) : $0.hexaValues.joined() }.joined() }
}

let textContainingUnicode = """
Let's go 🏊 in the 🌊.
And some new lines.
"""

let asciiRepresentation = textContainingUnicode.asciiRepresentation
print(asciiRepresentation) // "Let's go \\U0001F3CA in the \\U0001F30A.\n And some new lines."

关于swift - 将 Swift 字符串编码为转义的 unicode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55617860/

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