Implement star pattern matching (hopefully correct)
Some checks failed
CI / test (push) Failing after 53s
CI / test-core (14, ubuntu-latest, 3.x, nodejs) (push) Failing after 2m29s
CI / test-core (14, ubuntu-latest, 3.x, js) (push) Failing after 2m9s
CI / test-core (14, ubuntu-latest, 3.x, py) (push) Failing after 2m40s
CI / test-core (14, ubuntu-latest, 3.x, cpp) (push) Failing after 2m45s
CI / test-core (14, ubuntu-latest, 3.x, interp) (push) Failing after 2m30s
Some checks failed
CI / test (push) Failing after 53s
CI / test-core (14, ubuntu-latest, 3.x, nodejs) (push) Failing after 2m29s
CI / test-core (14, ubuntu-latest, 3.x, js) (push) Failing after 2m9s
CI / test-core (14, ubuntu-latest, 3.x, py) (push) Failing after 2m40s
CI / test-core (14, ubuntu-latest, 3.x, cpp) (push) Failing after 2m45s
CI / test-core (14, ubuntu-latest, 3.x, interp) (push) Failing after 2m30s
This commit is contained in:
8
src/kiss_tools/PatternUtil.hx
Normal file
8
src/kiss_tools/PatternUtil.hx
Normal file
@@ -0,0 +1,8 @@
|
||||
package kiss_tools;
|
||||
|
||||
import kiss.Prelude;
|
||||
import kiss.List;
|
||||
using StringTools;
|
||||
|
||||
@:build(kiss.Kiss.build())
|
||||
class PatternUtil {}
|
||||
27
src/kiss_tools/PatternUtil.kiss
Normal file
27
src/kiss_tools/PatternUtil.kiss
Normal file
@@ -0,0 +1,27 @@
|
||||
(function indicesOf [:String whole :String part &opt :Int startIdx]
|
||||
(unless startIdx (set startIdx 0))
|
||||
(let [indices []
|
||||
&mut nextIdx -1]
|
||||
(while !(= (set nextIdx (whole.indexOf part startIdx)) -1)
|
||||
(indices.push nextIdx)
|
||||
(set startIdx (+ nextIdx 1)))
|
||||
indices))
|
||||
|
||||
(function _starMatch [:Array<String> parts :String test]
|
||||
(unless (test.startsWith $>1 (first parts))
|
||||
(return false))
|
||||
(let [r (rest parts)]
|
||||
(case r
|
||||
([] (return true))
|
||||
([""] (return true))
|
||||
([last] (return (test.endsWith last)))
|
||||
(more (let [indices (indicesOf test (first r) .length $1)]
|
||||
(doFor index indices
|
||||
(when (_starMatch r (test.substr index))
|
||||
(return true)))
|
||||
(return false)))
|
||||
(never null))))
|
||||
|
||||
// Match patterns like "Part 1*Part 2" with text like "Part 1 <something> Part 2"
|
||||
(function starMatch [:String pattern :String test]
|
||||
(_starMatch (pattern.split "*") test))
|
||||
@@ -2,6 +2,8 @@
|
||||
(import sys.FileSystem)
|
||||
(import kiss_tools.JsonMap)
|
||||
(import kiss_tools.JsonArray)
|
||||
(import kiss_tools.PatternUtil)
|
||||
|
||||
|
||||
// Test FuzzyJson
|
||||
(loadFrom "kiss-tools" "src/kiss_tools/FuzzyJson.kiss")
|
||||
@@ -40,4 +42,14 @@
|
||||
// Uncomment this line when expanding the test:
|
||||
// (File.saveContent "test/map-end.json" (File.getContent TEST_JSON_FILE))
|
||||
(assertEquals (File.getContent "test/map-end.json") (File.getContent TEST_JSON_FILE))
|
||||
(FileSystem.deleteFile TEST_JSON_FILE)
|
||||
(FileSystem.deleteFile TEST_JSON_FILE)
|
||||
|
||||
|
||||
(assert (PatternUtil.starMatch "*" "test"))
|
||||
(assert (PatternUtil.starMatch "t*t" "test"))
|
||||
(assert (PatternUtil.starMatch "*t" "test"))
|
||||
(assert (PatternUtil.starMatch "t*" "test"))
|
||||
(assert (PatternUtil.starMatch "t*s*" "test"))
|
||||
(assert (PatternUtil.starMatch "*s*" "test"))
|
||||
(assert !(PatternUtil.starMatch "s*" "test"))
|
||||
(assert !(PatternUtil.starMatch "*s" "test"))
|
||||
Reference in New Issue
Block a user