Skip to content

Commit bf9f836

Browse files
committed
add varname checks by types in TIVarFile::var_entry_t::setVarName
1 parent ef98dd9 commit bf9f836

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

src/TIVarFile.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -255,21 +255,32 @@ namespace tivars
255255
// Here we handle various theta chars. Note thata in TI-ASCII, theta is at 0x5B which is "[" in ASCII.
256256
newName = std::regex_replace(newName, std::regex("(\u03b8|\u0398|\u03F4|\u1DBF)"), "[");
257257

258-
// TODO: handle names according to _type
259-
newName = std::regex_replace(newName, std::regex("[^[a-zA-Z0-9]"), "");
260-
if (newName.length() > sizeof(varname) || newName.empty() || is_numeric(newName.substr(0, 1)))
261-
{
262-
throw std::invalid_argument("Invalid name given. 8 chars (A-Z, 0-9, θ) max, starting by a letter or θ.");
263-
}
264-
265-
// TODO: again, properly handle names according to _type... (needs to be implemented by each type)
266-
// Quick hack for now...
267258
const auto& typeName = _type.getName();
259+
const auto& typeId = _type.getId();
260+
268261
if (typeName == "Real" || typeName == "Complex" || typeName == "Program" || typeName == "ProtectedProgram")
269262
{
270263
for (auto & c: newName) c = (char) toupper(c);
271264
}
272265

266+
if (((typeName == "Real" || typeName == "Complex" || (typeId >= 0x1B && typeId <= 0x21)) && !regex_match(newName, std::regex(R"(^[[A-Z]$)"))) // exact types
267+
|| ((typeName == "RealList" || typeName == "ComplexList") && !regex_match(newName, std::regex(R"(^\x5D([\x00-\x05])?|([[0-9A-F]{0,5})$)")))
268+
|| ((typeName == "Program" || typeName == "ProtectedProgram") && !regex_match(newName, std::regex(R"(^[[A-Z][[A-Z0-9]{0,7}$)")))
269+
|| (typeName == "Equation" && !regex_match(newName, std::regex(R"(^\x5E[\x00-\xFF]?$)")))
270+
|| (typeName == "Matrix" && !regex_match(newName, std::regex(R"(^\x5C[\x00-\x09]?$)")))
271+
|| (typeName == "String" && !regex_match(newName, std::regex(R"(^\xAA[\x00-\x09]?$)")))
272+
|| (typeName == "Pic" && !regex_match(newName, std::regex(R"(^\x60[\x00-\x09]?$)")))
273+
|| (typeName == "Image" && !regex_match(newName, std::regex(R"(^\x3C[\x00-\x09]?$)")))
274+
|| (typeName == "GraphDataBase" && !regex_match(newName, std::regex(R"(^\x61[\x00-\x09]?$)"))))
275+
{
276+
newName = "";
277+
}
278+
279+
if (newName.length() > sizeof(varname) || newName.empty())
280+
{
281+
throw std::invalid_argument("Invalid name given. 8 chars (A-Z, 0-9, θ) max, starting by a letter or θ.");
282+
}
283+
273284
newName = str_pad(newName, sizeof(varname), "\0");
274285
std::copy(newName.begin(), newName.end(), varname);
275286
}

tests.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int main(int argc, char** argv)
4747
assert(TIVarType{"ExactRealPi"}.getId() == 32);
4848

4949
{
50-
TIVarFile testReal = TIVarFile::createNew("Real");
50+
TIVarFile testReal = TIVarFile::createNew("Real", "A");
5151
for (const auto& str : { "0.0", "0", "+0.0", "+0" })
5252
{
5353
testReal.setContentFromString(str);
@@ -483,6 +483,14 @@ End)";
483483
testRealList.saveVarToFile("testData", "RealList_new");
484484
}
485485

486+
{
487+
TIVarFile testRealList = TIVarFile::createNew("RealList", "\x5D\x00"); // L₁
488+
assert(testRealList.getRawContent().empty());
489+
const std::string content = "{9,0,0.5,-6e-8}";
490+
testRealList.setContentFromString(content);
491+
assert(testRealList.getReadableContent() == content);
492+
}
493+
486494
{
487495
TIVarFile testStandardMatrix = TIVarFile::loadFromFile("testData/Matrix_3x3_standard.8xm");
488496
cout << "Before: " << testStandardMatrix.getReadableContent() << "\n Now: ";
@@ -640,7 +648,7 @@ End)";
640648
std::stringstream gdbJSON;
641649
gdbJSON << jsonFile.rdbuf();
642650

643-
TIVarFile paramGDB = TIVarFile::createNew("GraphDataBase", "a");
651+
TIVarFile paramGDB = TIVarFile::createNew("GraphDataBase", "\x61\x00"); // GDB0
644652
paramGDB.setContentFromString(gdbJSON.str());
645653
cout << "paramGDB.getReadableContent() : " << paramGDB.getReadableContent() << endl;
646654
paramGDB.saveVarToFile("testData", "GraphDataBase_Param_new");

0 commit comments

Comments
 (0)