diff options
| author | Ludovic Courtès <ludo@gnu.org> | 2022-09-01 15:54:08 +0200 |
|---|---|---|
| committer | Ludovic Courtès <ludo@gnu.org> | 2022-09-01 18:31:26 +0200 |
| commit | c3b1cfe76b7038f4030d7d207ffc417fed9a7ead (patch) | |
| tree | dd1e118336d2d1f915c003ac3af46b374fe9c043 | |
| parent | 8cf7997d7c068eb87eadbd28ac8be4e0aeddbba3 (diff) | |
read-print: Guess the base to use for integers being printed.
Fixes <https://issues.guix.gnu.org/57090>.
Reported by Christopher Rodriguez <yewscion@gmail.com>.
* guix/read-print.scm (%symbols-followed-by-octal-integers)
(%symbols-followed-by-hexadecimal-integers): New variables.
* guix/read-print.scm (integer->string): New procedure.
(pretty-print-with-comments): Use it.
* tests/read-print.scm: Add test.
| -rw-r--r-- | guix/read-print.scm | 38 | ||||
| -rw-r--r-- | tests/read-print.scm | 8 |
2 files changed, 43 insertions, 3 deletions
diff --git a/guix/read-print.scm b/guix/read-print.scm index 63ff9ca5bda..00dde870f42 100644 --- a/guix/read-print.scm +++ b/guix/read-print.scm | |||
| @@ -22,6 +22,7 @@ | |||
| 22 | #:use-module (ice-9 rdelim) | 22 | #:use-module (ice-9 rdelim) |
| 23 | #:use-module (ice-9 vlist) | 23 | #:use-module (ice-9 vlist) |
| 24 | #:use-module (srfi srfi-1) | 24 | #:use-module (srfi srfi-1) |
| 25 | #:use-module (srfi srfi-26) | ||
| 25 | #:use-module (srfi srfi-34) | 26 | #:use-module (srfi srfi-34) |
| 26 | #:use-module (srfi srfi-35) | 27 | #:use-module (srfi srfi-35) |
| 27 | #:use-module (guix i18n) | 28 | #:use-module (guix i18n) |
| @@ -426,6 +427,34 @@ each line except the first one (they're assumed to be already there)." | |||
| 426 | (display (make-string indent #\space) port) | 427 | (display (make-string indent #\space) port) |
| 427 | (loop tail))))) | 428 | (loop tail))))) |
| 428 | 429 | ||
| 430 | (define %symbols-followed-by-octal-integers | ||
| 431 | ;; Symbols for which the following integer must be printed as octal. | ||
| 432 | '(chmod umask mkdir mkstemp)) | ||
| 433 | |||
| 434 | (define %symbols-followed-by-hexadecimal-integers | ||
| 435 | ;; Likewise, for hexadecimal integers. | ||
| 436 | '(logand logior logxor lognot)) | ||
| 437 | |||
| 438 | (define (integer->string integer context) | ||
| 439 | "Render INTEGER as a string using a base suitable based on CONTEXT." | ||
| 440 | (define base | ||
| 441 | (match context | ||
| 442 | ((head . tail) | ||
| 443 | (cond ((memq head %symbols-followed-by-octal-integers) 8) | ||
| 444 | ((memq head %symbols-followed-by-hexadecimal-integers) | ||
| 445 | (if (any (cut memq <> %symbols-followed-by-octal-integers) | ||
| 446 | tail) | ||
| 447 | 8 | ||
| 448 | 16)) | ||
| 449 | (else 10))) | ||
| 450 | (_ 10))) | ||
| 451 | |||
| 452 | (string-append (match base | ||
| 453 | (10 "") | ||
| 454 | (16 "#x") | ||
| 455 | (8 "#o")) | ||
| 456 | (number->string integer base))) | ||
| 457 | |||
| 429 | (define* (pretty-print-with-comments port obj | 458 | (define* (pretty-print-with-comments port obj |
| 430 | #:key | 459 | #:key |
| 431 | (format-comment | 460 | (format-comment |
| @@ -661,9 +690,12 @@ FORMAT-VERTICAL-SPACE; a useful value of 'canonicalize-vertical-space'." | |||
| 661 | (display ")" port) | 690 | (display ")" port) |
| 662 | (+ column 1))))) | 691 | (+ column 1))))) |
| 663 | (_ | 692 | (_ |
| 664 | (let* ((str (if (string? obj) | 693 | (let* ((str (cond ((string? obj) |
| 665 | (escaped-string obj) | 694 | (escaped-string obj)) |
| 666 | (object->string obj))) | 695 | ((integer? obj) |
| 696 | (integer->string obj context)) | ||
| 697 | (else | ||
| 698 | (object->string obj)))) | ||
| 667 | (len (string-width str))) | 699 | (len (string-width str))) |
| 668 | (if (and (> (+ column 1 len) max-width) | 700 | (if (and (> (+ column 1 len) max-width) |
| 669 | (not delimited?)) | 701 | (not delimited?)) |
diff --git a/tests/read-print.scm b/tests/read-print.scm index 4dabcc1e64c..1b0d8659720 100644 --- a/tests/read-print.scm +++ b/tests/read-print.scm | |||
| @@ -248,6 +248,14 @@ mnopqrstuvwxyz.\")" | |||
| 248 | (list x y z))") | 248 | (list x y z))") |
| 249 | 249 | ||
| 250 | (test-pretty-print "\ | 250 | (test-pretty-print "\ |
| 251 | (begin | ||
| 252 | (chmod \"foo\" #o750) | ||
| 253 | (chmod port | ||
| 254 | (logand #o644 | ||
| 255 | (lognot (umask)))) | ||
| 256 | (logand #x7f xyz))") | ||
| 257 | |||
| 258 | (test-pretty-print "\ | ||
| 251 | (substitute-keyword-arguments (package-arguments x) | 259 | (substitute-keyword-arguments (package-arguments x) |
| 252 | ((#:phases phases) | 260 | ((#:phases phases) |
| 253 | `(modify-phases ,phases | 261 | `(modify-phases ,phases |
