Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3102)

Unified Diff: base/string_util.cc

Issue 18659: Simplify HexEncode. (Closed)
Patch Set: Add another test to make sure the shift is correct. Created 11 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/string_util_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/string_util.cc
diff --git a/base/string_util.cc b/base/string_util.cc
index 45a44426ccc558961c3addc29898d99d97822b54..5a06b64377c5a95e47b336fdd84747bf42b1b5a8 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;
}
« no previous file with comments | « no previous file | base/string_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698