且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

清除格式错误的UTF-8数据

更新时间:2023-11-27 09:41:46

这是一种从(可能)格式错误的
UTF-8数据创建 String 的方法:

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


  • 将网站内容读入数据对象。

  • 附加 0 字节使其成为C字符串

  • 使用字符串(cString :) 用于转换。此初始化程序使用Unicode替换字符(\ u {FFFD})替换格式错误的UTF-8代码单元序列。

  • 可选:删除所有出现的替换字符。

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

清理过程示例:

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

当然这可以定义为自定义init方法:

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: "")
        }
    }
}

用法:

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

清洁可以使用 transcode with

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)
    }
}

这基本上是字符串(cString:)
,比较
CString.swift
StringCreate.swift

另一种选择是使用 UTF8 编解码器 decode()方法
并忽略错误:

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
    }
}