[Top][All Lists]

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

Re: Change interpreter from octets to hex?

From: Mark H Weaver
Subject: Re: Change interpreter from octets to hex?
Date: Tue, 21 Aug 2018 00:32:16 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux)

Hi Joshua,

Joshua Datko <address@hidden> writes:

> Is it possible to change the default interpreter behavior to display
> hex instead of octets for bytevectors and u8-lists? And if so, how to
> do so?
> The default, as y'all know is:
> scheme@(guile-user)> #vu8(#x0a #xaa)
> $6 = #vu8(10 170)
> But I would like instead the output to be:
> $6 = #vu8(#x0a #xaa)

The printer used by the Guile REPL can be customized using the ",option"
REPL command.  For example:

--8<---------------cut here---------------start------------->8---
address@hidden ~$ guile
GNU Guile 2.2.3
Copyright (C) 1995-2017 Free Software Foundation, Inc.

Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
This program is free software, and you are welcome to redistribute it
under certain conditions; type `,show c' for details.

Enter `,help' for help.
scheme@(guile-user)> ,help system
System Commands [abbrev]:

 ,gc                               - Garbage collection.
 ,statistics               [,stat] - Display statistics.
 ,option [NAME] [EXP]         [,o] - List/show/set options.
 ,quit        [,q ,continue ,cont] - Quit this session.

scheme@(guile-user)> ,option
  compile-options       (#:warnings (unbound-variable 
macro-use-before-definition arity-mismatch format duplicate-case-datum 
  trace                 #f
  interp                #f
  prompt                #f
  print                 #f
  value-history         #t
  on-error              debug
scheme@(guile-user)> (use-modules (rnrs bytevectors)
                                  (ice-9 format))
scheme@(guile-user)> (define (format-octet octet)
                       (format #f "#x~2,'0x" octet))
scheme@(guile-user)> (define (print-bytevector bv)
                       (display "#vu8(")
                       (display (string-join (map format-octet 
(bytevector->u8-list bv))))
                       (display ")"))
scheme@(guile-user)> (define (my-repl-print repl val)
                       (if (bytevector? val)
                           (print-bytevector val)
                           (write val))
scheme@(guile-user)> ,o print my-repl-print
scheme@(guile-user)> #vu8(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20)
$1 = #vu8(#x00 #x01 #x02 #x03 #x04 #x05 #x06 #x07 #x08 #x09 #x0a #x0b #x0c #x0d 
#x0e #x0f #x10 #x11 #x12 #x13 #x14)
--8<---------------cut here---------------end--------------->8---

This simple strategy won't work for bytevectors nested within another
data structure.  However, you could handle some specific cases by
supporting more data structures in your custom printer.  For example, if
you enhanced the custom printer to print ordinary scheme lists and to
call itself recursively for the elements of those lists, then you could
support printing bytevectors in your preferred format as long as the
surrounding data structure is composed of normal list structure only.

Guile's built-in printer does not support customizing its formatting of
core data structures, so you cannot handle the general case here without
modifying Guile itself.  If you wanted to do that,
'scm_i_print_bytevector' in bytevectors.c is the function to modify.

Another option would be to wrap your bytevectors within your own record
type, in which case you could install your own custom printer for that
type using 'set-record-type-printer!'.


reply via email to

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