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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | base/string_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/string_util.h" 5 #include "base/string_util.h"
6 6
7 #include "build/build_config.h" 7 #include "build/build_config.h"
8 8
9 #include <ctype.h> 9 #include <ctype.h>
10 #include <errno.h> 10 #include <errno.h>
(...skipping 1516 matching lines...) Expand 10 before | Expand all | Expand 10 after
1527 output->assign(input.substr(0, lstr_len) + L"..." + 1527 output->assign(input.substr(0, lstr_len) + L"..." +
1528 input.substr(input.length() - rstr_len)); 1528 input.substr(input.length() - rstr_len));
1529 break; 1529 break;
1530 } 1530 }
1531 } 1531 }
1532 1532
1533 return true; 1533 return true;
1534 } 1534 }
1535 1535
1536 std::string HexEncode(const void* bytes, size_t size) { 1536 std::string HexEncode(const void* bytes, size_t size) {
1537 static const char kHexChars[] = { 1537 static const char kHexChars[] = "0123456789ABCDEF";
1538 '0', '1', '2', '3', '4', '5', '6', '7',
1539 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
1540 1538
1541 if (size == 0) 1539 // Each input byte creates two output hex characters.
1542 return std::string(); 1540 std::string ret(size * 2, '\0');
1543 1541
1544 std::string ret; 1542 for (size_t i = 0; i < size; ++i) {
1545 // For each byte, we print two characters. 1543 char b = reinterpret_cast<const char*>(bytes)[i];
1546 ret.resize(size * 2); 1544 ret[(i * 2)] = kHexChars[(b >> 4) & 0xf];
1547 1545 ret[(i * 2) + 1] = kHexChars[b & 0xf];
1548 const unsigned char* pos = reinterpret_cast<const unsigned char*>(bytes);
1549 const unsigned char* end = pos + size;
1550 std::string::iterator write = ret.begin();
1551 while (pos < end) {
1552 unsigned char b = *pos;
1553 pos++;
1554
1555 write[0] = kHexChars[(b >> 4) & 0xf];
1556 write++;
1557
1558 write[0] = kHexChars[b & 0xf];
1559 write++;
1560 } 1546 }
1561
1562 return ret; 1547 return ret;
1563 } 1548 }
OLDNEW
« 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