glob.scm 5.2 KB

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