1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2020 Giacomo Leidi <goodoldpaul@autistici.org>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (guix glob)
21 #:use-module (ice-9 match)
22 #:export (string->sglob
24 string->compiled-sglob
29 ;;; This is a minimal implementation of "glob patterns" (info "(libc)
30 ;;; Globbbing"). It is currently limited to simple patterns and does not
31 ;;; support braces, for instance.
35 (define (parse-bracket chars)
36 "Parse CHARS, a list of characters that extracted from a '[...]' sequence."
43 (define (string->sglob str)
44 "Return an sexp, called an \"sglob\", that represents the compiled form of
45 STR, a glob pattern such as \"foo*\" or \"foo??bar\"."
48 (((? string? str)) str)
51 (define (cons-string chars lst)
54 (_ (cons (list->string (reverse chars)) lst))))
56 (let loop ((chars (string->list str))
62 (flatten (reverse (if (null? pending)
64 (cons-string pending result)))))
68 (cons* '**/ (cons-string pending result)))
69 (loop rest (cons '**/ pending) brackets result)))
70 (((and chr (or #\? #\*)) . rest)
71 (let ((wildcard (match chr
76 (cons* wildcard (cons-string pending result)))
77 (loop rest (cons chr pending) brackets result))))
80 (loop rest '() (+ 1 brackets)
81 (cons-string pending result))
82 (loop rest (cons #\[ pending) (+ 1 brackets) result)))
84 (cond ((zero? brackets)
85 (error "unexpected closing bracket" str))
88 (cons (parse-bracket (reverse pending)) result)))
90 (loop rest (cons #\] pending) (- brackets 1) result))))
92 (loop rest (cons chr pending) brackets result)))))
94 (define (compile-sglob sglob)
95 "Compile SGLOB into a more efficient representation."
98 (let loop ((sglob sglob)
104 (loop rest (cons char-set:full result)))
105 ((('range start end) . rest)
106 (loop rest (cons (ucs-range->char-set
107 (char->integer start)
108 (+ 1 (char->integer end)))
110 ((('set . chars) . rest)
111 (loop rest (cons (list->char-set chars) result)))
113 (loop rest (cons head result)))))))
115 (define string->compiled-sglob
116 (compose compile-sglob string->sglob))
118 (define (glob-match? pattern str)
119 "Return true if STR matches PATTERN, a compiled glob pattern as returned by
121 (let loop ((pattern pattern)
125 (string=? literal str))
132 (('**/ suffix . rest)
133 (let ((rest (if (eq? '* suffix) (cdr rest) rest))
134 (suffix (if (eq? '* suffix) (car rest) suffix)))
135 (match (string-contains str suffix)
137 (index (loop rest (string-drop str
138 (+ index (string-length suffix))))))))
140 (match (string-contains str suffix)
144 (+ index (string-length suffix)))))))
145 (((? char-set? cs) . rest)
146 (and (>= (string-length str) 1)
147 (let ((chr (string-ref str 0)))
148 (and (char-set-contains? cs chr)
149 (loop rest (string-drop str 1))))))
151 (and (string-prefix? prefix str)
152 (loop rest (string-drop str (string-length prefix))))))))