slovo/benchmarks/json-quote-loop/common-lisp/json_quote_loop.lisp

48 lines
1.7 KiB
Common Lisp

(declaim (optimize (speed 3) (safety 0) (debug 0)))
(defconstant +loop-count+ 1000000)
(defconstant +expected-checksum+ 15000001)
(declaim (ftype (function () fixnum) configured-loop-count))
(defun configured-loop-count ()
(handler-case
(let ((line (read-line *standard-input* nil nil)))
(if line
(let ((value (parse-integer line :junk-allowed t)))
(if (> value 0) value +loop-count+))
+loop-count+))
(error () +loop-count+)))
(declaim (ftype (function () string) configured-target))
(defun configured-target ()
(or (second sb-ext:*posix-argv*) "slo\"vo\\path"))
(declaim (ftype (function (string) string) quote-json-string))
(defun quote-json-string (value)
(with-output-to-string (out)
(write-char #\" out)
(loop for ch across value
do (case ch
(#\" (write-string "\\\"" out))
(#\\ (write-string "\\\\" out))
(#\Newline (write-string "\\n" out))
(#\Tab (write-string "\\t" out))
(#\Return (write-string "\\r" out))
(otherwise (write-char ch out))))
(write-char #\" out)))
(declaim (ftype (function (fixnum string) fixnum) json-quote-loop))
(defun json-quote-loop (limit target)
(declare (type fixnum limit)
(type string target))
(loop with i of-type fixnum = 0
with acc of-type fixnum = 1
while (< i limit)
do (setf acc (+ acc (length (quote-json-string target)))
i (+ i 1))
finally (return acc)))
(let ((result (json-quote-loop (configured-loop-count) (configured-target))))
(format t "~D~%" result)
(sb-ext:exit :code (if (= result +expected-checksum+) 0 1)))