[latex3-commits] [git/LaTeX3-latex3-latex3] master: Implement \ior_map_variable:NNn (and ior_str version) (99c0642)

Bruno Le Floch bruno at le-floch.fr
Sun Jan 13 23:12:22 CET 2019


Repository : https://github.com/latex3/latex3
On branch  : master
Link       : https://github.com/latex3/latex3/commit/99c0642e160dd00a79c01a99e3905b2714738a1e

>---------------------------------------------------------------

commit 99c0642e160dd00a79c01a99e3905b2714738a1e
Author: Bruno Le Floch <bruno at le-floch.fr>
Date:   Sun Jan 13 23:10:35 2019 +0100

    Implement \ior_map_variable:NNn (and ior_str version)


>---------------------------------------------------------------

99c0642e160dd00a79c01a99e3905b2714738a1e
 l3kernel/l3candidates.dtx       |   30 ++++++++++++++++++++++++++++++
 l3kernel/l3file.dtx             |   31 +++++++++++++++++++++++++++++++
 l3kernel/testfiles/m3ior002.lvt |   31 ++++++++++++++++++++++++++++++-
 l3kernel/testfiles/m3ior002.tlg |   12 +++++++++++-
 4 files changed, 102 insertions(+), 2 deletions(-)

diff --git a/l3kernel/l3candidates.dtx b/l3kernel/l3candidates.dtx
index 50467d6..59509d5 100644
--- a/l3kernel/l3candidates.dtx
+++ b/l3kernel/l3candidates.dtx
@@ -404,6 +404,36 @@
 %   \end{texnote}
 % \end{function}
 %
+% \begin{function}[added = 2019-01-13]{\ior_map_variable:NNn}
+%   \begin{syntax}
+%     \cs{ior_map_variable:NNn} \meta{stream} \meta{tl~var} \Arg{code}
+%   \end{syntax}
+%   For each set of \meta{lines} obtained by calling \cs{ior_get:NN}
+%   until reaching the end of the file, stores the \meta{lines} in the
+%   \meta{tl~var} then applies the \meta{code}.  The \meta{code} will
+%   usually make use of the \meta{variable}, but this is not enforced.
+%   The assignments to the \meta{variable} are local.  \TeX{} ignores
+%   any trailing new-line marker from the file it reads.
+%   This function is typically faster than \cs{ior_map_inline:Nn}.
+% \end{function}
+%
+% \begin{function}[added = 2019-01-13]{\ior_str_map_variable:NNn}
+%   \begin{syntax}
+%     \cs{ior_str_map_variable:NNn} \meta{stream} \meta{tl~var} \Arg{code}
+%   \end{syntax}
+%   For each \meta{line} in the \meta{stream}, stores the \meta{line} in
+%   the \meta{tl~var} then applies the \meta{code}.  The material is
+%   read from the \meta{stream} as a series of tokens with category code
+%   $12$ (other), with the exception of space characters which are given
+%   category code $10$ (space).  The \meta{code} will usually make use
+%   of the \meta{variable}, but this is not enforced.  The assignments
+%   to the \meta{variable} are local.  Note that \TeX{} removes trailing
+%   space and tab characters (character codes 32 and 9) from every line
+%   upon input.  \TeX{} also ignores any trailing new-line marker from
+%   the file it reads.
+%   This function is typically faster than \cs{ior_str_map_inline:Nn}.
+% \end{function}
+%
 % \begin{function}[added = 2018-12-29]{\iow_allow_break:}
 %   \begin{syntax}
 %     \cs{iow_allow_break:}
diff --git a/l3kernel/l3file.dtx b/l3kernel/l3file.dtx
index 8ba370e..30855e9 100644
--- a/l3kernel/l3file.dtx
+++ b/l3kernel/l3file.dtx
@@ -1014,6 +1014,37 @@
 % \end{macro}
 % \end{macro}
 %
