[Scummvm-git-logs] scummvm master -> 4f5a851a3329e5254b519e5862762a40d012befb

sev- sev at scummvm.org
Sat Aug 1 21:03:00 UTC 2020


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
4f5a851a33 DIRECTOR: LINGO: Implement c_itemOf


Commit: 4f5a851a3329e5254b519e5862762a40d012befb
    https://github.com/scummvm/scummvm/commit/4f5a851a3329e5254b519e5862762a40d012befb
Author: Roland van Laar (roland at rolandvanlaar.nl)
Date: 2020-08-01T23:02:56+02:00

Commit Message:
DIRECTOR: LINGO: Implement c_itemOf

c_itemOf returns the specified item in a chunk expression.
A chunk expression is any sequence of characters delimited by
commas. It's 1 indexed in director.

Edge cases where index is:
  < 1: return the whole chunkExpression and
  > number of chunks: return ""

Changed paths:
    engines/director/lingo/lingo-code.cpp
    engines/director/lingo/tests/lists.lingo


diff --git a/engines/director/lingo/lingo-code.cpp b/engines/director/lingo/lingo-code.cpp
index ac0c1b0301..ef370b3b6e 100644
--- a/engines/director/lingo/lingo-code.cpp
+++ b/engines/director/lingo/lingo-code.cpp
@@ -1091,12 +1091,46 @@ void LC::c_charToOf() {
 }
 
 void LC::c_itemOf() {
-	Datum d2 = g_lingo->pop();
-	Datum d1 = g_lingo->pop();
 
-	warning("STUB: LC::c_itemOf(): %d %d", d1.u.i, d2.u.i);
+	Datum d2 = g_lingo->pop(); // chunkExpression
+	Datum d1 = g_lingo->pop(); // index
+
+	char delimiter = g_lingo->_itemDelimiter;
 
-	g_lingo->push(d1);
+	if ((d1.type != INT && d1.type != FLOAT) ||  d2.type != STRING) {
+		warning("LC::c_itemOf(): Called with wrong data types: %s and %s", d1.type2str(), d2.type2str());
+		g_lingo->push(Datum(""));
+		return;
+	}
+
+	int index = d1.asInt();
+
+	if (index < 1) {
+		// returns the input string
+		g_lingo->push(d2);
+		return;
+	}
+	Common::String chunkExpr = *d2.u.s;
+	uint startPos = 0;
+
+	while (index-- > 1) {
+		startPos = chunkExpr.find(delimiter, startPos);
+		if (startPos == Common::String::npos)
+			break;
+		startPos++;  // skipping comma
+	}
+
+	Datum res;
+	if (startPos == Common::String::npos) {
+		res = Datum("");
+	} else {
+		uint endPos = chunkExpr.find(delimiter, startPos);
+		if (endPos == Common::String::npos)
+			endPos = chunkExpr.size();
+		res = Datum(chunkExpr.substr(startPos, endPos - startPos));
+	}
+
+	g_lingo->push(res);
 }
 
 void LC::c_itemToOf() {
diff --git a/engines/director/lingo/tests/lists.lingo b/engines/director/lingo/tests/lists.lingo
index d695b5381a..c6389a0cad 100644
--- a/engines/director/lingo/tests/lists.lingo
+++ b/engines/director/lingo/tests/lists.lingo
@@ -46,3 +46,31 @@ set res to a = machinery
 set res to a >= machinery
 set res to machinery = a
 set res to machinery >= a
+
+-- itemOf
+set string_array to "one,, three, four"
+set res to item 2 of string_array
+scummvmAssert(res="")
+set res to item 3 of string_array
+scummvmAssert(res=" three")
+set res to item 4 of string_array
+scummvmAssert(res=" four")
+
+-- itemOf check for float
+set res to item 3.4 of string_array
+scummvmAssert(res=" three")
+
+-- itemOf out of bounds checks
+set res to item 5 of string_array
+scummvmAssert(res="")
+
+set res to item -1 of string_array
+scummvmAssert(res=string_array)
+
+-- itemOf: test delimiter
+set save = the itemDelimiter
+set the itemDelimiter = ":"
+set delim_array to "one: two: three: four"
+set res to item 3 of delim_array
+scummvmAssert(res=" three")
+set the itemDelimiter = save




More information about the Scummvm-git-logs mailing list