Spotlight has problems with case in text search

Happy Thanksgiving!

On the day when Americans celebrate a tradition dating back to the seventeenth century, this article looks back to the early years of this century, when Spotlight came to our Macs.

Normally when we search for text, whether in the name of a file or in its contents, we don’t want it to be sensitive to case. If you’re looking for the word runcible, you want to find it whether it’s the first word in a sentence and capitalised as Runcible, someone shouting RUNCIBLE, or even the victim of a typo turning it into runCible, perhaps.

As I explained a little earlier this week, this is more complex than merely dealing with standard upper and lower cases such as a and A. One notable exception is with the German letters ss, SS, ẞ and ß, which are all considered to be case variants of the same letter. There are other examples you will come across in typographically set text, such as ligatures, where the single Unicode character for ffi should be treated as a case variant of the three letters ffi, and more.

The case for case folding has been eloquently argued for Elasticsearch, and is explained in full detail in the Unicode FAQ.

Having castigated some third-party installers that don’t take into account how this works with case-insensitive APFS, I thought I had better check that macOS has its own house in order. I’m afraid the answer is that it hasn’t: Spotlight search doesn’t behave as it should.

Tests

I used my free Spotlight test utility SpotTest to search for a series of crafted words in two text files, one of which contained those using the more non-Roman variants, such as straẞe, and the other contained their case folded form, such as strasse. In all I used six test words, four reliant on correct case-folding, one involving both case-folding and Unicode normalisation forms, and a trick word which shouldn’t undergo case-folding at all:

  1. straẞe, 73 74 72 61 e1 ba 9e 65, folds to strasse, 73 74 72 61 73 73 65, one byte shorter;
  2. flourishing, ef ac 82 6f 75 72 69 73 68 69 6e 67, folds to flourishing, 66 6c 6f 75 72 69 73 68 69 6e 67, one byte shorter;
  3. efficiency, 65 ef ac 83 63 69 65 6e 63 79, folds to efficiency, 65 66 66 69 63 69 65 6e 63 79, the same length;
  4. first, 66 69 72 ef ac 86, folds to first, 66 69 72 73 74, one byte shorter;
  5. Ångstroms, e2 84 ab 6e 67 73 74 72 6f 6d 73, folds to ångstroms, c3 a5 6e 67 73 74 72 6f 6d 73, one byte shorter, but also normalises to Form C Ångstroms, c3 85 6e 67 73 74 72 6f 6d 73, one byte shorter;
  6. njinx, c7 8c 69 6e 78, which doesn’t undergo case folding or normalisation.

I also used the same text files with in-app text Find, in my free RTF editor DelightEd, which relies entirely on the default macOS API, and checked the Foundation API’s folding(options:local:) method for NSString as a reference. An alternative from CoreFoundation would have been CFStringFold().

Results

Tests 1 (straẞe) and 6 (njinx) performed as expected, confirming that in this respect case folding is performed in accordance with the Unicode standard.

Tests 2, 3 and 4 were unable to find the form not used in the test text. For example, searching for first found only first and not first, while searching for first found only first and not first. Case folding didn’t happen as expected. That applied to only global Spotlight search, and in-app Find worked as expected, finding the case variants.

Test 5 found both unnormalised and normalised forms in the text with folded case, but in the other file only the unfolded form could be found. Again, in-app Find found all variants correctly.

It’s apparent that in Spotlight search, case folding isn’t performed correctly on some characters, notably ligatures, although it is on ss/SS/ẞ/ß.

Do these characters occur?

Although uncommon if not rare, ligature characters are used in some text, for example they can occur in web pages, and are less uncommon in PDF documents created by software using typographic features. The ffi character is among the least infrequent, because of its potential occurrence in words derived from efficient and office.

As the only workaround, to search explicitly for versions of words containing ligature characters, is so arcane that it’s impractical.

The fact that only the most common case folding is applied by Spotlight suggests this might have been a deliberate design decision, made at the time in the light of the computational cost. Perhaps this merits reconsideration now that our Macs have ample capacity for them to use AI.

Conclusions

  • Spotlight search only appear to perform case folding for ss/SS/ẞ/ß characters, and not for ligatures like ffi. It thus isn’t fully compliant with current Unicode standards.
  • The only workaround requires manually extending searches to include characters that aren’t folded correctly, which is complicated.
  • Although characters affected are relatively rare, they do occur in some documents, and failing to find them is a significant shortcoming.
  • Given that in-app Find appears to perform correctly, it surely shouldn’t be difficult to correct this for Spotlight.
  • These shortcomings should have been easy to detect if Spotlight search was checked against a test suite, a procedure that’s easy to automate and widely advocated as best practice.