diff options
-rw-r--r-- | qmake/doc/src/qmake-manual.qdoc | 26 | ||||
-rw-r--r-- | qmake/library/qmakebuiltins.cpp | 19 | ||||
-rw-r--r-- | tests/auto/tools/qmakelib/evaltest.cpp | 48 |
3 files changed, 90 insertions, 3 deletions
diff --git a/qmake/doc/src/qmake-manual.qdoc b/qmake/doc/src/qmake-manual.qdoc index 5c6afcf191..c196cb841a 100644 --- a/qmake/doc/src/qmake-manual.qdoc +++ b/qmake/doc/src/qmake-manual.qdoc @@ -2847,6 +2847,7 @@ MY_VAR2 will contain '-Lone -Ltwo -Lthree -Lfour -Lfive', and MY_VAR3 will contain 'three two three'. + \target fn_first \section2 first(variablename) Returns the first value of \c variablename. @@ -2855,7 +2856,7 @@ \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...]) @@ -2903,6 +2904,7 @@ to empty strings. If you need to encode spaces in \c glue, \c before, or \c after, you must quote them. + \target fn_last \section2 last(variablename) Returns the last value of \c variablename. @@ -2911,7 +2913,7 @@ \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]) @@ -3063,6 +3065,26 @@ 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 \section2 unique(variablename) diff --git a/qmake/library/qmakebuiltins.cpp b/qmake/library/qmakebuiltins.cpp index 12c272db7d..e919f1a77e 100644 --- a/qmake/library/qmakebuiltins.cpp +++ b/qmake/library/qmakebuiltins.cpp @@ -80,7 +80,8 @@ QT_BEGIN_NAMESPACE #define fL1S(s) QString::fromLatin1(s) 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_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, @@ -105,7 +106,9 @@ void QMakeEvaluator::initFunctionStatics() } expandInits[] = { { "member", E_MEMBER }, { "first", E_FIRST }, + { "take_first", E_TAKE_FIRST }, { "last", E_LAST }, + { "take_last", E_TAKE_LAST }, { "size", E_SIZE }, { "cat", E_CAT }, { "fromfile", E_FROMFILE }, @@ -690,6 +693,20 @@ ProStringList QMakeEvaluator::evaluateBuiltinExpand( } } 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: if (args.count() != 1) evalError(fL1S("size(var) requires one argument.")); diff --git a/tests/auto/tools/qmakelib/evaltest.cpp b/tests/auto/tools/qmakelib/evaltest.cpp index 4642c822bc..69ccf4858f 100644 --- a/tests/auto/tools/qmakelib/evaltest.cpp +++ b/tests/auto/tools/qmakelib/evaltest.cpp @@ -742,6 +742,30 @@ void tst_qmakelib::addReplaceFunctions(const QString &qindir) << "##:1: first(var) requires one argument." << 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") << "IN = \nVAR = $$last(IN)" << "VAR =" @@ -766,6 +790,30 @@ void tst_qmakelib::addReplaceFunctions(const QString &qindir) << "##:1: last(var) requires one argument." << 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()") << "IN = one two three\nVAR = $$size(IN)" << "VAR = 3" |