From: CryptoManiac Date: Sun, 10 Apr 2016 13:03:15 +0000 (+0300) Subject: Rewrite ParseHex using istringstream; X-Git-Url: https://git.novaco.in/?p=novacoin.git;a=commitdiff_plain;h=65c601e3aa895b0dd357513ef02314b86fc66173 Rewrite ParseHex using istringstream; Turn phexdigit into IsHex's local variable. --- diff --git a/src/util.cpp b/src/util.cpp index 13ddce4..3cb5300 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -437,27 +437,28 @@ bool ParseMoney(const char* pszIn, int64_t& nRet) return true; } - -static const signed char phexdigit[256] = -{ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1, - -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, }; - bool IsHex(const string& str) { + static const signed char phexdigit[256] = + { + -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1, + -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + }; + for(unsigned char c : str) { if (phexdigit[c] < 0) @@ -466,30 +467,19 @@ bool IsHex(const string& str) return (str.size() > 0) && (str.size()%2 == 0); } -vector ParseHex(const char* psz) +vector ParseHex(const string& str) { - // convert hex dump to vector - vector vch; - for ( ; ; ) + auto nLen = str.length(); + vector vchOut; + for(size_t nPos = 0; nPos < nLen; nPos += 2) { - while (isspace(*psz)) - psz++; - signed char c = phexdigit[(unsigned char)*psz++]; - if (c == (signed char)-1) - break; - unsigned char n = (c << 4); - c = phexdigit[(unsigned char)*psz++]; - if (c == (signed char)-1) - break; - n |= c; - vch.push_back(n); + istringstream strm(str.substr(nPos, 2)); + uint8_t x; + strm >> hex >> x; + vchOut.push_back(x); } - return vch; -} -vector ParseHex(const string& str) -{ - return ParseHex(str.c_str()); + return vchOut; } static void InterpretNegativeSetting(string name, map& mapSettingsRet) diff --git a/src/util.h b/src/util.h index 507c54d..f22f8a9 100644 --- a/src/util.h +++ b/src/util.h @@ -198,8 +198,7 @@ void ParseString(const std::string& str, char c, std::vector& v); std::string FormatMoney(int64_t n, bool fPlus=false); bool ParseMoney(const std::string& str, int64_t& nRet); bool ParseMoney(const char* pszIn, int64_t& nRet); -std::vector ParseHex(const char* psz); -std::vector ParseHex(const std::string& str); +std::vector ParseHex(const std::string& str); bool IsHex(const std::string& str); std::vector DecodeBase64(const char* p, bool* pfInvalid = NULL); std::string DecodeBase64(const std::string& str); @@ -250,10 +249,6 @@ void runCommand(std::string strCommand); - - - - inline std::string i64tostr(int64_t n) { return strprintf("%" PRId64, n);