[latex3-commits] [git/LaTeX3-latex3-latex3] master: Refactor bounds checking in l3intarray (use lower-level code) (19c604b)

Bruno Le Floch bruno at le-floch.fr
Sun Apr 1 20:58:56 CEST 2018


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

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

commit 19c604bfeebe0ea83064bf6ca44b48702861c8cd
Author: Bruno Le Floch <bruno at le-floch.fr>
Date:   Sat Mar 31 17:00:17 2018 -0400

    Refactor bounds checking in l3intarray (use lower-level code)


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

19c604bfeebe0ea83064bf6ca44b48702861c8cd
 l3kernel/l3intarray.dtx                 |   81 +++++++++++++++++++++----------
 l3kernel/testfiles/m3expl001.luatex.tlg |    7 ++-
 l3kernel/testfiles/m3expl001.ptex.tlg   |    7 ++-
 l3kernel/testfiles/m3expl001.tlg        |    7 ++-
 l3kernel/testfiles/m3expl001.uptex.tlg  |    7 ++-
 l3kernel/testfiles/m3expl001.xetex.tlg  |    7 ++-
 l3kernel/testfiles/m3expl003.luatex.tlg |    7 ++-
 l3kernel/testfiles/m3expl003.ptex.tlg   |    7 ++-
 l3kernel/testfiles/m3expl003.tlg        |    7 ++-
 l3kernel/testfiles/m3expl003.uptex.tlg  |    7 ++-
 l3kernel/testfiles/m3expl003.xetex.tlg  |    7 ++-
 11 files changed, 105 insertions(+), 46 deletions(-)

diff --git a/l3kernel/l3intarray.dtx b/l3kernel/l3intarray.dtx
index f0464be..a4bc810 100644
--- a/l3kernel/l3intarray.dtx
+++ b/l3kernel/l3intarray.dtx
@@ -180,8 +180,35 @@
 %
 % \subsection{Array items}
 %
