You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

52 lines
2.0 KiB

  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013 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 import utils)
  19. #:use-module (ice-9 match)
  20. #:use-module (ice-9 regex)
  21. #:use-module (srfi srfi-1)
  22. #:export (factorize-uri))
  23. (define (factorize-uri uri version)
  24. "Factorize URI, a package tarball URI as a string, such that any occurrences
  25. of the string VERSION is replaced by the symbol 'version."
  26. (let ((version-rx (make-regexp (regexp-quote version))))
  27. (match (regexp-exec version-rx uri)
  28. (#f
  29. uri)
  30. (_
  31. (let ((indices (fold-matches version-rx uri
  32. '((0))
  33. (lambda (m result)
  34. (match result
  35. (((start) rest ...)
  36. `((,(match:end m))
  37. (,start . ,(match:start m))
  38. ,@rest)))))))
  39. (fold (lambda (index result)
  40. (match index
  41. ((start)
  42. (cons (substring uri start)
  43. result))
  44. ((start . end)
  45. (cons* (substring uri start end)
  46. 'version
  47. result))))
  48. '()
  49. indices))))))