Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-13 21:54:22 -06:00
commit 1ec33daf7a
1624 files changed
+43422

No files matched your search

+132
View File
@@ -0,0 +1,132 @@
#! @file glob.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains what a glob is read with, the mistakes a glob's text can hold, and
#! `escape`, which makes text a glob matching only itself.
#!
#! A glob is a path written with wildcards, as `src/**/*.sakura`, which
#! `pattern::parse` reads and `pattern::matches` and `pattern::find` match:
#!
#! - `*` matches any run of characters within a name, or none;
#! - `?` matches any one character;
#! - `[abc]` matches one of the characters it holds, `[a-z]` one in the range,
#! and `[!abc]` or `[^abc]` one it doesn't hold. A `]` first is one of them,
#! as is a `-` first or last;
#! - `**`, as a whole name, matches any number of whole names, none included,
#! so `src/**/*.sakura` matches `src/main.sakura` and `src/a/b/main.sakura`;
#! elsewhere it is `*`;
#! - `{a,b}` matches either, and may hold anything, `/` and other braces
#! included, so `{src,test}/*.sakura` is two globs;
#! - `\` takes away what the next character means, as `\*` matches a `*`. It
#! can't make `/` part of a name, as no name holds one;
#! - `/` separates names on every system, Windows included, and a glob
#! starting with one, or on Windows with a drive's, as `C:/`, starts at that
#! root. A glob ending in one matches directories only.
#!
#! A character is a code point of UTF-8, or a byte where the text isn't UTF-8.
#! A name starting with `.` is matched only by a name of the glob starting with
#! `.` as written, unless the options say otherwise, so `*` leaves out hidden
#! files and `**` the directories that hold them. Neither ever matches `.` or
#! `..`, which only a glob's `.` and `..` match.
#!
#! `std::io::glob` is a module within `std::io`, whose paths it matches, and so
#! sees its names without importing it. It is a module of its own so that its
#! `error`, `options`, `describe`, and `escape` are seen only where it is
#! imported.
module std::io::glob;
when os == linux {
#! Whether letters match in either case unless the options say otherwise: on
#! Linux they don't, as its file systems tell cases apart.
pub const folds_case_by_default: bool = false;
}
else {
#! Whether letters match in either case unless the options say otherwise: on
#! Windows and macOS they do, as their file systems don't tell cases apart.
pub const folds_case_by_default: bool = true;
}
#! How a glob is read and matched.
pub struct options {
#! Whether `*`, `?`, a set, and `**` match a name starting with `.`, which
#! otherwise only a name of the glob starting with `.` matches.
pub var hidden: bool = false;
#! Whether letters match in either case, as `*.TXT` matches `notes.txt`. Only
#! ASCII letters fold.
pub var folds_case: bool = folds_case_by_default;
}
#! The kinds of mistake a glob's text can hold.
pub enum error_kind: u8 {
empty, #! The text is empty.
trailing_escape, #! The text ends in a `\`, which escapes nothing.
unclosed_set, #! A `[` has no `]` after it.
unclosed_brace, #! A `{` has no `}` after it.
unopened_brace, #! A `}` has no `{` before it.
too_many_choices, #! The braces make more globs than `choice_limit`.
}
#! The most globs the braces of one may make, as `{a,b}{c,d}` makes four.
pub const choice_limit: u64 = 1024;
#! A mistake in a glob's text, and where it is.
pub struct error {
pub var kind: error_kind; #! What the mistake is.
pub var offset: u64; #! The byte of the text it is at.
}
#! Describes the mistake.
#! @param this The mistake.
#! @returns A sentence saying what it is and where.
pub def error::describe(&this): string {
return $"{describe(this.kind)} (at byte {this.offset})";
}
#! Describes a kind of mistake.
#! @param kind The kind.
#! @returns A sentence saying what it is.
pub def describe(kind: error_kind): strview {
return match kind {
empty => "A glob can't be empty: `.` names where a search starts.",
trailing_escape => "The glob ends in a `\\`, which escapes nothing.",
unclosed_set => "A `[` has no `]` closing it.",
unclosed_brace => "A `{` has no `}` closing it.",
unopened_brace => "A `}` has no `{` opening it.",
too_many_choices => "The braces make more than 1024 globs.",
};
}
#! Makes text into a glob matching only itself, by escaping each character
#! that would mean something else, as `notes[1].txt` becomes `notes\[1\].txt`.
#! @param text The text, as a name or a path.
#! @param alloc The optional allocator for the glob's text.
#! @returns The glob. On Windows, where `\` separates names too, each is
#! written as `/`, so a path's text is a glob naming that path.
pub def escape(text: strview, alloc: maybe<&mut allocator> = nothing{}): string {
var made := string::new(alloc);
made.reserve(text.length);
for at in 0..text.length {
val byte := text[at];
when os == windows {
if byte == '\\' {
made.push('/');
continue;
}
}
if is_special(byte) {
made.push('\\');
}
made.push(byte);
}
return made;
}
# Whether a byte means something in a glob other than itself, somewhere.
def is_special(byte: u8): bool {
return byte == '*' || byte == '?' || byte == '[' || byte == ']' || byte == '{' || byte == '}'
|| byte == ',' || byte == '\\';
}