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 | |
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
25 files changed, 57 insertions, 42 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_ diff --git a/chrome/browser/autofill/form_group.cc b/chrome/browser/autofill/form_group.cc index 49beb16..6c53303 100644 --- a/chrome/browser/autofill/form_group.cc +++ b/chrome/browser/autofill/form_group.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 "chrome/browser/autofill/form_group.h" string16 FormGroup::GetPreviewText(const AutoFillType& type) const { diff --git a/chrome/browser/extensions/default_apps.cc b/chrome/browser/extensions/default_apps.cc index cee5c32..bf8aa9a 100644 --- a/chrome/browser/extensions/default_apps.cc +++ b/chrome/browser/extensions/default_apps.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 "chrome/browser/extensions/default_apps.h" #include "base/command_line.h" diff --git a/chrome/browser/safe_browsing/chunk_range.cc b/chrome/browser/safe_browsing/chunk_range.cc index b2f60ba..f389342 100644 --- a/chrome/browser/safe_browsing/chunk_range.cc +++ b/chrome/browser/safe_browsing/chunk_range.cc @@ -4,6 +4,8 @@ // // Implementation of ChunkRange class. +#include <algorithm> + #include "chrome/browser/safe_browsing/chunk_range.h" #include "base/logging.h" diff --git a/chrome/browser/safe_browsing/filter_false_positive_perftest.cc b/chrome/browser/safe_browsing/filter_false_positive_perftest.cc index eec1c2e..34c4512 100644 --- a/chrome/browser/safe_browsing/filter_false_positive_perftest.cc +++ b/chrome/browser/safe_browsing/filter_false_positive_perftest.cc @@ -52,6 +52,7 @@ // <url>,<weight> where weight is an integer indicating the number of // unique views for the URL. +#include <algorithm> #include <fstream> #include <vector> @@ -380,4 +381,3 @@ TEST(SafeBrowsingBloomFilter, HashTime) { << ", per-check (us): " << time_per_check << std::endl; } - diff --git a/chrome/browser/safe_browsing/protocol_parser.cc b/chrome/browser/safe_browsing/protocol_parser.cc index c8d1691..08e521e 100644 --- a/chrome/browser/safe_browsing/protocol_parser.cc +++ b/chrome/browser/safe_browsing/protocol_parser.cc @@ -4,6 +4,8 @@ // // Parse the data returned from the SafeBrowsing v2.1 protocol response. +#include <stdlib.h> + #include "chrome/browser/safe_browsing/protocol_parser.h" #include "chrome/browser/safe_browsing/safe_browsing_util.h" diff --git a/chrome/common/service_process_util.cc b/chrome/common/service_process_util.cc index bcbac69..4062e0f 100644 --- a/chrome/common/service_process_util.cc +++ b/chrome/common/service_process_util.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_util.h" #include "base/logging.h" #include "base/path_service.h" @@ -237,4 +239,3 @@ bool ServiceProcessState::CreateSharedData() { std::string ServiceProcessState::GetAutoRunKey() { return GetServiceProcessScopedName("_service_run"); } - diff --git a/chrome/installer/util/l10n_string_util.cc b/chrome/installer/util/l10n_string_util.cc index b8429a8..94aaaff 100644 --- a/chrome/installer/util/l10n_string_util.cc +++ b/chrome/installer/util/l10n_string_util.cc @@ -5,6 +5,8 @@ // This file defines specific implementation of BrowserDistribution class for // Google Chrome. +#include <algorithm> + #include <atlbase.h> #include "base/logging.h" diff --git a/chrome/renderer/localized_error.cc b/chrome/renderer/localized_error.cc index 7b4ae31..8821267 100644 --- a/chrome/renderer/localized_error.cc +++ b/chrome/renderer/localized_error.cc @@ -355,7 +355,7 @@ void LocalizedError::GetStrings(const WebKit::WebURLError& error, l10n_util::GetStringUTF16(IDS_ERRORPAGES_SUGGESTION_HEADING)); } - string16 failed_url(ASCIIToUTF16(error.unreachableURL.spec())); + string16 failed_url(ASCIIToUTF16(std::string(error.unreachableURL.spec()))); // URLs are always LTR. if (rtl) base::i18n::WrapStringWithLTRFormatting(&failed_url); diff --git a/chrome/test/mini_installer_test/mini_installer_test_util.cc b/chrome/test/mini_installer_test/mini_installer_test_util.cc index 2df756f..c73139d 100644 --- a/chrome/test/mini_installer_test/mini_installer_test_util.cc +++ b/chrome/test/mini_installer_test/mini_installer_test_util.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 "chrome/test/mini_installer_test/mini_installer_test_util.h" #include "base/file_path.h" diff --git a/chrome_frame/test_utils.cc b/chrome_frame/test_utils.cc index 65b172d..708db6f 100644 --- a/chrome_frame/test_utils.cc +++ b/chrome_frame/test_utils.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 "chrome_frame/test_utils.h" #include <atlbase.h> diff --git a/net/base/cookie_monster_perftest.cc b/net/base/cookie_monster_perftest.cc index 8c64d9a..68ac4cc 100644 --- a/net/base/cookie_monster_perftest.cc +++ b/net/base/cookie_monster_perftest.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 "net/base/cookie_monster.h" #include "base/perftimer.h" diff --git a/net/base/net_util_win.cc b/net/base/net_util_win.cc index a6d37c1..dac93a9 100644 --- a/net/base/net_util_win.cc +++ b/net/base/net_util_win.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 "net/base/net_util.h" #include "base/file_path.h" diff --git a/net/tools/dnssec_chain_verify/dnssec_chain_verify.cc b/net/tools/dnssec_chain_verify/dnssec_chain_verify.cc index a1c5270..b27c375 100644 --- a/net/tools/dnssec_chain_verify/dnssec_chain_verify.cc +++ b/net/tools/dnssec_chain_verify/dnssec_chain_verify.cc @@ -4,6 +4,7 @@ #include <errno.h> #include <stdio.h> +#include <stdlib.h> #include "base/at_exit.h" #include "net/base/dns_util.h" diff --git a/net/tools/dump_cache/url_to_filename_encoder.cc b/net/tools/dump_cache/url_to_filename_encoder.cc index 84d79c7..4ca4a51 100644 --- a/net/tools/dump_cache/url_to_filename_encoder.cc +++ b/net/tools/dump_cache/url_to_filename_encoder.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 <stdlib.h> + #include "base/logging.h" #include "base/string_util.h" #include "net/base/net_util.h" diff --git a/tools/memory_watcher/memory_watcher.cc b/tools/memory_watcher/memory_watcher.cc index 29053f0..671d2b9 100644 --- a/tools/memory_watcher/memory_watcher.cc +++ b/tools/memory_watcher/memory_watcher.cc @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include <algorithm> #include <windows.h> #include <tlhelp32.h> // for CreateToolhelp32Snapshot() #include <map> diff --git a/webkit/appcache/view_appcache_internals_job.cc b/webkit/appcache/view_appcache_internals_job.cc index b4874db..c165375 100644 --- a/webkit/appcache/view_appcache_internals_job.cc +++ b/webkit/appcache/view_appcache_internals_job.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 "webkit/appcache/view_appcache_internals_job.h" #include "base/logging.h" diff --git a/webkit/glue/webclipboard_impl.cc b/webkit/glue/webclipboard_impl.cc index 1dfbc3a..4569749 100644 --- a/webkit/glue/webclipboard_impl.cc +++ b/webkit/glue/webclipboard_impl.cc @@ -145,7 +145,7 @@ void WebClipboardImpl::writeURL(const WebURL& url, const WebString& title) { scw.WriteBookmark(title, url.spec()); scw.WriteHTML(UTF8ToUTF16(URLToMarkup(url, title)), ""); - scw.WriteText(UTF8ToUTF16(url.spec())); + scw.WriteText(UTF8ToUTF16(std::string(url.spec()))); } void WebClipboardImpl::writeImage( diff --git a/webkit/plugins/npapi/plugin_group.cc b/webkit/plugins/npapi/plugin_group.cc index 1884650..7d45aa7 100644 --- a/webkit/plugins/npapi/plugin_group.cc +++ b/webkit/plugins/npapi/plugin_group.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 "webkit/plugins/npapi/plugin_group.h" #include "base/linked_ptr.h" diff --git a/webkit/plugins/npapi/test/plugin_arguments_test.cc b/webkit/plugins/npapi/test/plugin_arguments_test.cc index fe1e54e..bab2298 100644 --- a/webkit/plugins/npapi/test/plugin_arguments_test.cc +++ b/webkit/plugins/npapi/test/plugin_arguments_test.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 <stdlib.h> + #include "webkit/plugins/npapi/test/plugin_arguments_test.h" #include "base/basictypes.h" |