unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* [PATCH 1/2] rdelim: Add new procedure `for-rdelim-in-port`.
@ 2024-12-16 15:21 Adam Faiz
  2024-12-16 15:24 ` [PATCH 2/2] doc: Add documentation for `for-rdelim-in-port` and, related procedures Adam Faiz
  2024-12-16 16:46 ` [PATCH 1/2] rdelim: Add new procedure `for-rdelim-in-port` Nala Ginrut
  0 siblings, 2 replies; 27+ messages in thread
From: Adam Faiz @ 2024-12-16 15:21 UTC (permalink / raw)
  To: guile-devel; +Cc: Ricardo Wurmus, Tomas Volf, Maxime Devos, Nala Ginrut

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



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

end of thread, other threads:[~2024-12-20 18:40 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-16 15:21 [PATCH 1/2] rdelim: Add new procedure `for-rdelim-in-port` Adam Faiz
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-20  9:15           ` Maxime Devos
2024-12-20  9:57             ` Nala Ginrut
2024-12-20 11:51               ` Maxime Devos
2024-12-20 12:00                 ` Nala Ginrut
2024-12-20 12:53                   ` Maxime Devos
2024-12-20 13:00                     ` Nala Ginrut
2024-12-20 13:45                       ` Maxime Devos
2024-12-20 13:52                         ` Nala Ginrut
2024-12-20 14:18                           ` Maxime Devos
2024-12-20 14:30                             ` Nala Ginrut
2024-12-20 14:32                               ` Nala Ginrut
2024-12-20 14:47                               ` Maxime Devos
2024-12-20 14:56                                 ` Nala Ginrut
2024-12-20 15:07                                   ` Maxime Devos
2024-12-20 15:13                                     ` Nala Ginrut
2024-12-19 21:50     ` Mikael Djurfeldt
2024-12-20 15:15       ` [PATCH] test-suite: Add tests for `for-rdelim-in-port`-relatedfunctions Maxime Devos
2024-12-20 17:11         ` Mikael Djurfeldt
2024-12-20 17:31           ` Nala Ginrut
2024-12-20 18:40           ` Maxime Devos
2024-12-16 16:46 ` [PATCH 1/2] rdelim: Add new procedure `for-rdelim-in-port` 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).