diff options
author | suzhe@google.com <suzhe@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-28 02:40:46 +0000 |
---|---|---|
committer | suzhe@google.com <suzhe@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-28 02:40:46 +0000 |
commit | 39a749c1cc998edcd66edfc3ffc9004710825f46 (patch) | |
tree | 370b1a18564e743cfbfc16beb4d0053437064486 /base | |
parent | 911696b92b1b5a666e59433b8b64303a2f4eae62 (diff) | |
download | chromium_src-39a749c1cc998edcd66edfc3ffc9004710825f46.zip chromium_src-39a749c1cc998edcd66edfc3ffc9004710825f46.tar.gz chromium_src-39a749c1cc998edcd66edfc3ffc9004710825f46.tar.bz2 |
Change UTF8ToUTF16 to accept const StringPiece&.
BUG=70936
TEST=All unit tests should pass.
Review URL: http://codereview.chromium.org/6317016
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@72921 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/crypto/rsa_private_key.cc | 1 | ||||
-rw-r--r-- | base/file_path.cc | 2 | ||||
-rw-r--r-- | base/logging.h | 9 | ||||
-rw-r--r-- | base/string_piece.h | 12 | ||||
-rw-r--r-- | base/utf_string_conversions.cc | 18 | ||||
-rw-r--r-- | base/utf_string_conversions.h | 20 |
6 files changed, 24 insertions, 38 deletions
diff --git a/base/crypto/rsa_private_key.cc b/base/crypto/rsa_private_key.cc index 4048ed1..2ab0c14 100644 --- a/base/crypto/rsa_private_key.cc +++ b/base/crypto/rsa_private_key.cc @@ -4,6 +4,7 @@ #include "base/crypto/rsa_private_key.h" +#include <algorithm> #include <list> #include "base/logging.h" diff --git a/base/file_path.cc b/base/file_path.cc index cddb17e..9bc46f0 100644 --- a/base/file_path.cc +++ b/base/file_path.cc @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include <algorithm> + #include "base/file_path.h" #if defined(OS_WIN) diff --git a/base/logging.h b/base/logging.h index 662deae..771aa73 100644 --- a/base/logging.h +++ b/base/logging.h @@ -919,4 +919,13 @@ inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) { } while(0) #endif +namespace base { + +class StringPiece; + +// allow StringPiece to be logged (needed for unit testing). +extern std::ostream& operator<<(std::ostream& o, const StringPiece& piece); + +} // namespace base + #endif // BASE_LOGGING_H_ diff --git a/base/string_piece.h b/base/string_piece.h index 80c6cab..64326e1 100644 --- a/base/string_piece.h +++ b/base/string_piece.h @@ -19,8 +19,6 @@ #define BASE_STRING_PIECE_H_ #pragma once -#include <algorithm> -#include <iosfwd> #include <string> #include "base/basictypes.h" @@ -93,7 +91,8 @@ class StringPiece { } int compare(const StringPiece& x) const { - int r = wordmemcmp(ptr_, x.ptr_, std::min(length_, x.length_)); + int r = wordmemcmp( + ptr_, x.ptr_, (length_ < x.length_ ? length_ : x.length_)); if (r == 0) { if (length_ < x.length_) r = -1; else if (length_ > x.length_) r = +1; @@ -171,8 +170,8 @@ inline bool operator!=(const StringPiece& x, const StringPiece& y) { } inline bool operator<(const StringPiece& x, const StringPiece& y) { - const int r = StringPiece::wordmemcmp(x.data(), y.data(), - std::min(x.size(), y.size())); + const int r = StringPiece::wordmemcmp( + x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size())); return ((r < 0) || ((r == 0) && (x.size() < y.size()))); } @@ -188,9 +187,6 @@ inline bool operator>=(const StringPiece& x, const StringPiece& y) { return !(x < y); } -// allow StringPiece to be logged (needed for unit testing). -extern std::ostream& operator<<(std::ostream& o, const StringPiece& piece); - } // namespace base #endif // BASE_STRING_PIECE_H_ diff --git a/base/utf_string_conversions.cc b/base/utf_string_conversions.cc index 41a70db..7b73696 100644 --- a/base/utf_string_conversions.cc +++ b/base/utf_string_conversions.cc @@ -133,7 +133,7 @@ bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { return ConvertUnicode(src, src_len, output); } -string16 UTF8ToUTF16(const std::string& utf8) { +string16 UTF8ToUTF16(const base::StringPiece& utf8) { string16 ret; // Ignore the success flag of this call, it will do the best it can for // invalid input, which is what we want here. @@ -161,7 +161,7 @@ bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { return UTF8ToWide(src, src_len, output); } -string16 UTF8ToUTF16(const std::string& utf8) { +string16 UTF8ToUTF16(const base::StringPiece& utf8) { return UTF8ToWide(utf8); } @@ -175,22 +175,12 @@ std::string UTF16ToUTF8(const string16& utf16) { #endif -std::wstring ASCIIToWide(const char* ascii) { - DCHECK(IsStringASCII(ascii)) << ascii; - return std::wstring(ascii, &ascii[strlen(ascii)]); -} - -std::wstring ASCIIToWide(const std::string& ascii) { +std::wstring ASCIIToWide(const base::StringPiece& ascii) { DCHECK(IsStringASCII(ascii)) << ascii; return std::wstring(ascii.begin(), ascii.end()); } -string16 ASCIIToUTF16(const char* ascii) { - DCHECK(IsStringASCII(ascii)) << ascii; - return string16(ascii, &ascii[strlen(ascii)]); -} - -string16 ASCIIToUTF16(const std::string& ascii) { +string16 ASCIIToUTF16(const base::StringPiece& ascii) { DCHECK(IsStringASCII(ascii)) << ascii; return string16(ascii.begin(), ascii.end()); } diff --git a/base/utf_string_conversions.h b/base/utf_string_conversions.h index 6c49b41..4aa4d41 100644 --- a/base/utf_string_conversions.h +++ b/base/utf_string_conversions.h @@ -9,10 +9,7 @@ #include <string> #include "base/string16.h" - -namespace base { -class StringPiece; -} +#include "base/string_piece.h" // These convert between UTF-8, -16, and -32 strings. They are potentially slow, // so avoid unnecessary conversions. The low-level versions return a boolean @@ -31,7 +28,7 @@ bool UTF16ToWide(const char16* src, size_t src_len, std::wstring* output); std::wstring UTF16ToWide(const string16& utf16); bool UTF8ToUTF16(const char* src, size_t src_len, string16* output); -string16 UTF8ToUTF16(const std::string& utf8); +string16 UTF8ToUTF16(const base::StringPiece& utf8); bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output); std::string UTF16ToUTF8(const string16& utf16); @@ -50,16 +47,7 @@ std::string UTF16ToUTF8(const string16& utf16); // These convert an ASCII string, typically a hardcoded constant, to a // UTF16/Wide string. -// -// Note that this doesn't use StringPiece because it's very common to need -// ASCIIToUTF16("foo"), and either we have to include it in this file, or -// forward declare it and force all callers to include string_piece.h. Given -// that string_piece brings in complicated stuff like <algorithm>, it's -// easier to just duplicate these very simple definitions for the two calling -// cases we actually use. -std::wstring ASCIIToWide(const char* ascii); -std::wstring ASCIIToWide(const std::string& ascii); -string16 ASCIIToUTF16(const char* ascii); -string16 ASCIIToUTF16(const std::string& ascii); +std::wstring ASCIIToWide(const base::StringPiece& ascii); +string16 ASCIIToUTF16(const base::StringPiece& ascii); #endif // BASE_UTF_STRING_CONVERSIONS_H_ |