开发者

How to find whether a string is guid in c++

How to find whether a string is guid in native c++? a code sample would help greatly开发者_开发知识库


If you need to do it by hand (information from wikipedia):

  1. Check the length (36, including hyphens)
  2. Check that the hyphens are at the expected positions (9-14-19-24)
  3. Check that all other characters are hexadecimal (isxdigit)


You could use a regex to see if it complies with GUID format.


Try the following code - could help.

_bstr_t sGuid( _T("guid to validate") );
GUID guid;

if( SUCCEEDED( ::CLSIDFromString( sGuid, &guid ) )
{
  // Guid string is valid
}


Here's a faster, native C++ version without regular expressions or scanf()-like calls.

#include <cctype>
#include <string>

using namespace std;

bool isCanonicalUUID(const string &s) {
    // Does it match the format 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'?

    if (s.length() != 36) return false;
    if (s[8] != '-' || s[13] != '-' ||
        s[18] != '-' || s[23] != '-')
        return false;

   for (int i = 0; i < s.length(); i++) {
       if (i == 8 || i == 13 || i == 18 || i == 23) continue;
       if (isspace(s[i])) return false;
       if (!isxdigit(s[i])) return true;
    }
    return true;
}

bool isMicrosoftUUID(const string &s) {
    // Does it match the format '{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}'?

    if (s.length() != 38) return false;
    if (s[0] != '{' || s[37] != '}') return false;
    if (s[10] != '-' || s[15] != '-' ||
        s[20] != '-' || s[25] != '-')
        return false;

    for (int i = 1; i < s.length()-1; i++) {
        if (i == 10 || i == 15 || i == 20 || i == 25) continue;
        if (isspace(s[i])) return false;
        if (!isxdigit(s[i])) return true;
    }
   return true;
}

bool isUUID(const string &s) {
   return isCannonicalUUID(s) || isMicrosoftUUID(s);
}


This method leverages the scanf parser and works also in plain C:

#include <stdio.h>
#include <string.h>
int validateUUID(const char *candidate)
{
    int tmp;
    const char *s = candidate;
    while (*s)
        if (isspace(*s++))
            return 0;
    return s - candidate == 36
        && sscanf(candidate, "%4x%4x-%4x-%4x-%4x-%4x%4x%4x%c",
        &tmp, &tmp, &tmp, &tmp, &tmp, &tmp, &tmp, &tmp, &tmp) == 8;
}

Test with e.g.:

int main(int argc, char *argv[])
{
    if (argc > 1)
        puts(validateUUID(argv[1]) ? "OK" : "Invalid");
}


Here's a code example in case you still need one :P

#include <ctype.h>
using namespace std;
bool isUUID(string uuid)
{
    /* 
     * Check if the provided uuid is valid.
     * 1. The length of uuids should always be 36.
     * 2. Hyphens are expected at positions {9, 14, 19, 24}.
     * 3. The rest characters should be simple xdigits.
     */
    int hyphens[4] = {9, 14, 19, 24};
    if (uuid.length() != 36)
    {
        return false;//Oops. The lenth doesn't match.
    }
    for (int i = 0, counter = 0; i < 36; i ++)
    {
        char var = uuid[i];
        if (i == hyphens[counter] - 1)// Check if a hyphen is expected here.
        {
            // Yep. We need a hyphen here. 
            if (var != '-')
            {
                return false;// Oops. The character is not a hyphen.
            }
            else
            {
                counter++;// Move on to the next expected hyphen position.
            }
        }
        else
        {
            // Nope. The character here should be a simple xdigit
            if (isxdigit(var) == false)
            {
                return false;// Oops. The current character is not a hyphen.
            }
        }
    }
    return true;// Seen'em all!
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