summaryrefslogtreecommitdiff
path: root/module
diff options
context:
space:
mode:
authorDaniel Llorens <daniel.llorens@bluewin.ch>2018-12-10 11:57:05 +0100
committerDaniel Llorens <daniel.llorens@bluewin.ch>2018-12-11 22:44:34 +0100
commitf74cfce4f1572931ec66616a32633c74874769fc (patch)
tree4c14a1330ebd7aff1ec9628dba4d527798be4702 /module
parent8e86dd93a0640161fe0098a80ccc9b814dddd280 (diff)
Support ~N in SRFI-19 string->date
* module/srfi/srfi-19.scm (fractional-integer-reader, make-fractional-integer-reader): From reference implementation. (reader-directives): Handle #\N, from reference implementation. * test-suite/tests/srfi-19: Add tests for string->date ~N. * doc/ref/srfi-modules.texi (string->date): Add line for ~N.
Diffstat (limited to 'module')
-rw-r--r--module/srfi/srfi-19.scm22
1 files changed, 22 insertions, 0 deletions
diff --git a/module/srfi/srfi-19.scm b/module/srfi/srfi-19.scm
index 46de91a7e..66939f92c 100644
--- a/module/srfi/srfi-19.scm
+++ b/module/srfi/srfi-19.scm
@@ -1193,6 +1193,24 @@
(lambda (port)
(integer-reader upto port)))
+;; read an fractional integer upto n characters long on port; upto -> #f if any length
+;;
+;; The return value is normalized to upto decimal places. For example, if upto is 9 and
+;; the string read is "123", the return value is 123000000.
+(define (fractional-integer-reader upto port)
+ (define (accum-int port accum nchars)
+ (let ((ch (peek-char port)))
+ (if (or (eof-object? ch)
+ (not (char-numeric? ch))
+ (and upto (>= nchars upto)))
+ (* accum (expt 10 (- upto nchars)))
+ (accum-int port (+ (* accum 10) (char->int (read-char port))) (+ nchars 1)))))
+ (accum-int port 0 0))
+
+(define (make-fractional-integer-reader upto)
+ (lambda (port)
+ (fractional-integer-reader upto port)))
+
;; read *exactly* n characters and convert to integer; could be padded
(define (integer-reader-exact n port)
(let ((padding-ok #t))
@@ -1305,6 +1323,7 @@
(define read-directives
(let ((ireader4 (make-integer-reader 4))
(ireader2 (make-integer-reader 2))
+ (fireader9 (make-fractional-integer-reader 9))
(eireader2 (make-integer-exact-reader 2))
(locale-reader-abbr-weekday (make-locale-reader
locale-abbr-weekday->index))
@@ -1343,6 +1362,9 @@
(list #\M char-numeric? ireader2 (lambda (val object)
(set-date-minute!
object val)))
+ (list #\N char-numeric? fireader9 (lambda (val object)
+ (set-date-nanosecond!
+ object val)))
(list #\S char-numeric? ireader2 (lambda (val object)
(set-date-second! object val)))
(list #\y char-fail eireader2