From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Adam Faiz Newsgroups: gmane.lisp.guile.devel Subject: [PATCH 1/2] rdelim: Add new procedure `for-rdelim-in-port`. Date: Mon, 16 Dec 2024 23:21:51 +0800 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="26241"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Ricardo Wurmus , Tomas Volf <~@wolfsden.cz>, Maxime Devos , Nala Ginrut To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Mon Dec 16 16:22:32 2024 Return-path: Envelope-to: guile-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1tNCvY-0006iH-Cy for guile-devel@m.gmane-mx.org; Mon, 16 Dec 2024 16:22:32 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tNCvH-0004hp-Hc; Mon, 16 Dec 2024 10:22:15 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tNCvB-0004hI-Kz for guile-devel@gnu.org; Mon, 16 Dec 2024 10:22:11 -0500 Original-Received: from layka.disroot.org ([178.21.23.139]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tNCv9-0000fC-8A for guile-devel@gnu.org; Mon, 16 Dec 2024 10:22:09 -0500 Original-Received: from mail01.disroot.lan (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id CBA9E24AFE; Mon, 16 Dec 2024 16:22:03 +0100 (CET) X-Virus-Scanned: SPAM Filter at disroot.org Original-Received: from layka.disroot.org ([127.0.0.1]) by localhost (disroot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id XKE96svXmo0e; Mon, 16 Dec 2024 16:22:03 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1734362523; bh=onbojmiXObsQehmGBb0OsVvKPLLIuZ0Jk06om3zQjqw=; h=Date:To:Cc:From:Subject; b=XKn9cPmxexfHB7Xg98xuAoloM328UY7bYfqwOBu4GEXqQ0pbpl+mP/VimelELyWvN cYOWGFnvEAELDxTwX4w7/8ol1x/wE61uoRooQLTJIywg07dxVVkvtCV3dbHdtfwqEA BcQja1VR5ZoWeW2TQ+ih7wJUVsXb0mseOjhvNATUBVNBd8dpIIjP/mnkJb3VnN+F1m Jc/eICZutdrHHSFZoWuwy4Pq57zsqPT71EUrHSuYXYY0ENx40oPxLpPUc+I44wIxAS NE77fR/+X68OnmD0RJqoHnSfRd1NaMV1w55TN0O8Du/HsRgMXo2r1aMf80+5kVdd3/ Yww4slJfy34iQ== Content-Language: en-US Received-SPF: pass client-ip=178.21.23.139; envelope-from=adam.faiz@disroot.org; helo=layka.disroot.org X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.devel:22832 Archived-At: >From 302159fe61d9df526911ead8ea6ad823ad8b0443 Mon Sep 17 00:00:00 2001 From: AwesomeAdam54321 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