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 v3] rdelim: Add new procedure `for-line-in-file`. Date: Mon, 16 Dec 2024 16:43:44 +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="33512"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Nala Ginrut , Ricardo Wurmus , Maxime Devos To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Mon Dec 16 09:44:19 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 1tN6iB-0008bZ-Eb for guile-devel@m.gmane-mx.org; Mon, 16 Dec 2024 09:44:19 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tN6hn-0006gP-VL; Mon, 16 Dec 2024 03:43:55 -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 1tN6hl-0006fw-SF for guile-devel@gnu.org; Mon, 16 Dec 2024 03:43:53 -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 1tN6hj-0005iL-Js for guile-devel@gnu.org; Mon, 16 Dec 2024 03:43:53 -0500 Original-Received: from mail01.disroot.lan (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id 2C89C2535A; Mon, 16 Dec 2024 09:43:49 +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 65L3gpoQXQil; Mon, 16 Dec 2024 09:43:48 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1734338628; bh=nbf/vF/WR5yYXJB9ycN8Pympz6W0TS9H/ZEkKnzAnIA=; h=Date:To:Cc:From:Subject; b=N6iVnMqLhwzDxAeYb7mxE6OLQ1Pt7zUj64wZulNeweh2kBa2MhM4c/qq7f7xeTNgk Cz3UQVixG4ObSIp2ukBLNppHBe7V/lkItqf/SPsWDbT4W4K7xBan1rNEZFyDXqsgba iHZdDvSYY+mOlN3YTvvTScu3lAEjaz3IN+kdvHTGUHpqjoM5XihSOrXULflTBqUnEn +aeieP8eGB2AAYdy7FRWIW0KXkcfiWy9WjCrgjkDrbuY4/ftJfncw8vF2nTU3UtztC jR3GcF1UjxXX1ZTav1iaWfCaWAj6ay0TyMEI3CI4HxFgP/x7jAQEO7pQdUeV1Wzowi 8Wjr3MQYcpCfg== 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:22821 Archived-At: >From fe113e9efc08aae2a7e3792a1018c496212bd774 Mon Sep 17 00:00:00 2001 From: AwesomeAdam54321 Date: Sun, 15 Dec 2024 23:48:30 +0800 Subject: [PATCH v3] rdelim: Add new procedure `for-line-in-file`. * module/ice-9/rdelim.scm (for-line-in-file): Add it. This procedure makes it convenient to do per-line processing of a text file. --- module/ice-9/rdelim.scm | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/module/ice-9/rdelim.scm b/module/ice-9/rdelim.scm index d2cd081d7..b4c55c12e 100644 --- a/module/ice-9/rdelim.scm +++ b/module/ice-9/rdelim.scm @@ -23,7 +23,8 @@ ;;; similar to (scsh rdelim) but somewhat incompatible. (define-module (ice-9 rdelim) - #:export (read-line + #:export (for-line-in-file + read-line read-line! read-delimited read-delimited! @@ -206,3 +207,23 @@ characters to read. By default, there is no limit." line) (else (error "unexpected handle-delim value: " 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 can either be a filename string or an already opened input port. +The corresponding port is closed upon completion. + +The line provided to PROC is guaranteed to be a string." + (let ((port + (if (input-port? file) + file + (open-input-file file + #:encoding encoding + #:guess-encoding guess-encoding)))) + (let loop ((line (read-line port))) + (cond ((eof-object? line) + (close-port port)) + (else + (proc line) + (loop (read-line port))))))) -- 2.41.0