[latex3-commits] [git/LaTeX3-latex3-latex3] main: Implement \cctab_item:Nn to access catcode of a specific char (fixes #880) (5b24837f7)

Bruno Le Floch blflatex at gmail.com
Tue May 11 00:05:42 CEST 2021


Repository : https://github.com/latex3/latex3
On branch  : main
Link       : https://github.com/latex3/latex3/commit/5b24837f7d55ff3d6b8f0efea2e8c6b94e810a16

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

commit 5b24837f7d55ff3d6b8f0efea2e8c6b94e810a16
Author: Bruno Le Floch <blflatex at gmail.com>
Date:   Tue May 11 00:05:42 2021 +0200

    Implement \cctab_item:Nn to access catcode of a specific char (fixes #880)


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

5b24837f7d55ff3d6b8f0efea2e8c6b94e810a16
 l3kernel/CHANGELOG.md                    |  3 +++
 l3kernel/l3cctab.dtx                     | 37 ++++++++++++++++++++++++++++++++
 l3kernel/testfiles/m3cctab002.luatex.tlg | 24 ++++++++++++++++++++-
 l3kernel/testfiles/m3cctab002.lvt        | 24 +++++++++++++++++++--
 l3kernel/testfiles/m3cctab002.tlg        | 22 ++++++++++++++++++-
 5 files changed, 106 insertions(+), 4 deletions(-)

diff --git a/l3kernel/CHANGELOG.md b/l3kernel/CHANGELOG.md
index 43d7a8dc1..c6f03a29c 100644
--- a/l3kernel/CHANGELOG.md
+++ b/l3kernel/CHANGELOG.md
@@ -7,6 +7,9 @@ this project uses date-based 'snapshot' version identifiers.
 
 ## [Unreleased]
 
+### Added
+- `\cctab_item:Nn` (issue #880)
+
 ### Fixed
 - Loading of backend in generic DVI mode (issue #905)
 - Make `\keyval_parse:nnn` alignment-safe (issue #896)
diff --git a/l3kernel/l3cctab.dtx b/l3kernel/l3cctab.dtx
index 32b595bbd..5d9b3056f 100644
--- a/l3kernel/l3cctab.dtx
+++ b/l3kernel/l3cctab.dtx
@@ -120,6 +120,15 @@
 %   and \cs{cctab_gset:Nn}.
 % \end{function}
 %
+% \begin{function}[EXP, added = 2021-05-10]{\cctab_item:Nn, \cctab_item:cn}
+%   \begin{syntax}
+%     \cs{cctab_item:Nn} \meta{category code table} \Arg{integer expression}
+%   \end{syntax}
+%   Determines the \meta{character} with character code given by the
+%   \meta{integer expression} and expands to its category code specified
+%   by the \meta{category code table}.
+% \end{function}
+%
 % \section{Category code table conditionals}
 %
 % \begin{function}[pTF]{\cctab_if_exist:N, \cctab_if_exist:c}
@@ -660,6 +669,34 @@
   }
 %    \end{macrocode}
 %
+%
+% \begin{macro}{\cctab_item:Nn, \cctab_item:cn}
+%   Evaluate the integer argument only once.  In most engines the
+%   |cctab| variable only has $256$ entries so we only look up the
+%   catcode for these entries, otherwise we use the current catcode.  In
+%   particular, for out-of-range values we use whatever fall-back
+%   \cs{char_value_catcode:n}.  In \LuaTeX{}, we use the
+%   |tex.getcatcode| function.
+%    \begin{macrocode}
+\cs_new:Npn \cctab_item:Nn #1#2
+  { \exp_args:Nf \@@_item:nN { \int_eval:n {#2} } #1 }
+\sys_if_engine_luatex:TF
+  {
+    \cs_new:Npn \@@_item:nN #1#2
+      { \lua_now:e { tex.print(-2, tex.getcatcode(\int_use:N #2, #1)) } }
+  }
+  {
+    \cs_new:Npn \@@_item:nN #1#2
+      {
+        \int_compare:nNnTF {#1} < { 256 }
+          { \intarray_item:Nn #2 { #1 + 1 } }
+          { \char_value_catcode:n {#1} }
+      }
+  }
+\cs_generate_variant:Nn \cctab_item:Nn { c }
+%    \end{macrocode}
+% \end{macro}
+%
 % \subsection{Category code table conditionals}
 %
 % \begin{macro}{\cctab_if_exist:N,\cctab_if_exist:c}
diff --git a/l3kernel/testfiles/m3cctab002.luatex.tlg b/l3kernel/testfiles/m3cctab002.luatex.tlg
index 45f4d0e56..ddb5b724a 100644
--- a/l3kernel/testfiles/m3cctab002.luatex.tlg
+++ b/l3kernel/testfiles/m3cctab002.luatex.tlg
@@ -1,6 +1,6 @@
 This is a generated file for the LaTeX (2e + expl3) validation system.
 Don't change this file in any respect.
-Author: Phelype Oleinik
+Author: Phelype Oleinik, Bruno Le Floch
 ============================================================
 TEST 1: Undefined cctab for \cctab_end:N 
 ============================================================
@@ -157,3 +157,25 @@ them. LaTeX will try to proceed, but results may be unexpected.
 TRUE
 TRUE
 ============================================================
+============================================================
+TEST 8: catcode of a specific character
+============================================================
+8,12,0,10,12
+[\directlua]:1: incorrect character value -1 for tex.getcatcode()
+stack traceback:
+^^I[C]: in function 'tex.getcatcode'
+^^I[\directlua]:1: in main chunk.
+\lua_now:e #1->\__lua_now:n {#1}
+l. ...  }
+The lua interpreter ran into a problem, so the
+remainder of this lua chunk will be ignored.
+[\directlua]:1: incorrect character value 1114112 for tex.getcatcode()
+stack traceback:
+^^I[C]: in function 'tex.getcatcode'
+^^I[\directlua]:1: in main chunk.
+\lua_now:e #1->\__lua_now:n {#1}
+l. ...  }
+The lua interpreter ran into a problem, so the
+remainder of this lua chunk will be ignored.
+9,,
+============================================================
diff --git a/l3kernel/testfiles/m3cctab002.lvt b/l3kernel/testfiles/m3cctab002.lvt
index 7631cf1c3..aa4093eab 100644
--- a/l3kernel/testfiles/m3cctab002.lvt
+++ b/l3kernel/testfiles/m3cctab002.lvt
@@ -1,5 +1,5 @@
 %
-% Copyright (C) 2019,2020 The LaTeX Project
+% Copyright (C) 2019-2021 The LaTeX Project
 %
 
 \documentclass{minimal}
@@ -11,7 +11,7 @@
 \ExplSyntaxOff
 
 \START
-\AUTHOR{Phelype Oleinik}
+\AUTHOR{Phelype Oleinik, Bruno Le Floch}
 
 \ExplSyntaxOn
 
@@ -101,6 +101,26 @@
     \int_compare:nNnTF { \char_value_catcode:n { `_ } } = { 11 } { \TRUE } { \ERROR }
   }
 
+
+\TEST { catcode~of~a~specific~character }
+  {
+    \TYPE
+      {
+        \cctab_item:Nn \c_document_cctab { `\_ } ,
+        \cctab_item:cn { c_document_cctab } { `\: } ,
+        \cctab_item:Nn \c_document_cctab { `\\ } ,
+        \cctab_item:cn { c_str_cctab } { 32 } ,
+        \cctab_item:Nn \c_other_cctab { 32 }
+      }
+    \TYPE
+      {
+        \cctab_item:Nn \c_initex_cctab { 0 } ,
+        \cctab_item:Nn \c_initex_cctab { -1 } ,
+        \cctab_item:Nn \c_initex_cctab { 1114112 }
+      }
+  }
+
+
 \ExplSyntaxOff
 
 \END
diff --git a/l3kernel/testfiles/m3cctab002.tlg b/l3kernel/testfiles/m3cctab002.tlg
index 4af04cf76..30fa17143 100644
--- a/l3kernel/testfiles/m3cctab002.tlg
+++ b/l3kernel/testfiles/m3cctab002.tlg
@@ -1,6 +1,6 @@
 This is a generated file for the LaTeX (2e + expl3) validation system.
 Don't change this file in any respect.
-Author: Phelype Oleinik
+Author: Phelype Oleinik, Bruno Le Floch
 ============================================================
 TEST 1: Undefined cctab for \cctab_end:N 
 ============================================================
@@ -168,3 +168,23 @@ them. LaTeX will try to proceed, but results may be unexpected.
 TRUE
 TRUE
 ============================================================
+============================================================
+TEST 8: catcode of a specific character
+============================================================
+8,12,0,10,12
+! Undefined control sequence.
+<argument> \LaTeX3 error: 
+                           Access to an entry beyond an array's bounds.
+l. ...  }
+The control sequence at the end of the top line
+of your error message was never \def'ed. If you have
+misspelled it (e.g., `\hobx'), type `I' and the correct
+spelling (e.g., `I\hbox'). Otherwise just continue,
+and I'll forget about whatever was undefined.
+! Bad character code (1114112).
+\char_value_catcode:n ..._eval:n {#1}\exp_stop_f: 
+l. ...  }
+A character number must be between 0 and 255.
+I changed this one to zero.
+9,0,15
+============================================================





More information about the latex3-commits mailing list.