diff options
author | AndrewCEmil <andrew.emil@10gen.com> | 2013-04-09 16:58:21 -0700 |
---|---|---|
committer | AndrewCEmil <andrew.emil@10gen.com> | 2013-05-29 11:47:17 -0700 |
commit | 135f0fb362efd4be85b422cf386cc26c2cd5d2d5 (patch) | |
tree | a60fa72e02c7b128387a6c4227087d9cda2b799d /src/mongo | |
parent | 088557a3cf2b1ed0b6656e0fd9812ca5501e723b (diff) | |
download | mongo-135f0fb362efd4be85b422cf386cc26c2cd5d2d5.tar.gz |
SERVER-7324: added templated toHexTemp function to hex.h/hex.cpp
-replaced incorrect calls of toHex
-added test of integerToHex to stringutils_test.cpp
-changed building of bsondemo
-included hex.cpp to stringutils library package
Diffstat (limited to 'src/mongo')
-rw-r--r-- | src/mongo/SConscript | 2 | ||||
-rw-r--r-- | src/mongo/bson/bson-inl.h | 2 | ||||
-rw-r--r-- | src/mongo/db/pdfile.cpp | 4 | ||||
-rw-r--r-- | src/mongo/util/hex.cpp | 54 | ||||
-rw-r--r-- | src/mongo/util/hex.h | 2 | ||||
-rw-r--r-- | src/mongo/util/stringutils_test.cpp | 16 |
6 files changed, 76 insertions, 4 deletions
diff --git a/src/mongo/SConscript b/src/mongo/SConscript index 58e3116e57a..2abb1018dcc 100644 --- a/src/mongo/SConscript +++ b/src/mongo/SConscript @@ -50,7 +50,7 @@ env.StaticLibrary('foundation', env.CppUnitTest('text_test', 'util/text_test.cpp', LIBDEPS=['foundation']) -env.StaticLibrary('stringutils', ['util/stringutils.cpp', 'util/base64.cpp',]) +env.StaticLibrary('stringutils', ['util/stringutils.cpp', 'util/base64.cpp', 'util/hex.cpp']) env.StaticLibrary('md5', [ 'util/md5.cpp' diff --git a/src/mongo/bson/bson-inl.h b/src/mongo/bson/bson-inl.h index 40491bea09f..70bd7f714f0 100644 --- a/src/mongo/bson/bson-inl.h +++ b/src/mongo/bson/bson-inl.h @@ -208,7 +208,7 @@ dodouble: inline NOINLINE_DECL void BSONObj::_assertInvalid() const { StringBuilder ss; int os = objsize(); - ss << "BSONObj size: " << os << " (0x" << toHex( &os, 4 ) << ") is invalid. " + ss << "BSONObj size: " << os << " (0x" << integerToHex( os ) << ") is invalid. " << "Size must be between 0 and " << BSONObjMaxInternalSize << "(" << ( BSONObjMaxInternalSize/(1024*1024) ) << "MB)"; try { diff --git a/src/mongo/db/pdfile.cpp b/src/mongo/db/pdfile.cpp index 3314cb91921..0545f973e51 100644 --- a/src/mongo/db/pdfile.cpp +++ b/src/mongo/db/pdfile.cpp @@ -694,7 +694,7 @@ namespace mongo { LOG(3) << "_reuse extent was:" << nsDiagnostic.toString() << " now:" << nsname << endl; if (magic != extentSignature) { StringBuilder sb; - sb << "bad extent signature " << toHex(&magic, 4) + sb << "bad extent signature " << integerToHex(magic) << " for namespace '" << nsDiagnostic.toString() << "' found in Extent::_reuse"; msgasserted(10360, sb.str()); @@ -741,7 +741,7 @@ namespace mongo { if (magic != extentSignature) { if (errors) { StringBuilder sb; - sb << "bad extent signature " << toHex(&magic, 4) + sb << "bad extent signature " << integerToHex(magic) << " in extent " << diskLoc.toString(); *errors << sb.str(); } diff --git a/src/mongo/util/hex.cpp b/src/mongo/util/hex.cpp new file mode 100644 index 00000000000..ef8cc64a10b --- /dev/null +++ b/src/mongo/util/hex.cpp @@ -0,0 +1,54 @@ +// util/hex.cpp + +/* Copyright 2013 10gen Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <string> +#include "mongo/util/hex.h" + +namespace mongo { + + template<typename T> + std::string integerToHexDef(T inInt) { + if(!inInt) + return "0"; + + static const char hexchars[] = "0123456789ABCDEF"; + + static const size_t outbufSize = sizeof(T) * 2 + 1; + char outbuf[outbufSize]; + outbuf[outbufSize - 1] = '\0'; + + char c; + int lastSeenNumber = 0; + for (int j = int(outbufSize) - 2; j >= 0; j--) { + c = hexchars[inInt & 0xF]; + if(c != '0') + lastSeenNumber = j; + outbuf[j] = c; + inInt = inInt >> 4; + } + char *bufPtr = outbuf; + bufPtr += lastSeenNumber; + + return std::string(bufPtr); + } + + template<> std::string integerToHex<int>(int val) { return integerToHexDef(val); } + template<> std::string integerToHex<unsigned int>(unsigned int val) { + return integerToHexDef(val); } + template<> std::string integerToHex<long>(long val) { return integerToHexDef(val); } + template<> std::string integerToHex<long long>(long long val) { return integerToHexDef(val); } +} diff --git a/src/mongo/util/hex.h b/src/mongo/util/hex.h index a39a83e8c3e..9787784f445 100644 --- a/src/mongo/util/hex.h +++ b/src/mongo/util/hex.h @@ -57,6 +57,8 @@ namespace mongo { return out.str(); } + template <typename T> std::string integerToHex(T val); + inline std::string toHexLower(const void* inRaw, int len) { static const char hexchars[] = "0123456789abcdef"; diff --git a/src/mongo/util/stringutils_test.cpp b/src/mongo/util/stringutils_test.cpp index a17a66423f9..c7fb0796798 100644 --- a/src/mongo/util/stringutils_test.cpp +++ b/src/mongo/util/stringutils_test.cpp @@ -19,6 +19,7 @@ #include "mongo/bson/util/misc.h" #include "mongo/util/stringutils.h" +#include "mongo/util/hex.h" namespace mongo { @@ -170,4 +171,19 @@ namespace mongo { assertCmp( 0, StringData("0001", 3), StringData("0000", 3), false ); } + + TEST( IntegerToHex, VariousConversions ) { + ASSERT_EQUALS(std::string("0"), integerToHex(0)); + ASSERT_EQUALS(std::string("1"), integerToHex(1)); + ASSERT_EQUALS(std::string("1337"), integerToHex(0x1337)); + ASSERT_EQUALS(std::string("FFFFD499"), integerToHex(-11111)); + ASSERT_EQUALS(std::string("F1FE60C4"), integerToHex(-234987324)); + ASSERT_EQUALS(std::string("C0DE4F00D"), integerToHex(0xc0de4f00d)); + ASSERT_EQUALS(std::string("80000000"), integerToHex(std::numeric_limits<int>::min())); + ASSERT_EQUALS(std::string("7FFFFFFF"), integerToHex(std::numeric_limits<int>::max())); + ASSERT_EQUALS(std::string("7FFFFFFFFFFFFFFF"), + integerToHex(std::numeric_limits<long long>::max())); + ASSERT_EQUALS(std::string("8000000000000000"), + integerToHex(std::numeric_limits<long long>::min())); + } } |