Bugfix for sprintf option "%-<n>s"
This commit is contained in:
+51
-41
@@ -43,15 +43,18 @@
|
||||
(- (string-length r) (string-length r-trim))
|
||||
#\space)))
|
||||
r)))
|
||||
(let* ((pad-str (if (string=? zeros "") " " zeros))
|
||||
(min-width (if (eq? adjust-width #f) 0 adjust-width))
|
||||
(max-width (if (eq? precision #f) +inf.0 precision))
|
||||
(adjust (if (eq? zeros #f) 'left
|
||||
(if (string=? zeros "-") 'left 'right)))
|
||||
)
|
||||
(let* ((min-width (if (eq? adjust-width #f) 0 adjust-width))
|
||||
(adjust (if (string=? zeros "-") 'left 'right))
|
||||
(value (if (and (number? precision)
|
||||
(> (string-length arg) precision))
|
||||
(substring arg 0 precision)
|
||||
arg)))
|
||||
(unless (eq? kind 's)
|
||||
(error "argument is a string, but a number is expected"))
|
||||
(~a arg #:pad-string pad-str #:min-width min-width #:max-width max-width #:align adjust))
|
||||
(~a value
|
||||
#:pad-string " "
|
||||
#:min-width min-width
|
||||
#:align adjust))
|
||||
)
|
||||
)
|
||||
|
||||
@@ -61,47 +64,54 @@
|
||||
(format a ...))))
|
||||
|
||||
(define (do-format format args)
|
||||
(if (null? args)
|
||||
(let ((m (regexp-match re-format format)))
|
||||
(unless (eq? m #f)
|
||||
(error (fmt "formatting left, but no arguments left: ~a" format)))
|
||||
format)
|
||||
(let ((m (regexp-match re-format format)))
|
||||
(when (eq? m #f)
|
||||
(error (fmt "arguments left, but no formatting left: ~a" format)))
|
||||
(let ((m (regexp-match re-format format)))
|
||||
(if (eq? m #f)
|
||||
(begin
|
||||
(unless (null? args)
|
||||
(error (fmt "arguments left, but no formatting left: ~a" format)))
|
||||
format)
|
||||
(let* ((matched-length (string-length (list-ref m 0)))
|
||||
(prefix (list-ref m 1))
|
||||
(zeros (list-ref m 2))
|
||||
(adjust-width (list-ref m 3))
|
||||
(precision (list-ref m 5))
|
||||
(long (list-ref m 6))
|
||||
(kind (string->symbol (list-ref m 7)))
|
||||
)
|
||||
(unless (eq? adjust-width #f)
|
||||
(set! adjust-width (if (string=? adjust-width "*")
|
||||
(let ((n (shift args)))
|
||||
(when (null? args)
|
||||
(error "* requires >= 2 arguments left"))
|
||||
(unless (number? n)
|
||||
(error "* requires a number?"))
|
||||
n)
|
||||
(string->number adjust-width))))
|
||||
(unless (eq? precision #f)
|
||||
(set! precision (if (string=? precision "*")
|
||||
(let ((n (shift args)))
|
||||
(when (null? args)
|
||||
(error "* requires >= 2 arguments left"))
|
||||
(unless (number? n)
|
||||
(error "* requires a number?"))
|
||||
n)
|
||||
(string->number precision))))
|
||||
(string-append prefix
|
||||
(if (eq? kind '%)
|
||||
(kind (string->symbol (list-ref m 7))))
|
||||
(if (eq? kind '%)
|
||||
(string-append prefix
|
||||
"%"
|
||||
(format-part zeros adjust-width precision kind (shift args)))
|
||||
(do-format (substring format matched-length) args))))
|
||||
)
|
||||
)
|
||||
(do-format (substring format matched-length) args))
|
||||
(begin
|
||||
(when (null? args)
|
||||
(error (fmt "formatting left, but no arguments left: ~a" format)))
|
||||
(unless (eq? adjust-width #f)
|
||||
(set! adjust-width
|
||||
(if (string=? adjust-width "*")
|
||||
(let ((n (shift args)))
|
||||
(when (null? args)
|
||||
(error "* requires >= 2 arguments left"))
|
||||
(unless (number? n)
|
||||
(error "* requires a number?"))
|
||||
n)
|
||||
(string->number adjust-width))))
|
||||
(unless (eq? precision #f)
|
||||
(set! precision
|
||||
(if (string=? precision "*")
|
||||
(let ((n (shift args)))
|
||||
(when (null? args)
|
||||
(error "* requires >= 2 arguments left"))
|
||||
(unless (number? n)
|
||||
(error "* requires a number?"))
|
||||
n)
|
||||
(string->number precision))))
|
||||
(string-append prefix
|
||||
(format-part zeros
|
||||
adjust-width
|
||||
precision
|
||||
kind
|
||||
(shift args))
|
||||
(do-format (substring format matched-length) args))))))))
|
||||
|
||||
|
||||
|
||||
(define (sprintf format . args)
|
||||
|
||||
Reference in New Issue
Block a user