validate utf-8 bytes in swift

To validate UTF-8 bytes in Swift, you can use the String.Encoding.utf8 enumeration value to initialize a String.Encoding object, which can be used to validate the bytes using the String method init?(data: Data, encoding: String.Encoding).

Here's an example function that takes in a Data object and returns a Bool indicating whether the bytes are valid UTF-8:

main.swift
func validateUTF8Bytes(_ bytes: Data) -> Bool {
    if let _ = String(data: bytes, encoding: .utf8) {
        return true
    } else {
        return false
    }
}
164 chars
8 lines

You can use this function like so:

main.swift
// Example bytes
let validBytes: [UInt8] = [0x68, 0x65, 0x6C, 0x6C, 0xC3, 0xB6, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64]
let invalidBytes: [UInt8] = [0x68, 0x65, 0x6C, 0x6C, 0xC3, 0xC3, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64]
let validData = Data(validBytes)
let invalidData = Data(invalidBytes)

// Validation
print(validateUTF8Bytes(validData)) // true
print(validateUTF8Bytes(invalidData)) // false
393 chars
10 lines

Note that this validation only checks whether the bytes can be interpreted as valid UTF-8. It doesn't guarantee that the bytes actually represent valid Unicode characters.

gistlibby LogSnag