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

Re: Best way to convert integer to string?



>Is there a better way to print integers?
If you only want to the string to print it to a view, it would probably be
better not to allocate any storage at all, but to print every digit with
drawchar ,e.g. with the code in the appendix.

Karsten


(defmethod draw-integer-to-view ((view view) integer position-x position-y)
  (assert (integerp integer) (integer) "Arg ~a should be an integer" integer)
  (with-port (wptr view)
    (#_moveto position-x position-y)
    (when (minusp integer)
      (#_drawChar #\-)
      (setq integer (- integer)))
    ;integer is now 0 or positive
    (labels ((*fast-print-number (number)
               (multiple-value-bind
                 (rest current)
                 (floor number 10)
                 (unless (zerop rest)
                   (*fast-print-number rest))
                 (#_drawChar
                  (svref  #(#\0 #\1  #\2  #\3  #\4  #\5  #\6  #\7  #\8
#\9) current ))
                 )))
      (declare (dynamic-extent #'*fast-print-number))
      (*fast-print-number integer))))

#|

(make-instance 'window)
(draw-integer-to-view (target) -97 100 100)

(let ((window (make-instance 'window)))
  (time
   (dotimes (i 1000)
     (draw-integer-to-view window i 100 100))))
|#