Change to test all characters for multiple scalars. Fixes #3120

This commit is contained in:
Maurice Parker 2021-05-17 14:06:55 -05:00
parent 629d74b67b
commit 3dbfc9a3b7
1 changed files with 9 additions and 9 deletions

View File

@ -78,21 +78,21 @@ private extension TwitterStatus {
var html = String()
var prevIndex = displayStartIndex
var emojiOffset = 0
var unicodeScalarOffset = 0
for entity in entities {
// The twitter indices are messed up by emoji with more than one scalar, we are going to adjust for that here.
let emojiEndIndex = text.index(text.startIndex, offsetBy: entity.endIndex, limitedBy: text.endIndex) ?? text.endIndex
if prevIndex < emojiEndIndex {
let emojis = String(text[prevIndex..<emojiEndIndex]).emojis
for emoji in emojis {
emojiOffset += emoji.unicodeScalars.count - 1
// The twitter indices are messed up by characters with more than one scalar, we are going to adjust for that here.
let endIndex = text.index(text.startIndex, offsetBy: entity.endIndex, limitedBy: text.endIndex) ?? text.endIndex
if prevIndex < endIndex {
let characters = String(text[prevIndex..<endIndex])
for character in characters {
unicodeScalarOffset += character.unicodeScalars.count - 1
}
}
let offsetStartIndex = entity.startIndex - emojiOffset
let offsetEndIndex = entity.endIndex - emojiOffset
let offsetStartIndex = entity.startIndex - unicodeScalarOffset
let offsetEndIndex = entity.endIndex - unicodeScalarOffset
let entityStartIndex = text.index(text.startIndex, offsetBy: offsetStartIndex, limitedBy: text.endIndex) ?? text.startIndex
let entityEndIndex = text.index(text.startIndex, offsetBy: offsetEndIndex, limitedBy: text.endIndex) ?? text.endIndex