Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

ios - Cleaning malformed UTF-8 data

Background

With Swift, I'm trying to fetch HTML via URLSession rather than by loading it into a WKWebView first as I only need the HTML and none of the subresources. I'm running into a problem with certain pages that work when loaded into WKWebView but when loaded via URLSession (or even a simple NSString(contentsOf: url, encoding String.Encoding.utf8.rawValue)) the UTF-8 conversion fails.

How to reproduce

This fails (prints "nil"):

print(try? NSString(contentsOf: URL(string: "http://www.huffingtonpost.jp/techcrunch-japan/amazon-is-gobbling-whole-foods-for-a-reported-13-7-billion_b_17171132.html?utm_hp_ref=japan&ir=Japan")!, encoding: String.Encoding.utf8.rawValue))

But changing the URL to the site's homepage, it succeeds:

print(try? NSString(contentsOf: URL(string: "http://www.huffingtonpost.jp")!, encoding: String.Encoding.utf8.rawValue))

Question

How can I "clean" the data returned by a URL that contains malformed UTF-8? I'd like to either remove or replace any invalid sequences in the malformed UTF-8 so that the rest of it can be viewed. WKWebView is able to render the page just fine (and claims it's UTF-8 content as well), as you can see by visiting the URL: http://www.huffingtonpost.jp/techcrunch-japan/amazon-is-gobbling-whole-foods-for-a-reported-13-7-billion_b_17171132.html?utm_hp_ref=japan&ir=Japan

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is an approach to create a String from (possibly) malformed UTF-8 data:

  • Read the website contents into a Data object.
  • Append a 0 byte to make it a "C string"
  • Use String(cString:) for the conversion. This initializer replaces ill-formed UTF-8 code unit sequences with the Unicode replacement character ("u{FFFD}").
  • Optionally: Remove all occurrences of the replacement character.

Example for the "cleaning" process:

var data = Data(bytes: [65, 66, 200, 67]) // malformed UTF-8

data.append(0)
let s = data.withUnsafeBytes { (p: UnsafePointer<CChar>) in String(cString: p) }
let clean = s.replacingOccurrences(of: "u{FFFD}", with: "")

print(clean) // ABC

Swift 5:

var data = Data([65, 66, 200, 67]) // malformed UTF-8
data.append(0)
let s = data.withUnsafeBytes { p in
    String(cString: p.bindMemory(to: CChar.self).baseAddress!)
}
let clean = s.replacingOccurrences(of: "u{FFFD}", with: "")
print(clean) // ABC

Of course this can be defined as a custom init method:

extension String {
    init(malformedUTF8 data: Data) {
        var data = data
        data.append(0)
        self = data.withUnsafeBytes { (p: UnsafePointer<CChar>) in
            String(cString: p).replacingOccurrences(of: "u{FFFD}", with: "")
        }
    }
}

Swift 5:

extension String {
    init(malformedUTF8 data: Data) {
        var data = data
        data.append(0)
        self = data.withUnsafeBytes{ p in
            String(cString: p.bindMemory(to: CChar.self).baseAddress!)
        }.replacingOccurrences(of: "u{FFFD}", with: "")
    }
}

Usage:

let data = Data(bytes: [65, 66, 200, 67])
let s = String(malformedUTF8: data) 
print(s) // ABC

The cleaning can be done more "directly" using transcode with

extension String {
    init(malformedUTF8 data: Data) {
        var utf16units = [UInt16]()
        utf16units.reserveCapacity(data.count) // A rough estimate

        _ = transcode(data.makeIterator(), from: UTF8.self, to: UTF16.self,
                      stoppingOnError: false) { code in
            if code != 0xFFFD {
                utf16units.append(code)
            }
        }

        self = String(utf16CodeUnits: utf16units, count: utf16units.count)
    }
}

This is essentially what String(cString:) does, compare CString.swift and StringCreate.swift.

Yet another option is to use the UTF8 codecs decode() method and ignore errors:

extension String {
    init(malformedUTF8 data: Data) {
        var str = ""
        var iterator = data.makeIterator()
        var utf8codec = UTF8()
        var done = false
        while !done {
            switch utf8codec.decode(&iterator) {
            case .emptyInput:
                done = true
            case let .scalarValue(val):
                str.unicodeScalars.append(val)
            case .error:
                break // ignore errors
            }
        }
        self = str
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...