VirtualBox

Changeset 24658 in vbox for trunk/src/VBox/Runtime/common


Ignore:
Timestamp:
Nov 14, 2009 11:31:36 PM (15 years ago)
Author:
vboxsync
Message:

RTStrVersionToUInt32: bird review, things todo.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/string/strtonum.cpp

    r24645 r24658  
    3737#include "internal/iprt.h"
    3838
     39#include <iprt/assert.h>
    3940#include <iprt/ctype.h> /* needed for RT_C_IS_DIGIT */
    4041#include <iprt/err.h>
     
    107108 * @param   pszValue    Pointer to the string value.
    108109 * @param   pu32        Where to store the converted number.
     110 *
     111 * @remarks The returned value isn't really suitable for comparing two version
     112 *          strings.  Try see which result you get when converting "3.0.14" and
     113 *          "3.1.0" and comparing the values.  The way to fix this deficiency
     114 *          would be to convert the individual parts and dividing the return
     115 *          value into sections: bits 31:24 FirstNumber; 23:16 Second; 15:8
     116 *          Third; 7:0 Forth.  It would probably be a good idea to use a 64-bit
     117 *          return value instead of a 32-bit one, so there is room for revision
     118 *          number when found.
     119 *
     120 *          Actually, because of the above, the kind of API I had in mind was
     121 *          int RTStrVersionCompare(const char *pszVer1, const char *pszVer2).
     122 *          It wouldn't try convert input to numbers, just do a parallel parse.
     123 *          This would allow easy handling beta/alpha/++ indicators and any
     124 *          number of dots and dashes.
    109125 */
    110126RTDECL(int) RTStrVersionToUInt32(const char *pszVer, uint32_t *pu32)
    111127{
    112     const char *str = pszVer;
     128    const char *psz = pszVer;
    113129    AssertPtr(pu32);
    114     AssertPtr(str);
    115 
    116     char *strNew = (char*)RTMemAllocZ((strlen(pszVer) + 1) * sizeof(char));
    117     if (strNew == NULL)
     130    AssertPtr(psz);
     131
     132    char *pszNew = (char*)RTMemAllocZ((strlen(pszVer) + 1) * sizeof(char));
     133    if (pszNew == NULL)
    118134        return VERR_NO_MEMORY;
    119135
    120     int rc = VERR_NO_DIGITS;
    121     uint16_t c = 0;
    122     bool fLastInvalid = false;
    123     while (    str
    124            && *str != '\0')
     136    unsigned    i            = 0;
     137    bool        fLastInvalid = false;
     138    while (    psz
     139           && *psz != '\0')
    125140    {
    126141        if (fLastInvalid)
    127142        {
    128             if (   *str == '-'
    129                 || *str == '_')
    130             {
     143            if (   *psz == '-'
     144                || *psz == '_')
    131145                fLastInvalid = false;
    132             }
    133146        }
    134147        else
    135148        {
    136             if (RT_C_IS_DIGIT(*str))
    137             {
    138                 strNew[c++] = *str;
    139             }
    140             else if (   *str != '.'
    141                      && c == 0)
    142             {
     149            if (RT_C_IS_DIGIT(*psz))
     150                pszNew[i++] = *psz;
     151            else if (   *psz != '.'
     152                     && i == 0)
    143153                fLastInvalid = true;
    144             }
    145154        }
    146         str++;
    147     }
    148     strNew[c] = '\0';
     155        psz++;
     156    }
     157    pszNew[i] = '\0';
    149158
    150159    /* Convert final number string to number */
     160    int rc;
    151161    if (fLastInvalid)
    152162    {
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette