From: Marius Bakke <mbakke@fastmail.com>
To: "Ludovic Courtès" <ludo@gnu.org>, 30629@debbugs.gnu.org
Subject: [bug#30629] [PATCH 1/5] Add (guix glob).
Date: Tue, 27 Feb 2018 22:45:44 +0100 [thread overview]
Message-ID: <87bmgaumwn.fsf@fastmail.com> (raw)
In-Reply-To: <20180227142245.12674-2-ludo@gnu.org>
[-- Attachment #1: Type: text/plain, Size: 7938 bytes --]
Ludovic Courtès <ludo@gnu.org> writes:
> * guix/glob.scm, tests/glob.scm: New files.
> * Makefile.am (MODULES): Add guix/glob.scm.
> (SCM_TESTS): Add tests/glob.scm.
> ---
> Makefile.am | 4 ++-
> guix/glob.scm | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> tests/glob.scm | 58 +++++++++++++++++++++++++++++++++++
> 3 files changed, 158 insertions(+), 1 deletion(-)
> create mode 100644 guix/glob.scm
> create mode 100644 tests/glob.scm
>
> diff --git a/Makefile.am b/Makefile.am
> index e2c940ca8..6556799e6 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -1,5 +1,5 @@
> # GNU Guix --- Functional package management for GNU
> -# Copyright © 2012, 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
> +# Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
> # Copyright © 2013 Andreas Enge <andreas@enge.fr>
> # Copyright © 2015, 2017 Alex Kost <alezost@gmail.com>
> # Copyright © 2016, 2018 Mathieu Lirzin <mthl@gnu.org>
> @@ -83,6 +83,7 @@ MODULES = \
> guix/gnu-maintenance.scm \
> guix/upstream.scm \
> guix/licenses.scm \
> + guix/glob.scm \
> guix/git.scm \
> guix/graph.scm \
> guix/cache.scm \
> @@ -314,6 +315,7 @@ SCM_TESTS = \
> tests/substitute.scm \
> tests/builders.scm \
> tests/derivations.scm \
> + tests/glob.scm \
> tests/grafts.scm \
> tests/ui.scm \
> tests/records.scm \
> diff --git a/guix/glob.scm b/guix/glob.scm
> new file mode 100644
> index 000000000..4fc5173ac
> --- /dev/null
> +++ b/guix/glob.scm
> @@ -0,0 +1,97 @@
> +;;; GNU Guix --- Functional package management for GNU
> +;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org>
> +;;;
> +;;; This file is part of GNU Guix.
> +;;;
> +;;; GNU Guix is free software; you can redistribute it and/or modify it
> +;;; under the terms of the GNU General Public License as published by
> +;;; the Free Software Foundation; either version 3 of the License, or (at
> +;;; your option) any later version.
> +;;;
> +;;; GNU Guix is distributed in the hope that it will be useful, but
> +;;; WITHOUT ANY WARRANTY; without even the implied warranty of
> +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> +;;; GNU General Public License for more details.
> +;;;
> +;;; You should have received a copy of the GNU General Public License
> +;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
> +
> +(define-module (guix glob)
> + #:use-module (ice-9 match)
> + #:export (compile-glob-pattern
> + glob-match?))
> +
> +;;; Commentary:
> +;;;
> +;;; This is a minimal implementation of "glob patterns" (info "(libc)
> +;;; Globbbing"). It is currently limited to simple patterns and does not
^^^
This made my brain stutter :-)
> +;;; support braces and square brackets, for instance.
> +;;;
> +;;; Code:
> +
> +(define (wildcard-indices str)
> + "Return the list of indices in STR where wildcards can be found."
> + (let loop ((index 0)
> + (result '()))
> + (if (= index (string-length str))
> + (reverse result)
> + (loop (+ 1 index)
> + (case (string-ref str index)
> + ((#\? #\*) (cons index result))
> + (else result))))))
> +
> +(define (compile-glob-pattern str)
> + "Return an sexp that represents the compiled form of STR, a glob pattern
> +such as \"foo*\" or \"foo??bar\"."
> + (define flatten
> + (match-lambda
> + (((? string? str)) str)
> + (x x)))
> +
> + (let loop ((index 0)
> + (indices (wildcard-indices str))
> + (result '()))
> + (match indices
> + (()
> + (flatten (cond ((zero? index)
> + (list str))
> + ((= index (string-length str))
> + (reverse result))
> + (else
> + (reverse (cons (string-drop str index)
> + result))))))
> + ((wildcard-index . rest)
> + (let ((wildcard (match (string-ref str wildcard-index)
> + (#\? '?)
> + (#\* '*))))
> + (match (substring str index wildcard-index)
> + ("" (loop (+ 1 wildcard-index)
> + rest
> + (cons wildcard result)))
> + (str (loop (+ 1 wildcard-index)
> + rest
> + (cons* wildcard str result)))))))))
> +
> +(define (glob-match? pattern str)
> + "Return true if STR matches PATTERN, a compiled glob pattern as returned by
> +'compile-glob-pattern'."
> + (let loop ((pattern pattern)
> + (str str))
> + (match pattern
> + ((? string? literal) (string=? literal str))
> + (((? string? one)) (string=? one str))
> + (('*) #t)
> + (('?) (= 1 (string-length str)))
> + (() #t)
> + (('* suffix . rest)
> + (match (string-contains str suffix)
> + (#f #f)
> + (index (loop rest
> + (string-drop str
> + (+ index (string-length suffix)))))))
> + (('? . rest)
> + (and (>= (string-length str) 1)
> + (loop rest (string-drop str 1))))
> + ((prefix . rest)
> + (and (string-prefix? prefix str)
> + (loop rest (string-drop str (string-length prefix))))))))
> diff --git a/tests/glob.scm b/tests/glob.scm
> new file mode 100644
> index 000000000..033eeb10f
> --- /dev/null
> +++ b/tests/glob.scm
> @@ -0,0 +1,58 @@
> +;;; GNU Guix --- Functional package management for GNU
> +;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org>
> +;;;
> +;;; This file is part of GNU Guix.
> +;;;
> +;;; GNU Guix is free software; you can redistribute it and/or modify it
> +;;; under the terms of the GNU General Public License as published by
> +;;; the Free Software Foundation; either version 3 of the License, or (at
> +;;; your option) any later version.
> +;;;
> +;;; GNU Guix is distributed in the hope that it will be useful, but
> +;;; WITHOUT ANY WARRANTY; without even the implied warranty of
> +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> +;;; GNU General Public License for more details.
> +;;;
> +;;; You should have received a copy of the GNU General Public License
> +;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
> +
> +(define-module (test-glob)
> + #:use-module (guix glob)
> + #:use-module (srfi srfi-64))
> +
> +\f
> +(test-begin "glob")
> +
> +(test-equal "compile-glob-pattern, no wildcards"
> + "foo"
> + (compile-glob-pattern "foo"))
> +
> +(test-equal "compile-glob-pattern, Kleene star"
> + '("foo" * "bar")
> + (compile-glob-pattern "foo*bar"))
> +
> +(test-equal "compile-glob-pattern, question mark"
> + '(? "foo" *)
> + (compile-glob-pattern "?foo*"))
> +
> +(test-assert "literal match"
> + (let ((pattern (compile-glob-pattern "foo")))
> + (and (glob-match? pattern "foo")
> + (not (glob-match? pattern "foobar"))
> + (not (glob-match? pattern "barfoo")))))
> +
> +(test-assert "trailing star"
> + (let ((pattern (compile-glob-pattern "foo*")))
> + (and (glob-match? pattern "foo")
> + (glob-match? pattern "foobar")
> + (not (glob-match? pattern "xfoo")))))
> +
> +(test-assert "question marks"
> + (let ((pattern (compile-glob-pattern "foo??bar")))
> + (and (glob-match? pattern "fooxxbar")
> + (glob-match? pattern "fooZZbar")
> + (not (glob-match? pattern "foobar"))
> + (not (glob-match? pattern "fooxxxbar"))
> + (not (glob-match? pattern "fooxxbarzz")))))
> +
> +(test-end "glob")
> --
> 2.16.2
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]
next prev parent reply other threads:[~2018-02-27 21:46 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-02-27 14:17 [bug#30629] [PATCH 0/5] Detect missing modules in the initrd Ludovic Courtès
2018-02-27 14:22 ` Ludovic Courtès
2018-02-27 14:22 ` [bug#30629] [PATCH 1/5] Add (guix glob) Ludovic Courtès
2018-02-27 21:45 ` Marius Bakke [this message]
2018-02-28 11:25 ` Danny Milosavljevic
2018-03-01 9:57 ` Ludovic Courtès
2018-03-01 10:11 ` Danny Milosavljevic
2018-03-01 14:29 ` Danny Milosavljevic
2018-02-27 14:22 ` [bug#30629] [PATCH 2/5] linux-modules: Add 'device-module-aliases' and related procedures Ludovic Courtès
2018-02-27 19:33 ` Danny Milosavljevic
2018-02-27 20:55 ` Ludovic Courtès
2018-02-27 21:58 ` Danny Milosavljevic
2018-02-27 21:24 ` Ludovic Courtès
2018-02-27 14:22 ` [bug#30629] [PATCH 3/5] linux-initrd: Separate file system module logic Ludovic Courtès
2018-03-01 14:31 ` Danny Milosavljevic
2018-02-27 14:22 ` [bug#30629] [PATCH 4/5] system: Add 'initrd-modules' field Ludovic Courtès
2018-03-01 18:39 ` Danny Milosavljevic
2018-02-27 14:22 ` [bug#30629] [PATCH 5/5] guix system: Check for the lack of modules in the initrd Ludovic Courtès
2018-03-02 12:39 ` Danny Milosavljevic
2018-02-27 21:29 ` [bug#30629] [PATCH 0/5] Detect missing " Danny Milosavljevic
2018-02-27 21:15 ` Ludovic Courtès
2018-02-27 22:50 ` Danny Milosavljevic
2018-02-27 23:13 ` [bug#30638] [WIP v2] linux-initrd: Make modprobe pure-Guile Danny Milosavljevic
2018-02-27 23:17 ` Danny Milosavljevic
2018-02-28 11:47 ` [bug#30638] [WIP v3] " Danny Milosavljevic
2018-02-28 12:05 ` [bug#30638] [WIP v4] " Danny Milosavljevic
2018-02-28 11:36 ` [bug#30629] [PATCH 0/5] Detect missing modules in the initrd Danny Milosavljevic
2018-03-01 10:05 ` Ludovic Courtès
2018-03-01 10:11 ` Danny Milosavljevic
2018-03-01 11:46 ` Danny Milosavljevic
2018-03-01 13:39 ` Ludovic Courtès
2018-03-01 13:54 ` Danny Milosavljevic
2018-03-02 12:56 ` bug#30629: " Ludovic Courtès
2018-03-02 17:50 ` [bug#30629] " Danny Milosavljevic
2018-03-02 18:16 ` Danny Milosavljevic
2018-03-03 8:42 ` Ludovic Courtès
2018-03-01 13:55 ` Danny Milosavljevic
2018-03-01 21:20 ` Ludovic Courtès
2018-03-02 11:42 ` Danny Milosavljevic
2018-02-28 3:03 ` [bug#30629] Device mapper modalias Danny Milosavljevic
2018-03-01 8:56 ` Danny Milosavljevic
2018-03-01 10:11 ` Ludovic Courtès
2018-03-07 18:56 ` Danny Milosavljevic
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://guix.gnu.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87bmgaumwn.fsf@fastmail.com \
--to=mbakke@fastmail.com \
--cc=30629@debbugs.gnu.org \
--cc=ludo@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/guix.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).