* lib/m4sugar/m4sugar.m4 (_m4_map): Delete, merged with...
(_m4_foreach): ...this.
(m4_foreach, m4_map, m4_mapall, m4_map_sep, _m4_mapall_sep)
(m4_map_args, m4_map_args_sep): Adjust callers.
* lib/m4sugar/foreach.m4 (_m4_map): Rename...
(_m4_foreach): ...to this, overwriting old definition.
Signed-off-by: Eric Blake <ebb9@byu.net>
+2008-11-05 Eric Blake <ebb9@byu.net>
+
+ Unify _m4_foreach and _m4_map.
+ * lib/m4sugar/m4sugar.m4 (_m4_map): Delete, merged with...
+ (_m4_foreach): ...this.
+ (m4_foreach, m4_map, m4_mapall, m4_map_sep, _m4_mapall_sep)
+ (m4_map_args, m4_map_args_sep): Adjust callers.
+ * lib/m4sugar/foreach.m4 (_m4_map): Rename...
+ (_m4_foreach): ...to this, overwriting old definition.
+
2008-11-04 Eric Blake <ebb9@byu.net>
Add m4_map_args_sep, undocumented for now.
#
# Please keep this file in sync with m4sugar.m4.
-# m4_foreach(VARIABLE, LIST, EXPRESSION)
-# --------------------------------------
-# Expand EXPRESSION assigning each value of the LIST to VARIABLE.
-# LIST should have the form `item_1, item_2, ..., item_n', i.e. the
-# whole list must *quoted*. Quote members too if you don't want them
-# to be expanded.
+# _m4_foreach(PRE, POST, IGNORED, ARG...)
+# ---------------------------------------
+# Form the common basis of the m4_foreach and m4_map macros. For each
+# ARG, expand PRE[ARG]POST[]. The IGNORED argument makes recursion
+# easier, and must be supplied rather than implicit.
#
# This version minimizes the number of times that $@ is evaluated by
-# using m4_for to generate a boilerplate into VARIABLE then passing $@
-# to that temporary macro. Thus, the recursion is done in m4_for
-# without reparsing any user input, and is not quadratic. For an idea
-# of how this works, note that m4_foreach(i,[1,2],[i]) defines i to be
-# m4_define([$1],[$3])$2[]m4_define([$1],[$4])$2[]m4_popdef([i])
-# then calls i([i],[i],[1],[2]).
-m4_define([m4_foreach],
-[m4_if([$2], [], [], [_$0([$1], [$3], $2)])])
-
+# using m4_for to generate a boilerplate into _m4_f then passing $@ to
+# that temporary macro. Thus, the recursion is done in m4_for without
+# reparsing any user input, and is not quadratic. For an idea of how
+# this works, note that m4_foreach(i,[1,2],[i]) calls
+# _m4_foreach([m4_define([i],],[)i],[],[1],[2])
+# which defines _m4_f:
+# $1[$4]$2[]$1[$5]$2[]_m4_popdef([_m4_f])
+# then calls _m4_f([m4_define([i],],[)i],[],[1],[2]) for a net result:
+# m4_define([i],[1])i[]m4_define([i],[2])i[]_m4_popdef([_m4_f]).
m4_define([_m4_foreach],
-[m4_pushdef([$1], _m4_for([3], [$#], [1],
- [$0_([1], [2],], [)])[m4_popdef([$1])])m4_indir([$1], $@)])
+[m4_if([$#], [3], [],
+ [m4_pushdef([_m4_f], _m4_for([4], [$#], [1],
+ [$0_([1], [2],], [)])[_m4_popdef([_m4_f])])_m4_f($@)])])
m4_define([_m4_foreach_],
-[[m4_define([$$1], [$$3])$$2[]]])
+[[$$1[$$3]$$2[]]])
# m4_case(SWITCH, VAL1, IF-VAL1, VAL2, IF-VAL2, ..., DEFAULT)
# -----------------------------------------------------------
[[, ]m4_dquote($], [)])[_m4_popdef([_m4_r])])_m4_r($@)])])
-# _m4_map(PRE, POST, IGNORED, LIST)
-# ---------------------------------
-# Form the common basis of the m4_map macros. For each element of
-# LIST, expand PRE[element]POST[]. The IGNORED argument makes
-# recursion easier, and must be supplied rather than implicit.
-#
-# m4_map{,all,_args}{,_sep} each only execute once; the speedup comes
-# in fixing _m4_map. Build the temporary _m4_m:
-# $1[$4]$2[]$1[$5]$2[]...$1[$m]$2[]_m4_popdef([_m4_m])
-m4_define([_m4_map],
-[m4_if([$#], [3], [],
- [m4_pushdef([_m4_m], _m4_for([4], [$#], [1],
- [$0_([1], [2],], [)])[_m4_popdef([_m4_m])])_m4_m($@)])])
-
-m4_define([_m4_map_],
-[[$$1[$$3]$$2[]]])
-
# m4_map_args_pair(EXPRESSION, [END-EXPR = EXPRESSION], ARG...)
# -------------------------------------------------------------
# Perform a pairwise grouping of consecutive ARGs, by expanding
# more memory for expansion. So, rather than directly compare $2 against
# [] and use m4_car/m4_cdr for recursion, we instead unbox the list (which
# requires swapping the argument order in the helper), insert an ignored
-# third argument, and use m4_shift3 to detect when recursion is complete.
-#
-# Please keep foreach.m4 in sync with any adjustments made here.
+# third argument, and use m4_shift3 to detect when recursion is complete,
+# at which point this looks very much like m4_map_args.
m4_define([m4_foreach],
[m4_if([$2], [], [],
- [m4_pushdef([$1])_$0([$1], [$3], [], $2)m4_popdef([$1])])])
+ [m4_pushdef([$1])_$0([m4_define([$1],], [)$3], [],
+ $2)m4_popdef([$1])])])
+# _m4_foreach(PRE, POST, IGNORED, ARG...)
+# ---------------------------------------
+# Form the common basis of the m4_foreach and m4_map macros. For each
+# ARG, expand PRE[ARG]POST[]. The IGNORED argument makes recursion
+# easier, and must be supplied rather than implicit.
+#
+# Please keep foreach.m4 in sync with any adjustments made here.
m4_define([_m4_foreach],
[m4_if([$#], [3], [],
- [m4_define([$1], [$4])$2[]$0([$1], [$2], m4_shift3($@))])])
+ [$1[$4]$2[]$0([$1], [$2], m4_shift3($@))])])
# m4_foreach_w(VARIABLE, LIST, EXPRESSION)
[m4_foreach([$1], m4_split(m4_normalize([$2]), [ ]), [$3])])
-# _m4_map(PRE, POST, IGNORED, LIST)
-# ---------------------------------
-# Form the common basis of the m4_map macros. For each element of
-# LIST, expand PRE[element]POST[]. The IGNORED argument makes
-# recursion easier, and must be supplied rather than implicit.
-#
-# Please keep foreach.m4 in sync with any adjustments made here.
-m4_define([_m4_map],
-[m4_if([$#], [3], [],
- [$1[$4]$2[]$0([$1], [$2], m4_shift3($@))])])
-
# m4_map(MACRO, LIST)
# m4_mapall(MACRO, LIST)
# ----------------------
#
# Since LIST may be quite large, we want to minimize how often it
# appears in the expansion. Rather than use m4_car/m4_cdr iteration,
-# we unbox the list, and use _m4_map for iteration. For m4_map, an
-# empty list behaves like an empty sublist and gets ignored; for
+# we unbox the list, and use _m4_foreach for iteration. For m4_map,
+# an empty list behaves like an empty sublist and gets ignored; for
# m4_mapall, we must special-case the empty list.
m4_define([m4_map],
-[_m4_map([_m4_apply([$1],], [)], [], $2)])
+[_m4_foreach([_m4_apply([$1],], [)], [], $2)])
m4_define([m4_mapall],
[m4_if([$2], [], [],
- [_m4_map([m4_apply([$1],], [)], [], $2)])])
+ [_m4_foreach([m4_apply([$1],], [)], [], $2)])])
# m4_map_sep(MACRO, SEPARATOR, LIST)
# helper macro and use that as the separator instead.
m4_define([m4_map_sep],
[m4_pushdef([m4_Sep], [m4_define([m4_Sep], _m4_defn([m4_unquote]))])]dnl
-[_m4_map([_m4_apply([m4_Sep([$2])[]$1],], [)], [], $3)m4_popdef([m4_Sep])])
+[_m4_foreach([_m4_apply([m4_Sep([$2])[]$1],], [)], [], $3)m4_popdef([m4_Sep])])
m4_define([m4_mapall_sep],
[m4_if([$3], [], [], [_$0([$1], [$2], $3)])])
m4_define([_m4_mapall_sep],
-[m4_apply([$1], [$3])_m4_map([m4_apply([$2[]$1],], [)], m4_shift2($@))])
+[m4_apply([$1], [$3])_m4_foreach([m4_apply([$2[]$1],], [)], m4_shift2($@))])
# m4_map_args(EXPRESSION, ARG...)
# -------------------------------
[m4_if([$#], [0], [m4_fatal([$0: too few arguments: $#])],
[$#], [1], [],
[$#], [2], [$1([$2])[]],
- [_m4_map([$1(], [)], $@)])])
+ [_m4_foreach([$1(], [)], $@)])])
# m4_map_args_pair(EXPRESSION, [END-EXPR = EXPRESSION], ARG...)
[$#], [2], [],
[$#], [3], [],
[$#], [4], [$1[$4]$2[]],
- [$1[$4]$2[]_m4_map([$3[]$1], [$2], m4_shift3($@))])])
+ [$1[$4]$2[]_m4_foreach([$3[]$1], [$2], m4_shift3($@))])])
# m4_stack_foreach(MACRO, FUNC)