2017-03-09 07:36:09 +01:00
|
|
|
//
|
|
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
@objc class DisplayableTextFilter: NSObject {
|
|
|
|
|
2017-03-19 14:44:53 +01:00
|
|
|
let TAG = "[DisplayableTextFilter]"
|
2017-03-09 07:36:09 +01:00
|
|
|
|
2017-03-19 14:44:53 +01:00
|
|
|
@objc
|
|
|
|
func displayableText(_ text: String?) -> String? {
|
2017-03-09 07:36:09 +01:00
|
|
|
guard let text = text else {
|
2017-03-19 14:44:53 +01:00
|
|
|
return nil
|
2017-03-09 07:36:09 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 14:44:53 +01:00
|
|
|
if (self.hasExcessiveDiacriticals(text: text)) {
|
2017-03-22 20:03:04 +01:00
|
|
|
Logger.warn("\(TAG) filtering text for excessive diacriticals.")
|
|
|
|
let filteredText = text.folding(options: .diacriticInsensitive, locale: .current)
|
|
|
|
return filteredText
|
2017-03-09 07:36:09 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 14:44:53 +01:00
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
|
|
|
private func hasExcessiveDiacriticals(text: String) -> Bool {
|
|
|
|
// discard any zalgo style text, by detecting maximum number of glyphs per character
|
|
|
|
for char in text.characters.enumerated() {
|
|
|
|
let scalarCount = String(char.element).unicodeScalars.count
|
|
|
|
if scalarCount > 4 {
|
2017-03-22 20:03:04 +01:00
|
|
|
Logger.warn("\(TAG) detected excessive diacriticals at \(char.element) scalarCount: \(scalarCount)")
|
2017-03-19 14:44:53 +01:00
|
|
|
return true
|
|
|
|
}
|
2017-03-09 07:36:09 +01:00
|
|
|
}
|
2017-03-19 14:44:53 +01:00
|
|
|
|
|
|
|
return false
|
2017-03-09 07:36:09 +01:00
|
|
|
}
|
|
|
|
}
|