|
|
@ -17,8 +17,8 @@ public final class MentionUtilities : NSObject {
|
|
|
|
var string = string
|
|
|
|
var string = string
|
|
|
|
let regex = try! NSRegularExpression(pattern: "@[0-9a-fA-F]*", options: [])
|
|
|
|
let regex = try! NSRegularExpression(pattern: "@[0-9a-fA-F]*", options: [])
|
|
|
|
let knownPublicKeys = MentionsManager.userPublicKeyCache[threadID] ?? [] // Should always be populated at this point
|
|
|
|
let knownPublicKeys = MentionsManager.userPublicKeyCache[threadID] ?? [] // Should always be populated at this point
|
|
|
|
var mentions: [(range: NSRange, hexEncodedPublicKey: String)] = []
|
|
|
|
var mentions: [(range: NSRange, publicKey: String)] = []
|
|
|
|
var outerMatch = regex.firstMatch(in: string, options: .withoutAnchoringBounds, range: NSRange(location: 0, length: string.count))
|
|
|
|
var outerMatch = regex.firstMatch(in: string, options: .withoutAnchoringBounds, range: NSRange(location: 0, length: string.utf16.count))
|
|
|
|
while let match = outerMatch {
|
|
|
|
while let match = outerMatch {
|
|
|
|
let publicKey = String((string as NSString).substring(with: match.range).dropFirst()) // Drop the @
|
|
|
|
let publicKey = String((string as NSString).substring(with: match.range).dropFirst()) // Drop the @
|
|
|
|
let matchEnd: Int
|
|
|
|
let matchEnd: Int
|
|
|
@ -35,15 +35,15 @@ public final class MentionUtilities : NSObject {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let displayName = displayName {
|
|
|
|
if let displayName = displayName {
|
|
|
|
string = (string as NSString).replacingCharacters(in: match.range, with: "@\(displayName)")
|
|
|
|
string = (string as NSString).replacingCharacters(in: match.range, with: "@\(displayName)")
|
|
|
|
mentions.append((range: NSRange(location: match.range.location, length: displayName.count + 1), hexEncodedPublicKey: publicKey)) // + 1 to include the @
|
|
|
|
mentions.append((range: NSRange(location: match.range.location, length: displayName.utf16.count + 1), publicKey: publicKey)) // + 1 to include the @
|
|
|
|
matchEnd = match.range.location + displayName.count
|
|
|
|
matchEnd = match.range.location + displayName.utf16.count
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
matchEnd = match.range.location + match.range.length
|
|
|
|
matchEnd = match.range.location + match.range.length
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
matchEnd = match.range.location + match.range.length
|
|
|
|
matchEnd = match.range.location + match.range.length
|
|
|
|
}
|
|
|
|
}
|
|
|
|
outerMatch = regex.firstMatch(in: string, options: .withoutAnchoringBounds, range: NSRange(location: matchEnd, length: string.count - matchEnd))
|
|
|
|
outerMatch = regex.firstMatch(in: string, options: .withoutAnchoringBounds, range: NSRange(location: matchEnd, length: string.utf16.count - matchEnd))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let result = NSMutableAttributedString(string: string, attributes: attributes)
|
|
|
|
let result = NSMutableAttributedString(string: string, attributes: attributes)
|
|
|
|
mentions.forEach { mention in
|
|
|
|
mentions.forEach { mention in
|
|
|
|