[latex3-commits] [git/LaTeX3-latex3-latex3] luacmd: Emulate \tex_strcmp:D in LuaTeX (509d70e87)

Marcel Fabian Krüger tex at 2krueger.de
Fri Aug 21 15:43:53 CEST 2020


Repository : https://github.com/latex3/latex3
On branch  : luacmd
Link       : https://github.com/latex3/latex3/commit/509d70e87b20447b377909ba4ce213cc23069ca1

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

commit 509d70e87b20447b377909ba4ce213cc23069ca1
Author: Marcel Fabian Krüger <tex at 2krueger.de>
Date:   Tue Aug 18 23:33:30 2020 +0200

    Emulate \tex_strcmp:D in LuaTeX


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

509d70e87b20447b377909ba4ce213cc23069ca1
 l3kernel/l3file.dtx   | 23 +----------------------
 l3kernel/l3fp-aux.dtx | 22 ++--------------------
 l3kernel/l3luatex.dtx | 32 +++++++++++++++++++++-----------
 l3kernel/l3names.dtx  | 27 ++++++++++++++++++++++++++-
 l3kernel/l3str.dtx    | 33 +++------------------------------
 5 files changed, 53 insertions(+), 84 deletions(-)

diff --git a/l3kernel/l3file.dtx b/l3kernel/l3file.dtx
index de714b3ec..429fe9d96 100644
--- a/l3kernel/l3file.dtx
+++ b/l3kernel/l3file.dtx
@@ -3095,33 +3095,12 @@
 % \end{macro}
 %
 % \begin{macro}[EXP]{\@@_str_cmp:nn}
-% \begin{macro}[EXP]{\@@_str_escape:n}
 %   As we are doing a fixed-length \enquote{big} integer comparison, it
 %   is easiest to use the low-level behavior  of string comparisons.
 %    \begin{macrocode}
