54.body.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. ;;; Copyright (C) Joo ChurlSoo (2004). All Rights Reserved.
  2. ;;; Permission is hereby granted, free of charge, to any person obtaining a copy
  3. ;;; of this software and associated documentation files (the "Software"), to
  4. ;;; deal in the Software without restriction, including without limitation the
  5. ;;; rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  6. ;;; sell copies of the Software, and to permit persons to whom the Software is
  7. ;;; furnished to do so, subject to the following conditions:
  8. ;;; The above copyright notice and this permission notice shall be included in
  9. ;;; all copies or substantial portions of the Software.
  10. ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. ;;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. ;;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. ;;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  14. ;;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  15. ;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  16. ;;; IN THE SOFTWARE.
  17. (define-syntax alet-cat* ; borrowed from SRFI-86
  18. (syntax-rules ()
  19. ((alet-cat* z (a . e) bd ...)
  20. (let ((y z))
  21. (%alet-cat* y (a . e) bd ...)))))
  22. (define-syntax %alet-cat* ; borrowed from SRFI-86
  23. (syntax-rules ()
  24. ((%alet-cat* z ((n d t ...)) bd ...)
  25. (let ((n (if (null? z)
  26. d
  27. (if (null? (cdr z))
  28. (wow-cat-end z n t ...)
  29. (error "cat: too many arguments" (cdr z))))))
  30. bd ...))
  31. ((%alet-cat* z ((n d t ...) . e) bd ...)
  32. (let ((n (if (null? z)
  33. d
  34. (wow-cat! z n d t ...))))
  35. (%alet-cat* z e bd ...)))
  36. ((%alet-cat* z e bd ...)
  37. (let ((e z)) bd ...))))
  38. (define-syntax wow-cat! ; borrowed from SRFI-86
  39. (syntax-rules ()
  40. ((wow-cat! z n d)
  41. (let ((n (car z)))
  42. (set! z (cdr z))
  43. n))
  44. ((wow-cat! z n d t)
  45. (let ((n (car z)))
  46. (if t
  47. (begin (set! z (cdr z)) n)
  48. (let lp ((head (list n)) (tail (cdr z)))
  49. (if (null? tail)
  50. d
  51. (let ((n (car tail)))
  52. (if t
  53. (begin (set! z (append (reverse head) (cdr tail))) n)
  54. (lp (cons n head) (cdr tail)))))))))
  55. ((wow-cat! z n d t ts)
  56. (let ((n (car z)))
  57. (if t
  58. (begin (set! z (cdr z)) ts)
  59. (let lp ((head (list n)) (tail (cdr z)))
  60. (if (null? tail)
  61. d
  62. (let ((n (car tail)))
  63. (if t
  64. (begin (set! z (append (reverse head) (cdr tail))) ts)
  65. (lp (cons n head) (cdr tail)))))))))
  66. ((wow-cat! z n d t ts fs)
  67. (let ((n (car z)))
  68. (if t
  69. (begin (set! z (cdr z)) ts)
  70. (begin (set! z (cdr z)) fs))))))
  71. (define-syntax wow-cat-end ; borrowed from SRFI-86
  72. (syntax-rules ()
  73. ((wow-cat-end z n)
  74. (car z))
  75. ((wow-cat-end z n t)
  76. (let ((n (car z)))
  77. (if t n (error "cat: too many argument" z))))
  78. ((wow-cat-end z n t ts)
  79. (let ((n (car z)))
  80. (if t ts (error "cat: too many argument" z))))
  81. ((wow-cat-end z n t ts fs)
  82. (let ((n (car z)))
  83. (if t ts fs)))))
  84. (define (str-index str char)
  85. (let ((len (string-length str)))
  86. (let lp ((n 0))
  87. (and (< n len)
  88. (if (char=? char (string-ref str n))
  89. n
  90. (lp (+ n 1)))))))
  91. (define (every? pred ls)
  92. (let lp ((ls ls))
  93. (or (null? ls)
  94. (and (pred (car ls))
  95. (lp (cdr ls))))))
  96. (define (part pred ls)
  97. (let lp ((ls ls) (true '()) (false '()))
  98. (cond
  99. ((null? ls) (cons (reverse true) (reverse false)))
  100. ((pred (car ls)) (lp (cdr ls) (cons (car ls) true) false))
  101. (else (lp (cdr ls) true (cons (car ls) false))))))
  102. (define (e-mold num pre)
  103. (let* ((str (number->string (inexact num)))
  104. (e-index (str-index str #\e)))
  105. (if e-index
  106. (string-append (mold (substring str 0 e-index) pre)
  107. (substring str e-index (string-length str)))
  108. (mold str pre))))
  109. (define (mold str pre)
  110. (let ((ind (str-index str #\.)))
  111. (if ind
  112. (let ((d-len (- (string-length str) (+ ind 1))))
  113. (cond
  114. ((= d-len pre) str)
  115. ((< d-len pre) (string-append str (make-string (- pre d-len) #\0)))
  116. ;;((char<? #\4 (string-ref str (+ 1 ind pre)))
  117. ;;(let ((com (expt 10 pre)))
  118. ;; (number->string (/ (round (* (string->number str) com)) com))))
  119. ((or (char<? #\5 (string-ref str (+ 1 ind pre)))
  120. (and (char=? #\5 (string-ref str (+ 1 ind pre)))
  121. (or (< (+ 1 pre) d-len)
  122. (memv (string-ref str (+ ind (if (= 0 pre) -1 pre)))
  123. '(#\1 #\3 #\5 #\7 #\9)))))
  124. (apply
  125. string
  126. (let* ((minus (char=? #\- (string-ref str 0)))
  127. (str (substring str (if minus 1 0) (+ 1 ind pre)))
  128. (char-list
  129. (reverse
  130. (let lp ((index (- (string-length str) 1))
  131. (raise #t))
  132. (if (= -1 index)
  133. (if raise '(#\1) '())
  134. (let ((chr (string-ref str index)))
  135. (if (char=? #\. chr)
  136. (cons chr (lp (- index 1) raise))
  137. (if raise
  138. (if (char=? #\9 chr)
  139. (cons #\0 (lp (- index 1) raise))
  140. (cons (integer->char
  141. (+ 1 (char->integer chr)))
  142. (lp (- index 1) #f)))
  143. (cons chr (lp (- index 1) raise))))))))))
  144. (if minus (cons #\- char-list) char-list))))
  145. (else
  146. (substring str 0 (+ 1 ind pre)))))
  147. (string-append str "." (make-string pre #\0)))))
  148. (define (separate str sep num opt)
  149. (let* ((len (string-length str))
  150. (pos (if opt
  151. (let ((pos (remainder (if (eq? opt 'minus) (- len 1) len)
  152. num)))
  153. (if (= 0 pos) num pos))
  154. num)))
  155. (apply string-append
  156. (let loop ((ini 0)
  157. (pos (if (eq? opt 'minus) (+ pos 1) pos)))
  158. (if (< pos len)
  159. (cons (substring str ini pos)
  160. (cons sep (loop pos (+ pos num))))
  161. (list (substring str ini len)))))))
  162. (define (cat object . rest)
  163. (let* ((str-rest (part string? rest))
  164. (str-list (car str-rest))
  165. (rest-list (cdr str-rest)))
  166. (if (null? rest-list)
  167. (apply string-append
  168. (cond
  169. ((number? object) (number->string object))
  170. ((string? object) object)
  171. ((char? object) (string object))
  172. ((boolean? object) (if object "#t" "#f"))
  173. ((symbol? object) (symbol->string object))
  174. (else
  175. (get-output-string
  176. (let ((str-port (open-output-string)))
  177. (write object str-port)
  178. str-port))))
  179. str-list)
  180. (alet-cat* rest-list
  181. ((width 0 (and (integer? width) (exact? width)))
  182. (port #f (or (boolean? port) (output-port? port))
  183. (if (eq? port #t) (current-output-port) port))
  184. (char #\space (char? char))
  185. (converter #f (and (pair? converter)
  186. (procedure? (car converter))
  187. (procedure? (cdr converter))))
  188. (precision #f (and (integer? precision)
  189. (inexact? precision)))
  190. (sign #f (eq? 'sign sign))
  191. (radix 'decimal
  192. (memq radix '(decimal octal binary hexadecimal)))
  193. (exactness #f (memq exactness '(exact inexact)))
  194. (separator #f (and (list? separator)
  195. (< 0 (length separator) 3)
  196. (char? (car separator))
  197. (or (null? (cdr separator))
  198. (let ((n (cadr separator)))
  199. (and (integer? n) (exact? n)
  200. (< 0 n))))))
  201. (writer #f (procedure? writer))
  202. (pipe #f (and (list? pipe)
  203. (not (null? pipe))
  204. (every? procedure? pipe)))
  205. (take #f (and (list? take)
  206. (< 0 (length take) 3)
  207. (every? (lambda (x)
  208. (and (integer? x) (exact? x)))
  209. take))))
  210. (let* ((str
  211. (cond
  212. ((and converter
  213. ((car converter) object))
  214. (let* ((str ((cdr converter) object))
  215. (pad (- (abs width) (string-length str))))
  216. (cond
  217. ((<= pad 0) str)
  218. ((< 0 width) (string-append (make-string pad char) str))
  219. (else (string-append str (make-string pad char))))))
  220. ((number? object)
  221. (and (not (eq? radix 'decimal)) precision
  222. (error "cat: non-decimal cannot have a decimal point"))
  223. (and precision (< precision 0) (eq? exactness 'exact)
  224. (error "cat: exact number cannot have a decimal point without exact sign"))
  225. (let* ((exact-sign (and precision
  226. (<= 0 precision)
  227. (or (eq? exactness 'exact)
  228. (and (exact? object)
  229. (not (eq? exactness
  230. 'inexact))))
  231. "#e"))
  232. (inexact-sign (and (not (eq? radix 'decimal))
  233. (or (and (inexact? object)
  234. (not (eq? exactness
  235. 'exact)))
  236. (eq? exactness 'inexact))
  237. "#i"))
  238. (radix-sign (cdr (assq radix
  239. '((decimal . #f)
  240. (octal . "#o")
  241. (binary . "#b")
  242. (hexadecimal . "#x")))))
  243. (plus-sign (and sign (< 0 (real-part object)) "+"))
  244. (exactness-sign (or exact-sign inexact-sign))
  245. (str
  246. (if precision
  247. (let ((precision (exact
  248. (abs precision)))
  249. (imag (imag-part object)))
  250. (if (= 0 imag)
  251. (e-mold object precision)
  252. (string-append
  253. (e-mold (real-part object) precision)
  254. (if (< 0 imag) "+" "")
  255. (e-mold imag precision)
  256. "i")))
  257. (number->string
  258. (cond
  259. (inexact-sign (exact object))
  260. (exactness
  261. (if (eq? exactness 'exact)
  262. (exact object)
  263. (inexact object)))
  264. (else object))
  265. (cdr (assq radix '((decimal . 10)
  266. (octal . 8)
  267. (binary . 2)
  268. (hexadecimal . 16)))))))
  269. (str
  270. (if (and separator
  271. (not (or (and (eq? radix 'decimal)
  272. (str-index str #\e))
  273. (str-index str #\i)
  274. (str-index str #\/))))
  275. (let ((sep (string (car separator)))
  276. (num (if (null? (cdr separator))
  277. 3 (cadr separator)))
  278. (dot-index (str-index str #\.)))
  279. (if dot-index
  280. (string-append
  281. (separate (substring str 0 dot-index)
  282. sep num (if (< object 0)
  283. 'minus #t))
  284. "."
  285. (separate (substring
  286. str (+ 1 dot-index)
  287. (string-length str))
  288. sep num #f))
  289. (separate str sep num (if (< object 0)
  290. 'minus #t))))
  291. str))
  292. (pad (- (abs width)
  293. (+ (string-length str)
  294. (if exactness-sign 2 0)
  295. (if radix-sign 2 0)
  296. (if plus-sign 1 0))))
  297. (pad (if (< 0 pad) pad 0)))
  298. (if (< 0 width)
  299. (if (char-numeric? char)
  300. (if (< (real-part object) 0)
  301. (string-append (or exactness-sign "")
  302. (or radix-sign "")
  303. "-"
  304. (make-string pad char)
  305. (substring str 1
  306. (string-length
  307. str)))
  308. (string-append (or exactness-sign "")
  309. (or radix-sign "")
  310. (or plus-sign "")
  311. (make-string pad char)
  312. str))
  313. (string-append (make-string pad char)
  314. (or exactness-sign "")
  315. (or radix-sign "")
  316. (or plus-sign "")
  317. str))
  318. (string-append (or exactness-sign "")
  319. (or radix-sign "")
  320. (or plus-sign "")
  321. str
  322. (make-string pad char)))))
  323. (else
  324. (let* ((str (cond
  325. (writer (get-output-string
  326. (let ((str-port
  327. (open-output-string)))
  328. (writer object str-port)
  329. str-port)))
  330. ((string? object) object)
  331. ((char? object) (string object))
  332. ((boolean? object) (if object "#t" "#f"))
  333. ((symbol? object) (symbol->string object))
  334. (else (get-output-string
  335. (let ((str-port (open-output-string)))
  336. (write object str-port)
  337. str-port)))))
  338. (str (if pipe
  339. (let loop ((str ((car pipe) str))
  340. (fns (cdr pipe)))
  341. (if (null? fns)
  342. str
  343. (loop ((car fns) str)
  344. (cdr fns))))
  345. str))
  346. (str
  347. (if take
  348. (let ((left (car take))
  349. (right (if (null? (cdr take))
  350. 0 (cadr take)))
  351. (len (string-length str)))
  352. (define (substr str beg end)
  353. (let ((end (cond
  354. ((< end 0) 0)
  355. ((< len end) len)
  356. (else end)))
  357. (beg (cond
  358. ((< beg 0) 0)
  359. ((< len beg) len)
  360. (else beg))))
  361. (if (and (= beg 0) (= end len))
  362. str
  363. (substring str beg end))))
  364. (string-append
  365. (if (< left 0)
  366. (substr str (abs left) len)
  367. (substr str 0 left))
  368. (if (< right 0)
  369. (substr str 0 (+ len right))
  370. (substr str (- len right) len))))
  371. str))
  372. (pad (- (abs width) (string-length str))))
  373. (cond
  374. ((<= pad 0) str)
  375. ((< 0 width) (string-append (make-string pad char) str))
  376. (else (string-append str (make-string pad char))))))))
  377. (str (apply string-append str str-list)))
  378. (and port (display str port))
  379. str)))))
  380. ;;; eof