Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions source/MaterialXCore/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,28 @@ string incrementName(const string& name)
size_t split = name.length();
while (split > 0)
{
if (!isdigit(name[split - 1]))
if (!isdigit((unsigned char) name[split - 1]))
break;
split--;
}

if (split < name.length())
{
// Increment the numeric suffix as a string of digits, so that suffixes
// of any length are supported without integer overflow. Leading zeros
// are dropped, keeping at least one digit.
string prefix = name.substr(0, split);
string suffix = name.substr(split, name.length());
return prefix + std::to_string(std::stoi(suffix) + 1);
string suffix = name.substr(std::min(name.find_first_not_of('0', split), name.length() - 1));
size_t pos = suffix.length();
while (pos > 0 && suffix[pos - 1] == '9')
{
suffix[--pos] = '0';
}
if (pos > 0)
suffix[pos - 1]++;
else
suffix.insert(suffix.begin(), '1');
return prefix + suffix;
}
return name + "2";
}
Expand Down
3 changes: 3 additions & 0 deletions source/MaterialXTest/MaterialXCore/CoreUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ TEST_CASE("String utilities", "[coreutil]")
REQUIRE(mx::incrementName("testName99") == "testName100");
REQUIRE(mx::incrementName("1testName1") == "1testName2");
REQUIRE(mx::incrementName("41") == "42");
REQUIRE(mx::incrementName("testName007") == "testName8");
REQUIRE(mx::incrementName("testName2147483647") == "testName2147483648");
REQUIRE(mx::incrementName("testName99999999999999999999") == "testName100000000000000000000");

REQUIRE(mx::splitString("robot1, robot2", ", ") == (std::vector<std::string>{"robot1", "robot2"}));
REQUIRE(mx::splitString("[one...two...three]", "[.]") == (std::vector<std::string>{"one", "two", "three"}));
Expand Down
Loading