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