[latex3-commits] [git/LaTeX3-latex3-latex3] master: Add \bool_case:n(TF) (e978a2a9a)

Joseph Wright joseph.wright at morningstar2.co.uk
Mon Jan 6 17:20:12 CET 2020


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

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

commit e978a2a9a337b05a8134b68fc23a0ad288cc6d51
Author: Bruno Le Floch <blflatex at gmail.com>
Date:   Mon Jan 6 17:20:12 2020 +0100

    Add \bool_case:n(TF)


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

e978a2a9a337b05a8134b68fc23a0ad288cc6d51
 l3kernel/CHANGELOG.md           |  1 +
 l3kernel/l3candidates.dtx       | 87 +++++++++++++++++++++++++++++++++++++++++
 l3kernel/l3int.dtx              |  4 +-
 l3kernel/testfiles/m3prg005.lvt | 42 +++++++++++++++++++-
 l3kernel/testfiles/m3prg005.tlg |  8 ++++
 5 files changed, 139 insertions(+), 3 deletions(-)

diff --git a/l3kernel/CHANGELOG.md b/l3kernel/CHANGELOG.md
index 52f953821..9136db502 100644
--- a/l3kernel/CHANGELOG.md
+++ b/l3kernel/CHANGELOG.md
@@ -8,6 +8,7 @@ this project uses date-based 'snapshot' version identifiers.
 ## [Unreleased]
 
 ### Added
+- `bool_case_true:n(TF)` and `\bool_case_false:n(TF)`
 - `\file_hex_dump:n(nn)` and `\file_get_hex_dump:n(nn)N(TF)`
 
 ### Changed
diff --git a/l3kernel/l3candidates.dtx b/l3kernel/l3candidates.dtx
index 8a1296df8..c5abffa93 100644
--- a/l3kernel/l3candidates.dtx
+++ b/l3kernel/l3candidates.dtx
@@ -369,6 +369,47 @@
 %   conversely: sets it to the inverse of its current value.
 % \end{function}
 %
+% \begin{function}[added = 2019-02-10, EXP, noTF]
+%   {\bool_case_true:n, \bool_case_false:n}
+%   \begin{syntax}
+%     \cs{bool_case_true:nTF} \\
+%     ~~|{| \\
+%     ~~~~\Arg{boolexpr case_1} \Arg{code case_1} \\
+%     ~~~~\Arg{boolexpr case_2} \Arg{code case_2} \\
+%     ~~~~\ldots \\
+%     ~~~~\Arg{boolexpr case_n} \Arg{code case_n} \\
+%     ~~|}| \\
+%     ~~\Arg{true code}
+%     ~~\Arg{false code}
+%   \end{syntax}
+%   Evaluates in turn each of the \meta{boolean expression cases} until
+%   the first one that evaluates to \texttt{true} or to \texttt{false},
+%   for \cs{bool_case_true:n} and \cs{bool_case_false:n}, respectively.
+%   The \meta{code} associated to this first case is left in the input
+%   stream, followed by the \meta{true code}, and other cases are
+%   discarded.  If none of the cases match then only the \meta{false
+%   code} is inserted. The functions \cs{bool_case_true:n} and
+%   \cs{bool_case_false:n}, which do nothing if there is no match, are
+%   also available. For example
+%   \begin{verbatim}
+%     \bool_case_true:nF
+%       {
+%         { \dim_compare_p:n { \l__mypkg_wd_dim <= 10pt } }
+%             { Fits }
+%         { \int_compare_p:n { \l__mypkg_total_int >= 10 } }
+%             { Many }
+%         { \l__mypkg_special_bool }
+%             { Special }
+%       }
+%       { No idea! }
+%   \end{verbatim}
+%   leaves \enquote{\texttt{Fits}} or \enquote{\texttt{Many}} or
+%   \enquote{\texttt{Special}} or \enquote{\texttt{No idea!}} in the
+%   input stream, in a way similar to some other language's
+%   \enquote{\texttt{if} \ldots\ \texttt{elseif} \ldots\ \texttt{elseif} \ldots\
+%   \texttt{else} \ldots}.
+% \end{function}
+%
 % \section{Additions to \pkg{l3prop}}
 %
 % \begin{function}[EXP, added = 2016-12-06]
@@ -1238,6 +1279,52 @@
 %    \end{macrocode}
 % \end{macro}
 %
