VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/string/strversion.cpp

Last change on this file was 98103, checked in by vboxsync, 17 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 7.2 KB
RevLine 
[1]1/* $Id: strversion.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
[24662]3 * IPRT - Version String Parsing.
[1]4 */
5
6/*
[98103]7 * Copyright (C) 2009-2023 Oracle and/or its affiliates.
[1]8 *
[96407]9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
[5999]11 *
[96407]12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
[5999]25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
[96407]27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
[5999]29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
[96407]33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
[1]35 */
36
37
[57358]38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
[1]41#include <iprt/string.h>
[21337]42#include "internal/iprt.h"
43
[24658]44#include <iprt/assert.h>
[25005]45#include <iprt/ctype.h>
[76452]46#include <iprt/errcore.h>
[1]47
48
[57358]49/*********************************************************************************************************************************
50* Defined Constants And Macros *
51*********************************************************************************************************************************/
[25005]52#define RTSTRVER_IS_PUNCTUACTION(ch) \
53 ( (ch) == '_' || (ch) == '-' || (ch) == '+' || RT_C_IS_PUNCT(ch) )
[1]54
[24893]55
[25005]56/**
57 * Parses a out the next block from a version string.
58 *
59 * @returns true if numeric, false if not.
60 * @param ppszVer The string cursor, IN/OUT.
[25029]61 * @param pi32Value Where to return the value if numeric.
[25005]62 * @param pcchBlock Where to return the block length.
63 */
[25029]64static bool rtStrVersionParseBlock(const char **ppszVer, int32_t *pi32Value, size_t *pcchBlock)
[24893]65{
[25005]66 const char *psz = *ppszVer;
67
[35076]68 /*
69 * Check for end-of-string.
70 */
[25005]71 if (!*psz)
[24893]72 {
[25029]73 *pi32Value = 0;
[25005]74 *pcchBlock = 0;
75 return false;
[24893]76 }
77
[35076]78 /*
79 * Try convert the block to a number the simple way.
80 */
81 char ch;
[25005]82 bool fNumeric = RT_C_IS_DIGIT(*psz);
83 if (fNumeric)
[24893]84 {
[25005]85 do
[35076]86 ch = *++psz;
87 while (ch && RT_C_IS_DIGIT(ch));
[24893]88
[25031]89 int rc = RTStrToInt32Ex(*ppszVer, NULL, 10, pi32Value);
[25005]90 if (RT_FAILURE(rc) || rc == VWRN_NUMBER_TOO_BIG)
[24893]91 {
[25031]92 AssertRC(rc);
[25005]93 fNumeric = false;
[25029]94 *pi32Value = 0;
[24893]95 }
96 }
97 else
[25005]98 {
[35076]99 /*
100 * Find the end of the current string. Make a special case for SVN
101 * revision numbers that immediately follows a release tag string.
102 */
[25005]103 do
[35076]104 ch = *++psz;
105 while ( ch
106 && !RT_C_IS_DIGIT(ch)
107 && !RTSTRVER_IS_PUNCTUACTION(ch));
108
[25031]109 size_t cchBlock = psz - *ppszVer;
[35076]110 if ( cchBlock > 1
111 && psz[-1] == 'r'
112 && RT_C_IS_DIGIT(*psz))
113 {
114 psz--;
115 cchBlock--;
116 }
[25029]117
[35076]118
119 /*
120 * Translate standard pre release terms to negative values.
121 */
122 static const struct
123 {
124 size_t cch;
125 const char *psz;
126 int32_t iValue;
127 } s_aTerms[] =
128 {
129 { 2, "RC", -100000 },
130 { 3, "PRE", -200000 },
131 { 5, "GAMMA", -300000 },
132 { 4, "BETA", -400000 },
133 { 5, "ALPHA", -500000 }
134 };
135
136 int32_t iVal1 = 0;
137 for (unsigned i = 0; i < RT_ELEMENTS(s_aTerms); i++)
138 if ( cchBlock == s_aTerms[i].cch
139 && !RTStrNCmp(s_aTerms[i].psz, *ppszVer, cchBlock))
140 {
141 iVal1 = s_aTerms[i].iValue;
142 break;
143 }
[25031]144 if (iVal1 != 0)
[25029]145 {
[35076]146 /*
147 * Does the prelease term have a trailing number?
148 * Add it assuming BETA == BETA1.
149 */
[25031]150 if (RT_C_IS_DIGIT(*psz))
151 {
152 const char *psz2 = psz;
153 do
[35076]154 ch = *++psz;
155 while ( ch
156 && RT_C_IS_DIGIT(ch)
157 && !RTSTRVER_IS_PUNCTUACTION(ch));
[25031]158
159 int rc = RTStrToInt32Ex(psz2, NULL, 10, pi32Value);
160 if (RT_SUCCESS(rc) && rc != VWRN_NUMBER_TOO_BIG && *pi32Value)
161 iVal1 += *pi32Value - 1;
162 else
[26251]163 {
[25031]164 AssertRC(rc);
165 psz = psz2;
166 }
167 }
168 fNumeric = true;
[25029]169 }
[25031]170 *pi32Value = iVal1;
[25005]171 }
[25031]172 *pcchBlock = psz - *ppszVer;
[24893]173
[35076]174 /*
175 * Skip trailing punctuation.
176 */
[25005]177 if (RTSTRVER_IS_PUNCTUACTION(*psz))
178 psz++;
179 *ppszVer = psz;
180
181 return fNumeric;
[24893]182}
183
184
[25005]185RTDECL(int) RTStrVersionCompare(const char *pszVer1, const char *pszVer2)
[24639]186{
[24893]187 AssertPtr(pszVer1);
188 AssertPtr(pszVer2);
[1]189
[25005]190 /*
191 * Do a parallel parse of the strings.
192 */
193 while (*pszVer1 || *pszVer2)
194 {
195 const char *pszBlock1 = pszVer1;
196 size_t cchBlock1;
[25029]197 int32_t iVal1;
198 bool fNumeric1 = rtStrVersionParseBlock(&pszVer1, &iVal1, &cchBlock1);
[24639]199
[25005]200 const char *pszBlock2 = pszVer2;
201 size_t cchBlock2;
[25029]202 int32_t iVal2;
203 bool fNumeric2 = rtStrVersionParseBlock(&pszVer2, &iVal2, &cchBlock2);
[25005]204
205 if (fNumeric1 && fNumeric2)
[24639]206 {
[25029]207 if (iVal1 != iVal2)
208 return iVal1 < iVal2 ? -1 : 1;
[24639]209 }
[25031]210 else if ( fNumeric1 != fNumeric2
[26251]211 && ( fNumeric1
[25031]212 ? iVal1 == 0 && cchBlock2 == 0
213 : iVal2 == 0 && cchBlock1 == 0)
[25005]214 )
[24639]215 {
[25031]216 /*else: 1.0 == 1.0.0.0.0. */;
[24639]217 }
[25031]218 else if ( fNumeric1 != fNumeric2
219 && (fNumeric1 ? iVal1 : iVal2) < 0)
[26251]220 {
[25031]221 /* Pre-release indicators are smaller than all other strings. */
222 return fNumeric1 ? -1 : 1;
223 }
[25005]224 else
[24893]225 {
[25005]226 int iDiff = RTStrNICmp(pszBlock1, pszBlock2, RT_MIN(cchBlock1, cchBlock2));
227 if (!iDiff && cchBlock1 != cchBlock2)
228 iDiff = cchBlock1 < cchBlock2 ? -1 : 1;
229 if (iDiff)
[25014]230 return iDiff < 0 ? -1 : 1;
[24893]231 }
[24639]232 }
[25014]233 return 0;
[24639]234}
[24893]235RT_EXPORT_SYMBOL(RTStrVersionCompare);
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use