1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (guix glob)
20 #:use-module (ice-9 match)
21 #:export (string->sglob
23 string->compiled-sglob
28 ;;; This is a minimal implementation of "glob patterns" (info "(libc)
29 ;;; Globbbing"). It is currently limited to simple patterns and does not
30 ;;; support braces, for instance.
34 (define (parse-bracket chars)
35 "Parse CHARS, a list of characters that extracted from a '[...]' sequence."
42 (define (string->sglob str)
43 "Return an sexp, called an \"sglob\", that represents the compiled form of
44 STR, a glob pattern such as \"foo*\" or \"foo??bar\"."
47 (((? string? str)) str)
50 (define (cons-string chars lst)
53 (_ (cons (list->string (reverse chars)) lst))))
55 (let loop ((chars (string->list str))
61 (flatten (reverse (if (null? pending)
63 (cons-string pending result)))))
64 (((and chr (or #\? #\*)) . rest)
65 (let ((wildcard (match chr
70 (cons* wildcard (cons-string pending result)))
71 (loop rest (cons chr pending) brackets result))))
74 (loop rest '() (+ 1 brackets)
75 (cons-string pending result))
76 (loop rest (cons #\[ pending) (+ 1 brackets) result)))
78 (cond ((zero? brackets)
79 (error "unexpected closing bracket" str))
82 (cons (parse-bracket (reverse pending)) result)))
84 (loop rest (cons #\] pending) (- brackets 1) result))))
86 (loop rest (cons chr pending) brackets result)))))
88 (define (compile-sglob sglob)
89 "Compile SGLOB into a more efficient representation."
92 (let loop ((sglob sglob)
98 (loop rest (cons char-set:full result)))
99 ((('range start end) . rest)
100 (loop rest (cons (ucs-range->char-set
101 (char->integer start)
102 (+ 1 (char->integer end)))
104 ((('set . chars) . rest)
105 (loop rest (cons (list->char-set chars) result)))
107 (loop rest (cons head result)))))))
109 (define string->compiled-sglob
110 (compose compile-sglob string->sglob))
112 (define (glob-match? pattern str)
113 "Return true if STR matches PATTERN, a compiled glob pattern as returned by
115 (let loop ((pattern pattern)
119 (string=? literal str))
125 (match (string-contains str suffix)
129 (+ index (string-length suffix)))))))
130 (((? char-set? cs) . rest)
131 (and (>= (string-length str) 1)
132 (let ((chr (string-ref str 0)))
133 (and (char-set-contains? cs chr)
134 (loop rest (string-drop str 1))))))
136 (and (string-prefix? prefix str)
137 (loop rest (string-drop str (string-length prefix))))))))