diff options
author | deanm@chromium.org <deanm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-22 13:13:30 +0000 |
---|---|---|
committer | deanm@chromium.org <deanm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-22 13:13:30 +0000 |
commit | f2a9afc3a5aa9ca9d66b953a29e1b08b5d4be700 (patch) | |
tree | 43dece872329d7fa752f27d3986744907121fe34 /base/string_util.cc | |
parent | 06bf6e4df17fe8c82e49cf751d7ebc8ae32b0aa6 (diff) | |
download | chromium_src-f2a9afc3a5aa9ca9d66b953a29e1b08b5d4be700.zip chromium_src-f2a9afc3a5aa9ca9d66b953a29e1b08b5d4be700.tar.gz chromium_src-f2a9afc3a5aa9ca9d66b953a29e1b08b5d4be700.tar.bz2 |
Simplify HexEncode.
Review URL: http://codereview.chromium.org/18659
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8450 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/string_util.cc')
-rw-r--r-- | base/string_util.cc | 29 |
1 files changed, 7 insertions, 22 deletions
diff --git a/base/string_util.cc b/base/string_util.cc index 45a4442..5a06b64 100644 --- a/base/string_util.cc +++ b/base/string_util.cc @@ -1534,30 +1534,15 @@ bool ElideString(const std::wstring& input, int max_len, std::wstring* output) { } std::string HexEncode(const void* bytes, size_t size) { - static const char kHexChars[] = { - '0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + static const char kHexChars[] = "0123456789ABCDEF"; - if (size == 0) - return std::string(); + // Each input byte creates two output hex characters. + std::string ret(size * 2, '\0'); - std::string ret; - // For each byte, we print two characters. - ret.resize(size * 2); - - const unsigned char* pos = reinterpret_cast<const unsigned char*>(bytes); - const unsigned char* end = pos + size; - std::string::iterator write = ret.begin(); - while (pos < end) { - unsigned char b = *pos; - pos++; - - write[0] = kHexChars[(b >> 4) & 0xf]; - write++; - - write[0] = kHexChars[b & 0xf]; - write++; + for (size_t i = 0; i < size; ++i) { + char b = reinterpret_cast<const char*>(bytes)[i]; + ret[(i * 2)] = kHexChars[(b >> 4) & 0xf]; + ret[(i * 2) + 1] = kHexChars[b & 0xf]; } - return ret; } |