简体   繁体   中英

Encoding Swift string as escaped unicode?

The API data field only supports ASCII encoding -- but I need to support Unicode (emoji, foreign characters, etc.)

I'd like to encode the user's text input as an escaped unicode string:

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 here is "Let\\'s go \\U0001F3CA in the \\U0001F30A.\\n And some new lines."

And on the server decoding with python:

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

But this smells funny -- do i really need to do so much string manipulation in swift to get the escaped unicode? Are these formats not standardized across languages.

You can use reduce in your collection and check if each character isASCII, if true return that character otherwise convert the special character to 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."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM