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 html = String()
var prevIndex = displayStartIndex var prevIndex = displayStartIndex
var emojiOffset = 0 var unicodeScalarOffset = 0
for entity in entities { 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. // The twitter indices are messed up by characters 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 let endIndex = text.index(text.startIndex, offsetBy: entity.endIndex, limitedBy: text.endIndex) ?? text.endIndex
if prevIndex < emojiEndIndex { if prevIndex < endIndex {
let emojis = String(text[prevIndex..<emojiEndIndex]).emojis let characters = String(text[prevIndex..<endIndex])
for emoji in emojis { for character in characters {
emojiOffset += emoji.unicodeScalars.count - 1 unicodeScalarOffset += character.unicodeScalars.count - 1
} }
} }
let offsetStartIndex = entity.startIndex - emojiOffset let offsetStartIndex = entity.startIndex - unicodeScalarOffset
let offsetEndIndex = entity.endIndex - emojiOffset let offsetEndIndex = entity.endIndex - unicodeScalarOffset
let entityStartIndex = text.index(text.startIndex, offsetBy: offsetStartIndex, limitedBy: text.endIndex) ?? text.startIndex 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 let entityEndIndex = text.index(text.startIndex, offsetBy: offsetEndIndex, limitedBy: text.endIndex) ?? text.endIndex