File tree Expand file tree Collapse file tree 5 files changed +56
-11
lines changed
main/kotlin/dev/sphericalkat/sublimefuzzy
test/kotlin/dev/sphericalkat Expand file tree Collapse file tree 5 files changed +56
-11
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -5,11 +5,19 @@ package dev.sphericalkat
55
66import dev.sphericalkat.sublimefuzzy.Fuzzy
77import kotlin.test.Test
8+ import kotlin.test.assertFalse
89import 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}
You can’t perform that action at this time.
0 commit comments