From cff1fccb6e80125166972354f1e3e8c59703c374 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Wed, 28 Oct 2009 12:21:36 -0600 Subject: [PATCH] Minor optimizations to m4sh. * lib/m4sugar/m4sh.m4 (AS_VAR_IF, AS_IDENTIFIER_IF) (AS_LITERAL_IF): Parse fewer bytes during expansion, by visiting if-true and if-false arguments only once. Signed-off-by: Eric Blake --- ChangeLog | 5 +++++ lib/m4sugar/m4sh.m4 | 27 ++++++++++++--------------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index a9720393..f28d97c3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2009-10-28 Eric Blake + Minor optimizations to m4sh. + * lib/m4sugar/m4sh.m4 (AS_VAR_IF, AS_IDENTIFIER_IF) + (AS_LITERAL_IF): Parse fewer bytes during expansion, by visiting + if-true and if-false arguments only once. + Optimize m4_escape for common case. * lib/m4sugar/m4sugar.m4 (m4_escape): Don't use regex if string is already sane, by copying from AS_LITERAL_IF. Move guts... diff --git a/lib/m4sugar/m4sh.m4 b/lib/m4sugar/m4sh.m4 index 48640882..c1253091 100644 --- a/lib/m4sugar/m4sh.m4 +++ b/lib/m4sugar/m4sh.m4 @@ -1507,15 +1507,15 @@ m4_define([AS_HELP_STRING], # we worry if the first character also matches m4_cr_symbol1 (ie. does not # match m4_cr_digit). m4_define([AS_IDENTIFIER_IF], -[m4_if(m4_index([$1], [@]), [-1], - [_$0($@)], - [_$0(m4_bpatsubst([[$1]], [@&t@]), [$2], [$3])])]) +[m4_if(_$0(m4_if(m4_index([$1], [@]), [-1], + [[$1]], [m4_bpatsubst([[$1]], [@&t@])])), [-], [$2], [$3])]) + m4_define([_AS_IDENTIFIER_IF], -[m4_cond([[$1]], [], [$3], +[m4_cond([[$1]], [], [], [m4_eval(m4_len(m4_translit([[$1]], ]]dnl -m4_dquote(m4_dquote(m4_defn([m4_cr_symbols2])))[[)) > 0)], [1], [$3], +m4_dquote(m4_dquote(m4_defn([m4_cr_symbols2])))[[)) > 0)], [1], [], [m4_len(m4_translit(m4_format([[%.1s]], [$1]), ]]dnl -m4_dquote(m4_dquote(m4_defn([m4_cr_symbols1])))[[))], [0], [$2], [$3])]) +m4_dquote(m4_dquote(m4_defn([m4_cr_symbols1])))[[))], [0], [-])]) # AS_LITERAL_IF(EXPRESSION, IF-LITERAL, IF-NOT-LITERAL) @@ -1544,13 +1544,10 @@ m4_dquote(m4_dquote(m4_defn([m4_cr_symbols1])))[[))], [0], [$2], [$3])]) # Rather than expand m4_defn every time AS_LITERAL_IF is expanded, we # inline its expansion up front. m4_define([AS_LITERAL_IF], -[m4_cond([m4_eval(m4_index(m4_quote($1), [@S|@]) == -1)], [0], [$3], - [m4_index(m4_translit(m4_quote($1), - [[]`,#]]]dnl -m4_dquote(m4_dquote(m4_defn([m4_cr_symbols2])))[[, - [$$$]), - [$])], [-1], [$2], - [$3])]) +[m4_if(m4_cond([m4_eval(m4_index(m4_quote($1), [@S|@]) == -1)], [0], [], + [m4_index(m4_translit(m4_quote($1), [[]`,#]]]dnl +m4_dquote(m4_dquote(m4_defn([m4_cr_symbols2])))[[, [$$$]), [$])], + [-1], [-]), [-], [$2], [$3])]) # AS_TMPDIR(PREFIX, [DIRECTORY = $TMPDIR [= /tmp]]) @@ -1909,9 +1906,9 @@ m4_define([AS_VAR_GET], # Polymorphic, and avoids sh expansion error upon interrupt or term signal. m4_define([AS_VAR_IF], [AS_LITERAL_IF([$1], - [AS_IF([test "x$$1" = x""$2], [$3], [$4])], + [AS_IF([test "x$$1" = x""$2]], [AS_VAR_COPY([as_val], [$1]) - AS_IF([test "x$as_val" = x""$2], [$3], [$4])])]) + AS_IF([test "x$as_val" = x""$2]]), [$3], [$4])]) # AS_VAR_PUSHDEF and AS_VAR_POPDEF