blob: f9d027452f54184b3f886fd2c5fa3de03bff35da (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
//
// String+FuzzyMatch.swift
// MullvadVPN
//
// Created by Jon Petersson on 2023-04-02.
// Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import Foundation
extension String {
func fuzzyMatch(_ needle: String) -> Bool {
guard !needle.isEmpty else { return false }
let haystack = lowercased()
let needle = needle.lowercased()
var indices: [Index] = []
var remainder = needle[...].utf8
for index in haystack.utf8.indices {
let character = haystack.utf8[index]
if character == remainder[remainder.startIndex] {
indices.append(index)
remainder.removeFirst()
if remainder.isEmpty {
return !indices.isEmpty
}
}
}
return false
}
}
|