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

时间:2019-04-10 17:02:01

标签: 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吗?这些格式是否跨语言标准化?

1 个答案:

答案 0 :(得分:0)

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

Swift 5•Xcode 10.2

extension StringProtocol where Self: RangeReplaceableCollection {
    var asciiRepresentation: SubSequence {
        return reduce("") { string, char in
            if char.isASCII { return string + String(char) }
            let hexa = char.unicodeScalars
                .map { String($0.value, radix: 16, uppercase: true) }
                .joined()
            return string + "\\\\U" + repeatElement("0", count: 8-hexa.count) + hexa
        }
    }
}

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."