swift - 将 Swift 字符串编码为转义的 unicode?
问题描述
API 数据字段仅支持 ASCII 编码——但我需要支持 Unicode(表情符号、外来字符等)
我想将用户的文本输入编码为转义的 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')
但这闻起来很有趣——我真的需要快速进行如此多的字符串操作来获得转义的 unicode 吗?这些格式是否没有跨语言标准化。
解决方案
您可以在集合中使用 reduce 并检查每个字符是否为 ASCII,如果为 true,则返回该字符,否则将特殊字符转换为 unicode:
斯威夫特 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."
推荐阅读
- python - 如何在 Python 中仅将新密钥对从一个字典附加到另一个字典?
- java - Shell排序算法时间复杂度
- amazon-web-services - 使用 Kubernetes (EKS) 时如何将 Spark 日志写入 S3?错误:无法识别的选项:--spark.kubernetes.driver.secretKeyRef.AWS_ACCESS_KEY_ID
- javascript - 如何从圆括号中获取所有单词,然后用 html div 代码替换所有单词
- selenium - Power Automate 和 Selenium IDE
- python - 使用 PIL 旋转图像而不保存它
- sql - 使用 BY TIMESTAMP 的带有翻滚窗口的 Azure 流分析查询在测试中运行良好,但在运行作业结果时关闭
- javascript - 代码绘制随机贝塞尔线...未正确渲染
- sql-server - SQL Server - 识别 Varchar 中的十六进制范围
- javascript - How to make a react-strap Button responsive?