m4: Format
1
1 11.7 Formatting strings (printf-like)
1 =====================================
1
1 Formatted output can be made with 'format':
1
1 -- Builtin: format (FORMAT-STRING, ...)
1 Works much like the C function 'printf'. The first argument
1 FORMAT-STRING can contain '%' specifications which are satisfied by
1 additional arguments, and the expansion of 'format' is the
1 formatted string.
1
1 The macro 'format' is recognized only with parameters.
1
1 Its use is best described by a few examples:
1
1 define(`foo', `The brown fox jumped over the lazy dog')
1 =>
1 format(`The string "%s" uses %d characters', foo, len(foo))
1 =>The string "The brown fox jumped over the lazy dog" uses 38 characters
1 format(`%*.*d', `-1', `-1', `1')
1 =>1
1 format(`%.0f', `56789.9876')
1 =>56790
1 len(format(`%-*X', `5000', `1'))
1 =>5000
1 ifelse(format(`%010F', `infinity'), ` INF', `success',
1 format(`%010F', `infinity'), ` INFINITY', `success',
1 format(`%010F', `infinity'))
1 =>success
1 ifelse(format(`%.1A', `1.999'), `0X1.0P+1', `success',
1 format(`%.1A', `1.999'), `0X2.0P+0', `success',
1 format(`%.1A', `1.999'))
1 =>success
1 format(`%g', `0xa.P+1')
1 =>20
1
1 Using the 'forloop' macro defined earlier (⇒Forloop), this
1 example shows how 'format' can be used to produce tabular output.
1
1 $ m4 -I examples
1 include(`forloop.m4')
1 =>
1 forloop(`i', `1', `10', `format(`%6d squared is %10d
1 ', i, eval(i**2))')
1 => 1 squared is 1
1 => 2 squared is 4
1 => 3 squared is 9
1 => 4 squared is 16
1 => 5 squared is 25
1 => 6 squared is 36
1 => 7 squared is 49
1 => 8 squared is 64
1 => 9 squared is 81
1 => 10 squared is 100
1 =>
1
1 The builtin 'format' is modeled after the ANSI C 'printf' function,
1 and supports these '%' specifiers: 'c', 's', 'd', 'o', 'x', 'X', 'u',
1 'a', 'A', 'e', 'E', 'f', 'F', 'g', 'G', and '%'; it supports field
1 widths and precisions, and the flags '+', '-', ' ', '0', '#', and '''.
1 For integer specifiers, the width modifiers 'hh', 'h', and 'l' are
1 recognized, and for floating point specifiers, the width modifier 'l' is
1 recognized. Items not yet supported include positional arguments, the
1 'n', 'p', 'S', and 'C' specifiers, the 'z', 't', 'j', 'L' and 'll'
1 modifiers, and any platform extensions available in the native 'printf'.
1 For more details on the functioning of 'printf', see the C Library
1 Manual, or the POSIX specification (for example, '%a' is supported even
1 on platforms that haven't yet implemented C99 hexadecimal floating point
1 output natively).
1
1 Unrecognized specifiers result in a warning. It is anticipated that
1 a future release of GNU 'm4' will support more specifiers, and give
1 better warnings when various problems such as overflow are encountered.
1 Likewise, escape sequences are not yet recognized.
1
1 format(`%p', `0')
1 error->m4:stdin:1: Warning: unrecognized specifier in `%p'
1 =>
1