Skip to content

Commit

Permalink
switched to non regex based
Browse files Browse the repository at this point in the history
  • Loading branch information
gniquil committed Apr 7, 2015
1 parent 680146f commit 2014e49
Show file tree
Hide file tree
Showing 15 changed files with 1,345 additions and 825 deletions.
728 changes: 38 additions & 690 deletions lib/ex_minimatch.ex

Large diffs are not rendered by default.

718 changes: 718 additions & 0 deletions lib/ex_minimatch/compiler.ex

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions lib/ex_minimatch/helper.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
defmodule ExMinimatch.Helper do
import Dict, only: [merge: 2]

def debug(obj, options) do
if options[:log] in [:debug], do: IO.inspect(obj)
end

def info(obj, options) do
if options[:log] in [:info, :debug], do: IO.inspect(obj)
end

# preserves the state
def tap(state, sideback) do
sideback.(state)

state
end

def transform(state, callback) do
callback.(state)
end

def len(a) when is_binary(a), do: String.length(a)
def len(a), do: length(a)

def at(a, i) when is_binary(a), do: String.at(a, i)
def at(a, i), do: Enum.at(a, i)

def slice(a, rng) when is_binary(a), do: String.slice(a, rng)
def slice(a, rng), do: Enum.slice(a, rng)

def slice(a, i, l) when is_binary(a), do: String.slice(a, i, l)
def slice(a, i, l), do: Enum.slice(a, i, l)

# minimatch.js supports 2 types of matching, one's regex based, one is below
# here we don't support the 2nd type as there are too many inconsistencies
# between the versions.


end
226 changes: 185 additions & 41 deletions test/basic/apple_bash_glob_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule AppleBashGlobTest do
use ExUnit.Case

import ExMinimatch, only: [match: 2, match: 3]
import Enum, only: [filter: 2, sort: 1]
import ExMinimatch, only: [match: 2, match: 3, compile: 1, fnmatch: 2, fnfilter: 2]
import Enum, only: [sort: 1]

IO.puts "Test cases for: http://www.opensource.apple.com/source/bash/bash-23/bash/tests/glob-test"

Expand Down Expand Up @@ -34,151 +34,295 @@ defmodule AppleBashGlobTest do
]

test "*/man*/bash.*" do
assert @files |> filter(fn file -> match(file, "*/man*/bash.*") end) |> sort == ["man/man1/bash.1"]
matcher = compile("*/man*/bash.*")

# assert matcher.regex == Regex.compile!("^(?:(?!\\.)(?=.)[^/]*?\\/(?=.)man[^/]*?\\/(?=.)bash\\.[^/]*?)$")

assert @files |> fnfilter(matcher) |> sort == ["man/man1/bash.1"] |> sort
end

test "man/man1/bash.1" do
assert @files |> filter(fn file -> match(file, "man/man1/bash.1") end) |> sort == ["man/man1/bash.1"]
matcher = compile("man/man1/bash.1")

# assert matcher.regex == Regex.compile!("^(?:man\\/man1\\/bash\\.1)$")

assert @files |> fnfilter(matcher) |> sort == ["man/man1/bash.1"]
end

test "a***c" do
assert ["abc"] |> filter(fn file -> match(file, "a***c") end) |> sort == ["abc"]
matcher = compile("a***c")

# assert matcher.regex == Regex.compile!("^(?:(?=.)a[^/]*?[^/]*?[^/]*?c)$")

assert ["abc"] |> fnfilter(matcher) |> sort == ["abc"]
end

test "a*****?c" do
assert ["abc"] |> filter(fn file -> match(file, "a*****?c") end) |> sort == ["abc"]
matcher = compile("a*****?c")

# assert matcher.regex == Regex.compile!("^(?:(?=.)a[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]c)$")

assert ["abc"] |> fnfilter(matcher) |> sort == ["abc"]
end

test "?*****??" do
assert ["abc"] |> filter(fn file -> match(file, "?*****??") end) |> sort == ["abc"]
matcher = compile("?*****??")

# assert matcher.regex == Regex.compile!("^(?:(?!\\.)(?=.)[^/][^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/][^/])$")

assert ["abc"] |> fnfilter(matcher) |> sort == ["abc"]
end

