glob.scm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  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.
  10. ;;;
  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.
  15. ;;;
  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/>.
  18. (define-module (guix glob)
  19. #:use-module (ice-9 match)
  20. #:export (string->sglob
  21. compile-sglob
  22. string->compiled-sglob
  23. glob-match?))
  24. ;;; Commentary:
  25. ;;;
  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.
  29. ;;;
  30. ;;; Code:
  31. (define (parse-bracket chars)
  32. "Parse CHARS, a list of characters that extracted from a '[...]' sequence."
  33. (match chars
  34. ((start #\- end)
  35. `(range ,start ,end))
  36. (lst
  37. `(set ,@lst))))
  38. (define (string->sglob str)
  39. "Return an sexp, called an \"sglob\", that represents the compiled form of
  40. STR, a glob pattern such as \"foo*\" or \"foo??bar\"."
  41. (define flatten
  42. (match-lambda
  43. (((? string? str)) str)
  44. (x x)))
  45. (define (cons-string chars lst)
  46. (match chars
  47. (() lst)
  48. (_ (cons (list->string (reverse chars)) lst))))
  49. (let loop ((chars (string->list str))
  50. (pending '())
  51. (brackets 0)
  52. (result '()))
  53. (match chars
  54. (()
  55. (flatten (reverse (if (null? pending)
  56. result
  57. (cons-string pending result)))))
  58. (((and chr (or #\? #\*)) . rest)
  59. (let ((wildcard (match chr
  60. (#\? '?)
  61. (#\* '*))))
  62. (if (zero? brackets)
  63. (loop rest '() 0
  64. (cons* wildcard (cons-string pending result)))
  65. (loop rest (cons chr pending) brackets result))))
  66. ((#\[ . rest)
  67. (if (zero? brackets)
  68. (loop rest '() (+ 1 brackets)
  69. (cons-string pending result))
  70. (loop rest (cons #\[ pending) (+ 1 brackets) result)))
  71. ((#\] . rest)
  72. (cond ((zero? brackets)
  73. (error "unexpected closing bracket" str))
  74. ((= 1 brackets)
  75. (loop rest '() 0
  76. (cons (parse-bracket (reverse pending)) result)))
  77. (else
  78. (loop rest (cons #\] pending) (- brackets 1) result))))
  79. ((chr . rest)
  80. (loop rest (cons chr pending) brackets result)))))
  81. (define (compile-sglob sglob)
  82. "Compile SGLOB into a more efficient representation."
  83. (if (string? sglob)
  84. sglob
  85. (let loop ((sglob sglob)
  86. (result '()))
  87. (match sglob
  88. (()
  89. (reverse result))
  90. (('? . rest)
  91. (loop rest (cons char-set:full result)))
  92. ((('range start end) . rest)
  93. (loop rest (cons (ucs-range->char-set
  94. (char->integer start)
  95. (+ 1 (char->integer end)))
  96. result)))
  97. ((('set . chars) . rest)
  98. (loop rest (cons (list->char-set chars) result)))
  99. ((head . rest)
  100. (loop rest (cons head result)))))))
  101. (define string->compiled-sglob
  102. (compose compile-sglob string->sglob))
  103. (define (glob-match? pattern str)
  104. "Return true if STR matches PATTERN, a compiled glob pattern as returned by
  105. 'compile-sglob'."
  106. (let loop ((pattern pattern)
  107. (str str))
  108. (match pattern
  109. ((? string? literal)
  110. (string=? literal str))
  111. (()
  112. (string-null? str))
  113. (('*)
  114. #t)
  115. (('* suffix . rest)
  116. (match (string-contains str suffix)
  117. (#f #f)
  118. (index (loop rest
  119. (string-drop str
  120. (+ index (string-length suffix)))))))
  121. (((? char-set? cs) . rest)
  122. (and (>= (string-length str) 1)
  123. (let ((chr (string-ref str 0)))
  124. (and (char-set-contains? cs chr)
  125. (loop rest (string-drop str 1))))))
  126. ((prefix . rest)
  127. (and (string-prefix? prefix str)
  128. (loop rest (string-drop str (string-length prefix))))))))