Grant Emerson

Same String, Different Diacritics

Same String, Different Diacritics โ€” SwiftMoji Entry #7

When searching a database for text entry, it can often be useful to compare results with a predicate without taking into account any differences in capitalization or accents. The Foundation framework has a convenient method called folding(options:locale:) for standardizing Strings for comparison. It is an instance method on String and can be called with dot syntax. The first parameter labeled options takes in an Array of Strings.CompareOptions. CompareOptions are values representing how search algorithms should compare Strings. For example, diacriticInsensitive disregards accents and caseInsensitive omits the need for matching capitalization. The locale parameter simply provides the linguistic context for removing character distinctions. The method is called folding because it performs what is often referred to as character folding. Character folding is the process of transforming a character into another one of its representations. In the code example, the โ€œรกโ€ in ๐Ÿ‘จโ€๐ŸŒพโ€™s name is folded to โ€œaโ€ and the โ€œSโ€ in ๐Ÿ‘จโ€๐Ÿ’ปโ€™s name is folded to โ€œsโ€. Lastly, if you wish to explore the capabilities of folding further, the documentation can be found under the Objective-C version on NSString. The folding method was ported over to StringProtocol, but all the corresponding information regarding its usage remains absent.

import Foundation

struct ๐Ÿ‘ค {
    var name: String
    var comparisonName: String {
        name.folding(options: [.diacriticInsensitive, .widthInsensitive, .caseInsensitive],
                     locale: .current)
    }
}

var ๐Ÿ‘จโ€๐Ÿ’ป = ๐Ÿ‘ค(name: "Sean")
var ๐Ÿ‘จโ€๐ŸŒพ = ๐Ÿ‘ค(name: "seรกn")

if ๐Ÿ‘จโ€๐Ÿ’ป.comparisonName == ๐Ÿ‘จโ€๐ŸŒพ.comparisonName {
    print("๐Ÿ‘จโ€๐Ÿ’ป and ๐Ÿ‘จโ€๐ŸŒพ have the same name.") // โœ…
}
Tagged with: