Commit fb9d8f3c authored by tfarina's avatar tfarina Committed by Commit bot

base: Use a simple tricky to remove a temporary variable in MD5DigestToBase16.

With this tricky we don't need this strange 'j' variable laying around
outside the loop, even better, since that variable is not even used
outside of the for loop.

The iteration using this tricky goes like the following:

The old tricky using just 'i':
For i = 0:
    0 * 2     = 0
    0 * 2 + 1 = 1

For i = 1:
    1 * 2     = 2
    1 * 2 + 1 = 3

For i = 2:
    2 * 2     = 4
    2 * 2 + 1 = 5

The new tricky using 'j':
For i = 0:
    j = 0
    0     = 0
    0 + 1 = 1

For i = 1:
    j = 2
    2     = 2
    2 + 1 = 3

For i = 2:
    j = 4
    4     = 4
    4 + 1 = 5

So the new code does the same thing as the old but with the advantage of
not having to use a temporary variable outside the loop.

And the tricky with 'j' has also the benefit of avoid a multiplication and the rules of operator precedence.

BUG=None
TEST=base_unittests --gtest_filter=MD5*
R=thestig@chromium.org

Review URL: https://codereview.chromium.org/1065733005

Cr-Commit-Position: refs/heads/master@{#324171}
parent f584802a
...@@ -276,11 +276,10 @@ std::string MD5DigestToBase16(const MD5Digest& digest) { ...@@ -276,11 +276,10 @@ std::string MD5DigestToBase16(const MD5Digest& digest) {
std::string ret; std::string ret;
ret.resize(32); ret.resize(32);
int j = 0; for (int i = 0, j = 0; i < 16; i++, j += 2) {
for (int i = 0; i < 16; i++) {
int a = digest.a[i]; int a = digest.a[i];
ret[j++] = zEncode[(a >> 4) & 0xf]; ret[j] = zEncode[(a >> 4) & 0xf];
ret[j++] = zEncode[a & 0xf]; ret[j + 1] = zEncode[a & 0xf];
} }
return ret; return ret;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment