Skip to content

Commit

Permalink
Perform prefix trim directly on BaseVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
Trenly committed Jun 18, 2024
1 parent f221d23 commit f7cfe50
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/AppInstallerCLITests/Versions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ TEST_CASE("VersionCompare", "[versions]")
RequireEqual("foo1", "bar1");
RequireLessThan("v0.0.1", "0.0.2");
RequireLessThan("v0.0.1", "v0.0.2");
RequireLessThan("1.a1", "1.b2");
RequireLessThan("1.a2", "1.b1");
RequireLessThan("alpha", "beta");
}

Expand Down
23 changes: 9 additions & 14 deletions src/AppInstallerSharedLib/Versions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ namespace AppInstaller::Utility
baseVersion = m_version.substr(s_Approximate_Greater_Than.length(), m_version.length() - s_Approximate_Greater_Than.length());
}

// If there is a digit before the split character, or no split characters exist, trim off all leading non-digit characters
size_t digitPos = baseVersion.find_first_of(AICLI_DIGIT_CHARS);
size_t splitPos = baseVersion.find_first_of(splitChars);
if (digitPos != std::string::npos && (splitPos == std::string::npos || digitPos < splitPos))
{
baseVersion.erase(0, digitPos);
}

// Then parse the base version
size_t pos = 0;

Expand All @@ -64,20 +72,7 @@ namespace AppInstaller::Utility
size_t newPos = baseVersion.find_first_of(splitChars, pos);

size_t length = (newPos == std::string::npos ? baseVersion.length() : newPos) - pos;
std::string interimPart = baseVersion.substr(pos, length);

// For the first version part, if there is a digit before the split character, trim off all leading non-digit characters
if (pos == 0)
{
size_t digitPos = interimPart.find_first_of(AICLI_DIGIT_CHARS);
if (digitPos != std::string::npos)
{
interimPart.erase(0, digitPos);
}
}
// If the part contains any leading or trailing whitespace, it will be trimmed off as part of the constructor which
// emplaces it into the m_parts vector
m_parts.emplace_back(interimPart);
m_parts.emplace_back(baseVersion.substr(pos, length));

pos += length + 1;
}
Expand Down

0 comments on commit f7cfe50

Please sign in to comment.