+% \begin{macro}{\ior_map_variable:NNn, \ior_str_map_variable:NNn}
+% \begin{macro}{\@@_map_variable:NNNn}
+% \begin{macro}{\@@_map_variable_loop:NNNn}
+%   Since the \TeX{} primitive (\tn{read} or \tn{readline}) assigns the
+%   tokens read in the same way as a token list assignment, we simply
+%   call the appropriate primitive.  The end-of-loop is checked using
+%   the primitive conditional for speed.
+%    \begin{macrocode}
+\cs_new_protected:Npn \ior_map_variable:NNn
+  { \@@_map_variable:NNNn \ior_get:NN }
+\cs_new_protected:Npn \ior_str_map_variable:NNn
+  { \@@_map_variable:NNNn \ior_str_get:NN }
+\cs_new_protected:Npn \@@_map_variable:NNNn #1#2#3#4
+  {
+    \ior_if_eof:NF #2 { \@@_map_variable_loop:NNNn #1#2#3 {#4} }
+    \prg_break_point:Nn \ior_map_break: { }
+  }
+\cs_new_protected:Npn \@@_map_variable_loop:NNNn #1#2#3#4
+  {
+    #1 #2 #3
+    \if_eof:w #3
+      \exp_after:wN \ior_map_break:
+    \fi:
+    #4
+    \@@_map_variable_loop:NNNn #1#2#3 {#4}
+  }
+%    \end{macrocode}
+% \end{macro}
+% \end{macro}
+% \end{macro}
+%
 % \subsection{Output operations}
 %
 %    \begin{macrocode}
diff --git a/l3kernel/testfiles/m3ior002.lvt b/l3kernel/testfiles/m3ior002.lvt
index 843e718..bc62838 100644
--- a/l3kernel/testfiles/m3ior002.lvt
+++ b/l3kernel/testfiles/m3ior002.lvt
@@ -1,5 +1,5 @@
 %
-% Copyright (C) 2012,2018 The LaTeX3 Project
+% Copyright (C) 2012,2018,2019 The LaTeX3 Project
 %
 
 \documentclass{minimal}
@@ -76,6 +76,35 @@
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
+\TEST { Mapping~variable~with~newline~at~end-of-file }
+  {
+    \iow_open:Nn \testwrite { secondfile.txt }
+    \iow_now:Nn \testwrite { Foobar ~ baz }
+    \iow_now:Nn \testwrite { }
+    \iow_close:N \testwrite
+    \ior_open:Nn \test  { secondfile.txt }
+    \int_zero:N \l_tmpa_int
+    \ior_map_variable:NNn \test \l_tmpa_tl
+      {
+        \TYPE {|\cs_meaning:N \l_tmpa_tl|}
+        \int_incr:N \l_tmpa_int
+      }
+    \TYPE { \int_use:N \l_tmpa_int \c_space_tl lines }
+    \ior_open:Nn \test { secondfile.txt }
+    \int_zero:N \l_tmpa_int
+    \ior_str_map_variable:NNn \test \l_tmpa_tl
+      {
+        \TYPE {|\cs_meaning:N \l_tmpa_tl|}
+        \int_incr:N \l_tmpa_int
+      }
+    \TYPE { \int_use:N \l_tmpa_int \c_space_tl lines }
+    \OMIT
+    \ior_close:N \test
+    \TIMO
+  }
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
 \TEST { Open~read~stream~with~conditional }
   {
     \ior_new:N \anothertestior
diff --git a/l3kernel/testfiles/m3ior002.tlg b/l3kernel/testfiles/m3ior002.tlg
index 997ea85..c440ad5 100644
--- a/l3kernel/testfiles/m3ior002.tlg
+++ b/l3kernel/testfiles/m3ior002.tlg
@@ -28,7 +28,17 @@ Defining \testwrite on line ...
 2 lines
 ============================================================
 ============================================================
-TEST 3: Open read stream with conditional
+TEST 3: Mapping variable with newline at end-of-file
+============================================================
+|macro:->Foobarbaz|
+|macro:->|
+2 lines
+|macro:->Foobar baz|
+|macro:->|
+2 lines
+============================================================
+============================================================
+TEST 4: Open read stream with conditional
 ============================================================
 Defining \anothertestior on line ...
 FALSE





More information about the latex3-commits mailing list