+% \begin{macro}[EXP, noTF]{\bool_case_true:n, \bool_case_false:n}
+% \begin{macro}{\@@_case:NnTF}
+% \begin{macro}{\@@_case_true:w, \@@_case_false:w, \@@_case_end:nw}
+%   For boolean cases the overall idea is the same as for
+%   \cs{tl_case:nn(TF)} as described in \pkg{l3tl}.
+%    \begin{macrocode}
+\cs_new:Npn \bool_case_true:nTF
+  { \exp:w \@@_case:NnTF \c_true_bool }
+\cs_new:Npn \bool_case_true:nT #1#2
+  { \exp:w \@@_case:NnTF \c_true_bool {#1} {#2} { } }
+\cs_new:Npn \bool_case_true:nF #1
+  { \exp:w \@@_case:NnTF \c_true_bool {#1} { } }
+\cs_new:Npn \bool_case_true:n #1
+  { \exp:w \@@_case:NnTF \c_true_bool {#1} { } { } }
+\cs_new:Npn \bool_case_false:nTF
+  { \exp:w \@@_case:NnTF \c_false_bool }
+\cs_new:Npn \bool_case_false:nT #1#2
+  { \exp:w \@@_case:NnTF \c_false_bool {#1} {#2} { } }
+\cs_new:Npn \bool_case_false:nF #1
+  { \exp:w \@@_case:NnTF \c_false_bool {#1} { } }
+\cs_new:Npn \bool_case_false:n #1
+  { \exp:w \@@_case:NnTF \c_false_bool {#1} { } { } }
+\cs_new:Npn \@@_case:NnTF #1#2#3#4
+  {
+    \bool_if:NTF #1 \@@_case_true:w \@@_case_false:w
+    #2 #1 { } \q_mark {#3} \q_mark {#4} \q_stop
+  }
+\cs_new:Npn \@@_case_true:w #1#2
+  {
+    \bool_if:nTF {#1}
+      { \@@_case_end:nw {#2} }
+      { \@@_case_true:w }
+  }
+\cs_new:Npn \@@_case_false:w #1#2
+  {
+    \bool_if:nTF {#1}
+      { \@@_case_false:w }
+      { \@@_case_end:nw {#2} }
+  }
+\cs_new:Npn \@@_case_end:nw #1#2#3 \q_mark #4#5 \q_stop
+  { \exp_end: #1 #4 }
+%    \end{macrocode}
+% \end{macro}
+% \end{macro}
+% \end{macro}
+%
 % \subsection{Additions to \pkg{l3prop}}
 %
 %    \begin{macrocode}
diff --git a/l3kernel/l3int.dtx b/l3kernel/l3int.dtx
index 8241e85fa..58f0be854 100644
--- a/l3kernel/l3int.dtx
+++ b/l3kernel/l3int.dtx
@@ -420,8 +420,8 @@
 %         { -2 * 10 } { Negative }
 %       }
 %       { No idea! }
-%    \end{verbatim}
-%    leaves \enquote{\texttt{Medium}} in the input stream.
+%   \end{verbatim}
+%   leaves \enquote{\texttt{Medium}} in the input stream.
 % \end{function}
 %
 % \begin{function}[EXP,pTF]{\int_if_even:n, \int_if_odd:n}
diff --git a/l3kernel/testfiles/m3prg005.lvt b/l3kernel/testfiles/m3prg005.lvt
index 21e744f98..f4838623f 100644
--- a/l3kernel/testfiles/m3prg005.lvt
+++ b/l3kernel/testfiles/m3prg005.lvt
@@ -1,5 +1,5 @@
 %
-% Copyright (C) 2015 The LaTeX3 project
+% Copyright (C) 2015,2018 The LaTeX3 project
 %
 
 \documentclass{minimal}
@@ -71,4 +71,44 @@
     \bool_lazy_or_p:nn { \c_false_bool }{ \c_false_bool }
   }
 
+\TESTEXP { bool_case }
+  {
+    \bool_case_true:nTF
+      {
+        { \int_compare_p:n { 1 > 2 } } { \ERROR }
+        { \dim_compare_p:n { 10pt < 20pt } } { \TRUE }
+        \ERROR \ERROR
+      }
+      { \TRUE }
+      { \ERROR }
+    \NEWLINE
+    \bool_case_true:nTF
+      {
+        { \int_compare_p:n { 1 > 2 } } { \ERROR }
+        { \dim_compare_p:n { 10pt < 20pt } && \c_false_bool } { \ERROR }
+        { \c_false_bool } { \ERROR }
+      }
+      { \ERROR }
+      { \FALSE }
+    \NEWLINE
+    \bool_case_false:nTF
+      {
+        { \int_compare_p:n { 1 < 2 } } { \ERROR }
+        { \dim_compare_p:n { 10pt > 20pt } } { \TRUE }
+        \ERROR \ERROR
+      }
+      { \TRUE }
+      { \ERROR }
+    \NEWLINE
+    \bool_case_false:nTF
+      {
+        { \int_compare_p:n { 1 < 2 } } { \ERROR }
+        { \dim_compare_p:n { 10pt > 20pt } || \c_true_bool } { \ERROR }
+        { \c_true_bool } { \ERROR }
+      }
+      { \ERROR }
+      { \FALSE }
+    \NEWLINE
+  }
+
 \END
diff --git a/l3kernel/testfiles/m3prg005.tlg b/l3kernel/testfiles/m3prg005.tlg
index 5cfd72a67..7791e05b5 100644
--- a/l3kernel/testfiles/m3prg005.tlg
+++ b/l3kernel/testfiles/m3prg005.tlg
@@ -49,3 +49,11 @@ TRUE
 \c_true_bool 
 \c_false_bool 
 ============================================================
+============================================================
+TEST 5: bool_case
+============================================================
+TRUETRUE
+FALSE
+TRUETRUE
+FALSE
+============================================================





More information about the latex3-commits mailing list