[latex3-commits] [git/LaTeX3-latex3-latex3] main: Simplify code of \prop_if_in:NnTF and \prop_item:Nn (7c126b9dd)

Bruno Le Floch blflatex at gmail.com
Sat May 15 01:32:20 CEST 2021


Repository : https://github.com/latex3/latex3
On branch  : main
Link       : https://github.com/latex3/latex3/commit/7c126b9dd124170360baf22cedbcb9b0b3f628e2

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

commit 7c126b9dd124170360baf22cedbcb9b0b3f628e2
Author: Bruno Le Floch <blflatex at gmail.com>
Date:   Sat May 15 01:32:20 2021 +0200

    Simplify code of \prop_if_in:NnTF and \prop_item:Nn
    
    When these were written \prop_map_tokens:Nn did not exist.  Now that
    \prop_map_tokens:Nn is reasonably fast, it is reasonable to have simpler
    code there.


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

7c126b9dd124170360baf22cedbcb9b0b3f628e2
 l3kernel/l3prop.dtx | 70 +++++++++++++++--------------------------------------
 1 file changed, 19 insertions(+), 51 deletions(-)

diff --git a/l3kernel/l3prop.dtx b/l3kernel/l3prop.dtx
index 5d71f9399..5bf3e271d 100644
--- a/l3kernel/l3prop.dtx
+++ b/l3kernel/l3prop.dtx
@@ -956,29 +956,22 @@
 % \end{macro}
 %
 % \begin{macro}[EXP]{\prop_item:Nn, \prop_item:cn}
-% \begin{macro}[EXP]{\@@_item_Nn:nwwn}
+% \begin{macro}[EXP]{\@@_item:nnn}
 %   Getting the value corresponding to a key in a property list in an
-%   expandable fashion is similar to mapping some tokens.  Go through
-%   the property list one \meta{key}--\meta{value} pair at a time: the
-%   arguments of \cs{@@_item_Nn:nwn} are the \meta{key} we are looking
-%   for, a \meta{key} of the property list, and its associated value.
-%   The \meta{keys} are compared (as strings).  If they match, the
-%   \meta{value} is returned, within \cs{exp_not:n}.  The loop
-%   terminates even if the \meta{key} is missing, and yields an empty
-%   value, because we have appended the appropriate
-%   \meta{key}--\meta{empty value} pair to the property list.
+%   expandable fashion simply uses \cs{prop_map_tokens:Nn} to go through
+%   the property list.  The auxiliary \cs{@@_item:nnn} receives the
+%   search string~|#1|, the key~|#2| and the value~|#3| and returns as
+%   appropriate.
 %    \begin{macrocode}
 \cs_new:Npn \prop_item:Nn #1#2
   {
-    \exp_last_unbraced:Noo \@@_item_Nn:nwwn { \tl_to_str:n {#2} } #1
-      \@@_pair:wn \tl_to_str:n {#2} \s_@@ { }
-    \prg_break_point:
+    \exp_args:NNo \prop_map_tokens:Nn #1
+      { \exp_after:wN \@@_item:nnn \exp_after:wN { \tl_to_str:n {#2} } }
   }
-\cs_new:Npn \@@_item_Nn:nwwn #1#2 \@@_pair:wn #3 \s_@@ #4
+\cs_new:Npn \@@_item:nnn #1#2#3
   {
-    \str_if_eq:eeTF {#1} {#3}
-      { \prg_break:n { \exp_not:n {#4} } }
-      { \@@_item_Nn:nwwn {#1} }
+    \str_if_eq:eeT {#1} {#2}
+      { \prop_map_break:n { \exp_not:n {#3} } }
   }
 \cs_generate_variant:Nn \prop_item:Nn { c }
 %    \end{macrocode}
@@ -1159,7 +1152,7 @@
 %     \prop_if_in:Nn, \prop_if_in:NV, \prop_if_in:No,
 %     \prop_if_in:cn, \prop_if_in:cV, \prop_if_in:co
 %   }
-% \begin{macro}[EXP]{\@@_if_in:nwwn, \@@_if_in:N}
+% \begin{macro}[EXP]{\@@_if_in:nnn}
 %   Testing expandably if a key is in a property list
 %   requires to go through the key--value pairs one by one.
 %   This is rather slow, and a faster test would be
@@ -1172,44 +1165,19 @@
 %       }
 %   \end{verbatim}
 %   but \cs{@@_split:NnTF} is non-expandable.
-%
-%   Instead, the key is compared to each key in turn using
-%   \cs{str_if_eq:ee}, which is expandable.  To terminate the mapping,
-%   we append to the property list the key that is searched for.  This
-%   second \cs{tl_to_str:n} is not expanded at the start, but only when
-%   included in the \cs{str_if_eq:ee}.  It cannot make the breaking
-%   mechanism choke, because the arbitrary token list material is
-%   enclosed in braces.  The second argument of \cs{@@_if_in:nwwn} is
-%   most often empty.  When the \meta{key} is found in the list,
-%   \cs{@@_if_in:N} receives \cs{@@_pair:wn}, and if it is
-%   found as the extra item, the function receives
-%   \cs{q_@@_recursion_tail}, easily recognizable.
-%
-%   Here, \cs{prop_map_function:NN} is not sufficient for the mapping,
-%   since it can only map a single token, and cannot carry the key that
-%   is searched for.
+%   Instead, we use \cs{prop_map_tokens:Nn} to compare the search key to
+%   each key in turn using \cs{str_if_eq:ee}, which is expandable.
 %    \begin{macrocode}
 \prg_new_conditional:Npnn \prop_if_in:Nn #1#2 { p , T , F , TF }
   {
-    \exp_last_unbraced:Noo \@@_if_in:nwwn { \tl_to_str:n {#2} } #1
-      \@@_pair:wn \tl_to_str:n {#2} \s_@@ { }
-      \q_@@_recursion_tail
-    \prg_break_point:
-  }
-\cs_new:Npn \@@_if_in:nwwn #1#2 \@@_pair:wn #3 \s_@@ #4
-  {
-    \str_if_eq:eeTF {#1} {#3}
-      { \@@_if_in:N }
-      { \@@_if_in:nwwn {#1} }
+    \exp_args:NNo \prop_map_tokens:Nn #1
+      { \exp_after:wN \@@_if_in:nnn \exp_after:wN { \tl_to_str:n {#2} } }
+    \prg_return_false:
   }
-\cs_new:Npn \@@_if_in:N #1
+\cs_new:Npn \@@_if_in:nnn #1#2#3
   {
-    \if_meaning:w \q_@@_recursion_tail #1
-      \prg_return_false:
-    \else:
-      \prg_return_true:
-    \fi:
-    \prg_break:
+    \str_if_eq:eeT {#1} {#2}
+      { \prop_map_break:n { \use_i:nn \prg_return_true: } }
   }
 \prg_generate_conditional_variant:Nnn \prop_if_in:Nn
   { NV , No , c , cV , co } { p , T , F , TF }





More information about the latex3-commits mailing list.