+% \begin{macro}[EXP]{\@@_bounds:NNnTF, \@@_bounds_error:NNn}
+%   The functions \cs{intarray_gset:Nnn} and \cs{intarray_item:Nn} share
+%   bounds checking.  The |T| branch is used if |#3| is within bounds of
+%   the array |#2|.
+%    \begin{macrocode}
+\cs_new:Npn \@@_bounds:NNnTF #1#2#3#4#5
+  {
+    \if_int_compare:w 1 > #3 \exp_stop_f:
+      \@@_bounds_error:NNn #1 #2 {#3}
+      #5
+    \else:
+      \if_int_compare:w #3 > \intarray_count:N #2 \exp_stop_f:
+        \@@_bounds_error:NNn #1 #2 {#3}
+        #5
+      \else:
+        #4
+      \fi:
+    \fi:
+  }
+\cs_new:Npn \@@_bounds_error:NNn #1#2#3
+  {
+    #1 { kernel } { out-of-bounds }
+      { \token_to_str:N #2 } {#3} { \intarray_count:N #2 }
+  }
+%    \end{macrocode}
+% \end{macro}
+%
 % \begin{macro}{\intarray_gset:Nnn, \__kernel_intarray_gset:Nnn}
-% \begin{macro}{\@@_gset:Nnn}
+% \begin{macro}{\@@_gset:Nnn, \@@_gset_overflow:Nnn}
 %   Set the appropriate \tn{fontdimen}.  The
 %   \cs{__kernel_intarray_gset:Nnn} function does not use
 %   \cs{int_eval:n}, namely its arguments must be suitable for
@@ -192,27 +219,29 @@
   { \tex_fontdimen:D #2 #1 #3 \c_@@_sp_dim }
 \cs_new_protected:Npn \intarray_gset:Nnn #1#2#3
   {
-    \exp_args:Nff \@@_gset:Nnn #1
-      { \int_eval:n {#2} } { \int_eval:n {#3} }
+    \exp_after:wN \@@_gset:Nww
+    \exp_after:wN #1
+    \int_value:w \int_eval:n {#2} \exp_after:wN ;
+    \int_value:w \int_eval:n {#3} ;
   }
-\cs_new_protected:Npn \@@_gset:Nnn #1#2#3
+\cs_new_protected:Npn \@@_gset:Nww #1#2 ; #3 ;
   {
-    \int_compare:nTF { 1 <= #2 <= \intarray_count:N #1 }
+    \@@_bounds:NNnTF \__kernel_msg_error:nnxxx #1 {#2}
       {
-        \int_compare:nNnTF { \int_abs:n {#3} } > \c_max_dim
-          {
-            \__kernel_msg_error:nnxxxx { kernel } { overflow }
-              { \token_to_str:N #1 } {#2} {#3}
-              { \int_compare:nNnT {#3} < 0 { - } \int_value:w \c_max_dim }
-            \__kernel_intarray_gset:Nnn #1 {#2}
-              { \int_value:w \int_compare:nNnT {#3} < 0 { - } \c_max_dim }
-          }
-          { \__kernel_intarray_gset:Nnn #1 {#2} {#3} }
-      }
-      {
-        \__kernel_msg_error:nnxxx { kernel } { out-of-bounds }
-          { \token_to_str:N #1 } {#2} { \intarray_count:N #1 }
+        \if_int_compare:w \int_abs:n {#3} > \c_max_dim
+          \exp_after:wN \@@_gset_overflow:NNnn
+        \fi:
+        \__kernel_intarray_gset:Nnn #1 {#2} {#3}
       }
+      { }
+  }
+\cs_new_protected:Npn \@@_gset_overflow:NNnn #1#2#3#4
+  {
+    \__kernel_msg_error:nnxxxx { kernel } { overflow }
+      { \token_to_str:N #2 } {#3} {#4}
+      { \int_value:w \int_compare:nNnT {#4} < 0 { - } \c_max_dim }
+    \__kernel_intarray_gset:Nnn #2 {#3}
+      { \int_value:w \int_compare:nNnT {#4} < 0 { - } \c_max_dim }
   }
 %    \end{macrocode}
 % \end{macro}
@@ -228,16 +257,16 @@
 \cs_new:Npn \__kernel_intarray_item:Nn #1#2
   { \int_value:w \tex_fontdimen:D #2 #1 }
 \cs_new:Npn \intarray_item:Nn #1#2
-  { \exp_args:Nf \@@_item:Nn #1 { \int_eval:n {#2} } }
-\cs_new:Npn \@@_item:Nn #1#2
   {
-    \int_compare:nTF { 1 <= #2 <= \intarray_count:N #1 }
+    \exp_after:wN \@@_item:Nw
+    \exp_after:wN #1
+    \int_value:w \int_eval:n {#2} ;
+  }
+\cs_new:Npn \@@_item:Nw #1#2 ;
+  {
+    \@@_bounds:NNnTF \__kernel_msg_expandable_error:nnnnn #1 {#2}
       { \__kernel_intarray_item:Nn #1 {#2} }
-      {
-        \__kernel_msg_expandable_error:nnnnn { kernel } { out-of-bounds }
-          { \token_to_str:N #1 } {#2} { \intarray_count:N #1 }
-        0
-      }
+      { 0 }
   }
 %    \end{macrocode}
 % \end{macro}
diff --git a/l3kernel/testfiles/m3expl001.luatex.tlg b/l3kernel/testfiles/m3expl001.luatex.tlg
index 177d2a4..facf478 100644
--- a/l3kernel/testfiles/m3expl001.luatex.tlg
+++ b/l3kernel/testfiles/m3expl001.luatex.tlg
@@ -4611,12 +4611,15 @@ Defining \g__intarray_font_int on line ...
 \g__intarray_font_int=\count...
 Defining \intarray_new:Nn on line ...
 Defining \intarray_count:N on line ...
+Defining \__intarray_bounds:NNnTF on line ...
+Defining \__intarray_bounds_error:NNn on line ...
 Defining \__kernel_intarray_gset:Nnn on line ...
 Defining \intarray_gset:Nnn on line ...
-Defining \__intarray_gset:Nnn on line ...
+Defining \__intarray_gset:Nww on line ...
+Defining \__intarray_gset_overflow:NNnn on line ...
 Defining \__kernel_intarray_item:Nn on line ...
 Defining \intarray_item:Nn on line ...
-Defining \__intarray_item:Nn on line ...
+Defining \__intarray_item:Nw on line ...
 Defining \l__sort_length_int on line ...
 \l__sort_length_int=\count...
 Defining \l__sort_min_int on line ...
diff --git a/l3kernel/testfiles/m3expl001.ptex.tlg b/l3kernel/testfiles/m3expl001.ptex.tlg
index 8e757a3..bc219db 100644
--- a/l3kernel/testfiles/m3expl001.ptex.tlg
+++ b/l3kernel/testfiles/m3expl001.ptex.tlg
@@ -4548,12 +4548,15 @@ Defining \g__intarray_font_int on line ...
 \g__intarray_font_int=\count...
 Defining \intarray_new:Nn on line ...
 Defining \intarray_count:N on line ...
+Defining \__intarray_bounds:NNnTF on line ...
+Defining \__intarray_bounds_error:NNn on line ...
 Defining \__kernel_intarray_gset:Nnn on line ...
 Defining \intarray_gset:Nnn on line ...
-Defining \__intarray_gset:Nnn on line ...
+Defining \__intarray_gset:Nww on line ...
+Defining \__intarray_gset_overflow:NNnn on line ...
 Defining \__kernel_intarray_item:Nn on line ...
 Defining \intarray_item:Nn on line ...
-Defining \__intarray_item:Nn on line ...
+Defining \__intarray_item:Nw on line ...
 Defining \l__sort_length_int on line ...
 \l__sort_length_int=\count...
 Defining \l__sort_min_int on line ...
diff --git a/l3kernel/testfiles/m3expl001.tlg b/l3kernel/testfiles/m3expl001.tlg
index 3205a1e..08d1c05 100644
--- a/l3kernel/testfiles/m3expl001.tlg
+++ b/l3kernel/testfiles/m3expl001.tlg
@@ -4548,12 +4548,15 @@ Defining \g__intarray_font_int on line ...
 \g__intarray_font_int=\count...
 Defining \intarray_new:Nn on line ...
 Defining \intarray_count:N on line ...
+Defining \__intarray_bounds:NNnTF on line ...
+Defining \__intarray_bounds_error:NNn on line ...
 Defining \__kernel_intarray_gset:Nnn on line ...
 Defining \intarray_gset:Nnn on line ...
-Defining \__intarray_gset:Nnn on line ...
+Defining \__intarray_gset:Nww on line ...
+Defining \__intarray_gset_overflow:NNnn on line ...
 Defining \__kernel_intarray_item:Nn on line ...
 Defining \intarray_item:Nn on line ...
-Defining \__intarray_item:Nn on line ...
+Defining \__intarray_item:Nw on line ...
 Defining \l__sort_length_int on line ...
 \l__sort_length_int=\count...
 Defining \l__sort_min_int on line ...
diff --git a/l3kernel/testfiles/m3expl001.uptex.tlg b/l3kernel/testfiles/m3expl001.uptex.tlg
index 0ad53ce..0d8d5e7 100644
--- a/l3kernel/testfiles/m3expl001.uptex.tlg
+++ b/l3kernel/testfiles/m3expl001.uptex.tlg
@@ -4548,12 +4548,15 @@ Defining \g__intarray_font_int on line ...
 \g__intarray_font_int=\count...
 Defining \intarray_new:Nn on line ...
 Defining \intarray_count:N on line ...
+Defining \__intarray_bounds:NNnTF on line ...
+Defining \__intarray_bounds_error:NNn on line ...
 Defining \__kernel_intarray_gset:Nnn on line ...
 Defining \intarray_gset:Nnn on line ...
-Defining \__intarray_gset:Nnn on line ...
+Defining \__intarray_gset:Nww on line ...
+Defining \__intarray_gset_overflow:NNnn on line ...
 Defining \__kernel_intarray_item:Nn on line ...
 Defining \intarray_item:Nn on line ...
-Defining \__intarray_item:Nn on line ...
+Defining \__intarray_item:Nw on line ...
 Defining \l__sort_length_int on line ...
 \l__sort_length_int=\count...
 Defining \l__sort_min_int on line ...
diff --git a/l3kernel/testfiles/m3expl001.xetex.tlg b/l3kernel/testfiles/m3expl001.xetex.tlg
index 32e17f3..830231a 100644
--- a/l3kernel/testfiles/m3expl001.xetex.tlg
+++ b/l3kernel/testfiles/m3expl001.xetex.tlg
@@ -4588,12 +4588,15 @@ Defining \g__intarray_font_int on line ...
 \g__intarray_font_int=\count...
 Defining \intarray_new:Nn on line ...
 Defining \intarray_count:N on line ...
+Defining \__intarray_bounds:NNnTF on line ...
+Defining \__intarray_bounds_error:NNn on line ...
 Defining \__kernel_intarray_gset:Nnn on line ...
 Defining \intarray_gset:Nnn on line ...
-Defining \__intarray_gset:Nnn on line ...
+Defining \__intarray_gset:Nww on line ...
+Defining \__intarray_gset_overflow:NNnn on line ...
 Defining \__kernel_intarray_item:Nn on line ...
 Defining \intarray_item:Nn on line ...
-Defining \__intarray_item:Nn on line ...
+Defining \__intarray_item:Nw on line ...
 Defining \l__sort_length_int on line ...
 \l__sort_length_int=\count...
 Defining \l__sort_min_int on line ...
diff --git a/l3kernel/testfiles/m3expl003.luatex.tlg b/l3kernel/testfiles/m3expl003.luatex.tlg
index 177d2a4..facf478 100644
--- a/l3kernel/testfiles/m3expl003.luatex.tlg
+++ b/l3kernel/testfiles/m3expl003.luatex.tlg
@@ -4611,12 +4611,15 @@ Defining \g__intarray_font_int on line ...
 \g__intarray_font_int=\count...
 Defining \intarray_new:Nn on line ...
 Defining \intarray_count:N on line ...
+Defining \__intarray_bounds:NNnTF on line ...
+Defining \__intarray_bounds_error:NNn on line ...
 Defining \__kernel_intarray_gset:Nnn on line ...
 Defining \intarray_gset:Nnn on line ...
-Defining \__intarray_gset:Nnn on line ...
+Defining \__intarray_gset:Nww on line ...
+Defining \__intarray_gset_overflow:NNnn on line ...
 Defining \__kernel_intarray_item:Nn on line ...
 Defining \intarray_item:Nn on line ...
-Defining \__intarray_item:Nn on line ...
+Defining \__intarray_item:Nw on line ...
 Defining \l__sort_length_int on line ...
 \l__sort_length_int=\count...
 Defining \l__sort_min_int on line ...
diff --git a/l3kernel/testfiles/m3expl003.ptex.tlg b/l3kernel/testfiles/m3expl003.ptex.tlg
index 8e757a3..bc219db 100644
--- a/l3kernel/testfiles/m3expl003.ptex.tlg
+++ b/l3kernel/testfiles/m3expl003.ptex.tlg
@@ -4548,12 +4548,15 @@ Defining \g__intarray_font_int on line ...
 \g__intarray_font_int=\count...
 Defining \intarray_new:Nn on line ...
 Defining \intarray_count:N on line ...
+Defining \__intarray_bounds:NNnTF on line ...
+Defining \__intarray_bounds_error:NNn on line ...
 Defining \__kernel_intarray_gset:Nnn on line ...
 Defining \intarray_gset:Nnn on line ...
-Defining \__intarray_gset:Nnn on line ...
+Defining \__intarray_gset:Nww on line ...
+Defining \__intarray_gset_overflow:NNnn on line ...
 Defining \__kernel_intarray_item:Nn on line ...
 Defining \intarray_item:Nn on line ...
-Defining \__intarray_item:Nn on line ...
+Defining \__intarray_item:Nw on line ...
 Defining \l__sort_length_int on line ...
 \l__sort_length_int=\count...
 Defining \l__sort_min_int on line ...
diff --git a/l3kernel/testfiles/m3expl003.tlg b/l3kernel/testfiles/m3expl003.tlg
index 3205a1e..08d1c05 100644
--- a/l3kernel/testfiles/m3expl003.tlg
+++ b/l3kernel/testfiles/m3expl003.tlg
@@ -4548,12 +4548,15 @@ Defining \g__intarray_font_int on line ...
 \g__intarray_font_int=\count...
 Defining \intarray_new:Nn on line ...
 Defining \intarray_count:N on line ...
+Defining \__intarray_bounds:NNnTF on line ...
+Defining \__intarray_bounds_error:NNn on line ...
 Defining \__kernel_intarray_gset:Nnn on line ...
 Defining \intarray_gset:Nnn on line ...
-Defining \__intarray_gset:Nnn on line ...
+Defining \__intarray_gset:Nww on line ...
+Defining \__intarray_gset_overflow:NNnn on line ...
 Defining \__kernel_intarray_item:Nn on line ...
 Defining \intarray_item:Nn on line ...
-Defining \__intarray_item:Nn on line ...
+Defining \__intarray_item:Nw on line ...
 Defining \l__sort_length_int on line ...
 \l__sort_length_int=\count...
 Defining \l__sort_min_int on line ...
diff --git a/l3kernel/testfiles/m3expl003.uptex.tlg b/l3kernel/testfiles/m3expl003.uptex.tlg
index 0ad53ce..0d8d5e7 100644
--- a/l3kernel/testfiles/m3expl003.uptex.tlg
+++ b/l3kernel/testfiles/m3expl003.uptex.tlg
@@ -4548,12 +4548,15 @@ Defining \g__intarray_font_int on line ...
 \g__intarray_font_int=\count...
 Defining \intarray_new:Nn on line ...
 Defining \intarray_count:N on line ...
+Defining \__intarray_bounds:NNnTF on line ...
+Defining \__intarray_bounds_error:NNn on line ...
 Defining \__kernel_intarray_gset:Nnn on line ...
 Defining \intarray_gset:Nnn on line ...
-Defining \__intarray_gset:Nnn on line ...
+Defining \__intarray_gset:Nww on line ...
+Defining \__intarray_gset_overflow:NNnn on line ...
 Defining \__kernel_intarray_item:Nn on line ...
 Defining \intarray_item:Nn on line ...
-Defining \__intarray_item:Nn on line ...
+Defining \__intarray_item:Nw on line ...
 Defining \l__sort_length_int on line ...
 \l__sort_length_int=\count...
 Defining \l__sort_min_int on line ...
diff --git a/l3kernel/testfiles/m3expl003.xetex.tlg b/l3kernel/testfiles/m3expl003.xetex.tlg
index 32e17f3..830231a 100644
--- a/l3kernel/testfiles/m3expl003.xetex.tlg
+++ b/l3kernel/testfiles/m3expl003.xetex.tlg
@@ -4588,12 +4588,15 @@ Defining \g__intarray_font_int on line ...
 \g__intarray_font_int=\count...
 Defining \intarray_new:Nn on line ...
 Defining \intarray_count:N on line ...
+Defining \__intarray_bounds:NNnTF on line ...
+Defining \__intarray_bounds_error:NNn on line ...
 Defining \__kernel_intarray_gset:Nnn on line ...
 Defining \intarray_gset:Nnn on line ...
-Defining \__intarray_gset:Nnn on line ...
+Defining \__intarray_gset:Nww on line ...
+Defining \__intarray_gset_overflow:NNnn on line ...
 Defining \__kernel_intarray_item:Nn on line ...
 Defining \intarray_item:Nn on line ...
-Defining \__intarray_item:Nn on line ...
+Defining \__intarray_item:Nw on line ...
 Defining \l__sort_length_int on line ...
 \l__sort_length_int=\count...
 Defining \l__sort_min_int on line ...





More information about the latex3-commits mailing list