format.scm 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603
  1. ;;;; "format.scm" Common LISP text output formatter for SLIB
  2. ;;; Copyright (C) 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
  3. ;;;
  4. ;;; This library is free software; you can redistribute it and/or
  5. ;;; modify it under the terms of the GNU Lesser General Public
  6. ;;; License as published by the Free Software Foundation; either
  7. ;;; version 3 of the License, or (at your option) any later version.
  8. ;;;
  9. ;;; This library is distributed in the hope that it will be useful,
  10. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;; Lesser General Public License for more details.
  13. ;;;
  14. ;;; You should have received a copy of the GNU Lesser General Public
  15. ;;; License along with this library; if not, write to the Free Software
  16. ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;;
  18. ;;; This code was orignally in the public domain.
  19. ;;;
  20. ;;; Written 1992-1994 by Dirk Lutzebaeck (lutzeb@cs.tu-berlin.de).
  21. ;;;
  22. ;;; Authors of the version from SLIB (< 1.4) were Ken Dickey and Aubrey
  23. ;;; Jaffer.
  24. ;;;
  25. ;;; Assimilated into Guile May 1999.
  26. ;;;
  27. ;;; Please don't bother the original authors with bug reports, though;
  28. ;;; send them to bug-guile@gnu.org.
  29. ;;;
  30. (define-module (ice-9 format)
  31. #:autoload (ice-9 pretty-print) (pretty-print truncated-print)
  32. #:autoload (ice-9 i18n) (%global-locale number->locale-string)
  33. #:replace (format))
  34. (define format:version "3.0")
  35. (define (format destination format-string . format-args)
  36. (if (not (string? format-string))
  37. (error "format: expected a string for format string" format-string))
  38. (let* ((port
  39. (cond
  40. ((not destination) (open-output-string))
  41. ((boolean? destination) (current-output-port)) ; boolean but not false
  42. ((output-port? destination) destination)
  43. (else
  44. (error "format: bad destination `~a'" destination))))
  45. (output-col (or (port-column port) 0))
  46. (flush-output? #f))
  47. (define format:case-conversion #f)
  48. (define format:pos 0) ; curr. format string parsing position
  49. (define format:arg-pos 0) ; curr. format argument position
  50. ; this is global for error presentation
  51. ;; format string and char output routines on port
  52. (define (format:out-str str)
  53. (if format:case-conversion
  54. (display (format:case-conversion str) port)
  55. (display str port))
  56. (set! output-col
  57. (+ output-col (string-length str))))
  58. (define (format:out-char ch)
  59. (if format:case-conversion
  60. (display (format:case-conversion (string ch))
  61. port)
  62. (write-char ch port))
  63. (set! output-col
  64. (if (char=? ch #\newline)
  65. 0
  66. (+ output-col 1))))
  67. ;;(define (format:out-substr str i n) ; this allocates a new string
  68. ;; (display (substring str i n) port)
  69. ;; (set! output-col (+ output-col n)))
  70. (define (format:out-substr str i n)
  71. (do ((k i (+ k 1)))
  72. ((= k n))
  73. (write-char (string-ref str k) port))
  74. (set! output-col (+ output-col (- n i))))
  75. ;;(define (format:out-fill n ch) ; this allocates a new string
  76. ;; (format:out-str (make-string n ch)))
  77. (define (format:out-fill n ch)
  78. (do ((i 0 (+ i 1)))
  79. ((= i n))
  80. (write-char ch port))
  81. (set! output-col (+ output-col n)))
  82. ;; format's user error handler
  83. (define (format:error . args) ; never returns!
  84. (let ((port (current-error-port)))
  85. (set! format:error format:intern-error)
  86. (if (not (zero? format:arg-pos))
  87. (set! format:arg-pos (- format:arg-pos 1)))
  88. (format port
  89. "~%FORMAT: error with call: (format ~a \"~a<===~a\" ~
  90. ~{~a ~}===>~{~a ~})~% "
  91. destination
  92. (substring format-string 0 format:pos)
  93. (substring format-string format:pos
  94. (string-length format-string))
  95. (list-head format-args format:arg-pos)
  96. (list-tail format-args format:arg-pos))
  97. (apply format port args)
  98. (newline port)
  99. (set! format:error format:error-save)
  100. (format:abort)))
  101. (define (format:intern-error . args)
  102. ;;if something goes wrong in format:error
  103. (display "FORMAT: INTERNAL ERROR IN FORMAT:ERROR!") (newline)
  104. (display " destination: ") (write destination) (newline)
  105. (display " format string: ") (write format-string) (newline)
  106. (display " format args: ") (write format-args) (newline)
  107. (display " error args: ") (write args) (newline)
  108. (set! format:error format:error-save)
  109. (format:abort))
  110. (define format:error-save format:error)
  111. (define format:parameter-characters
  112. '(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9 #\- #\+ #\v #\# #\'))
  113. (define (format:format-work format-string arglist) ; does the formatting work
  114. (letrec
  115. ((format-string-len (string-length format-string))
  116. (arg-pos 0) ; argument position in arglist
  117. (arg-len (length arglist)) ; number of arguments
  118. (modifier #f) ; 'colon | 'at | 'colon-at | #f
  119. (params '()) ; directive parameter list
  120. (param-value-found #f) ; a directive
  121. ; parameter value
  122. ; found
  123. (conditional-nest 0) ; conditional nesting level
  124. (clause-pos 0) ; last cond. clause
  125. ; beginning char pos
  126. (clause-default #f) ; conditional default
  127. ; clause string
  128. (clauses '()) ; conditional clause
  129. ; string list
  130. (conditional-type #f) ; reflects the
  131. ; contional modifiers
  132. (conditional-arg #f) ; argument to apply the conditional
  133. (iteration-nest 0) ; iteration nesting level
  134. (iteration-pos 0) ; iteration string
  135. ; beginning char pos
  136. (iteration-type #f) ; reflects the
  137. ; iteration modifiers
  138. (max-iterations #f) ; maximum number of
  139. ; iterations
  140. (recursive-pos-save format:pos)
  141. (next-char ; gets the next char
  142. ; from format-string
  143. (lambda ()
  144. (let ((ch (peek-next-char)))
  145. (set! format:pos (+ 1 format:pos))
  146. ch)))
  147. (peek-next-char
  148. (lambda ()
  149. (if (>= format:pos format-string-len)
  150. (format:error "illegal format string")
  151. (string-ref format-string format:pos))))
  152. (one-positive-integer?
  153. (lambda (params)
  154. (cond
  155. ((null? params) #f)
  156. ((and (integer? (car params))
  157. (>= (car params) 0)
  158. (= (length params) 1)) #t)
  159. (else
  160. (format:error
  161. "one positive integer parameter expected")))))
  162. (next-arg
  163. (lambda ()
  164. (if (>= arg-pos arg-len)
  165. (begin
  166. (set! format:arg-pos (+ arg-len 1))
  167. (format:error "missing argument(s)")))
  168. (add-arg-pos 1)
  169. (list-ref arglist (- arg-pos 1))))
  170. (prev-arg
  171. (lambda ()
  172. (add-arg-pos -1)
  173. (if (negative? arg-pos)
  174. (format:error "missing backward argument(s)"))
  175. (list-ref arglist arg-pos)))
  176. (rest-args
  177. (lambda ()
  178. (let loop ((l arglist) (k arg-pos)) ; list-tail definition
  179. (if (= k 0) l (loop (cdr l) (- k 1))))))
  180. (add-arg-pos
  181. (lambda (n)
  182. (set! arg-pos (+ n arg-pos))
  183. (set! format:arg-pos arg-pos)))
  184. (anychar-dispatch ; dispatches the format-string
  185. (lambda ()
  186. (if (>= format:pos format-string-len)
  187. arg-pos ; used for ~? continuance
  188. (let ((char (next-char)))
  189. (cond
  190. ((char=? char #\~)
  191. (set! modifier #f)
  192. (set! params '())
  193. (set! param-value-found #f)
  194. (tilde-dispatch))
  195. (else
  196. (if (and (zero? conditional-nest)
  197. (zero? iteration-nest))
  198. (format:out-char char))
  199. (anychar-dispatch)))))))
  200. (tilde-dispatch
  201. (lambda ()
  202. (cond
  203. ((>= format:pos format-string-len)
  204. (format:out-str "~") ; tilde at end of
  205. ; string is just
  206. ; output
  207. arg-pos) ; used for ~?
  208. ; continuance
  209. ((and (or (zero? conditional-nest)
  210. (memv (peek-next-char) ; find conditional
  211. ; directives
  212. (append '(#\[ #\] #\; #\: #\@ #\^)
  213. format:parameter-characters)))
  214. (or (zero? iteration-nest)
  215. (memv (peek-next-char) ; find iteration
  216. ; directives
  217. (append '(#\{ #\} #\: #\@ #\^)
  218. format:parameter-characters))))
  219. (case (char-upcase (next-char))
  220. ;; format directives
  221. ((#\A) ; Any -- for humans
  222. (set! format:read-proof
  223. (memq modifier '(colon colon-at)))
  224. (format:out-obj-padded (memq modifier '(at colon-at))
  225. (next-arg) #f params)
  226. (anychar-dispatch))
  227. ((#\S) ; Slashified -- for parsers
  228. (set! format:read-proof
  229. (memq modifier '(colon colon-at)))
  230. (format:out-obj-padded (memq modifier '(at colon-at))
  231. (next-arg) #t params)
  232. (anychar-dispatch))
  233. ((#\D) ; Decimal
  234. (format:out-num-padded modifier (next-arg) params 10)
  235. (anychar-dispatch))
  236. ((#\H) ; Localized number
  237. (let* ((num (next-arg))
  238. (locale (case modifier
  239. ((colon) (next-arg))
  240. (else %global-locale)))
  241. (argc (length params))
  242. (width (format:par params argc 0 #f "width"))
  243. (decimals (format:par params argc 1 #t "decimals"))
  244. (padchar (integer->char
  245. (format:par params argc 2 format:space-ch
  246. "padchar")))
  247. (str (number->locale-string num decimals
  248. locale)))
  249. (format:out-str (if (and width
  250. (< (string-length str) width))
  251. (string-pad str width padchar)
  252. str)))
  253. (anychar-dispatch))
  254. ((#\X) ; Hexadecimal
  255. (format:out-num-padded modifier (next-arg) params 16)
  256. (anychar-dispatch))
  257. ((#\O) ; Octal
  258. (format:out-num-padded modifier (next-arg) params 8)
  259. (anychar-dispatch))
  260. ((#\B) ; Binary
  261. (format:out-num-padded modifier (next-arg) params 2)
  262. (anychar-dispatch))
  263. ((#\R)
  264. (if (null? params)
  265. (format:out-obj-padded ; Roman, cardinal,
  266. ; ordinal numerals
  267. #f
  268. ((case modifier
  269. ((at) format:num->roman)
  270. ((colon-at) format:num->old-roman)
  271. ((colon) format:num->ordinal)
  272. (else format:num->cardinal))
  273. (next-arg))
  274. #f params)
  275. (format:out-num-padded ; any Radix
  276. modifier (next-arg) (cdr params) (car params)))
  277. (anychar-dispatch))
  278. ((#\F) ; Fixed-format floating-point
  279. (format:out-fixed modifier (next-arg) params)
  280. (anychar-dispatch))
  281. ((#\E) ; Exponential floating-point
  282. (format:out-expon modifier (next-arg) params)
  283. (anychar-dispatch))
  284. ((#\G) ; General floating-point
  285. (format:out-general modifier (next-arg) params)
  286. (anychar-dispatch))
  287. ((#\$) ; Dollars floating-point
  288. (format:out-dollar modifier (next-arg) params)
  289. (anychar-dispatch))
  290. ((#\I) ; Complex numbers
  291. (let ((z (next-arg)))
  292. (if (not (complex? z))
  293. (format:error "argument not a complex number"))
  294. (format:out-fixed modifier (real-part z) params)
  295. (format:out-fixed 'at (imag-part z) params)
  296. (format:out-char #\i))
  297. (anychar-dispatch))
  298. ((#\C) ; Character
  299. (let ((ch (if (one-positive-integer? params)
  300. (integer->char (car params))
  301. (next-arg))))
  302. (if (not (char? ch))
  303. (format:error "~~c expects a character"))
  304. (case modifier
  305. ((at)
  306. (format:out-str (object->string ch)))
  307. ((colon)
  308. (let ((c (char->integer ch)))
  309. (if (< c 0)
  310. (set! c (+ c 256))) ; compensate
  311. ; complement
  312. ; impl.
  313. (cond
  314. ((< c #x20) ; assumes that control
  315. ; chars are < #x20
  316. (format:out-char #\^)
  317. (format:out-char
  318. (integer->char (+ c #x40))))
  319. ((>= c #x7f)
  320. (format:out-str "#\\")
  321. (format:out-str
  322. (number->string c 8)))
  323. (else
  324. (format:out-char ch)))))
  325. (else (format:out-char ch))))
  326. (anychar-dispatch))
  327. ((#\P) ; Plural
  328. (if (memq modifier '(colon colon-at))
  329. (prev-arg))
  330. (let ((arg (next-arg)))
  331. (if (not (number? arg))
  332. (format:error "~~p expects a number argument"))
  333. (if (= arg 1)
  334. (if (memq modifier '(at colon-at))
  335. (format:out-char #\y))
  336. (if (memq modifier '(at colon-at))
  337. (format:out-str "ies")
  338. (format:out-char #\s))))
  339. (anychar-dispatch))
  340. ((#\~) ; Tilde
  341. (if (one-positive-integer? params)
  342. (format:out-fill (car params) #\~)
  343. (format:out-char #\~))
  344. (anychar-dispatch))
  345. ((#\%) ; Newline
  346. (if (one-positive-integer? params)
  347. (format:out-fill (car params) #\newline)
  348. (format:out-char #\newline))
  349. (set! output-col 0)
  350. (anychar-dispatch))
  351. ((#\&) ; Fresh line
  352. (if (one-positive-integer? params)
  353. (begin
  354. (if (> (car params) 0)
  355. (format:out-fill (- (car params)
  356. (if (>
  357. output-col
  358. 0) 0 1))
  359. #\newline))
  360. (set! output-col 0))
  361. (if (> output-col 0)
  362. (format:out-char #\newline)))
  363. (anychar-dispatch))
  364. ((#\_) ; Space character
  365. (if (one-positive-integer? params)
  366. (format:out-fill (car params) #\space)
  367. (format:out-char #\space))
  368. (anychar-dispatch))
  369. ((#\/) ; Tabulator character
  370. (if (one-positive-integer? params)
  371. (format:out-fill (car params) #\tab)
  372. (format:out-char #\tab))
  373. (anychar-dispatch))
  374. ((#\|) ; Page seperator
  375. (if (one-positive-integer? params)
  376. (format:out-fill (car params) #\page)
  377. (format:out-char #\page))
  378. (set! output-col 0)
  379. (anychar-dispatch))
  380. ((#\T) ; Tabulate
  381. (format:tabulate modifier params)
  382. (anychar-dispatch))
  383. ((#\Y) ; Structured print
  384. (let ((width (if (one-positive-integer? params)
  385. (car params)
  386. 79)))
  387. (case modifier
  388. ((at)
  389. (format:out-str
  390. (call-with-output-string
  391. (lambda (p)
  392. (truncated-print (next-arg) p
  393. #:width width)))))
  394. ((colon-at)
  395. (format:out-str
  396. (call-with-output-string
  397. (lambda (p)
  398. (truncated-print (next-arg) p
  399. #:width
  400. (max (- width
  401. output-col)
  402. 1))))))
  403. ((colon)
  404. (format:error "illegal modifier in ~~?"))
  405. (else
  406. (pretty-print (next-arg) port
  407. #:width width)
  408. (set! output-col 0))))
  409. (anychar-dispatch))
  410. ((#\? #\K) ; Indirection (is "~K" in T-Scheme)
  411. (cond
  412. ((memq modifier '(colon colon-at))
  413. (format:error "illegal modifier in ~~?"))
  414. ((eq? modifier 'at)
  415. (let* ((frmt (next-arg))
  416. (args (rest-args)))
  417. (add-arg-pos (format:format-work frmt args))))
  418. (else
  419. (let* ((frmt (next-arg))
  420. (args (next-arg)))
  421. (format:format-work frmt args))))
  422. (anychar-dispatch))
  423. ((#\!) ; Flush output
  424. (set! flush-output? #t)
  425. (anychar-dispatch))
  426. ((#\newline) ; Continuation lines
  427. (if (eq? modifier 'at)
  428. (format:out-char #\newline))
  429. (if (< format:pos format-string-len)
  430. (do ((ch (peek-next-char) (peek-next-char)))
  431. ((or (not (char-whitespace? ch))
  432. (= format:pos (- format-string-len 1))))
  433. (if (eq? modifier 'colon)
  434. (format:out-char (next-char))
  435. (next-char))))
  436. (anychar-dispatch))
  437. ((#\*) ; Argument jumping
  438. (case modifier
  439. ((colon) ; jump backwards
  440. (if (one-positive-integer? params)
  441. (do ((i 0 (+ i 1)))
  442. ((= i (car params)))
  443. (prev-arg))
  444. (prev-arg)))
  445. ((at) ; jump absolute
  446. (set! arg-pos (if (one-positive-integer? params)
  447. (car params) 0)))
  448. ((colon-at)
  449. (format:error "illegal modifier `:@' in ~~* directive"))
  450. (else ; jump forward
  451. (if (one-positive-integer? params)
  452. (do ((i 0 (+ i 1)))
  453. ((= i (car params)))
  454. (next-arg))
  455. (next-arg))))
  456. (anychar-dispatch))
  457. ((#\() ; Case conversion begin
  458. (set! format:case-conversion
  459. (case modifier
  460. ((at) string-capitalize-first)
  461. ((colon) string-capitalize)
  462. ((colon-at) string-upcase)
  463. (else string-downcase)))
  464. (anychar-dispatch))
  465. ((#\)) ; Case conversion end
  466. (if (not format:case-conversion)
  467. (format:error "missing ~~("))
  468. (set! format:case-conversion #f)
  469. (anychar-dispatch))
  470. ((#\[) ; Conditional begin
  471. (set! conditional-nest (+ conditional-nest 1))
  472. (cond
  473. ((= conditional-nest 1)
  474. (set! clause-pos format:pos)
  475. (set! clause-default #f)
  476. (set! clauses '())
  477. (set! conditional-type
  478. (case modifier
  479. ((at) 'if-then)
  480. ((colon) 'if-else-then)
  481. ((colon-at) (format:error "illegal modifier in ~~["))
  482. (else 'num-case)))
  483. (set! conditional-arg
  484. (if (one-positive-integer? params)
  485. (car params)
  486. (next-arg)))))
  487. (anychar-dispatch))
  488. ((#\;) ; Conditional separator
  489. (if (zero? conditional-nest)
  490. (format:error "~~; not in ~~[~~] conditional"))
  491. (if (not (null? params))
  492. (format:error "no parameter allowed in ~~;"))
  493. (if (= conditional-nest 1)
  494. (let ((clause-str
  495. (cond
  496. ((eq? modifier 'colon)
  497. (set! clause-default #t)
  498. (substring format-string clause-pos
  499. (- format:pos 3)))
  500. ((memq modifier '(at colon-at))
  501. (format:error "illegal modifier in ~~;"))
  502. (else
  503. (substring format-string clause-pos
  504. (- format:pos 2))))))
  505. (set! clauses (append clauses (list clause-str)))
  506. (set! clause-pos format:pos)))
  507. (anychar-dispatch))
  508. ((#\]) ; Conditional end
  509. (if (zero? conditional-nest) (format:error "missing ~~["))
  510. (set! conditional-nest (- conditional-nest 1))
  511. (if modifier
  512. (format:error "no modifier allowed in ~~]"))
  513. (if (not (null? params))
  514. (format:error "no parameter allowed in ~~]"))
  515. (cond
  516. ((zero? conditional-nest)
  517. (let ((clause-str (substring format-string clause-pos
  518. (- format:pos 2))))
  519. (if clause-default
  520. (set! clause-default clause-str)
  521. (set! clauses (append clauses (list clause-str)))))
  522. (case conditional-type
  523. ((if-then)
  524. (if conditional-arg
  525. (format:format-work (car clauses)
  526. (list conditional-arg))))
  527. ((if-else-then)
  528. (add-arg-pos
  529. (format:format-work (if conditional-arg
  530. (cadr clauses)
  531. (car clauses))
  532. (rest-args))))
  533. ((num-case)
  534. (if (or (not (integer? conditional-arg))
  535. (< conditional-arg 0))
  536. (format:error "argument not a positive integer"))
  537. (if (not (and (>= conditional-arg (length clauses))
  538. (not clause-default)))
  539. (add-arg-pos
  540. (format:format-work
  541. (if (>= conditional-arg (length clauses))
  542. clause-default
  543. (list-ref clauses conditional-arg))
  544. (rest-args))))))))
  545. (anychar-dispatch))
  546. ((#\{) ; Iteration begin
  547. (set! iteration-nest (+ iteration-nest 1))
  548. (cond
  549. ((= iteration-nest 1)
  550. (set! iteration-pos format:pos)
  551. (set! iteration-type
  552. (case modifier
  553. ((at) 'rest-args)
  554. ((colon) 'sublists)
  555. ((colon-at) 'rest-sublists)
  556. (else 'list)))
  557. (set! max-iterations (if (one-positive-integer? params)
  558. (car params) #f))))
  559. (anychar-dispatch))
  560. ((#\}) ; Iteration end
  561. (if (zero? iteration-nest) (format:error "missing ~~{"))
  562. (set! iteration-nest (- iteration-nest 1))
  563. (case modifier
  564. ((colon)
  565. (if (not max-iterations) (set! max-iterations 1)))
  566. ((colon-at at) (format:error "illegal modifier")))
  567. (if (not (null? params))
  568. (format:error "no parameters allowed in ~~}"))
  569. (if (zero? iteration-nest)
  570. (let ((iteration-str
  571. (substring format-string iteration-pos
  572. (- format:pos (if modifier 3 2)))))
  573. (if (string=? iteration-str "")
  574. (set! iteration-str (next-arg)))
  575. (case iteration-type
  576. ((list)
  577. (let ((args (next-arg))
  578. (args-len 0))
  579. (if (not (list? args))
  580. (format:error "expected a list argument"))
  581. (set! args-len (length args))
  582. (do ((arg-pos 0 (+ arg-pos
  583. (format:format-work
  584. iteration-str
  585. (list-tail args arg-pos))))
  586. (i 0 (+ i 1)))
  587. ((or (>= arg-pos args-len)
  588. (and max-iterations
  589. (>= i max-iterations)))))))
  590. ((sublists)
  591. (let ((args (next-arg))
  592. (args-len 0))
  593. (if (not (list? args))
  594. (format:error "expected a list argument"))
  595. (set! args-len (length args))
  596. (do ((arg-pos 0 (+ arg-pos 1)))
  597. ((or (>= arg-pos args-len)
  598. (and max-iterations
  599. (>= arg-pos max-iterations))))
  600. (let ((sublist (list-ref args arg-pos)))
  601. (if (not (list? sublist))
  602. (format:error
  603. "expected a list of lists argument"))
  604. (format:format-work iteration-str sublist)))))
  605. ((rest-args)
  606. (let* ((args (rest-args))
  607. (args-len (length args))
  608. (usedup-args
  609. (do ((arg-pos 0 (+ arg-pos
  610. (format:format-work
  611. iteration-str
  612. (list-tail
  613. args arg-pos))))
  614. (i 0 (+ i 1)))
  615. ((or (>= arg-pos args-len)
  616. (and max-iterations
  617. (>= i max-iterations)))
  618. arg-pos))))
  619. (add-arg-pos usedup-args)))
  620. ((rest-sublists)
  621. (let* ((args (rest-args))
  622. (args-len (length args))
  623. (usedup-args
  624. (do ((arg-pos 0 (+ arg-pos 1)))
  625. ((or (>= arg-pos args-len)
  626. (and max-iterations
  627. (>= arg-pos max-iterations)))
  628. arg-pos)
  629. (let ((sublist (list-ref args arg-pos)))
  630. (if (not (list? sublist))
  631. (format:error "expected list arguments"))
  632. (format:format-work iteration-str sublist)))))
  633. (add-arg-pos usedup-args)))
  634. (else (format:error "internal error in ~~}")))))
  635. (anychar-dispatch))
  636. ((#\^) ; Up and out
  637. (let* ((continue
  638. (cond
  639. ((not (null? params))
  640. (not
  641. (case (length params)
  642. ((1) (zero? (car params)))
  643. ((2) (= (list-ref params 0) (list-ref params 1)))
  644. ((3) (<= (list-ref params 0)
  645. (list-ref params 1)
  646. (list-ref params 2)))
  647. (else (format:error "too much parameters")))))
  648. (format:case-conversion ; if conversion stop conversion
  649. (set! format:case-conversion string-copy) #t)
  650. ((= iteration-nest 1) #t)
  651. ((= conditional-nest 1) #t)
  652. ((>= arg-pos arg-len)
  653. (set! format:pos format-string-len) #f)
  654. (else #t))))
  655. (if continue
  656. (anychar-dispatch))))
  657. ;; format directive modifiers and parameters
  658. ((#\@) ; `@' modifier
  659. (if (memq modifier '(at colon-at))
  660. (format:error "double `@' modifier"))
  661. (set! modifier (if (eq? modifier 'colon) 'colon-at 'at))
  662. (tilde-dispatch))
  663. ((#\:) ; `:' modifier
  664. (if (memq modifier '(colon colon-at))
  665. (format:error "double `:' modifier"))
  666. (set! modifier (if (eq? modifier 'at) 'colon-at 'colon))
  667. (tilde-dispatch))
  668. ((#\') ; Character parameter
  669. (if modifier (format:error "misplaced modifier"))
  670. (set! params (append params (list (char->integer (next-char)))))
  671. (set! param-value-found #t)
  672. (tilde-dispatch))
  673. ((#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9 #\- #\+) ; num. paramtr
  674. (if modifier (format:error "misplaced modifier"))
  675. (let ((num-str-beg (- format:pos 1))
  676. (num-str-end format:pos))
  677. (do ((ch (peek-next-char) (peek-next-char)))
  678. ((not (char-numeric? ch)))
  679. (next-char)
  680. (set! num-str-end (+ 1 num-str-end)))
  681. (set! params
  682. (append params
  683. (list (string->number
  684. (substring format-string
  685. num-str-beg
  686. num-str-end))))))
  687. (set! param-value-found #t)
  688. (tilde-dispatch))
  689. ((#\V) ; Variable parameter from next argum.
  690. (if modifier (format:error "misplaced modifier"))
  691. (set! params (append params (list (next-arg))))
  692. (set! param-value-found #t)
  693. (tilde-dispatch))
  694. ((#\#) ; Parameter is number of remaining args
  695. (if param-value-found (format:error "misplaced '#'"))
  696. (if modifier (format:error "misplaced modifier"))
  697. (set! params (append params (list (length (rest-args)))))
  698. (set! param-value-found #t)
  699. (tilde-dispatch))
  700. ((#\,) ; Parameter separators
  701. (if modifier (format:error "misplaced modifier"))
  702. (if (not param-value-found)
  703. (set! params (append params '(#f)))) ; append empty paramtr
  704. (set! param-value-found #f)
  705. (tilde-dispatch))
  706. ((#\Q) ; Inquiry messages
  707. (if (eq? modifier 'colon)
  708. (format:out-str format:version)
  709. (let ((nl (string #\newline)))
  710. (format:out-str
  711. (string-append
  712. "SLIB Common LISP format version " format:version nl
  713. " (C) copyright 1992-1994 by Dirk Lutzebaeck" nl
  714. " please send bug reports to `lutzeb@cs.tu-berlin.de'"
  715. nl))))
  716. (anychar-dispatch))
  717. (else ; Unknown tilde directive
  718. (format:error "unknown control character `~c'"
  719. (string-ref format-string (- format:pos 1))))))
  720. (else (anychar-dispatch)))))) ; in case of conditional
  721. (set! format:pos 0)
  722. (set! format:arg-pos 0)
  723. (anychar-dispatch) ; start the formatting
  724. (set! format:pos recursive-pos-save)
  725. arg-pos)) ; return the position in the arg. list
  726. ;; when format:read-proof is true, format:obj->str will wrap
  727. ;; result strings starting with "#<" in an extra pair of double
  728. ;; quotes.
  729. (define format:read-proof #f)
  730. ;; format:obj->str returns a R4RS representation as a string of
  731. ;; an arbitrary scheme object.
  732. (define (format:obj->str obj slashify)
  733. (let ((res (if slashify
  734. (object->string obj)
  735. (call-with-output-string (lambda (p) (display obj p))))))
  736. (if (and format:read-proof (string-prefix? "#<" res))
  737. (object->string res)
  738. res)))
  739. (define format:space-ch (char->integer #\space))
  740. (define format:zero-ch (char->integer #\0))
  741. (define (format:par pars length index default name)
  742. (if (> length index)
  743. (let ((par (list-ref pars index)))
  744. (if par
  745. (if name
  746. (if (< par 0)
  747. (format:error
  748. "~s parameter must be a positive integer" name)
  749. par)
  750. par)
  751. default))
  752. default))
  753. (define (format:out-obj-padded pad-left obj slashify pars)
  754. (if (null? pars)
  755. (format:out-str (format:obj->str obj slashify))
  756. (let ((l (length pars)))
  757. (let ((mincol (format:par pars l 0 0 "mincol"))
  758. (colinc (format:par pars l 1 1 "colinc"))
  759. (minpad (format:par pars l 2 0 "minpad"))
  760. (padchar (integer->char
  761. (format:par pars l 3 format:space-ch #f)))
  762. (objstr (format:obj->str obj slashify)))
  763. (if (not pad-left)
  764. (format:out-str objstr))
  765. (do ((objstr-len (string-length objstr))
  766. (i minpad (+ i colinc)))
  767. ((>= (+ objstr-len i) mincol)
  768. (format:out-fill i padchar)))
  769. (if pad-left
  770. (format:out-str objstr))))))
  771. (define (format:out-num-padded modifier number pars radix)
  772. (if (not (integer? number)) (format:error "argument not an integer"))
  773. (let ((numstr (number->string number radix)))
  774. (if (and (null? pars) (not modifier))
  775. (format:out-str numstr)
  776. (let ((l (length pars))
  777. (numstr-len (string-length numstr)))
  778. (let ((mincol (format:par pars l 0 #f "mincol"))
  779. (padchar (integer->char
  780. (format:par pars l 1 format:space-ch #f)))
  781. (commachar (integer->char
  782. (format:par pars l 2 (char->integer #\,) #f)))
  783. (commawidth (format:par pars l 3 3 "commawidth")))
  784. (if mincol
  785. (let ((numlen numstr-len)) ; calc. the output len of number
  786. (if (and (memq modifier '(at colon-at)) (>= number 0))
  787. (set! numlen (+ numlen 1)))
  788. (if (memq modifier '(colon colon-at))
  789. (set! numlen (+ (quotient (- numstr-len
  790. (if (< number 0) 2 1))
  791. commawidth)
  792. numlen)))
  793. (if (> mincol numlen)
  794. (format:out-fill (- mincol numlen) padchar))))
  795. (if (and (memq modifier '(at colon-at))
  796. (>= number 0))
  797. (format:out-char #\+))
  798. (if (memq modifier '(colon colon-at)) ; insert comma character
  799. (let ((start (remainder numstr-len commawidth))
  800. (ns (if (< number 0) 1 0)))
  801. (format:out-substr numstr 0 start)
  802. (do ((i start (+ i commawidth)))
  803. ((>= i numstr-len))
  804. (if (> i ns)
  805. (format:out-char commachar))
  806. (format:out-substr numstr i (+ i commawidth))))
  807. (format:out-str numstr)))))))
  808. (define (format:tabulate modifier pars)
  809. (let ((l (length pars)))
  810. (let ((colnum (format:par pars l 0 1 "colnum"))
  811. (colinc (format:par pars l 1 1 "colinc"))
  812. (padch (integer->char (format:par pars l 2 format:space-ch #f))))
  813. (case modifier
  814. ((colon colon-at)
  815. (format:error "unsupported modifier for ~~t"))
  816. ((at) ; relative tabulation
  817. (format:out-fill
  818. (if (= colinc 0)
  819. colnum ; colnum = colrel
  820. (do ((c 0 (+ c colinc))
  821. (col (+ output-col colnum)))
  822. ((>= c col)
  823. (- c output-col))))
  824. padch))
  825. (else ; absolute tabulation
  826. (format:out-fill
  827. (cond
  828. ((< output-col colnum)
  829. (- colnum output-col))
  830. ((= colinc 0)
  831. 0)
  832. (else
  833. (do ((c colnum (+ c colinc)))
  834. ((>= c output-col)
  835. (- c output-col)))))
  836. padch))))))
  837. ;; roman numerals (from dorai@cs.rice.edu).
  838. (define format:roman-alist
  839. '((1000 #\M) (500 #\D) (100 #\C) (50 #\L)
  840. (10 #\X) (5 #\V) (1 #\I)))
  841. (define format:roman-boundary-values
  842. '(100 100 10 10 1 1 #f))
  843. (define (format:num->old-roman n)
  844. (if (and (integer? n) (>= n 1))
  845. (let loop ((n n)
  846. (romans format:roman-alist)
  847. (s '()))
  848. (if (null? romans) (list->string (reverse s))
  849. (let ((roman-val (caar romans))
  850. (roman-dgt (cadar romans)))
  851. (do ((q (quotient n roman-val) (- q 1))
  852. (s s (cons roman-dgt s)))
  853. ((= q 0)
  854. (loop (remainder n roman-val)
  855. (cdr romans) s))))))
  856. (format:error "only positive integers can be romanized")))
  857. (define (format:num->roman n)
  858. (if (and (integer? n) (> n 0))
  859. (let loop ((n n)
  860. (romans format:roman-alist)
  861. (boundaries format:roman-boundary-values)
  862. (s '()))
  863. (if (null? romans)
  864. (list->string (reverse s))
  865. (let ((roman-val (caar romans))
  866. (roman-dgt (cadar romans))
  867. (bdry (car boundaries)))
  868. (let loop2 ((q (quotient n roman-val))
  869. (r (remainder n roman-val))
  870. (s s))
  871. (if (= q 0)
  872. (if (and bdry (>= r (- roman-val bdry)))
  873. (loop (remainder r bdry) (cdr romans)
  874. (cdr boundaries)
  875. (cons roman-dgt
  876. (append
  877. (cdr (assv bdry romans))
  878. s)))
  879. (loop r (cdr romans) (cdr boundaries) s))
  880. (loop2 (- q 1) r (cons roman-dgt s)))))))
  881. (format:error "only positive integers can be romanized")))
  882. ;; cardinals & ordinals (from dorai@cs.rice.edu)
  883. (define format:cardinal-ones-list
  884. '(#f "one" "two" "three" "four" "five"
  885. "six" "seven" "eight" "nine" "ten" "eleven" "twelve" "thirteen"
  886. "fourteen" "fifteen" "sixteen" "seventeen" "eighteen"
  887. "nineteen"))
  888. (define format:cardinal-tens-list
  889. '(#f #f "twenty" "thirty" "forty" "fifty" "sixty" "seventy" "eighty"
  890. "ninety"))
  891. (define (format:num->cardinal999 n)
  892. ;; this procedure is inspired by the Bruno Haible's CLisp
  893. ;; function format-small-cardinal, which converts numbers
  894. ;; in the range 1 to 999, and is used for converting each
  895. ;; thousand-block in a larger number
  896. (let* ((hundreds (quotient n 100))
  897. (tens+ones (remainder n 100))
  898. (tens (quotient tens+ones 10))
  899. (ones (remainder tens+ones 10)))
  900. (append
  901. (if (> hundreds 0)
  902. (append
  903. (string->list
  904. (list-ref format:cardinal-ones-list hundreds))
  905. (string->list" hundred")
  906. (if (> tens+ones 0) '(#\space) '()))
  907. '())
  908. (if (< tens+ones 20)
  909. (if (> tens+ones 0)
  910. (string->list
  911. (list-ref format:cardinal-ones-list tens+ones))
  912. '())
  913. (append
  914. (string->list
  915. (list-ref format:cardinal-tens-list tens))
  916. (if (> ones 0)
  917. (cons #\-
  918. (string->list
  919. (list-ref format:cardinal-ones-list ones)))
  920. '()))))))
  921. (define format:cardinal-thousand-block-list
  922. '("" " thousand" " million" " billion" " trillion" " quadrillion"
  923. " quintillion" " sextillion" " septillion" " octillion" " nonillion"
  924. " decillion" " undecillion" " duodecillion" " tredecillion"
  925. " quattuordecillion" " quindecillion" " sexdecillion" " septendecillion"
  926. " octodecillion" " novemdecillion" " vigintillion"))
  927. (define (format:num->cardinal n)
  928. (cond ((not (integer? n))
  929. (format:error
  930. "only integers can be converted to English cardinals"))
  931. ((= n 0) "zero")
  932. ((< n 0) (string-append "minus " (format:num->cardinal (- n))))
  933. (else
  934. (let ((power3-word-limit
  935. (length format:cardinal-thousand-block-list)))
  936. (let loop ((n n)
  937. (power3 0)
  938. (s '()))
  939. (if (= n 0)
  940. (list->string s)
  941. (let ((n-before-block (quotient n 1000))
  942. (n-after-block (remainder n 1000)))
  943. (loop n-before-block
  944. (+ power3 1)
  945. (if (> n-after-block 0)
  946. (append
  947. (if (> n-before-block 0)
  948. (string->list ", ") '())
  949. (format:num->cardinal999 n-after-block)
  950. (if (< power3 power3-word-limit)
  951. (string->list
  952. (list-ref
  953. format:cardinal-thousand-block-list
  954. power3))
  955. (append
  956. (string->list " times ten to the ")
  957. (string->list
  958. (format:num->ordinal
  959. (* power3 3)))
  960. (string->list " power")))
  961. s)
  962. s)))))))))
  963. (define format:ordinal-ones-list
  964. '(#f "first" "second" "third" "fourth" "fifth"
  965. "sixth" "seventh" "eighth" "ninth" "tenth" "eleventh" "twelfth"
  966. "thirteenth" "fourteenth" "fifteenth" "sixteenth" "seventeenth"
  967. "eighteenth" "nineteenth"))
  968. (define format:ordinal-tens-list
  969. '(#f #f "twentieth" "thirtieth" "fortieth" "fiftieth" "sixtieth"
  970. "seventieth" "eightieth" "ninetieth"))
  971. (define (format:num->ordinal n)
  972. (cond ((not (integer? n))
  973. (format:error
  974. "only integers can be converted to English ordinals"))
  975. ((= n 0) "zeroth")
  976. ((< n 0) (string-append "minus " (format:num->ordinal (- n))))
  977. (else
  978. (let ((hundreds (quotient n 100))
  979. (tens+ones (remainder n 100)))
  980. (string-append
  981. (if (> hundreds 0)
  982. (string-append
  983. (format:num->cardinal (* hundreds 100))
  984. (if (= tens+ones 0) "th" " "))
  985. "")
  986. (if (= tens+ones 0) ""
  987. (if (< tens+ones 20)
  988. (list-ref format:ordinal-ones-list tens+ones)
  989. (let ((tens (quotient tens+ones 10))
  990. (ones (remainder tens+ones 10)))
  991. (if (= ones 0)
  992. (list-ref format:ordinal-tens-list tens)
  993. (string-append
  994. (list-ref format:cardinal-tens-list tens)
  995. "-"
  996. (list-ref format:ordinal-ones-list ones))))
  997. )))))))
  998. ;; format inf and nan.
  999. (define (format:out-inf-nan number width digits edigits overch padch)
  1000. ;; inf and nan are always printed exactly as "+inf.0", "-inf.0" or
  1001. ;; "+nan.0", suitably justified in their field. We insist on
  1002. ;; printing this exact form so that the numbers can be read back in.
  1003. (let* ((str (number->string number))
  1004. (len (string-length str))
  1005. (dot (string-index str #\.))
  1006. (digits (+ (or digits 0)
  1007. (if edigits (+ edigits 2) 0))))
  1008. (if (and width overch (< width len))
  1009. (format:out-fill width (integer->char overch))
  1010. (let* ((leftpad (if width
  1011. (max (- width (max len (+ dot 1 digits))) 0)
  1012. 0))
  1013. (rightpad (if width
  1014. (max (- width leftpad len) 0)
  1015. 0))
  1016. (padch (integer->char (or padch format:space-ch))))
  1017. (format:out-fill leftpad padch)
  1018. (format:out-str str)
  1019. (format:out-fill rightpad padch)))))
  1020. ;; format fixed flonums (~F)
  1021. (define (format:out-fixed modifier number pars)
  1022. (if (not (or (number? number) (string? number)))
  1023. (format:error "argument is not a number or a number string"))
  1024. (let ((l (length pars)))
  1025. (let ((width (format:par pars l 0 #f "width"))
  1026. (digits (format:par pars l 1 #f "digits"))
  1027. (scale (format:par pars l 2 0 #f))
  1028. (overch (format:par pars l 3 #f #f))
  1029. (padch (format:par pars l 4 format:space-ch #f)))
  1030. (cond
  1031. ((and (number? number)
  1032. (or (inf? number) (nan? number)))
  1033. (format:out-inf-nan number width digits #f overch padch))
  1034. (digits
  1035. (format:parse-float number #t scale)
  1036. (if (<= (- format:fn-len format:fn-dot) digits)
  1037. (format:fn-zfill #f (- digits (- format:fn-len format:fn-dot)))
  1038. (format:fn-round digits))
  1039. (if width
  1040. (let ((numlen (+ format:fn-len 1)))
  1041. (if (or (not format:fn-pos?) (eq? modifier 'at))
  1042. (set! numlen (+ numlen 1)))
  1043. (if (and (= format:fn-dot 0) (> width (+ digits 1)))
  1044. (set! numlen (+ numlen 1)))
  1045. (if (< numlen width)
  1046. (format:out-fill (- width numlen) (integer->char padch)))
  1047. (if (and overch (> numlen width))
  1048. (format:out-fill width (integer->char overch))
  1049. (format:fn-out modifier (> width (+ digits 1)))))
  1050. (format:fn-out modifier #t)))
  1051. (else
  1052. (format:parse-float number #t scale)
  1053. (format:fn-strip)
  1054. (if width
  1055. (let ((numlen (+ format:fn-len 1)))
  1056. (if (or (not format:fn-pos?) (eq? modifier 'at))
  1057. (set! numlen (+ numlen 1)))
  1058. (if (= format:fn-dot 0)
  1059. (set! numlen (+ numlen 1)))
  1060. (if (< numlen width)
  1061. (format:out-fill (- width numlen) (integer->char padch)))
  1062. (if (> numlen width) ; adjust precision if possible
  1063. (let ((dot-index (- numlen
  1064. (- format:fn-len format:fn-dot))))
  1065. (if (> dot-index width)
  1066. (if overch ; numstr too big for required width
  1067. (format:out-fill width (integer->char overch))
  1068. (format:fn-out modifier #t))
  1069. (begin
  1070. (format:fn-round (- width dot-index))
  1071. (format:fn-out modifier #t))))
  1072. (format:fn-out modifier #t)))
  1073. (format:fn-out modifier #t)))))))
  1074. ;; format exponential flonums (~E)
  1075. (define (format:out-expon modifier number pars)
  1076. (if (not (or (number? number) (string? number)))
  1077. (format:error "argument is not a number"))
  1078. (let ((l (length pars)))
  1079. (let ((width (format:par pars l 0 #f "width"))
  1080. (digits (format:par pars l 1 #f "digits"))
  1081. (edigits (format:par pars l 2 #f "exponent digits"))
  1082. (scale (format:par pars l 3 1 #f))
  1083. (overch (format:par pars l 4 #f #f))
  1084. (padch (format:par pars l 5 format:space-ch #f))
  1085. (expch (format:par pars l 6 #f #f)))
  1086. (cond
  1087. ((and (number? number)
  1088. (or (inf? number) (nan? number)))
  1089. (format:out-inf-nan number width digits edigits overch padch))
  1090. (digits ; fixed precision
  1091. (let ((digits (if (> scale 0)
  1092. (if (< scale (+ digits 2))
  1093. (+ (- digits scale) 1)
  1094. 0)
  1095. digits)))
  1096. (format:parse-float number #f scale)
  1097. (if (<= (- format:fn-len format:fn-dot) digits)
  1098. (format:fn-zfill #f (- digits (- format:fn-len format:fn-dot)))
  1099. (format:fn-round digits))
  1100. (if width
  1101. (if (and edigits overch (> format:en-len edigits))
  1102. (format:out-fill width (integer->char overch))
  1103. (let ((numlen (+ format:fn-len 3))) ; .E+
  1104. (if (or (not format:fn-pos?) (eq? modifier 'at))
  1105. (set! numlen (+ numlen 1)))
  1106. (if (and (= format:fn-dot 0) (> width (+ digits 1)))
  1107. (set! numlen (+ numlen 1)))
  1108. (set! numlen
  1109. (+ numlen
  1110. (if (and edigits (>= edigits format:en-len))
  1111. edigits
  1112. format:en-len)))
  1113. (if (< numlen width)
  1114. (format:out-fill (- width numlen)
  1115. (integer->char padch)))
  1116. (if (and overch (> numlen width))
  1117. (format:out-fill width (integer->char overch))
  1118. (begin
  1119. (format:fn-out modifier (> width (- numlen 1)))
  1120. (format:en-out edigits expch)))))
  1121. (begin
  1122. (format:fn-out modifier #t)
  1123. (format:en-out edigits expch)))))
  1124. (else
  1125. (format:parse-float number #f scale)
  1126. (format:fn-strip)
  1127. (if width
  1128. (if (and edigits overch (> format:en-len edigits))
  1129. (format:out-fill width (integer->char overch))
  1130. (let ((numlen (+ format:fn-len 3))) ; .E+
  1131. (if (or (not format:fn-pos?) (eq? modifier 'at))
  1132. (set! numlen (+ numlen 1)))
  1133. (if (= format:fn-dot 0)
  1134. (set! numlen (+ numlen 1)))
  1135. (set! numlen
  1136. (+ numlen
  1137. (if (and edigits (>= edigits format:en-len))
  1138. edigits
  1139. format:en-len)))
  1140. (if (< numlen width)
  1141. (format:out-fill (- width numlen)
  1142. (integer->char padch)))
  1143. (if (> numlen width) ; adjust precision if possible
  1144. (let ((f (- format:fn-len format:fn-dot))) ; fract len
  1145. (if (> (- numlen f) width)
  1146. (if overch ; numstr too big for required width
  1147. (format:out-fill width
  1148. (integer->char overch))
  1149. (begin
  1150. (format:fn-out modifier #t)
  1151. (format:en-out edigits expch)))
  1152. (begin
  1153. (format:fn-round (+ (- f numlen) width))
  1154. (format:fn-out modifier #t)
  1155. (format:en-out edigits expch))))
  1156. (begin
  1157. (format:fn-out modifier #t)
  1158. (format:en-out edigits expch)))))
  1159. (begin
  1160. (format:fn-out modifier #t)
  1161. (format:en-out edigits expch))))))))
  1162. ;; format general flonums (~G)
  1163. (define (format:out-general modifier number pars)
  1164. (if (not (or (number? number) (string? number)))
  1165. (format:error "argument is not a number or a number string"))
  1166. (let ((l (length pars)))
  1167. (let ((width (if (> l 0) (list-ref pars 0) #f))
  1168. (digits (if (> l 1) (list-ref pars 1) #f))
  1169. (edigits (if (> l 2) (list-ref pars 2) #f))
  1170. (overch (if (> l 4) (list-ref pars 4) #f))
  1171. (padch (if (> l 5) (list-ref pars 5) #f)))
  1172. (cond
  1173. ((and (number? number)
  1174. (or (inf? number) (nan? number)))
  1175. ;; FIXME: this isn't right.
  1176. (format:out-inf-nan number width digits edigits overch padch))
  1177. (else
  1178. (format:parse-float number #t 0)
  1179. (format:fn-strip)
  1180. (let* ((ee (if edigits (+ edigits 2) 4)) ; for the following algorithm
  1181. (ww (if width (- width ee) #f)) ; see Steele's CL book p.395
  1182. (n (if (= format:fn-dot 0) ; number less than (abs 1.0) ?
  1183. (- (format:fn-zlead))
  1184. format:fn-dot))
  1185. (d (if digits
  1186. digits
  1187. (max format:fn-len (min n 7)))) ; q = format:fn-len
  1188. (dd (- d n)))
  1189. (if (<= 0 dd d)
  1190. (begin
  1191. (format:out-fixed modifier number (list ww dd #f overch padch))
  1192. (format:out-fill ee #\space)) ;~@T not implemented yet
  1193. (format:out-expon modifier number pars))))))))
  1194. ;; format dollar flonums (~$)
  1195. (define (format:out-dollar modifier number pars)
  1196. (if (not (or (number? number) (string? number)))
  1197. (format:error "argument is not a number or a number string"))
  1198. (let ((l (length pars)))
  1199. (let ((digits (format:par pars l 0 2 "digits"))
  1200. (mindig (format:par pars l 1 1 "mindig"))
  1201. (width (format:par pars l 2 0 "width"))
  1202. (padch (format:par pars l 3 format:space-ch #f)))
  1203. (cond
  1204. ((and (number? number)
  1205. (or (inf? number) (nan? number)))
  1206. (format:out-inf-nan number width digits #f #f padch))
  1207. (else
  1208. (format:parse-float number #t 0)
  1209. (if (<= (- format:fn-len format:fn-dot) digits)
  1210. (format:fn-zfill #f (- digits (- format:fn-len format:fn-dot)))
  1211. (format:fn-round digits))
  1212. (let ((numlen (+ format:fn-len 1)))
  1213. (if (or (not format:fn-pos?) (memq modifier '(at colon-at)))
  1214. (set! numlen (+ numlen 1)))
  1215. (if (and mindig (> mindig format:fn-dot))
  1216. (set! numlen (+ numlen (- mindig format:fn-dot))))
  1217. (if (and (= format:fn-dot 0) (not mindig))
  1218. (set! numlen (+ numlen 1)))
  1219. (if (< numlen width)
  1220. (case modifier
  1221. ((colon)
  1222. (if (not format:fn-pos?)
  1223. (format:out-char #\-))
  1224. (format:out-fill (- width numlen) (integer->char padch)))
  1225. ((at)
  1226. (format:out-fill (- width numlen) (integer->char padch))
  1227. (format:out-char (if format:fn-pos? #\+ #\-)))
  1228. ((colon-at)
  1229. (format:out-char (if format:fn-pos? #\+ #\-))
  1230. (format:out-fill (- width numlen) (integer->char padch)))
  1231. (else
  1232. (format:out-fill (- width numlen) (integer->char padch))
  1233. (if (not format:fn-pos?)
  1234. (format:out-char #\-))))
  1235. (if format:fn-pos?
  1236. (if (memq modifier '(at colon-at)) (format:out-char #\+))
  1237. (format:out-char #\-))))
  1238. (if (and mindig (> mindig format:fn-dot))
  1239. (format:out-fill (- mindig format:fn-dot) #\0))
  1240. (if (and (= format:fn-dot 0) (not mindig))
  1241. (format:out-char #\0))
  1242. (format:out-substr format:fn-str 0 format:fn-dot)
  1243. (format:out-char #\.)
  1244. (format:out-substr format:fn-str format:fn-dot format:fn-len))))))
  1245. ; the flonum buffers
  1246. (define format:fn-max 400) ; max. number of number digits
  1247. (define format:fn-str (make-string format:fn-max)) ; number buffer
  1248. (define format:fn-len 0) ; digit length of number
  1249. (define format:fn-dot #f) ; dot position of number
  1250. (define format:fn-pos? #t) ; number positive?
  1251. (define format:en-max 10) ; max. number of exponent digits
  1252. (define format:en-str (make-string format:en-max)) ; exponent buffer
  1253. (define format:en-len 0) ; digit length of exponent
  1254. (define format:en-pos? #t) ; exponent positive?
  1255. (define (format:parse-float num fixed? scale)
  1256. (let ((num-str (if (string? num)
  1257. num
  1258. (number->string (exact->inexact num)))))
  1259. (set! format:fn-pos? #t)
  1260. (set! format:fn-len 0)
  1261. (set! format:fn-dot #f)
  1262. (set! format:en-pos? #t)
  1263. (set! format:en-len 0)
  1264. (do ((i 0 (+ i 1))
  1265. (left-zeros 0)
  1266. (mantissa? #t)
  1267. (all-zeros? #t)
  1268. (num-len (string-length num-str))
  1269. (c #f)) ; current exam. character in num-str
  1270. ((= i num-len)
  1271. (if (not format:fn-dot)
  1272. (set! format:fn-dot format:fn-len))
  1273. (if all-zeros?
  1274. (begin
  1275. (set! left-zeros 0)
  1276. (set! format:fn-dot 0)
  1277. (set! format:fn-len 1)))
  1278. ;; now format the parsed values according to format's need
  1279. (if fixed?
  1280. (begin ; fixed format m.nnn or .nnn
  1281. (if (and (> left-zeros 0) (> format:fn-dot 0))
  1282. (if (> format:fn-dot left-zeros)
  1283. (begin ; norm 0{0}nn.mm to nn.mm
  1284. (format:fn-shiftleft left-zeros)
  1285. (set! format:fn-dot (- format:fn-dot left-zeros))
  1286. (set! left-zeros 0))
  1287. (begin ; normalize 0{0}.nnn to .nnn
  1288. (format:fn-shiftleft format:fn-dot)
  1289. (set! left-zeros (- left-zeros format:fn-dot))
  1290. (set! format:fn-dot 0))))
  1291. (if (or (not (= scale 0)) (> format:en-len 0))
  1292. (let ((shift (+ scale (format:en-int))))
  1293. (cond
  1294. (all-zeros? #t)
  1295. ((> (+ format:fn-dot shift) format:fn-len)
  1296. (format:fn-zfill
  1297. #f (- shift (- format:fn-len format:fn-dot)))
  1298. (set! format:fn-dot format:fn-len))
  1299. ((< (+ format:fn-dot shift) 0)
  1300. (format:fn-zfill #t (- (- shift) format:fn-dot))
  1301. (set! format:fn-dot 0))
  1302. (else
  1303. (if (> left-zeros 0)
  1304. (if (<= left-zeros shift) ; shift always > 0 here
  1305. (format:fn-shiftleft shift) ; shift out 0s
  1306. (begin
  1307. (format:fn-shiftleft left-zeros)
  1308. (set! format:fn-dot (- shift left-zeros))))
  1309. (set! format:fn-dot (+ format:fn-dot shift))))))))
  1310. (let ((negexp ; expon format m.nnnEee
  1311. (if (> left-zeros 0)
  1312. (- left-zeros format:fn-dot -1)
  1313. (if (= format:fn-dot 0) 1 0))))
  1314. (if (> left-zeros 0)
  1315. (begin ; normalize 0{0}.nnn to n.nn
  1316. (format:fn-shiftleft left-zeros)
  1317. (set! format:fn-dot 1))
  1318. (if (= format:fn-dot 0)
  1319. (set! format:fn-dot 1)))
  1320. (format:en-set (- (+ (- format:fn-dot scale) (format:en-int))
  1321. negexp))
  1322. (cond
  1323. (all-zeros?
  1324. (format:en-set 0)
  1325. (set! format:fn-dot 1))
  1326. ((< scale 0) ; leading zero
  1327. (format:fn-zfill #t (- scale))
  1328. (set! format:fn-dot 0))
  1329. ((> scale format:fn-dot)
  1330. (format:fn-zfill #f (- scale format:fn-dot))
  1331. (set! format:fn-dot scale))
  1332. (else
  1333. (set! format:fn-dot scale)))))
  1334. #t)
  1335. ;; do body
  1336. (set! c (string-ref num-str i)) ; parse the output of number->string
  1337. (cond ; which can be any valid number
  1338. ((char-numeric? c) ; representation of R4RS except
  1339. (if mantissa? ; complex numbers
  1340. (begin
  1341. (if (char=? c #\0)
  1342. (if all-zeros?
  1343. (set! left-zeros (+ left-zeros 1)))
  1344. (begin
  1345. (set! all-zeros? #f)))
  1346. (string-set! format:fn-str format:fn-len c)
  1347. (set! format:fn-len (+ format:fn-len 1)))
  1348. (begin
  1349. (string-set! format:en-str format:en-len c)
  1350. (set! format:en-len (+ format:en-len 1)))))
  1351. ((or (char=? c #\-) (char=? c #\+))
  1352. (if mantissa?
  1353. (set! format:fn-pos? (char=? c #\+))
  1354. (set! format:en-pos? (char=? c #\+))))
  1355. ((char=? c #\.)
  1356. (set! format:fn-dot format:fn-len))
  1357. ((char=? c #\e)
  1358. (set! mantissa? #f))
  1359. ((char=? c #\E)
  1360. (set! mantissa? #f))
  1361. ((char-whitespace? c) #t)
  1362. ((char=? c #\d) #t) ; decimal radix prefix
  1363. ((char=? c #\#) #t)
  1364. (else
  1365. (format:error "illegal character `~c' in number->string" c))))))
  1366. (define (format:en-int) ; convert exponent string to integer
  1367. (if (= format:en-len 0)
  1368. 0
  1369. (do ((i 0 (+ i 1))
  1370. (n 0))
  1371. ((= i format:en-len)
  1372. (if format:en-pos?
  1373. n
  1374. (- n)))
  1375. (set! n (+ (* n 10) (- (char->integer (string-ref format:en-str i))
  1376. format:zero-ch))))))
  1377. (define (format:en-set en) ; set exponent string number
  1378. (set! format:en-len 0)
  1379. (set! format:en-pos? (>= en 0))
  1380. (let ((en-str (number->string en)))
  1381. (do ((i 0 (+ i 1))
  1382. (en-len (string-length en-str))
  1383. (c #f))
  1384. ((= i en-len))
  1385. (set! c (string-ref en-str i))
  1386. (if (char-numeric? c)
  1387. (begin
  1388. (string-set! format:en-str format:en-len c)
  1389. (set! format:en-len (+ format:en-len 1)))))))
  1390. (define (format:fn-zfill left? n) ; fill current number string with 0s
  1391. (if (> (+ n format:fn-len) format:fn-max) ; from the left or right
  1392. (format:error "number is too long to format (enlarge format:fn-max)"))
  1393. (set! format:fn-len (+ format:fn-len n))
  1394. (if left?
  1395. (do ((i format:fn-len (- i 1))) ; fill n 0s to left
  1396. ((< i 0))
  1397. (string-set! format:fn-str i
  1398. (if (< i n)
  1399. #\0
  1400. (string-ref format:fn-str (- i n)))))
  1401. (do ((i (- format:fn-len n) (+ i 1))) ; fill n 0s to the right
  1402. ((= i format:fn-len))
  1403. (string-set! format:fn-str i #\0))))
  1404. (define (format:fn-shiftleft n) ; shift left current number n positions
  1405. (if (> n format:fn-len)
  1406. (format:error "internal error in format:fn-shiftleft (~d,~d)"
  1407. n format:fn-len))
  1408. (do ((i n (+ i 1)))
  1409. ((= i format:fn-len)
  1410. (set! format:fn-len (- format:fn-len n)))
  1411. (string-set! format:fn-str (- i n) (string-ref format:fn-str i))))
  1412. (define (format:fn-round digits) ; round format:fn-str
  1413. (set! digits (+ digits format:fn-dot))
  1414. (do ((i digits (- i 1)) ; "099",2 -> "10"
  1415. (c 5)) ; "023",2 -> "02"
  1416. ((or (= c 0) (< i 0)) ; "999",2 -> "100"
  1417. (if (= c 1) ; "005",2 -> "01"
  1418. (begin ; carry overflow
  1419. (set! format:fn-len digits)
  1420. (format:fn-zfill #t 1) ; add a 1 before fn-str
  1421. (string-set! format:fn-str 0 #\1)
  1422. (set! format:fn-dot (+ format:fn-dot 1)))
  1423. (set! format:fn-len digits)))
  1424. (set! c (+ (- (char->integer (string-ref format:fn-str i))
  1425. format:zero-ch) c))
  1426. (string-set! format:fn-str i (integer->char
  1427. (if (< c 10)
  1428. (+ c format:zero-ch)
  1429. (+ (- c 10) format:zero-ch))))
  1430. (set! c (if (< c 10) 0 1))))
  1431. (define (format:fn-out modifier add-leading-zero?)
  1432. (if format:fn-pos?
  1433. (if (eq? modifier 'at)
  1434. (format:out-char #\+))
  1435. (format:out-char #\-))
  1436. (if (= format:fn-dot 0)
  1437. (if add-leading-zero?
  1438. (format:out-char #\0))
  1439. (format:out-substr format:fn-str 0 format:fn-dot))
  1440. (format:out-char #\.)
  1441. (format:out-substr format:fn-str format:fn-dot format:fn-len))
  1442. (define (format:en-out edigits expch)
  1443. (format:out-char (if expch (integer->char expch) #\E))
  1444. (format:out-char (if format:en-pos? #\+ #\-))
  1445. (if edigits
  1446. (if (< format:en-len edigits)
  1447. (format:out-fill (- edigits format:en-len) #\0)))
  1448. (format:out-substr format:en-str 0 format:en-len))
  1449. (define (format:fn-strip) ; strip trailing zeros but one
  1450. (string-set! format:fn-str format:fn-len #\0)
  1451. (do ((i format:fn-len (- i 1)))
  1452. ((or (not (char=? (string-ref format:fn-str i) #\0))
  1453. (<= i format:fn-dot))
  1454. (set! format:fn-len (+ i 1)))))
  1455. (define (format:fn-zlead) ; count leading zeros
  1456. (do ((i 0 (+ i 1)))
  1457. ((or (= i format:fn-len)
  1458. (not (char=? (string-ref format:fn-str i) #\0)))
  1459. (if (= i format:fn-len) ; found a real zero
  1460. 0
  1461. i))))
  1462. ;;; some global functions not found in SLIB
  1463. (define (string-capitalize-first str) ; "hello" -> "Hello"
  1464. (let ((cap-str (string-copy str)) ; "hELLO" -> "Hello"
  1465. (non-first-alpha #f) ; "*hello" -> "*Hello"
  1466. (str-len (string-length str))) ; "hello you" -> "Hello you"
  1467. (do ((i 0 (+ i 1)))
  1468. ((= i str-len) cap-str)
  1469. (let ((c (string-ref str i)))
  1470. (if (char-alphabetic? c)
  1471. (if non-first-alpha
  1472. (string-set! cap-str i (char-downcase c))
  1473. (begin
  1474. (set! non-first-alpha #t)
  1475. (string-set! cap-str i (char-upcase c)))))))))
  1476. ;; Aborts the program when a formatting error occures. This is a null
  1477. ;; argument closure to jump to the interpreters toplevel continuation.
  1478. (define (format:abort) (error "error in format"))
  1479. (let ((arg-pos (format:format-work format-string format-args))
  1480. (arg-len (length format-args)))
  1481. (cond
  1482. ((> arg-pos arg-len)
  1483. (set! format:arg-pos (+ arg-len 1))
  1484. (display format:arg-pos)
  1485. (format:error "~a missing argument~:p" (- arg-pos arg-len)))
  1486. (else
  1487. (if flush-output?
  1488. (force-output port))
  1489. (if destination
  1490. #t
  1491. (let ((str (get-output-string port)))
  1492. (close-port port)
  1493. str)))))))
  1494. ;; Thanks to Shuji Narazaki
  1495. (module-set! the-root-module 'format format)