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 (compile-glob-pattern
26 ;;; This is a minimal implementation of "glob patterns" (info "(libc)
27 ;;; Globbbing"). It is currently limited to simple patterns and does not
28 ;;; support braces, for instance.
32 (define (parse-bracket chars)
33 "Parse CHARS, a list of characters that extracted from a '[...]' sequence."
40 (define (compile-glob-pattern str)
41 "Return an sexp that represents the compiled form of STR, a glob pattern
42 such as \"foo*\" or \"foo??bar\"."
45 (((? string? str)) str)
48 (define (cons-string chars lst)
51 (_ (cons (list->string (reverse chars)) lst))))
53 (let loop ((chars (string->list str))
59 (flatten (reverse (if (null? pending)
61 (cons-string pending result)))))
62 (((and chr (or #\? #\*)) . rest)
63 (let ((wildcard (match chr
68 (cons* wildcard (cons-string pending result)))
69 (loop rest (cons chr pending) brackets result))))
72 (loop rest '() (+ 1 brackets)
73 (cons-string pending result))
74 (loop rest (cons #\[ pending) (+ 1 brackets) result)))
76 (cond ((zero? brackets)
77 (error "unexpected closing bracket" str))
80 (cons (parse-bracket (reverse pending)) result)))
82 (loop rest (cons #\] pending) (- brackets 1) result))))
84 (loop rest (cons chr pending) brackets result)))))
86 (define (glob-match? pattern str)
87 "Return true if STR matches PATTERN, a compiled glob pattern as returned by
88 'compile-glob-pattern'."
89 (let loop ((pattern pattern)
93 (string=? literal str))
99 (match (string-contains str suffix)
103 (+ index (string-length suffix)))))))
105 (and (>= (string-length str) 1)
106 (loop rest (string-drop str 1))))
107 ((('range start end) . rest)
108 (and (>= (string-length str) 1)
109 (let ((chr (string-ref str 0)))
110 (and (char-set-contains? (ucs-range->char-set
111 (char->integer start)
112 (+ 1 (char->integer end)))
114 (loop rest (string-drop str 1))))))
115 ((('set . chars) . rest)
116 (and (>= (string-length str) 1)
117 (let ((chr (string-ref str 0)))
118 (and (char-set-contains? (list->char-set chars) chr)
119 (loop rest (string-drop str 1))))))
121 (and (string-prefix? prefix str)
122 (loop rest (string-drop str (string-length prefix))))))))