Skip to content

Instantly share code, notes, and snippets.

@ukitaka
Last active February 6, 2017 08:10
Show Gist options
  • Select an option

  • Save ukitaka/c8a745c569cccab871ed417eef3a6b2b to your computer and use it in GitHub Desktop.

Select an option

Save ukitaka/c8a745c569cccab871ed417eef3a6b2b to your computer and use it in GitHub Desktop.
utf8mb4
extension UTF8 {
static func byteCount(_ input: UnicodeScalar) -> Int {
var count = 0
encode(input, into: { _ in count = count + 1 })
return count
}
static func is4Bytes(_ input: UnicodeScalar) -> Bool {
return byteCount(input) == 4
}
}
extension String {
var contains4BytesCharacterByUTF8Encoding: Bool {
return unicodeScalars.contains(where: UTF8.is4Bytes)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment