Skip to content

About

A simple library for macOS, iOS and tvOS that enables fast, in-memory, prefix-based string searching using a trie.

Resources

Stars

4 stars

Watchers

1 watching

Forks

Repository files navigation

StringSearchKit

A simple library for macOS, iOS and tvOS that enables fast, in-memory, prefix-based string searching using a trie.

CI License

Requirements

  • macOS 12+
  • iOS 15+
  • tvOS 15+
  • Swift 5.9+

Installation

Swift Package Manager

In Xcode, choose File > Add Package Dependencies… and enter https://github.com/mabidakun/StringSearchKit.git.

Or add it to your Package.swift:

dependencies: [
    .package(url: "https://github.com/mabidakun/StringSearchKit.git", from: "3.0.0")
]

Then add "StringSearchKit" to your target's dependencies.

CocoaPods

CocoaPods Trunk goes read-only on 2 December 2026, so 3.0.0 is the final version published there. Swift Package Manager is the supported route for future releases.

pod "StringSearchKit"

Why is this framework useful?

Because it makes it really easy to implement Auto-Complete functionality in an application - as I did in my macOS™ app - Sequence Diagram

How does it work?

StringDictionary stores its strings in a Trie: one node per character, with each string as a path from the root. A prefix search walks down the path for the prefix, then collects every string below that node, so its speed depends on the length of the prefix and the number of matches, not on the size of the dictionary.

A Trie that holds act, app, apple, apologise and apology. A search for the prefix app walks root, a, p, p, then collects app and apple.

How do I get started?

Usage

  • StringDictionary(withStrings: [String], preserveCase: Bool = false)
  • StringDictionary(withTextFileNamed: <A name of a text file in your Bundle>, bundle: Bundle = .main, preserveCase: Bool = false)
  • StringDictionary(withTextFilepath: <The path to a text file>, preserveCase: Bool = false)

Using one of the initialisers above, create an instance of StringDictionary.

When initialising from a file, it should simply contain one string per-line.

Strings are not limited to being single words - but are in the examples, for simplicity.

Example file contents :-

act
apologise
apology
app
apple

Once instantiated, use the StringDictionary to quickly find strings (case-insensitively) with a given prefix.

let stringDictionary = StringDictionary(withStrings: ["act", "app", "apple", "apologise", "apology"])
let searchResults = stringDictionary.strings(withPrefix: "app")

// searchResults will then contain ["app", "apple"]

Call strings() with no prefix to get every string in the dictionary.

let allStrings = stringDictionary.strings()

// allStrings will then contain ["act", "apologise", "apology", "app", "apple"]

Search Result Case Preservation

If you set preserveCase: true in any of the init methods (default is false for backwards compatibility), the results will be returned in the same case that the original words were in when added.

For example:

let stringDictionary = StringDictionary(withStrings: ["Act", "App", "Apple", "Apologise", "Apology"], preserveCase: true)
let searchResults = stringDictionary.strings(withPrefix: "app")

// searchResults will then contain ["App", "Apple"]

Matching Rules

  • Matching ignores case, including Unicode case rules: "Straße" matches the prefix "strass", and "İstanbul" matches "ist".
  • A prefix ignores leading whitespace, but trailing whitespace is part of it: "new " matches "new york" but not "newark".
  • Text files may use \n or \r\n line endings. Empty and whitespace-only lines are skipped.
  • Strings that differ only in case are stored once. The one added last decides the case of the result when preserveCase is true.

Author

Mike O. Abidakun github@mikeosoft.co.uk

License

StringSearchKit is available under the MIT license. See the LICENSE file for more info.

About

A simple library for macOS, iOS and tvOS that enables fast, in-memory, prefix-based string searching using a trie.

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages