2 * Copyright (C) 2024 Mikulas Patocka
4 * This file is part of Ajla.
6 * Ajla is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, either version 3 of the License, or (at your option) any later
11 * Ajla is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * Ajla. If not, see <https://www.gnu.org/licenses/>.
27 env : treemap(bytes, bytes);
33 const months~cache := [ `Jan`, `Feb`, `Mar`, `Apr`, `May`, `Jun`, `Jul`, `Aug`, `Sep`, `Oct`, `Nov`, `Dec` ];
34 const weekdays~cache := [ `Sun`, `Mon`, `Tue`, `Wed`, `Thu`, `Fri`, `Sat` ];
36 fn ex_str(t : type, e : t) : bytes;
38 fn glob(file pattern : string, case_sensitive : bool) : bool;
42 fn ex_str(t : type, e : t) : bytes
44 var p := exception_payload e;
45 if len_greater_than(p, 0) then
47 return exception_string e;
50 fn glob_no_star(file pattern : string) : bool
52 if len(file) <> len(pattern) then
54 for i := 0 to len(file) do [
55 if pattern[i] = '?' then
57 if file[i] <> pattern[i] then
63 fn glob(file pattern : string, case_sensitive : bool) : bool
65 if not case_sensitive then [
66 file := string_upcase(file);
67 pattern := string_upcase(pattern);
69 var star := list_search(pattern, '*');
71 return glob_no_star(file, pattern);
72 if len(file) < star then
74 if not glob_no_star(file[ .. star], pattern[ .. star]) then
76 file := file[star .. ];
77 pattern := pattern[star + 1 .. ];
79 var star2 := list_search(pattern, '*');
81 if len(file) < len(pattern) then
83 return glob_no_star(file[len(file) - len(pattern) .. ], pattern);
85 var pattern_stars := pattern[ .. star2];
86 for i := 0 to len(file) - len(pattern_stars) do [
87 if glob_no_star(file[i .. i + len(pattern_stars)], pattern_stars) then [
88 file := file[i + len(pattern_stars) .. ];
89 pattern := pattern[star2 + 1 .. ];