aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2013-11-04 05:55:37 -0800
committerWladimir J. van der Laan <laanwj@gmail.com>2013-11-04 05:55:37 -0800
commitbac72640ee4fbf32c639901229dcd714defa1baa (patch)
tree78ea27076d3d41c62d7ad873335c34a80f8343b9 /src
parent97f844dd95c54b0fe2f2a1bb006c74ff544ff125 (diff)
parentf171ec0c7d084b6bb163d1466edd814cf4dcbc93 (diff)
downloadbitcoin-bac72640ee4fbf32c639901229dcd714defa1baa.tar.xz
Merge pull request #3076 from lano1106/uint256_util
Make util phexdigit array reusable
Diffstat (limited to 'src')
-rw-r--r--src/bignum.h6
-rw-r--r--src/test/bignum_tests.cpp8
-rw-r--r--src/test/uint256_tests.cpp9
-rw-r--r--src/uint256.h15
-rw-r--r--src/util.cpp10
5 files changed, 34 insertions, 14 deletions
diff --git a/src/bignum.h b/src/bignum.h
index 582e6f1517..e9d9007d3d 100644
--- a/src/bignum.h
+++ b/src/bignum.h
@@ -347,13 +347,13 @@ public:
psz++;
// hex string to bignum
- static const signed char phexdigit[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0 };
*this = 0;
- while (isxdigit(*psz))
+ int n;
+ while ((n = HexDigit(*psz)) != -1)
{
*this <<= 4;
- int n = phexdigit[(unsigned char)*psz++];
*this += n;
+ ++psz;
}
if (fNegative)
*this = 0 - *this;
diff --git a/src/test/bignum_tests.cpp b/src/test/bignum_tests.cpp
index 196b7274f4..f16c26fd18 100644
--- a/src/test/bignum_tests.cpp
+++ b/src/test/bignum_tests.cpp
@@ -175,4 +175,12 @@ BOOST_AUTO_TEST_CASE(bignum_SetCompact)
BOOST_CHECK_EQUAL(num.GetCompact(), 0xff123456U);
}
+BOOST_AUTO_TEST_CASE(bignum_SetHex)
+{
+ std::string hexStr = "deecf97fd890808b9cc0f1b6a3e7a60b400f52710e6ad075b1340755bfa58cc9";
+ CBigNum num;
+ num.SetHex(hexStr);
+ BOOST_CHECK_EQUAL(num.GetHex(), hexStr);
+}
+
BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/test/uint256_tests.cpp b/src/test/uint256_tests.cpp
index efdc8a6aeb..e34efcc47e 100644
--- a/src/test/uint256_tests.cpp
+++ b/src/test/uint256_tests.cpp
@@ -1,6 +1,7 @@
#include <boost/test/unit_test.hpp>
#include "uint256.h"
+#include <string>
BOOST_AUTO_TEST_SUITE(uint256_tests)
@@ -15,4 +16,12 @@ BOOST_AUTO_TEST_CASE(uint256_equality)
BOOST_CHECK(num1+num2 == num3+num2);
}
+BOOST_AUTO_TEST_CASE(uint256_hex)
+{
+ std::string hexStr = "d35583ed493a5eee756931353144f558e6a9ab3ad6024a63ced7f10daf7faad9";
+ uint256 num1;
+ num1.SetHex(hexStr);
+ BOOST_CHECK(num1.GetHex() == hexStr);
+}
+
BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/uint256.h b/src/uint256.h
index 79404f1b16..b5a4950eaa 100644
--- a/src/uint256.h
+++ b/src/uint256.h
@@ -14,7 +14,12 @@
typedef long long int64;
typedef unsigned long long uint64;
+extern const signed char p_util_hexdigit[256]; // defined in util.cpp
+inline signed char HexDigit(char c)
+{
+ return p_util_hexdigit[(unsigned char)c];
+}
inline int Testuint256AdHoc(std::vector<std::string> vArg);
@@ -305,8 +310,7 @@ public:
void SetHex(const char* psz)
{
- for (int i = 0; i < WIDTH; i++)
- pn[i] = 0;
+ memset(pn,0,sizeof(pn));
// skip leading spaces
while (isspace(*psz))
@@ -317,19 +321,18 @@ public:
psz += 2;
// hex string to uint
- static const unsigned char phexdigit[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0 };
const char* pbegin = psz;
- while (phexdigit[(unsigned char)*psz] || *psz == '0')
+ while (::HexDigit(*psz) != -1)
psz++;
psz--;
unsigned char* p1 = (unsigned char*)pn;
unsigned char* pend = p1 + WIDTH * 4;
while (psz >= pbegin && p1 < pend)
{
- *p1 = phexdigit[(unsigned char)*psz--];
+ *p1 = ::HexDigit(*psz--);
if (psz >= pbegin)
{
- *p1 |= (phexdigit[(unsigned char)*psz--] << 4);
+ *p1 |= ((unsigned char)::HexDigit(*psz--) << 4);
p1++;
}
}
diff --git a/src/util.cpp b/src/util.cpp
index 085df8a7d5..539e759ddd 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -460,7 +460,7 @@ bool ParseMoney(const char* pszIn, int64& nRet)
}
-static const signed char phexdigit[256] =
+const signed char p_util_hexdigit[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,
@@ -480,9 +480,9 @@ static const signed char phexdigit[256] =
bool IsHex(const string& str)
{
- BOOST_FOREACH(unsigned char c, str)
+ BOOST_FOREACH(char c, str)
{
- if (phexdigit[c] < 0)
+ if (HexDigit(c) < 0)
return false;
}
return (str.size() > 0) && (str.size()%2 == 0);
@@ -496,11 +496,11 @@ vector<unsigned char> ParseHex(const char* psz)
{
while (isspace(*psz))
psz++;
- signed char c = phexdigit[(unsigned char)*psz++];
+ signed char c = HexDigit(*psz++);
if (c == (signed char)-1)
break;
unsigned char n = (c << 4);
- c = phexdigit[(unsigned char)*psz++];
+ c = HexDigit(*psz++);
if (c == (signed char)-1)
break;
n |= c;