summary refs log tree commit diff
diff options
context:
space:
mode:
authorRicardo Wurmus <rekado@elephly.net>2017-11-06 16:53:39 +0100
committerRicardo Wurmus <rekado@elephly.net>2017-11-07 08:07:51 +0100
commit6b46b04f919881e0122e201db4590ba5da77aa88 (patch)
tree7b7b07c44d1b0c44f6527f52bf1fb61532ddda0f
parent89618fa8b8d303a41591e52c7d8cabe3aa8f635d (diff)
downloadguix-6b46b04f919881e0122e201db4590ba5da77aa88.tar.gz
import: utils: Add string helpers.
* guix/import/utils.scm (read-lines, chunk-lines): New procedures.
-rw-r--r--guix/import/utils.scm28
1 files changed, 27 insertions, 1 deletions
diff --git a/guix/import/utils.scm b/guix/import/utils.scm
index 1e2f0c809d..d4cef6b503 100644
--- a/guix/import/utils.scm
+++ b/guix/import/utils.scm
@@ -34,6 +34,8 @@
   #:use-module (guix download)
   #:use-module (gnu packages)
   #:use-module (ice-9 match)
+  #:use-module (ice-9 rdelim)
+  #:use-module (ice-9 receive)
   #:use-module (ice-9 regex)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-11)
@@ -56,7 +58,10 @@
             snake-case
             beautify-description
 
-            alist->package))
+            alist->package
+
+            read-lines
+            chunk-lines))
 
 (define (factorize-uri uri version)
   "Factorize URI, a package tarball URI as a string, such that any occurrences
@@ -329,3 +334,24 @@ the expected fields of an <origin> object."
        (or (module-ref (resolve-interface '(guix licenses) #:prefix 'license:)
                        (spdx-string->license l))
            (license:fsdg-compatible l))))))
+
+(define* (read-lines #:optional (port (current-input-port)))
+  "Read lines from PORT and return them as a list."
+  (let loop ((line (read-line port))
+             (lines '()))
+    (if (eof-object? line)
+        (reverse lines)
+        (loop (read-line port)
+              (cons line lines)))))
+
+(define* (chunk-lines lines #:optional (pred string-null?))
+  "Return a list of chunks, each of which is a list of lines.  The chunks are
+separated by PRED."
+  (let loop ((rest lines)
+             (parts '()))
+    (receive (before after)
+        (break pred rest)
+      (let ((res (cons before parts)))
+        (if (null? after)
+            (reverse res)
+            (loop (cdr after) res))))))