test "*****??" do
assert ["abc"] |> filter(fn file -> match(file, "*****??") end) |> sort == ["abc"]
matcher = compile("*****??")

# assert matcher.regex == Regex.compile!("^(?:(?!\\.)(?=.)[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/][^/])$")

assert ["abc"] |> fnfilter(matcher) |> sort == ["abc"]
end

test "?*****?c" do
assert ["abc"] |> filter(fn file -> match(file, "?*****?c") end) |> sort == ["abc"]
matcher = compile("?*****?c")

# assert matcher.regex == Regex.compile!("^(?:(?!\\.)(?=.)[^/][^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]c)$")

assert ["abc"] |> fnfilter(matcher) |> sort == ["abc"]
end

test "?***?****c" do
assert ["abc"] |> filter(fn file -> match(file, "?***?****c") end) |> sort == ["abc"]
matcher = compile("?***?****c")

# assert matcher.regex == Regex.compile!("^(?:(?!\\.)(?=.)[^/][^/]*?[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/]*?[^/]*?c)$")

assert ["abc"] |> fnfilter(matcher) |> sort == ["abc"]
end

test "?***?****?" do
assert ["abc"] |> filter(fn file -> match(file, "?***?****?") end) |> sort == ["abc"]
matcher = compile("?***?****?")

# assert matcher.regex == Regex.compile!("^(?:(?!\\.)(?=.)[^/][^/]*?[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/]*?[^/]*?[^/])$")

assert ["abc"] |> fnfilter(matcher) |> sort == ["abc"]
end

test "?***?****" do
assert ["abc"] |> filter(fn file -> match(file, "?***?****") end) |> sort == ["abc"]
matcher = compile("?***?****")

# assert matcher.regex == Regex.compile!("^(?:(?!\\.)(?=.)[^/][^/]*?[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/]*?[^/]*?)$")

assert ["abc"] |> fnfilter(matcher) |> sort == ["abc"]
end

test "*******c" do
assert ["abc"] |> filter(fn file -> match(file, "*******c") end) |> sort == ["abc"]
matcher = compile("*******c")

# assert matcher.regex == Regex.compile!("^(?:(?!\\.)(?=.)[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?c)$")

assert ["abc"] |> fnfilter(matcher) |> sort == ["abc"]
end

test "*******?" do
assert ["abc"] |> filter(fn file -> match(file, "*******?") end) |> sort == ["abc"]
matcher = compile("*******?")

# assert matcher.regex == Regex.compile!("^(?:(?!\\.)(?=.)[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/])$")

assert ["abc"] |> fnfilter(matcher) |> sort == ["abc"]
end

test "a*cd**?**??k" do
assert ["abcdecdhjk"] |> filter(fn file -> match(file, "a*cd**?**??k") end) |> sort == ["abcdecdhjk"]
matcher = compile("a*cd**?**??k")

# assert matcher.regex == Regex.compile!("^(?:(?=.)a[^/]*?cd[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/][^/]k)$")

assert ["abcdecdhjk"] |> fnfilter(matcher) |> sort == ["abcdecdhjk"]
end

test "a**?**cd**?**??k" do
assert ["abcdecdhjk"] |> filter(fn file -> match(file, "a**?**cd**?**??k") end) |> sort == ["abcdecdhjk"]
matcher = compile("a**?**cd**?**??k")

# assert matcher.regex == Regex.compile!("^(?:(?=.)a[^/]*?[^/]*?[^/][^/]*?[^/]*?cd[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/][^/]k)$")

assert ["abcdecdhjk"] |> fnfilter(matcher) |> sort == ["abcdecdhjk"]
end

test "a**?**cd**?**??k***" do
assert ["abcdecdhjk"] |> filter(fn file -> match(file, "a**?**cd**?**??k***") end) |> sort == ["abcdecdhjk"]
matcher = compile("a**?**cd**?**??k***")

# assert matcher.regex == Regex.compile!("^(?:(?=.)a[^/]*?[^/]*?[^/][^/]*?[^/]*?cd[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/][^/]k[^/]*?[^/]*?[^/]*?)$")

assert ["abcdecdhjk"] |> fnfilter(matcher) |> sort == ["abcdecdhjk"]
end