-\cs_new:Npn \@@_str_cmp:nn #1#2 { \tex_strcmp:D {#1} {#2} }
-\sys_if_engine_luatex:T
-  {
-    \cs_set:Npn \@@_str_cmp:nn #1#2
-      {
-        \lua_now:e
-          {
-            l3kernel.strcmp
-              (
-                " \@@_str_escape:n {#1}",
-                " \@@_str_escape:n {#2}"
-              )
-          }
-      }
-   \cs_new:Npn \@@_str_escape:n #1
-     {
-       \lua_escape:e
-         { \__kernel_tl_to_str:w \use:e { {#1} } }
-     }
-  }
+\cs_new_eq:NN \@@_str_cmp:nn \tex_strcmp:D
 %    \end{macrocode}
 % \end{macro}
-% \end{macro}
 %
 % \begin{macro}[EXP, pTF]{\file_compare_timestamp:nNn}
 % \begin{macro}[EXP]{\@@_compare_timestamp:nnN}
diff --git a/l3kernel/l3fp-aux.dtx b/l3kernel/l3fp-aux.dtx
index 38fd8b136..45ad42202 100644
--- a/l3kernel/l3fp-aux.dtx
+++ b/l3kernel/l3fp-aux.dtx
@@ -1239,27 +1239,9 @@
 % \subsection{Fast string comparison}
 %
 % \begin{macro}{\@@_str_if_eq:nn}
-%   A private version of the low-level string comparison function. As
-%   the nature of the arguments is restricted and as speed is of the essence,
-%   this version does not seek to deal with |#| tokens.
-%   No \pkg{l3sys} or \pkg{l3luatex} just yet so we have to define in terms of
-%   primitives.
+%   A private version of the low-level string comparison function.
 %    \begin{macrocode}
-\sys_if_engine_luatex:TF
-  {
-    \cs_new:Npn \@@_str_if_eq:nn #1#2
-      {
-        \tex_directlua:D
-          {
-            l3kernel.strcmp
-              (
-                " \tex_luaescapestring:D {#1}",
-                " \tex_luaescapestring:D {#2}"
-              )
-          }
-      }
-  }
-  { \cs_new_eq:NN \@@_str_if_eq:nn \tex_strcmp:D }
+\cs_new_eq:NN \@@_str_if_eq:nn \tex_strcmp:D
 %    \end{macrocode}
 % \end{macro}
 %
diff --git a/l3kernel/l3luatex.dtx b/l3kernel/l3luatex.dtx
index 4e87ca2bc..7f9bcc825 100644
--- a/l3kernel/l3luatex.dtx
+++ b/l3kernel/l3luatex.dtx
@@ -125,7 +125,7 @@
 %   |l3kernel| table.
 % \end{function}
 %
-% \begin{function}{l3kernel.charcat}
+% \begin{function}[dep]{l3kernel.charcat}
 %   \begin{syntax}
 %     |l3kernel.charcat(|\meta{charcode}, \meta{catcode}|)|
 %   \end{syntax}
@@ -201,7 +201,7 @@
 %   Executes the \meta{cmd} and prints to the log as for \pdfTeX{}.
 % \end{function}
 %
-% \begin{function}{l3kernel.strcmp}
+% \begin{function}[dep]{l3kernel.strcmp}
 %   \begin{syntax}
 %     |l3kernel.strcmp(|\meta{str one}, \meta{str two}|)|
 %   \end{syntax}
@@ -356,9 +356,20 @@ local cprint     = tex.cprint
 local write      = tex.write
 local write_nl   = texio.write_nl
 local scan_int   = token.scan_int or token.scan_integer
+local scan_string= token.scan_string
 local utf8_char  = utf8.char
 %    \end{macrocode}
 %
+%    \begin{macrocode}
+local function deprecated(table, name, func)
+  table[name] = function(...)
+    write_nl("Calling deprecated Lua function %s", name)
+    table[name] = func
+    return func(...)
+  end
+end
+%    \end{macrocode
+%
 %   Deal with Con\TeX{}t: doesn't use |kpse| library.
 %    \begin{macrocode}
 local kpse_find = (resolvers and resolvers.findfile) or kpse.find_file
@@ -376,15 +387,14 @@ end
 %    \end{macrocode}
 % \end{macro}
 %
-% \begin{macro}{l3kernel.charcat}
+% \begin{macro}[dep]{l3kernel.charcat}
 %   Creating arbitrary chars using |tex.cprint|.
 %   The alternative approach using |token.put_next(token.create(...))|
 %   would be about 10\% slower.
 %    \begin{macrocode}
-local function charcat(charcode, catcode)
+deprecated(l3kernel, 'charcat', function(charcode, catcode)
   cprint(catcode, utf8_char(charcode))
-end
-l3kernel.charcat = charcat
+end)
 %    \end{macrocode}
 % \end{macro}
 %
@@ -522,11 +532,11 @@ l3kernel.filesize = filesize
 %    \end{macrocode}
 % \end{macro}
 %
-% \begin{macro}{l3kernel.strcmp}
+% \begin{macro}[dep]{l3kernel.strcmp}
 %   String comparison which gives the same results as \pdfTeX{}'s
 %   \tn{pdfstrcmp}, although the ordering should likely not be relied upon!
 %    \begin{macrocode}
-function l3kernel.strcmp(A, B)
+deprecated(l3kernel, "strcmp", function (A, B)
   if A == B then
     write("0")
   elseif A < B then
@@ -534,14 +544,14 @@ function l3kernel.strcmp(A, B)
   else
     write("1")
   end
-end
+end)
 %    \end{macrocode}
 % \end{macro}
 %
 % \begin{macro}{l3kernel.shellescape}
 %   Replicating the \pdfTeX{} log interaction for shell escape.
 %    \begin{macrocode}
-local function shellescape(cmd)
+function l3kernel.shellescape(cmd)
   local status,msg = os_exec(cmd)
   if status == nil then
     write_nl("log","runsystem(" .. cmd .. ")...(" .. msg .. ")\n")
@@ -551,7 +561,6 @@ local function shellescape(cmd)
     write_nl("log","runsystem(" .. cmd .. ")...failed " .. (msg or "") .. "\n")
   end
 end
-l3kernel.shellescape = shellescape
 %    \end{macrocode}
 % \end{macro}
 %
@@ -565,6 +574,7 @@ local luacmd do
   local set_lua = token.set_lua
   local undefined_cs = token.command_id'undefined_cs'
 
+  if not context and not luatexbase then require'ltluatex' end
   if luatexbase then
     local new_luafunction = luatexbase.new_luafunction
     local functions = lua.get_functions_table()
diff --git a/l3kernel/l3names.dtx b/l3kernel/l3names.dtx
index 994355c38..5da288e93 100644
--- a/l3kernel/l3names.dtx
+++ b/l3kernel/l3names.dtx
@@ -641,7 +641,6 @@
   \@@_primitive:NN \pdfrandomseed         \tex_randomseed:D
   \@@_primitive:NN \pdfresettimer         \tex_resettimer:D
   \@@_primitive:NN \pdfsavepos            \tex_savepos:D
-  \@@_primitive:NN \pdfstrcmp             \tex_strcmp:D
   \@@_primitive:NN \pdfsetrandomseed      \tex_setrandomseed:D
   \@@_primitive:NN \pdfshellescape        \tex_shellescape:D
   \@@_primitive:NN \pdftracingfonts       \tex_tracingfonts:D
@@ -687,6 +686,11 @@
 %</package>
 %<*names|package>
 %    \end{macrocode}
+% Some \pdfTeX{} primitives are handled here because they got dropped in
+% \LuaTeX{} but can be emulated there.
+%    \begin{macrocode}
+  \@@_primitive:NN \pdfstrcmp             \tex_strcmp:D
+%    \end{macrocode}
 % \XeTeX{}-specific primitives. Note that \XeTeX{}'s \tn{strcmp} is
 % handled earlier and is \enquote{rolled up} into \tn{pdfstrcmp}.
 % A few cross-compatibility names which lack
@@ -1447,7 +1451,26 @@
 %<*lua>
 %    \end{macrocode}
 %
+% \begin{macro}{\tex_strcmp:D}
+%   Compare two strings, expanding to |0| if they are equal,
+%   |-1| if the first one is smaller and |1| if the second one is smaller.
+%   Here ``smaller'' refers to codepoint order which does not correspond to
+%   the user expected order for most non-ASCII strings.
+%    \begin{macrocode}
+luacmd('tex_strcmp:D', function()
+  local first = scan_string()
+  local second = scan_string()
+  write(first == second and '0'
+     or first < second and '-1'
+     or '1')
+end, 'global')
+%    \end{macrocode}
+% \end{macro}
+%
 % \begin{macro}{\tex_Ucharcat:D}
+%   Creating arbitrary chars using |tex.cprint|.
+%   The alternative approach using |token.put_next(token.create(...))|
+%   would be about 10\% slower.
 %    \begin{macrocode}
 local cprint = tex.cprint
 luacmd('tex_Ucharcat:D', function()
@@ -1455,6 +1478,8 @@ luacmd('tex_Ucharcat:D', function()
   local catcode = scan_int()
   cprint(catcode, utf8_char(charcode))
 end, 'global')
+%    \end{macrocode}
+% \end{macro}
 %
 %    \begin{macrocode}
 %</lua>
diff --git a/l3kernel/l3str.dtx b/l3kernel/l3str.dtx
index 466519ac8..e9034cee5 100644
--- a/l3kernel/l3str.dtx
+++ b/l3kernel/l3str.dtx
@@ -1121,44 +1121,17 @@
 % \end{macro}
 %
 % \begin{macro}[EXP]{\@@_if_eq:nn}
-% \begin{macro}[EXP]{\@@_escape:n}
-%   String comparisons rely on the primitive \cs[index=pdfstrcmp]{(pdf)strcmp} if available:
-%   \LuaTeX{} does not have it, so emulation is required. As the net result
-%   is that we do not \emph{always} use the primitive, the correct approach
-%   is to wrap up in a function with defined behaviour. That's done by
-%   providing a wrapper and then redefining in the \LuaTeX{} case. Note that
-%   the necessary Lua code is loaded in \pkg{l3boostrap}.
-%   The need to detokenize and force
-%   expansion of input arises from the case where a |#| token is used in the
-%   input, \emph{e.g.}~|\__str_if_eq:nn {#} { \tl_to_str:n {#} }|, which
-%   otherwise would fail as \cs{tex_luaescapestring:D} does not double
-%   such tokens.
+%   String comparisons rely on the primitive \cs[index=pdfstrcmp]{(pdf)strcmp},
+%   so we define a new name for it.
 %    \begin{macrocode}
-\cs_new:Npn \@@_if_eq:nn #1#2 { \tex_strcmp:D {#1} {#2} }
+\cs_new_eq:NN \@@_if_eq:nn \tex_strcmp:D
 \cs_if_exist:NT \tex_luatexversion:D
    {
      \cs_set_eq:NN \lua_escape:e \tex_luaescapestring:D
      \cs_set_eq:NN \lua_now:e    \tex_directlua:D
-     \cs_set:Npn \@@_if_eq:nn #1#2
-       {
-          \lua_now:e
-            {
-              l3kernel.strcmp
-                (
-                  " \@@_escape:n {#1} " ,
-                  " \@@_escape:n {#2} "
-                )
-            }
-       }
-     \cs_new:Npn \@@_escape:n #1
-       {
-         \lua_escape:e
-           { \__kernel_tl_to_str:w \use:e { {#1} } }
-       }
    }
 %    \end{macrocode}
 % \end{macro}
-% \end{macro}
 %
 % \begin{macro}[pTF, EXP]
 %   {





More information about the latex3-commits mailing list.