gnunet-svn
[Top][All Lists]
Advanced

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

[gnunet-scheme] 02/03: bv-slice: Don't print bytes in unreadable slices.


From: gnunet
Subject: [gnunet-scheme] 02/03: bv-slice: Don't print bytes in unreadable slices.
Date: Tue, 11 Jan 2022 12:03:47 +0100

This is an automated email from the git hooks/post-receive script.

maxime-devos pushed a commit to branch master
in repository gnunet-scheme.

commit 0f9ef00e396497e3e5a2eb098245ec0849d0ce90
Author: Maxime Devos <maximedevos@telenet.be>
AuthorDate: Tue Jan 11 10:29:47 2022 +0000

    bv-slice: Don't print bytes in unreadable slices.
    
    * gnu/gnunet/utils/bv-slice.scm (print-slice): When not readable,
      only print the length and not the actual bytes.
    * tests/bv-slice.scm
      ("slice to string, read-write", "slice to string, read-only")
      ("slice to string, write-only"): Test print-slice.
---
 gnu/gnunet/utils/bv-slice.scm | 29 +++++++++++++++++++----------
 tests/bv-slice.scm            | 16 ++++++++++++++++
 2 files changed, 35 insertions(+), 10 deletions(-)

diff --git a/gnu/gnunet/utils/bv-slice.scm b/gnu/gnunet/utils/bv-slice.scm
index 6637852..add0f1b 100644
--- a/gnu/gnunet/utils/bv-slice.scm
+++ b/gnu/gnunet/utils/bv-slice.scm
@@ -95,18 +95,27 @@
                       "CAP_READ")
                      ((slice-writable? slice)
                       "CAP_WRITE")
+                     ;; Currently not constructible.
                      (#t
                       "0")))
-    (put-string port "):")
-    (let ((bv (slice-bv slice))
-         (end (+ (slice-offset slice)
-                 (slice-length slice))))
-      (let loop ((i (slice-offset slice)))
-       (if (< i end)
-           (let ((val (bytevector-u8-ref bv i)))
-             (put-char port #\ )
-             (put-string port (number->string val))
-             (loop (+ i 1))))))
+    ;; When the slice is readable, print the bytes in the slice.
+    (cond ((slice-readable? slice)
+          (put-string port "):")
+          (let ((bv (slice-bv slice))
+                (end (+ (slice-offset slice)
+                        (slice-length slice))))
+            (let loop ((i (slice-offset slice)))
+              (if (< i end)
+                  (let ((val (bytevector-u8-ref bv i)))
+                    (put-char port #\ )
+                    (put-string port (number->string val))
+                    (loop (+ i 1)))))))
+         (#true
+          ;; While the bytes in the slice cannot be printer because
+          ;; the slice is not readable, the length of the slice can
+          ;; still be printed.
+          (put-string port ") length: ")
+          (put-string port (number->string (slice-length slice)))))
     (put-char port #\>))
   (set-record-type-printer! <slice> print-slice)
 
diff --git a/tests/bv-slice.scm b/tests/bv-slice.scm
index e8f5bb7..e110592 100644
--- a/tests/bv-slice.scm
+++ b/tests/bv-slice.scm
@@ -139,6 +139,22 @@
   -128
   (slice-s8-ref (bv-slice/read-write #vu8(#b10000000)) 0))
 
+(test-equal "slice to string, read-write"
+  "#<slice (CAP_READ | CAP_WRITE): 1 2 3>"
+  (object->string (bv-slice/read-write #vu8(1 2 3))))
+
+(test-equal "slice to string, read-only"
+  "#<slice (CAP_READ): 1 2 3>"
+  (object->string
+   (slice/read-only (bv-slice/read-write #vu8(1 2 3)))))
+
+;; Make sure the lack of a read capability cannot be circumvented by
+;; object->string.
+(test-equal "slice to string, write-only"
+  "#<slice (CAP_WRITE) length: 3>"
+  (object->string
+   (slice/write-only (bv-slice/read-write #vu8(1 2 3)))))
+
 (test-end "bv-slice")
 
 ;; ^ TODO: test other procedures

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

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