Thank you, based on that code sample here's my latest version.
extension String {
func nsRange(fromByteRange byteRange: NSRange) -> NSRange {
let utf8 = self.utf8
let utf16 = self.utf16
let a = utf8.index(utf8.startIndex, offsetBy: byteRange.location)
let b = utf8.index(a, offsetBy: byteRange.length)
let loc = utf16.distance(from: utf16.startIndex, to: a)
let len = utf16.distance(from: a, to: b)
let nsRange = NSRange(location: loc, length: len)
return nsRange
}
func byteRange(fromNSRange nsRange: NSRange) -> NSRange {
let utf8 = self.utf8
let utf16 = self.utf16
let a = utf16.index(utf16.startIndex, offsetBy: nsRange.location)
let b = utf16.index(a, offsetBy: nsRange.length)
let loc = utf8.distance(from: utf8.startIndex, to: a)
let len = utf8.distance(from: a, to: b)
let byteRange = NSRange(location: loc, length: len)
return byteRange
}
}
Do you know if "string.utf8" / "string.utf16" caches its result so the subsequent calls of those are fast (provided the string is not changed)?