如何在 m4 中定义不区分大小写的宏名称?

How to define case insensitive macro names in m4?

有没有办法让宏名在定义时不区分大小写?

例如,
考虑输入流:Mov MOV moV mOv
我希望 m4 输出为:mov mov mov mov

最简单的方法是定义以下 m4 宏:

define(Mov,mov)
define(MOV,mov)
define(moV,mov)
define(mOv,mov)

当我们想对一个 4 或 5 个字母的单词执行相同的操作时,这种方法就变得乏味了。有更好的方法吗?

如果您只想进行字符串转换(希望 m4 输出为 ),您可以使用 translit:

     translit(string, mapfrom, mapto)
              Transliterate the characters in the first argument from the
              set given by the second argument to the set given by the
              third.  You cannot use tr(1) style abbreviations.

您的情况:

translit(`MoV',`ABCDEFGHIJKLMNOPQRSTUVWXYZ',`abcdefghijklmnopqrstuvwxyz')

让我们创建一个名为 to_lowercase 的 m4 宏。它的定义如下所示。

define(`to_lowercase',`translit(,`ABCDEFGHIJKLMNOPQRSTUVWXYZ',`abcdefghijklmnopqrstuvwxyz')')

现在,我们可以使用 to_lowercase(Mov)', to_lowercase(mOV)' 调用我们的宏。