DIRECTOR: LINGO: Implement the last item of

Returns the last item of a chunkexpression.
It follows the item Delimiter.
This commit is contained in:
Roland van Laar 2020-08-04 22:36:51 +02:00
parent d7ed2e453e
commit 6b09c3de62
2 changed files with 25 additions and 2 deletions

View File

@ -2429,10 +2429,23 @@ void LB::b_lastcharof(int nargs) {
void LB::b_lastitemof(int nargs) {
Datum d = g_lingo->pop();
if (d.type != STRING) {
warning("LB::b_lastitemof(): Called with wrong data type: %s", d.type2str());
g_lingo->push(Datum(""));
return;
}
warning("STUB: b_lastitemof");
Common::String res;
Common::String chunkExpr = d.asString();
uint pos = chunkExpr.findLastOf(g_lingo->_itemDelimiter);
if (pos == Common::String::npos) {
res = chunkExpr;
} else {
pos++; // skip the item delimiter
res = chunkExpr.substr(pos , chunkExpr.size() - pos);
}
g_lingo->push(Datum(0));
g_lingo->push(Datum(res));
}
void LB::b_lastlineof(int nargs) {

View File

@ -88,3 +88,13 @@ scummvmAssertEqual(res, "")
-- LB::b_lastcharof
scummvmAssertEqual(the last char of "", "")
scummvmAssertEqual(the last char of "hello", "o")
-- LB::b_lastitemof
scummvmAssertEqual(the last item of "", "")
scummvmAssertEqual(the last item of "onetwo", "onetwo")
scummvmAssertEqual(the last item of "one,two", "two")
set save to the itemDelimiter
set the itemDelimiter to ":"
scummvmAssertEqual(the last item of "one:two", "two")
set the itemDelimiter to save
scummvmAssertEqual(the last item of "onetwo", "onetwo")