-
-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Returned facets indexes are not always aligned with text #52
Comments
Update: Seems that the problem is not due to emoji but somehow related to something else. It also occur with simple texts without emoji or special characters at all. |
Update2: For the ones that are facing the same problem, the issue is not due to the ATProtoKit itself, that indeed behaves correctly. The problem is due to the fact that UTF8, UTF16 and RAW bytes conversion is needed in order to work with AttributedString (in my case I'm using SwiftUI). Below is a simple helper method to deal with that: private func colorSubstring(in text: String,
attributedText: AttributedString,
start: Int,
end: Int,
color: Color = .accentColor,
font: Font = .system(size: 15.0, weight: .regular, design: .rounded)
) -> AttributedString {
var retString = attributedText
let utf8View = text.utf8
let highlightedRange: Range<AttributedString.Index>? = {
guard let utf8Start = utf8View.index(utf8View.startIndex, offsetBy: start, limitedBy: utf8View.endIndex),
let utf8End = utf8View.index(utf8Start, offsetBy: end - start, limitedBy: utf8View.endIndex),
let stringStart = String.Index(utf8Start, within: text),
let stringEnd = String.Index(utf8End, within: text),
let attrStart = AttributedString.Index(stringStart, within: attributedText),
let attrEnd = AttributedString.Index(stringEnd, within: attributedText) else { return nil }
return attrStart..<attrEnd
}()
if let highlightedRange {
retString[highlightedRange].foregroundColor = color
retString[highlightedRange].font = font
}
return retString
} |
Thanks for posting up a solution. I'll take this into account in a future update so you don't have to make a helper method yourself. ATProtoKit should be responsible with handling this, ultimately. |
Summary
Returned facets indexes are not always aligned with text
Reproduction Steps
Probably due to the emoji handling in terms of bytes, the returned start and end indexes are not always usable.
For example, take into account the following record.text:
The returned indexes are as follow:
But they're not working nor aligned with the text, producing a glitched output as well.
Expected Results
Actual Results
What operating systems did you experience this bug? (We'll count Docker as an operating system.)
macOS
Operating System Version
14.6.1 (23G93)
ATProtoKit Version
0.19.2
Additional Context
No response
The text was updated successfully, but these errors were encountered: