guix-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

01/03: syscalls: Extract 'bytes->string'.


From: Ludovic Courtès
Subject: 01/03: syscalls: Extract 'bytes->string'.
Date: Thu, 19 Jan 2017 22:48:06 +0000 (UTC)

civodul pushed a commit to branch master
in repository guix.

commit 57f068bec5349e250ce321262609ca8978a81f7f
Author: Ludovic Courtès <address@hidden>
Date:   Thu Jan 19 23:20:57 2017 +0100

    syscalls: Extract 'bytes->string'.
    
    * guix/build/syscalls.scm (bytes->string): New procedure.
    (bytevector->string-list): Use it.
---
 guix/build/syscalls.scm |   13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/guix/build/syscalls.scm b/guix/build/syscalls.scm
index 2e37846..c06013c 100644
--- a/guix/build/syscalls.scm
+++ b/guix/build/syscalls.scm
@@ -900,6 +900,15 @@ bytevector BV at INDEX."
   ;; The most terrible interface, live from Scheme.
   (syscall->procedure int "ioctl" (list int unsigned-long '*)))
 
+(define (bytes->string bytes)
+  "Read BYTES, a list of bytes, and return the null-terminated string decoded
+from there, or #f if that would be an empty string."
+  (match (take-while (negate zero?) bytes)
+    (()
+     #f)
+    (non-zero
+     (list->string (map integer->char non-zero)))))
+
 (define (bytevector->string-list bv stride len)
   "Return the null-terminated strings found in BV every STRIDE bytes.  Read at
 most LEN bytes from BV."
@@ -911,9 +920,7 @@ most LEN bytes from BV."
        (reverse result))
       (_
        (loop (drop bytes stride)
-             (cons (list->string (map integer->char
-                                      (take-while (negate zero?) bytes)))
-                   result))))))
+             (cons (bytes->string bytes) result))))))
 
 (define* (network-interface-names #:optional sock)
   "Return the names of existing network interfaces.  This is typically limited



reply via email to

[Prev in Thread] Current Thread [Next in Thread]