Have SetHex return validity and add strict option.

This commit is contained in:
Arthur Britto
2012-12-29 17:37:39 -08:00
parent ab0da033c3
commit 0339aa9669

View File

@@ -219,14 +219,18 @@ public:
return strHex(begin(), size()); return strHex(begin(), size());
} }
void SetHex(const char* psz) // Allow leading whitespace.
// Allow leading "0x".
// To be valid must be '\0' terminated.
bool SetHex(const char* psz, bool bStrict=false)
{ {
// skip leading spaces // skip leading spaces
while (isspace(*psz)) if (!bStrict)
psz++; while (isspace(*psz))
psz++;
// skip 0x // skip 0x
if (psz[0] == '0' && tolower(psz[1]) == 'x') if (!bStrict && psz[0] == '0' && tolower(psz[1]) == 'x')
psz += 2; psz += 2;
// hex char to int // hex char to int
@@ -278,11 +282,13 @@ public:
: phexdigit[*pBegin++]; : phexdigit[*pBegin++];
*pOut++ = cHigh | cLow; *pOut++ = cHigh | cLow;
} }
return !*pEnd;
} }
void SetHex(const std::string& str) bool SetHex(const std::string& str, bool bStrict=false)
{ {
SetHex(str.c_str()); return SetHex(str.c_str(), bStrict);
} }
std::string ToString() const std::string ToString() const