From: Adam Faiz <adam.faiz@disroot.org>
To: guile-devel@gnu.org
Cc: Ricardo Wurmus <rekado@elephly.net>, Tomas Volf <~@wolfsden.cz>,
Maxime Devos <maximedevos@telenet.be>,
Nala Ginrut <nalaginrut@gmail.com>
Subject: [PATCH 1/2] rdelim: Add new procedure `for-rdelim-in-port`.
Date: Mon, 16 Dec 2024 23:21:51 +0800 [thread overview]
Message-ID: <d6abae37-02d8-a7e0-e184-9db06159b975@disroot.org> (raw)
From 302159fe61d9df526911ead8ea6ad823ad8b0443 Mon Sep 17 00:00:00 2001
From: AwesomeAdam54321 <adam.faiz@disroot.org>
Date: Sun, 15 Dec 2024 23:48:30 +0800
Subject: [PATCH 1/2] rdelim: Add new procedure `for-rdelim-in-port`.
* module/ice-9/rdelim.scm (for-rdelim-in-port): Add it.
(for-delimited-in-port): Define as a specialised `for-rdelim-in-port`.
(for-line-in-file): Define as a specialised `for-delimited-in-port`.
These procedures provide a backbone for parsing, with domain-specific
logic implemented as a separate procedure to be passed as an argument.
---
module/ice-9/rdelim.scm | 35 ++++++++++++++++++++++++++++++++++-
1 file changed, 34 insertions(+), 1 deletion(-)
diff --git a/module/ice-9/rdelim.scm b/module/ice-9/rdelim.scm
index d2cd081d7..0bbaaf904 100644
--- a/module/ice-9/rdelim.scm
+++ b/module/ice-9/rdelim.scm
@@ -23,7 +23,10 @@
;;; similar to (scsh rdelim) but somewhat incompatible.
(define-module (ice-9 rdelim)
- #:export (read-line
+ #:export (for-delimited-in-port
+ for-line-in-file
+ for-rdelim-in-port
+ read-line
read-line!
read-delimited
read-delimited!
@@ -206,3 +209,33 @@ characters to read. By default, there is no limit."
line)
(else
(error "unexpected handle-delim value: " handle-delim)))))
+
+(define* (for-rdelim-in-port port proc rdelim-proc
+ #:key (stop-pred eof-object?))
+ "Call PROC for every (RDELIM-PROC PORT) in PORT until STOP-PRED returns #t.
+RDELIM-PROC has to advance through PORT with every call."
+ (let loop ((rdelim (rdelim-proc port)))
+ (cond ((stop-pred rdelim)
+ (close-port port))
+ (else
+ (proc rdelim)
+ (loop (rdelim-proc port))))))
+
+(define* (for-delimited-in-port port proc
+ #:key (delims "\n") (handle-delim 'trim))
+ "Call PROC for every delimited line in PORT until the eof-object is reached."
+ (for-rdelim-in-port port proc
+ (lambda (port)
+ (read-delimited delims port handle-delim))))
+
+(define* (for-line-in-file file proc
+ #:key (encoding #f) (guess-encoding #f))
+ "Call PROC for every line in FILE until the eof-object is reached.
+FILE must be a filename string.
+
+The line provided to PROC is guaranteed to be a string."
+ (call-with-input-file
+ file
+ (lambda (port)
+ (for-delimited-in-port port proc))
+ #:encoding encoding #:guess-encoding guess-encoding))
--
2.41.0
next reply other threads:[~2024-12-16 15:21 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-16 15:21 Adam Faiz [this message]
2024-12-16 15:24 ` [PATCH 2/2] doc: Add documentation for `for-rdelim-in-port` and, related procedures Adam Faiz
2024-12-17 4:31 ` [PATCH] test-suite: Add tests for `for-rdelim-in-port`-related functions Adam Faiz
2024-12-17 5:11 ` Nala Ginrut
2024-12-17 7:21 ` Mikael Djurfeldt
2024-12-17 16:43 ` Mikael Djurfeldt
2024-12-16 16:46 ` [PATCH 1/2] rdelim: Add new procedure `for-rdelim-in-port` Nala Ginrut
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=d6abae37-02d8-a7e0-e184-9db06159b975@disroot.org \
--to=adam.faiz@disroot.org \
--cc=guile-devel@gnu.org \
--cc=maximedevos@telenet.be \
--cc=nalaginrut@gmail.com \
--cc=rekado@elephly.net \
--cc=~@wolfsden.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).