format.scm 66 KB

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