From 72431d8c84fdbf893fd5ef724cce99220539f7f8 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Sat, 13 Oct 2007 11:11:44 -0600 Subject: [PATCH] Change m4_join to match libtool's ltsugar semantics. * lib/m4sugar/m4sugar.m4 (m4_join): Just define this, not defun. Ignore empty arguments, using... (_m4_join): ...this new helper. * tests/m4sugar.at (m4@&t@_join): New test. * doc/autoconf.texi (Text processing Macros): Document new semantics of m4_join. Signed-off-by: Eric Blake --- ChangeLog | 8 ++++++++ doc/autoconf.texi | 10 ++++++++-- lib/m4sugar/m4sugar.m4 | 25 ++++++++++++++++++------- tests/m4sugar.at | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index ac752fd9..9efbbe41 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2007-10-13 Eric Blake + Change m4_join to match libtool's ltsugar semantics. + * lib/m4sugar/m4sugar.m4 (m4_join): Just define this, not defun. + Ignore empty arguments, using... + (_m4_join): ...this new helper. + * tests/m4sugar.at (m4@&t@_join): New test. + * doc/autoconf.texi (Text processing Macros): Document new + semantics of m4_join. + Make AC_PREREQ faster and more robust. * lib/m4sugar/m4sugar.m4 (m4_ignore, m4_unquote): New macros. (m4_version_prereq): Inline constant expansions. diff --git a/doc/autoconf.texi b/doc/autoconf.texi index f63dfa20..3a8c126c 100644 --- a/doc/autoconf.texi +++ b/doc/autoconf.texi @@ -10832,8 +10832,14 @@ still a quoted string. @defmac m4_join (@ovar{separator}, @var{args}@dots{}) @msindex{join} -Concatenate each @var{arg}, separated by @var{separator}. The result is -a quoted string. +Concatenate each @var{arg}, separated by @var{separator}, with the +exception that no back-to-back separators are issued for empty +arguments. The result is a quoted string. +@example +m4_define([active], [ACTIVE])dnl +m4_join([|], [one], [], [active], [two]) +@result{}one|active|two +@end example @end defmac @defmac m4_newline diff --git a/lib/m4sugar/m4sugar.m4 b/lib/m4sugar/m4sugar.m4 index dd5138ef..8c7070ba 100644 --- a/lib/m4sugar/m4sugar.m4 +++ b/lib/m4sugar/m4sugar.m4 @@ -1663,13 +1663,24 @@ m4_define([m4_normalize], # m4_join(SEP, ARG1, ARG2...) # --------------------------- -# Produce ARG1SEPARG2...SEPARGn. -m4_defun([m4_join], -[m4_case([$#], - [1], [], - [2], [[$2]], - [[$2][$1]$0([$1], m4_shift2($@))])]) - +# Produce ARG1SEPARG2...SEPARGn. Avoid back-to-back SEP when a given ARG +# is the empty string. +# +# Since the number of arguments to join can be arbitrarily long, we +# want to avoid having more than one $@ in the macro definition; +# otherwise, the expansion would require twice the memory of the already +# long list. Hence, m4_join merely looks for the first non-empty element, +# and outputs just that element; while _m4_join looks for all non-empty +# elements, and outputs them following a separator. The final trick to +# note is that we decide between recursing with $0 or _$0 based on the +# nested m4_if ending with `_'. +m4_define([m4_join], +[m4_if([$#], [1], [], + [$#], [2], [[$2]], + [m4_if([$2], [], [], [[$2]_])$0([$1], m4_shift2($@))])]) +m4_define([_m4_join], +[m4_if([$#$2], [2], [], + [m4_if([$2], [], [], [[$1$2]])$0([$1], m4_shift2($@))])]) # m4_append(MACRO-NAME, STRING, [SEPARATOR]) diff --git a/tests/m4sugar.at b/tests/m4sugar.at index a946104d..f42c6c87 100644 --- a/tests/m4sugar.at +++ b/tests/m4sugar.at @@ -44,6 +44,8 @@ AT_CHECK_M4SUGAR([-o-],, [$2], [$3]) # # - m4_append # +# - m4_join +# # - m4_text_wrap # uses m4_split code. @@ -251,6 +253,43 @@ one, two, three AT_CLEANUP +## --------- ## +## m4_join. ## +## --------- ## + +AT_SETUP([m4@&t@_join]) + +AT_CHECK_M4SUGAR_TEXT( +[[m4_define([active], [ACTIVE]) +m4_join +m4_join([|]) +m4_join([, ], [one], [two]) +m4_dquote(m4_join([, ], [one], [two])) +m4_join([|], [active], [active]) +m4_join([|], ,,,[one]) +m4_join([|], [one],,,) +m4_join([], ,,,[two]) +m4_join([], [two],,,) +m4_join([ active ], [one], , [two]) +m4_join([], [one], [two]) +]], +[[ + + +one, two +[one, two] +active|active +one +one +two +two +one active two +onetwo +]]) + +AT_CLEANUP + + ## -------------- ## ## m4_text_wrap. ## ## -------------- ## -- 2.11.4.GIT