add $$take_first() and $$take_last() functions

while implementing stacks and queues was possible before with the help
of $$member(), these functions make it much more straight-forward.

[ChangeLog][qmake] Added $$take_first() and $$take_last() functions.

Change-Id: I4922a5331780e468a42c663c9ad3c6456a95a6bf
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
This commit is contained in:
Oswald Buddenhagen 2016-05-12 20:38:54 +02:00
parent 8bd0c3d4fc
commit 3d21634fb6
3 changed files with 90 additions and 3 deletions

View File

@ -2847,6 +2847,7 @@
MY_VAR2 will contain '-Lone -Ltwo -Lthree -Lfour -Lfive', and MY_VAR3 will MY_VAR2 will contain '-Lone -Ltwo -Lthree -Lfour -Lfive', and MY_VAR3 will
contain 'three two three'. contain 'three two three'.
\target fn_first
\section2 first(variablename) \section2 first(variablename)
Returns the first value of \c variablename. Returns the first value of \c variablename.
@ -2855,7 +2856,7 @@
\snippet code/doc_src_qmake-manual.pro 161 \snippet code/doc_src_qmake-manual.pro 161
See also \l{last(variablename)}{last()}. See also \l{take_first()}, \l{fn_last}{last()}.
\section2 format_number(number[, options...]) \section2 format_number(number[, options...])
@ -2903,6 +2904,7 @@
to empty strings. If you need to encode spaces in \c glue, \c before, or \c to empty strings. If you need to encode spaces in \c glue, \c before, or \c
after, you must quote them. after, you must quote them.
\target fn_last
\section2 last(variablename) \section2 last(variablename)
Returns the last value of \c variablename. Returns the last value of \c variablename.
@ -2911,7 +2913,7 @@
\snippet code/doc_src_qmake-manual.pro 162 \snippet code/doc_src_qmake-manual.pro 162
See also \l{first(variablename)}{first()}. See also \l{take_last()}, \l{fn_first}{first()}.
\section2 list(arg1 [, arg2 ..., argn]) \section2 list(arg1 [, arg2 ..., argn])
@ -3063,6 +3065,26 @@
See also \l{shell_quote(arg)}{shell_quote()}. See also \l{shell_quote(arg)}{shell_quote()}.
\target take_first()
\section2 take_first(variablename)
Returns the first value of \c variablename and removes it from the
source variable.
This provides convenience for implementing queues, for example.
See also \l{take_last()}, \l{fn_first}{first()}.
\target take_last()
\section2 take_last(variablename)
Returns the last value of \c variablename and removes it from the
source variable.
This provides convenience for implementing stacks, for example.
See also \l{take_first()}, \l{fn_last}{last()}.
\target unique \target unique
\section2 unique(variablename) \section2 unique(variablename)

View File

