[luatex] \luaescapestring and # tokens

Joseph Wright joseph.wright at morningstar2.co.uk
Mon Jun 2 09:46:26 CEST 2014


On 02/06/2014 07:24, Joseph Wright wrote:
> Hello all,
> 
> The usual approach to emulating \pdfstrcmp in LuaTeX involves escaping
> input using \luaescapestring and then doing a Lua-based comparison,
> something like:
> 
> \catcode`\@=11 %
> \ifdefined\directlua
>   \directlua{%
>     function strcmp (A, B)
>       if A == B then
>         tex.write ("0")
>       elseif A < B then
>         tex.write ("-1")
>       else
>         tex.write ("1")
>       end
>     end
>   }%
>   \long\def\pdf at strcmp#1#2{%
>     \directlua{%
>       strcmp ("\luaescapestring{#1}", "\luaescapestring{#2}")
>     }%
>   }%
> \else
>   \ifdefined\pdfstrcmp
>     \let\pdf at strcmp\pdfstrcmp
>   \else
>     \let\pdf at strcmp\strcmp
>   \fi
> \fi
> \message{Value is: \pdf at strcmp{#}{\detokenize{#}}}
> \bye
> 
> However, as is shown by the demo, this may fail to give the same result
> as for the 'real' primitive if # tokens (more generally, catcode 6
> tokens). The reason is \luaescapestring doesn't double # tokens.
> 
> \message{"\luaescapestring{#}" "\detokenize{#}"}
> 
> Is this deliberate, and if so is there a reliable way of emulating
> \pdfstrcmp in it's entirety?

Suggestion from Bruno Le Floch to the LaTeX team:

  \long\def\pdf at strcmp#1#2{%
    \directlua{%
      strcmp (
        "\luaescapestring{\detokenize\expandafter{\expanded{#1}}}",
        "\luaescapestring{\detokenize\expandafter{\expanded{#2}}}"
      )
    }%
  }

which does certainly work. Thoughts?
--
Joseph Wright




More information about the luatex mailing list