KiCad PCB EDA Suite
Loading...
Searching...
No Matches
version_compare.cpp
Go to the documentation of this file.
1/*
2* This program source code file is part of KiCad, a free EDA CAD application.
3*
4* Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
5*
6* This program is free software: you can redistribute it and/or modify it
7* under the terms of the GNU General Public License as published by the
8* Free Software Foundation, either version 3 of the License, or (at your
9* option) any later version.
10*
11* This program is distributed in the hope that it will be useful, but
12* WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14* General Public License for more details.
15*
16* You should have received a copy of the GNU General Public License along
17* with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#include <charconv>
22
23const std::vector<int> parseVersionString( const std::string& versionString )
24{
25 std::vector<int> versionComponents;
26 auto it = versionString.begin();
27 auto end = versionString.end();
28 const char* end_char = versionString.data() + versionString.size();
29 while( it != end )
30 {
31 versionComponents.emplace_back( 0 );
32
33 // On error, the last element will be 0
34 auto result = std::from_chars( &( *it ), end_char, versionComponents.back() );
35
36 it += std::distance( &( *it ), result.ptr ); // Update the iterator
37 if( it != end && *it == '.' )
38 {
39 ++it; // Skip the dot separator
40 }
41 }
42
43 return versionComponents;
44}
45
46
47bool compareVersionStrings( const std::string& aVersionStr1, const std::string& aVersionStr2 )
48{
49 std::vector<int> ver1 = parseVersionString( aVersionStr1 );
50 std::vector<int> ver2 = parseVersionString( aVersionStr2 );
51 std::size_t len = std::min( ver1.size(), ver2.size() );
52
53 // Compare each component
54 for( size_t ii = 0; ii < len; ++ii )
55 {
56 if( ver1[ii] < ver2[ii] )
57 {
58 return true; // aVersionStr1 < aVersionStr2
59 }
60 else if( ver1[ii] > ver2[ii] )
61 {
62 return false; // aVersionStr1 > aVersionStr2
63 }
64 }
65
66 return ver1.size() >= ver2.size();
67}
bool compareVersionStrings(const std::string &aVersionStr1, const std::string &aVersionStr2)
Compare two version strings of the form "major.minor.patch.build".
const std::vector< int > parseVersionString(const std::string &versionString)
Parse a version string into a vector of integers.