I found current read-delimited will return the whole string if delimiter
can't be found. It's inconvenient for some cases.
I expect it return #f for this.
And Andy said it maybe because some back compatible reasons. So I decide to
add an option to do this job.
If we use the original version, we must do this:
-----------------------------------cut-----------------------------------------------------
(let ((token (call-with-input-string "asdf" (lambda (port) (read-delimited
"@" port 'split)))))
(if (eof-object? (cdr token))
""
(car token)))
-----------------------------------end----------------------------------------------------
It's rather ugly.
Now it's better:
-----------------------------------cut-----------------------------------------------------
(call-with-input-string "asdf" (lambda (port) (read-delimited "@" port
'fail))))
==> #f
(call-with-input-string "as@df" (lambda (port) (read-delimited "@" port
'fail))))
==> "as"
-----------------------------------end----------------------------------------------------
If delimiter exists, it works like 'trim mode.
Comments?
Regards.
From f206d95c5be214fff0f30d15a794b40d3dee2577 Mon Sep 17 00:00:00 2001
From: NalaGinrut <[email protected]>
Date: Wed, 7 Mar 2012 20:05:38 +0800
Subject: [PATCH 1/2] add an option to let read-delimited return false while delimiter missing
---
module/ice-9/rdelim.scm | 2 ++
1 files changed, 2 insertions(+), 0 deletions(-)
diff --git a/module/ice-9/rdelim.scm b/module/ice-9/rdelim.scm
index c6ab2ba..9ab52b8 100644
--- a/module/ice-9/rdelim.scm
+++ b/module/ice-9/rdelim.scm
@@ -74,6 +74,7 @@
((concat) (string-set! buf (+ nchars start) terminator)
(+ nchars 1))
((split) (cons nchars terminator))
+ ((fail) (if (eof-object? terminator) #f nchars))
(else (error "unexpected handle-delim value: "
handle-delim)))))))
@@ -111,6 +112,7 @@
(string-append joined (string terminator))))
((trim peek) joined)
((split) (cons joined terminator))
+ ((fail) (if (eof-object? terminator) #f joined))
(else (error "unexpected handle-delim value: "
handle-delim)))))))))
--
1.7.0.4
From bcb495d67717d15414cdf08daa10562b9e884174 Mon Sep 17 00:00:00 2001
From: NalaGinrut <[email protected]>
Date: Wed, 7 Mar 2012 23:11:40 +0800
Subject: [PATCH 2/2] Add new handle-delim option 'fail to the manual.
---
doc/ref/api-io.texi | 3 +++
1 files changed, 3 insertions(+), 0 deletions(-)
diff --git a/doc/ref/api-io.texi b/doc/ref/api-io.texi
index 24c2706..fdb6966 100644
--- a/doc/ref/api-io.texi
+++ b/doc/ref/api-io.texi
@@ -534,6 +534,9 @@ Read text until one of the characters in the string @var{delims} is found
or end-of-file is reached. Read from @var{port} if supplied, otherwise
from the value returned by @code{(current-input-port)}.
@var{handle-delim} takes the same values as described for @code{read-line}.
+But there's a special @var{handle-delim} @code{'fail} for @code{read-delimited}.
+Which return #f if terminating delimiter can not be found.
+Otherwise the result would be the same as trim.
@end deffn
@c begin (scm-doc-string "rdelim.scm" "read-delimited!")
--
1.7.0.4