Skip to content

Commit 9f10b23

Browse files
committed
feat: implement simple fuzzy match
Signed-off-by: SphericalKat <amolele@gmail.com>
1 parent 262e0e5 commit 9f10b23

File tree

5 files changed

+56
-11
lines changed

5 files changed

+56
-11
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package dev.sphericalkat.sublimefuzzy
2+
3+
object Constants {
4+
const val SEQUENTIAL_BONUS = 15
5+
const val SEPARATOR_BONUS = 30
6+
const val CAMEL_BONUS = 30
7+
const val FIRST_LETTER_BONUS = 15
8+
9+
const val LEADING_LETTER_PENALTY = -5
10+
const val MAX_LEADING_LETTER_PENALTY = -15
11+
const val UNMATCHED_LETTER_PENALTY = -1
12+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package dev.sphericalkat.sublimefuzzy
2+
3+
object Fuzzy {
4+
/**
5+
* Returns true if each character in pattern is found sequentially within str
6+
*
7+
* @param pattern the pattern to match
8+
* @param str the string to search
9+
*/
10+
fun fuzzyMatchSimple(pattern: String, str: String): Boolean {
11+
var patternIdx = 0
12+
var strIdx = 0
13+
14+
val patternLength = pattern.length
15+
val strLength = str.length
16+
17+
while (patternIdx != patternLength && strIdx != strLength) {
18+
val patternChar = pattern.toCharArray()[patternIdx].toLowerCase()
19+
val strChar = str.toCharArray()[strIdx].toLowerCase()
20+
if (patternChar == strChar) ++patternIdx
21+
++strIdx
22+
}
23+
24+
return patternLength != 0 && strLength != 0 && patternIdx == patternLength
25+
}
26+
}

lib/src/main/kotlin/dev/sphericalkat/sublimefuzzy/Library.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.

lib/src/test/kotlin/dev/sphericalkat/LibraryTest.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@ package dev.sphericalkat
55

66
import dev.sphericalkat.sublimefuzzy.Fuzzy
77
import kotlin.test.Test
8+
import kotlin.test.assertFalse
89
import kotlin.test.assertTrue
910

10-
class LibraryTest {
11-
@Test fun testSomeLibraryMethod() {
12-
val classUnderTest = Fuzzy()
13-
assertTrue(classUnderTest.someLibraryMethod(), "someLibraryMethod should return 'true'")
11+
class FuzzyTest {
12+
@Test
13+
fun `fuzzy match simple success`() {
14+
val result = Fuzzy.fuzzyMatchSimple("test", "tests")
15+
assertTrue(result, "Each character in pattern should be found sequentially in str")
16+
}
17+
18+
@Test
19+
fun `fuzzy match simple failure`() {
20+
val result = Fuzzy.fuzzyMatchSimple("test", "tset")
21+
assertFalse(result, "Each character in pattern should be found sequentially in str")
1422
}
1523
}

0 commit comments

Comments
 (0)