@ -80,7 +80,8 @@ QT_BEGIN_NAMESPACE
#define fL1S(s) QString::fromLatin1(s) #define fL1S(s) QString::fromLatin1(s)
enum ExpandFunc { enum ExpandFunc {
E_INVALID = 0, E_MEMBER, E_FIRST, E_LAST, E_SIZE, E_CAT, E_FROMFILE, E_EVAL, E_LIST, E_INVALID = 0, E_MEMBER, E_FIRST, E_TAKE_FIRST, E_LAST, E_TAKE_LAST, E_SIZE,
E_CAT, E_FROMFILE, E_EVAL, E_LIST,
E_SPRINTF, E_FORMAT_NUMBER, E_JOIN, E_SPLIT, E_BASENAME, E_DIRNAME, E_SECTION, E_SPRINTF, E_FORMAT_NUMBER, E_JOIN, E_SPLIT, E_BASENAME, E_DIRNAME, E_SECTION,
E_FIND, E_SYSTEM, E_UNIQUE, E_REVERSE, E_QUOTE, E_ESCAPE_EXPAND, E_FIND, E_SYSTEM, E_UNIQUE, E_REVERSE, E_QUOTE, E_ESCAPE_EXPAND,
E_UPPER, E_LOWER, E_TITLE, E_FILES, E_PROMPT, E_RE_ESCAPE, E_VAL_ESCAPE, E_UPPER, E_LOWER, E_TITLE, E_FILES, E_PROMPT, E_RE_ESCAPE, E_VAL_ESCAPE,
@ -105,7 +106,9 @@ void QMakeEvaluator::initFunctionStatics()
} expandInits[] = { } expandInits[] = {
{ "member", E_MEMBER }, { "member", E_MEMBER },
{ "first", E_FIRST }, { "first", E_FIRST },
{ "take_first", E_TAKE_FIRST },
{ "last", E_LAST }, { "last", E_LAST },
{ "take_last", E_TAKE_LAST },
{ "size", E_SIZE }, { "size", E_SIZE },
{ "cat", E_CAT }, { "cat", E_CAT },
{ "fromfile", E_FROMFILE }, { "fromfile", E_FROMFILE },
@ -690,6 +693,20 @@ ProStringList QMakeEvaluator::evaluateBuiltinExpand(
} }
} }
break; break;
case E_TAKE_FIRST:
case E_TAKE_LAST:
if (args.count() != 1) {
evalError(fL1S("%1(var) requires one argument.").arg(func.toQString(m_tmp1)));
} else {
ProStringList &var = valuesRef(map(args.at(0)));
if (!var.isEmpty()) {
if (func_t == E_TAKE_FIRST)
ret.append(var.takeFirst());
else
ret.append(var.takeLast());
}
}
break;
case E_SIZE: case E_SIZE:
if (args.count() != 1) if (args.count() != 1)
evalError(fL1S("size(var) requires one argument.")); evalError(fL1S("size(var) requires one argument."));

View File

@ -742,6 +742,30 @@ void tst_qmakelib::addReplaceFunctions(const QString &qindir)
<< "##:1: first(var) requires one argument." << "##:1: first(var) requires one argument."
<< true; << true;
QTest::newRow("$$take_first(): empty")
<< "IN = \nVAR = $$take_first(IN)"
<< "VAR =\nIN ="
<< ""
<< true;
QTest::newRow("$$take_first(): one")
<< "IN = one\nVAR = $$take_first(IN)"
<< "VAR = one\nIN ="
<< ""
<< true;
QTest::newRow("$$take_first(): multiple")
<< "IN = one two three\nVAR = $$take_first(IN)"
<< "VAR = one\nIN = two three"
<< ""
<< true;
QTest::newRow("$$take_first(): bad number of arguments")
<< "VAR = $$take_first(1, 2)"
<< "VAR ="
<< "##:1: take_first(var) requires one argument."
<< true;
QTest::newRow("$$last(): empty") QTest::newRow("$$last(): empty")
<< "IN = \nVAR = $$last(IN)" << "IN = \nVAR = $$last(IN)"
<< "VAR =" << "VAR ="
@ -766,6 +790,30 @@ void tst_qmakelib::addReplaceFunctions(const QString &qindir)
<< "##:1: last(var) requires one argument." << "##:1: last(var) requires one argument."
<< true; << true;
QTest::newRow("$$take_last(): empty")
<< "IN = \nVAR = $$take_last(IN)"
<< "VAR =\nIN ="
<< ""
<< true;
QTest::newRow("$$take_last(): one")
<< "IN = one\nVAR = $$take_last(IN)"
<< "VAR = one\nIN ="
<< ""
<< true;
QTest::newRow("$$take_last(): multiple")
<< "IN = one two three\nVAR = $$take_last(IN)"
<< "VAR = three\nIN = one two"
<< ""
<< true;
QTest::newRow("$$take_last(): bad number of arguments")
<< "VAR = $$take_last(1, 2)"
<< "VAR ="
<< "##:1: take_last(var) requires one argument."
<< true;
QTest::newRow("$$size()") QTest::newRow("$$size()")
<< "IN = one two three\nVAR = $$size(IN)" << "IN = one two three\nVAR = $$size(IN)"
<< "VAR = 3" << "VAR = 3"