format.scm 74 KB

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