diff options
author | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-31 21:03:16 +0000 |
---|---|---|
committer | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-31 21:03:16 +0000 |
commit | 61197dffa1137d7e4fe003281ad3e38108e90411 (patch) | |
tree | 5b6b76500ee42dedaa6e8d0bfd994432930fb4a4 /base | |
parent | 38c93edcd09cf787333470019f2d2c1bb23f8b98 (diff) | |
download | chromium_src-61197dffa1137d7e4fe003281ad3e38108e90411.zip chromium_src-61197dffa1137d7e4fe003281ad3e38108e90411.tar.gz chromium_src-61197dffa1137d7e4fe003281ad3e38108e90411.tar.bz2 |
base: Add HexDigitToInt function to string_util.h
Removed duplicated HexToInt functions and converted the callers along the way.
BUG=None
TEST=trybots and out/Debug/base_unittests --gtest_filter=StringUtilTest.HexDigitToInt
Signed-off-by: Thiago Farina <tfarina@chromium.org>
Review URL: http://codereview.chromium.org/2836069
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@54473 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/json/json_reader.cc | 26 | ||||
-rw-r--r-- | base/string_util.h | 12 | ||||
-rw-r--r-- | base/string_util_unittest.cc | 27 |
3 files changed, 46 insertions, 19 deletions
diff --git a/base/json/json_reader.cc b/base/json/json_reader.cc index 1255ac5..0d6abc4 100644 --- a/base/json/json_reader.cc +++ b/base/json/json_reader.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -20,18 +20,6 @@ static const int kStackLimit = 100; namespace { -inline int HexToInt(wchar_t c) { - if ('0' <= c && c <= '9') { - return c - '0'; - } else if ('A' <= c && c <= 'F') { - return c - 'A' + 10; - } else if ('a' <= c && c <= 'f') { - return c - 'a' + 10; - } - NOTREACHED(); - return 0; -} - // A helper method for ParseNumberToken. It reads an int from the end of // token. The method returns false if there is no valid integer at the end of // the token. @@ -493,15 +481,15 @@ Value* JSONReader::DecodeString(const Token& token) { break; case 'x': - decoded_str.push_back((HexToInt(*(token.begin + i + 1)) << 4) + - HexToInt(*(token.begin + i + 2))); + decoded_str.push_back((HexDigitToInt(*(token.begin + i + 1)) << 4) + + HexDigitToInt(*(token.begin + i + 2))); i += 2; break; case 'u': - decoded_str.push_back((HexToInt(*(token.begin + i + 1)) << 12 ) + - (HexToInt(*(token.begin + i + 2)) << 8) + - (HexToInt(*(token.begin + i + 3)) << 4) + - HexToInt(*(token.begin + i + 4))); + decoded_str.push_back((HexDigitToInt(*(token.begin + i + 1)) << 12 ) + + (HexDigitToInt(*(token.begin + i + 2)) << 8) + + (HexDigitToInt(*(token.begin + i + 3)) << 4) + + HexDigitToInt(*(token.begin + i + 4))); i += 4; break; diff --git a/base/string_util.h b/base/string_util.h index 762779c..97b5100 100644 --- a/base/string_util.h +++ b/base/string_util.h @@ -380,6 +380,18 @@ inline bool IsHexDigit(Char c) { (c >= 'a' && c <= 'f'); } +template <typename Char> +inline Char HexDigitToInt(Char c) { + DCHECK(IsHexDigit(c)); + if (c >= '0' && c <= '9') + return c - '0'; + if (c >= 'A' && c <= 'F') + return c - 'A' + 10; + if (c >= 'a' && c <= 'f') + return c - 'a' + 10; + return 0; +} + // Returns true if it's a whitespace character. inline bool IsWhitespace(wchar_t c) { return wcschr(kWhitespaceWide, c) != NULL; diff --git a/base/string_util_unittest.cc b/base/string_util_unittest.cc index f063fcc..4d3fc1c 100644 --- a/base/string_util_unittest.cc +++ b/base/string_util_unittest.cc @@ -633,6 +633,33 @@ TEST(StringUtilTest, ReplaceFirstSubstringAfterOffset) { } } +TEST(StringUtilTest, HexDigitToInt) { + EXPECT_EQ(0, HexDigitToInt('0')); + EXPECT_EQ(1, HexDigitToInt('1')); + EXPECT_EQ(2, HexDigitToInt('2')); + EXPECT_EQ(3, HexDigitToInt('3')); + EXPECT_EQ(4, HexDigitToInt('4')); + EXPECT_EQ(5, HexDigitToInt('5')); + EXPECT_EQ(6, HexDigitToInt('6')); + EXPECT_EQ(7, HexDigitToInt('7')); + EXPECT_EQ(8, HexDigitToInt('8')); + EXPECT_EQ(9, HexDigitToInt('9')); + EXPECT_EQ(10, HexDigitToInt('A')); + EXPECT_EQ(11, HexDigitToInt('B')); + EXPECT_EQ(12, HexDigitToInt('C')); + EXPECT_EQ(13, HexDigitToInt('D')); + EXPECT_EQ(14, HexDigitToInt('E')); + EXPECT_EQ(15, HexDigitToInt('F')); + + // Verify the lower case as well. + EXPECT_EQ(10, HexDigitToInt('a')); + EXPECT_EQ(11, HexDigitToInt('b')); + EXPECT_EQ(12, HexDigitToInt('c')); + EXPECT_EQ(13, HexDigitToInt('d')); + EXPECT_EQ(14, HexDigitToInt('e')); + EXPECT_EQ(15, HexDigitToInt('f')); +} + // This checks where we can use the assignment operator for a va_list. We need // a way to do this since Visual C doesn't support va_copy, but assignment on // va_list is not guaranteed to be a copy. See StringAppendVT which uses this |