Skip to content

glob

Match paths against shell-style patterns like src/**/*.brass, and find the files on disk that match one.

Pattern Matches
* any run of characters within one name
? one character within one name
[a-z], [!x] one character in, or not in, the set
** any number of folders, including none
\* a literal *

* and ? never match /. A name starting with . is matched only by a pattern part that starts with . too, so * skips hidden files. ** is the exception: it crosses hidden folders too, so **/*.txt matches .git/a.txt.

import glob
print(glob.matches("src/**/*.brass", "src/main.brass"))
print(glob.matches("src/**/*.brass", "src/lib/deep/util.brass"))
print(glob.matches("*.txt", "notes/a.txt"))
print(glob.matches("*", ".hidden"))
true
true
false
false

find takes an fs.Files permission and returns the matching paths, sorted. A relative pattern is searched from the current folder.

import fs
import glob
let dir = try fs.temp_dir()
try fs.create_dir_all(&dir.join("src/lib"))
try fs.write(&dir.join("src/main.brass"), "")
try fs.write(&dir.join("src/lib/util.brass"), "")
try fs.write(&dir.join("src/notes.txt"), "")
let base = dir.path()
for p in try glob.find(&fs.files(), &(base + "/src/**/*.brass")) {
print(p.slice(base.len() + 1, p.len()))
}
src/lib/util.brass
src/main.brass
import glob
pub fn matches(pattern: &Str, path: &Str) -> bool

* and ? stay within one name, [a-z]/[!x] match one character, and ** spans folders.

pub fn match_name(pattern: &Str, name: &Str) -> bool

A name starting with . is matched only by a pattern that starts with . too.

Matches one name, with no / handling. matches uses it for each part of a path.

pub fn find(files: &fs.Files, pattern: &Str) -> Vec<Str> ! fs.IoError

The paths matching pattern, sorted; the search starts below its folders without wildcards.

The search starts in the longest leading part of the pattern without wildcards (src in src/**/*.brass), so a pattern with a fixed folder does not walk the whole tree. Folders are matched as well as files: src/* lists both.