unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* [PATCH] add new read-delimited option to return #f while terminating delimiter can't be found.
@ 2012-03-07 16:32 Nala Ginrut
  2013-01-16 17:42 ` Andy Wingo
  2013-01-23 10:00 ` Andy Wingo
  0 siblings, 2 replies; 7+ messages in thread
From: Nala Ginrut @ 2012-03-07 16:32 UTC (permalink / raw)
  To: guile-devel


[-- Attachment #1.1: Type: text/plain, Size: 1124 bytes --]

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.

[-- Attachment #1.2: Type: text/html, Size: 1535 bytes --]

[-- Attachment #2: 0001-add-an-option-to-let-read-delimited-return-false-whi.patch --]
[-- Type: text/x-patch, Size: 1178 bytes --]

From f206d95c5be214fff0f30d15a794b40d3dee2577 Mon Sep 17 00:00:00 2001
From: NalaGinrut <NalaGinrut@gmail.com>
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


[-- Attachment #3: 0002-Add-new-handle-delim-option-fail-to-the-manual.patch --]
[-- Type: text/x-patch, Size: 1020 bytes --]

From bcb495d67717d15414cdf08daa10562b9e884174 Mon Sep 17 00:00:00 2001
From: NalaGinrut <NalaGinrut@gmail.com>
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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2013-01-23 10:24 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-03-07 16:32 [PATCH] add new read-delimited option to return #f while terminating delimiter can't be found Nala Ginrut
2013-01-16 17:42 ` Andy Wingo
2013-01-17  9:07   ` Nala Ginrut
2013-01-22 12:44     ` Andy Wingo
2013-01-22 16:41       ` Nala Ginrut
2013-01-23 10:00 ` Andy Wingo
2013-01-23 10:24   ` Nala Ginrut

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).