test "a**?**cd**?**??***k" do
assert ["abcdecdhjk"] |> filter(fn file -> match(file, "a**?**cd**?**??***k") end) |> sort == ["abcdecdhjk"]
matcher = compile("a**?**cd**?**??***k")

# assert matcher.regex == Regex.compile!("^(?:(?=.)a[^/]*?[^/]*?[^/][^/]*?[^/]*?cd[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/][^/][^/]*?[^/]*?[^/]*?k)$")

assert ["abcdecdhjk"] |> fnfilter(matcher) |> sort == ["abcdecdhjk"]
end

test "a**?**cd**?**??***k**" do
assert ["abcdecdhjk"] |> filter(fn file -> match(file, "a**?**cd**?**??***k**") end) |> sort == ["abcdecdhjk"]
matcher = compile("a**?**cd**?**??***k**")

# assert matcher.regex == Regex.compile!("^(?:(?=.)a[^/]*?[^/]*?[^/][^/]*?[^/]*?cd[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/][^/][^/]*?[^/]*?[^/]*?k[^/]*?[^/]*?)$")

assert ["abcdecdhjk"] |> fnfilter(matcher) |> sort == ["abcdecdhjk"]
end

test "a****c**?**??*****" do
assert ["abcdecdhjk"] |> filter(fn file -> match(file, "a****c**?**??*****") end) |> sort == ["abcdecdhjk"]
matcher = compile("a****c**?**??*****")

# assert matcher.regex == Regex.compile!("^(?:(?=.)a[^/]*?[^/]*?[^/]*?[^/]*?c[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/][^/][^/]*?[^/]*?[^/]*?[^/]*?[^/]*?)$")

assert ["abcdecdhjk"] |> fnfilter(matcher) |> sort == ["abcdecdhjk"]
end

test "[-abc]" do
assert ["-"] |> filter(fn file -> match(file, "[-abc]") end) |> sort == ["-"]
matcher = compile("[-abc]")

# assert matcher.regex == Regex.compile!("^(?:(?!\\.)(?=.)[-abc])$")

assert ["-"] |> fnfilter(matcher) |> sort == ["-"]
end

test "[abc-]" do
assert ["-"] |> filter(fn file -> match(file, "[abc-]") end) |> sort == ["-"]
matcher = compile("[abc-]")

# assert matcher.regex == Regex.compile!("^(?:(?!\\.)(?=.)[abc-])$")

assert ["-"] |> fnfilter(matcher) |> sort == ["-"]
end

test "\\" do
assert ["\\"] |> filter(fn file -> match(file, "\\") end) |> sort == ["\\"]
matcher = compile("\\")

# assert matcher.regex == Regex.compile!("^(?:\\\\)$")

assert ["\\"] |> fnfilter(matcher) |> sort == ["\\"]
end

test "[\\\\]" do
assert ["\\"] |> filter(fn file -> match(file, "[\\\\]") end) |> sort == ["\\"]
matcher = compile("[\\\\]")

# assert matcher.regex == Regex.compile!("^(?:(?!\\.)(?=.)[\\\\])$")

assert ["\\"] |> fnfilter(matcher) |> sort == ["\\"]
end

test "[[]" do
assert ["["] |> filter(fn file -> match(file, "[[]") end) |> sort == ["["]
matcher = compile("[[]")

# assert matcher.regex == Regex.compile!("^(?:(?!\\.)(?=.)[\\[])$")

assert ["["] |> fnfilter(matcher) == ["["]
end

test "[" do
assert ["["] |> filter(fn file -> match(file, "[") end) |> sort == ["["]
matcher = compile("[")

# assert matcher.regex == Regex.compile!("^(?:\\[)$")

assert ["["] |> fnfilter(matcher) == ["["]
end

test "[*" do
assert ["[abc"] |> filter(fn file -> match(file, "[*") end) |> sort == ["[abc"]
matcher = compile("[*")

# assert matcher.regex == Regex.compile!("^(?:(?=.)\\[(?!\\.)(?=.)[^/]*?)$")

assert ["[abc"] |> fnfilter(matcher) == ["[abc"]
end

