Checking if string match Glob patterns.
def isGlob(str: String): Boolean = {
val chars_oc = Map('{' -> '}', '[' -> ']')
val regex = """\\(.)|(\?|^!|\*|[\].+)]\?|\[[^\\\]]+\]|\{[^\\}]+\}|\(\?[:!=][^\\)]+\)|\([^|]+\|[^\\)]+\))""".r
chars_oc.map{ case (key, value) =>
if (str.count(_ == key) != str.count(_ == value)){
return false
}
}
regex.findAllIn(str).matchData foreach { matched =>
if (matched.group(2) != null) {
return true
}
}
return false
}
assert(isGlob("/tmp/folderx/{[abc],[def]}") == true, "Falha no teste 1")
assert(isGlob("/tmp/folderx/{[abc,[def]}") == false, "Falha no teste 2")
assert(isGlob("/tmp/folderx/*") == true, "Falha no teste 3")
assert(isGlob("*.js") == true, "Falha no teste 4")
assert(isGlob("??????.gif") == true, "Falha no teste 5")
assert(isGlob("/tmp/201810*[^9]") == true, "Falha no teste 6")
assert(isGlob("/tmp/arq_????.txt") == true, "Falha no teste 7")
assert(isGlob("/tmp/{ab,c{de, fh}}") == true, "Falha no teste 8")
assert(isGlob("/tmp/{ab,c{de, fh}") == false, "Falha no teste 9")
assert(isGlob("\\*.js") == false, "Falha no teste 10")
assert(isGlob("abc.js") == false, "Falha no teste 11")
assert(isGlob("abc/[abc.js") == false, "Falha no teste 12")
assert(isGlob("abc/[^abc.js") == false, "Falha no teste 13")
assert(isGlob("abc/[1-3.js") == false, "Falha no teste 14")
assert(isGlob("abc/{a,b}.js") == true, "Falha no teste 15")
assert(isGlob("abc/{a,b.js") == false, "Falha no teste 16")
assert(isGlob("abc/{a-z}.js") == true, "Falha no teste 17")
assert(isGlob("") == false, "Falha no teste 18")
println("OK! TODOS TESTES PASSARAM.")