Home > Enterprise >  How to define case insensitive macro names in m4?
How to define case insensitive macro names in m4?

Time:11-10

Is there a way to make a macro name case insensitive when defining it?

For example,
Consider the input stream: Mov MOV moV mOv
I want the m4 to output to be: mov mov mov mov

The naive way to do this is to define the following m4 macros:

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

This method becomes tedious when we want to do the same for a 4 or 5 letter word. Is there a better way to do this?

CodePudding user response:

If you want only string transform (want the m4 to output to be) you can use 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.

Your case:

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

Let's create an m4 macro called to_lowercase. Its definition looks as shown below.

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

Now, we can call our macro using to_lowercase(Mov)', to_lowercase(mOV)'.

  • Related