# a right bracket shall lose its special meaning and
# represent itself in a bracket expression if it occurs
# first in the list. -- POSIX.2 2.8.3.2
# # a right bracket shall lose its special meaning and
# # represent itself in a bracket expression if it occurs
# # first in the list. -- POSIX.2 2.8.3.2

test "[]]" do
assert ["]"] |> filter(fn file -> match(file, "[]]") end) |> sort == ["]"]
matcher = compile("[]]")

# assert matcher.regex == Regex.compile!("^(?:(?!\\.)(?=.)[\\]])$")

assert ["]"] |> fnfilter(matcher) |> sort == ["]"]
end

test "[]-]" do
assert ["]"] |> filter(fn file -> match(file, "[]-]") end) |> sort == ["]"]
matcher = compile("[]-]")

# assert matcher.regex == Regex.compile!("^(?:(?!\\.)(?=.)[\\]-])$")

assert ["]"] |> fnfilter(matcher) |> sort == ["]"]
end

test "[a-\z]" do
assert ["p"] |> filter(fn file -> match(file, "[a-\z]") end) |> sort ==["p"]
matcher = compile("[a-\z]")

# assert matcher.regex == Regex.compile!("^(?:(?!\\.)(?=.)[a-z])$")

assert ["p"] |> fnfilter(matcher) |> sort ==["p"]
end

test "??**********?****?" do
assert ["abc"] |> filter(fn file -> match(file, "??**********?****?") end) |> sort == []
matcher = compile("??**********?****?")

# assert matcher.regex == Regex.compile!("^(?:(?!\\.)(?=.)[^/][^/][^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/]*?[^/]*?[^/])$")

assert ["abc"] |> fnfilter(matcher) |> sort == []
end

test "??**********?****c" do
assert ["abc"] |> filter(fn file -> match(file, "??**********?****c") end) |> sort == []
matcher = compile("??**********?****c")

# assert matcher.regex == Regex.compile!("^(?:(?!\\.)(?=.)[^/][^/][^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/]*?[^/]*?c)$")

assert ["abc"] |> fnfilter(matcher) |> sort == []
end

test "?************c****?****" do
assert ["abc"] |> filter(fn file -> match(file, "?************c****?****") end) |> sort == []
matcher = compile("?************c****?****")

# assert matcher.regex == Regex.compile!("^(?:(?!\\.)(?=.)[^/][^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?c[^/]*?[^/]*?[^/]*?[^/]*?[^/][^/]*?[^/]*?[^/]*?[^/]*?)$")

assert ["abc"] |> fnfilter(matcher) |> sort == []
end

test "*c*?**" do
assert ["abc"] |> filter(fn file -> match(file, "*c*?**") end) |> sort == []
matcher = compile("*c*?**")

# assert matcher.regex == Regex.compile!("^(?:(?!\\.)(?=.)[^/]*?c[^/]*?[^/][^/]*?[^/]*?)$")

assert ["abc"] |> fnfilter(matcher) |> sort == []
end

test "a*****c*?**" do
assert ["abc"] |> filter(fn file -> match(file, "a*****c*?**") end) |> sort == []
matcher = compile("a*****c*?**")

# assert matcher.regex == Regex.compile!("^(?:(?=.)a[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?c[^/]*?[^/][^/]*?[^/]*?)$")

assert ["abc"] |> fnfilter(matcher) |> sort == []
end

test "a********???*******" do
assert ["abc"] |> filter(fn file -> match(file, "a********???*******") end) |> sort == []
matcher = compile("a********???*******")

# assert matcher.regex == Regex.compile!("^(?:(?=.)a[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/][^/][^/][^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?[^/]*?)$")

assert ["abc"] |> fnfilter(matcher) |> sort == []
end

test "[]" do
assert ["a"] |> filter(fn file -> match(file, "[]") end) |> sort == []
matcher = compile("[]")

# assert matcher.regex == Regex.compile!("^(?:\\[\\])$")

assert ["a"] |> fnfilter(matcher) |> sort == []
end

test "[abc" do
assert ["["] |> filter(fn file -> match(file, "[abc") end) |> sort == []
matcher = compile("[abc")

# assert matcher.regex == Regex.compile!("^(?:\\[abc)$")

assert ["["] |> fnfilter(matcher) |> sort == []
end

end
Loading

0 comments on commit 2014e49

Please sign in to comment.