Skip to content

Commit 035da02

Browse files
committed
List/Matrix: Trim readable list and matrix input.
1 parent 3f128b9 commit 035da02

4 files changed

Lines changed: 16 additions & 3 deletions

File tree

scripts/cli_smoke_tests.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ cmp -s "$TMP_DIR/one_real_raw.bin" "$TMP_DIR/one_real_roundtrip.bin"
1515
printf '1' > "$TMP_DIR/one_real.txt"
1616
"$CLI" -i "$TMP_DIR/one_real.txt" -j readable -o "$TMP_DIR/one_real_83.83n" -k varfile -t Real -m 83 -n A
1717
"$CLI" -i "$TMP_DIR/one_real.txt" -j readable -o "$TMP_DIR/one_real_default.8xn" -k varfile -t Real
18+
19+
printf '{1,2}\n' > "$TMP_DIR/list_with_newline.txt"
20+
"$CLI" -i "$TMP_DIR/list_with_newline.txt" -j readable -o "$TMP_DIR/list_with_newline.8xl" -k varfile -t RealList -n L1
21+
22+
printf '[[1,2][3,4]]\n' > "$TMP_DIR/matrix_with_newline.txt"
23+
"$CLI" -i "$TMP_DIR/matrix_with_newline.txt" -j readable -o "$TMP_DIR/matrix_with_newline.8xm" -k varfile -t Matrix -n A

src/TypeHandlers/TH_GenericList.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace tivars::TypeHandlers
2828
throw std::invalid_argument("Invalid type for given string");
2929
}
3030

31-
const std::string inner = trim(str, "{}");
31+
const std::string inner = trim(trim(str), "{}");
3232
if (inner.empty())
3333
{
3434
return {0, 0};

src/TypeHandlers/TH_Matrix.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ namespace tivars::TypeHandlers
2121

2222
data_t data(2); // reserve 2 bytes for size fields
2323

24-
if (str.length() < 5 || str.substr(0, 2) != "[[" || str.substr(str.length()-2, 2) != "]]")
24+
const std::string trimmed = trim(str);
25+
if (trimmed.length() < 5 || trimmed.substr(0, 2) != "[[" || trimmed.substr(trimmed.length()-2, 2) != "]]")
2526
{
2627
throw std::invalid_argument("Invalid input string. Needs to be a valid matrix");
2728
}
2829

2930
std::vector<std::vector<std::string>> matrix;
3031

31-
std::vector<std::string> rows = explode(str.substr(2, str.length() - 4), "][");
32+
std::vector<std::string> rows = explode(trimmed.substr(2, trimmed.length() - 4), "][");
3233
const size_t rowCount = rows.size();
3334
matrix.resize(rowCount);
3435

tests.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,8 @@ End)";
14521452
const std::string content = "{9,0,0.5,-6e-8}";
14531453
testRealList.setContentFromString(content);
14541454
assert(testRealList.getReadableContent() == content);
1455+
testRealList.setContentFromString("{1,2}\n");
1456+
assert(testRealList.getReadableContent() == "{1,2}");
14551457
}
14561458

14571459
{
@@ -1462,6 +1464,10 @@ End)";
14621464
testStandardMatrix.setContentFromString("[[1,2,3][4,5,6][-7.5,-8,-9][1,2,3][4,5,6][-0.002,-8,-9]]");
14631465
cout << testStandardMatrix.getReadableContent() << "\n";
14641466
testStandardMatrix.saveVarToFile("testData", "Matrix_new");
1467+
1468+
TIVarFile matrixWithNewline = TIVarFile::createNew("Matrix", "A");
1469+
matrixWithNewline.setContentFromString("[[1,2][3,4]]\n");
1470+
assert(matrixWithNewline.getReadableContent() == "[[1,2][3,4]]");
14651471
}
14661472

14671473
#ifndef __EMSCRIPTEN__

0 commit comments

Comments
 (0)