[Spreadsheet] fix isValidAlias() (#18567)

This commit is contained in:
Mark Ganson TheMarkster
2024-12-23 11:18:30 -06:00
committed by GitHub
parent 2240932ef3
commit 27889739bd
2 changed files with 20 additions and 13 deletions

View File

@@ -129,20 +129,16 @@ const Cell* PropertySheet::getValueFromAlias(const std::string& alias) const
bool PropertySheet::isValidCellAddressName(const std::string& candidate)
{
static const boost::regex gen("^[A-Za-z][_A-Za-z0-9]*$");
/* Check if it matches a cell reference */
static const boost::regex e("\\${0,1}([A-Z]{1,2})\\${0,1}([0-9]{1,5})");
boost::cmatch cm;
/* Check if it matches a cell reference */
if (boost::regex_match(candidate.c_str(), cm, gen)) {
static const boost::regex e("\\${0,1}([A-Z]{1,2})\\${0,1}([0-9]{1,5})");
if (boost::regex_match(candidate.c_str(), cm, e)) {
const boost::sub_match<const char*> colstr = cm[1];
const boost::sub_match<const char*> rowstr = cm[2];
if (boost::regex_match(candidate.c_str(), cm, e)) {
const boost::sub_match<const char*> colstr = cm[1];
const boost::sub_match<const char*> rowstr = cm[2];
if (App::validRow(rowstr.str()) >= 0 && App::validColumn(colstr.str())) {
return true;
}
if (App::validRow(rowstr.str()) >= 0 && App::validColumn(colstr.str())) {
return true;
}
}
return false;
@@ -150,13 +146,19 @@ bool PropertySheet::isValidCellAddressName(const std::string& candidate)
bool PropertySheet::isValidAlias(const std::string& candidate)
{
/* Ensure it only contains allowed characters */
static const boost::regex gen("^[A-Za-z][_A-Za-z0-9]*$");
boost::cmatch cm;
if (!boost::regex_match(candidate.c_str(), cm, gen)) {
return false;
}
/* Check if it is used before */
if (getValueFromAlias(candidate)) {
return false;
}
/* check if it would be a valid cell address name, e.g. "A2" or "C3" */
/* Check if it would be a valid cell address name, e.g. "A2" or "C3" */
if (isValidCellAddressName(candidate)) {
return false;
}

View File

@@ -76,7 +76,12 @@ TEST_F(PropertySheetTest, validAliases) // NOLINT
TEST_F(PropertySheetTest, invalidAliases) // NOLINT
{
std::vector<std::string> invalidAliases {"A1", "ZZ1234", "mm"};
std::vector<std::string> invalidAliases {"A1",
"ZZ1234",
"mm",
"no spaces allowed",
"\'NoLeadingQuotes"};
for (const auto& name : invalidAliases) {
EXPECT_FALSE(propertySheet()->isValidAlias(name))
<< "\"" << name << "\" was accepted as an alias name, and should not be";