diff options
author | deanm@google.com <deanm@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-19 13:19:24 +0000 |
---|---|---|
committer | deanm@google.com <deanm@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-19 13:19:24 +0000 |
commit | 4bdaceb429c7cd6a3a2a8bc692f418969c24ff28 (patch) | |
tree | e399572c17bb4ed5de86bd702ea28a041e40b9b0 | |
parent | 1e3f8396c6f205b2be2a102d4fbca870638e7f6b (diff) | |
download | chromium_src-4bdaceb429c7cd6a3a2a8bc692f418969c24ff28.zip chromium_src-4bdaceb429c7cd6a3a2a8bc692f418969c24ff28.tar.gz chromium_src-4bdaceb429c7cd6a3a2a8bc692f418969c24ff28.tar.bz2 |
Move the system string conversion functions from std::string to StringPiece. We often work with char* in the system layer, so this should save extra copying.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1027 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/base_paths_linux.cc | 1 | ||||
-rw-r--r-- | base/command_line.cc | 1 | ||||
-rw-r--r-- | base/logging.cc | 1 | ||||
-rw-r--r-- | base/sys_string_conversions.h | 8 | ||||
-rw-r--r-- | base/sys_string_conversions_linux.cc | 9 | ||||
-rw-r--r-- | base/sys_string_conversions_mac.cc | 9 | ||||
-rw-r--r-- | base/sys_string_conversions_win.cc | 12 | ||||
-rw-r--r-- | base/sys_string_conversions_win_unittest.cc | 1 |
8 files changed, 28 insertions, 14 deletions
diff --git a/base/base_paths_linux.cc b/base/base_paths_linux.cc index 298c231..a848440 100644 --- a/base/base_paths_linux.cc +++ b/base/base_paths_linux.cc @@ -34,6 +34,7 @@ #include "base/file_util.h" #include "base/logging.h" #include "base/path_service.h" +#include "base/string_piece.h" #include "base/sys_string_conversions.h" namespace base { diff --git a/base/command_line.cc b/base/command_line.cc index 8849149..0fb4624 100644 --- a/base/command_line.cc +++ b/base/command_line.cc @@ -38,6 +38,7 @@ #include "base/logging.h" #include "base/singleton.h" +#include "base/string_piece.h" #include "base/string_util.h" #include "base/sys_string_conversions.h" diff --git a/base/logging.cc b/base/logging.cc index 87ac934..27781ae0 100644 --- a/base/logging.cc +++ b/base/logging.cc @@ -62,6 +62,7 @@ typedef pthread_mutex_t* MutexHandle; #include "base/debug_util.h" #include "base/lock_impl.h" #include "base/logging.h" +#include "base/string_piece.h" #include "base/string_util.h" #include "base/sys_string_conversions.h" diff --git a/base/sys_string_conversions.h b/base/sys_string_conversions.h index fc0a5eb..ed9b8fa 100644 --- a/base/sys_string_conversions.h +++ b/base/sys_string_conversions.h @@ -37,18 +37,20 @@ #include <string> #include "base/basictypes.h" +class StringPiece; + namespace base { // Converts between wide and UTF-8 representations of a string. On error, the // result is system-dependent. std::string SysWideToUTF8(const std::wstring& wide); -std::wstring SysUTF8ToWide(const std::string& utf8); +std::wstring SysUTF8ToWide(StringPiece utf8); // Converts between wide and the system multi-byte representations of a string. // DANGER: This will lose information and can change (on Windows, this can // change between reboots). std::string SysWideToNativeMB(const std::wstring& wide); -std::wstring SysNativeMBToWide(const std::string& native_mb); +std::wstring SysNativeMBToWide(StringPiece native_mb); // Windows-specific ------------------------------------------------------------ @@ -57,7 +59,7 @@ std::wstring SysNativeMBToWide(const std::string& native_mb); // Converts between 8-bit and wide strings, using the given code page. The // code page identifier is one accepted by the Windows function // MultiByteToWideChar(). -std::wstring SysMultiByteToWide(const std::string& mb, uint32 code_page); +std::wstring SysMultiByteToWide(StringPiece mb, uint32 code_page); std::string SysWideToMultiByte(const std::wstring& wide, uint32 code_page); #endif // defined(OS_WIN) diff --git a/base/sys_string_conversions_linux.cc b/base/sys_string_conversions_linux.cc index 639b402..9547ef9 100644 --- a/base/sys_string_conversions_linux.cc +++ b/base/sys_string_conversions_linux.cc @@ -29,6 +29,7 @@ #include "base/sys_string_conversions.h" +#include "base/string_piece.h" #include "base/string_util.h" namespace base { @@ -38,10 +39,12 @@ std::string SysWideToUTF8(const std::wstring& wide) { // than our ICU, but this will do for now. return WideToUTF8(wide); } -std::wstring SysUTF8ToWide(const std::string& utf8) { +std::wstring SysUTF8ToWide(StringPiece utf8) { // In theory this should be using the system-provided conversion rather // than our ICU, but this will do for now. - return UTF8ToWide(utf8); + std::wstring out; + UTF8ToWide(utf8.data(), utf8.size(), &out); + return out; } std::string SysWideToNativeMB(const std::wstring& wide) { @@ -49,7 +52,7 @@ std::string SysWideToNativeMB(const std::wstring& wide) { return SysWideToUTF8(wide); } -std::wstring SysNativeMBToWide(const std::string& native_mb) { +std::wstring SysNativeMBToWide(StringPiece native_mb) { // TODO(evanm): we can't assume Linux is UTF-8. return SysUTF8ToWide(native_mb); } diff --git a/base/sys_string_conversions_mac.cc b/base/sys_string_conversions_mac.cc index 41b693a..6614fa0 100644 --- a/base/sys_string_conversions_mac.cc +++ b/base/sys_string_conversions_mac.cc @@ -28,9 +28,12 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "base/sys_string_conversions.h" + #include <CoreFoundation/CoreFoundation.h> #include <vector> + #include "base/scoped_cftyperef.h" +#include "base/string_piece.h" namespace base { @@ -139,8 +142,8 @@ std::string SysWideToUTF8(const std::wstring& wide) { } // Do not assert in this function since it is used by the asssertion code! -std::wstring SysUTF8ToWide(const std::string& utf8) { - return STLStringToSTLStringWithEncodingsT<std::string, std::wstring>( +std::wstring SysUTF8ToWide(StringPiece utf8) { + return STLStringToSTLStringWithEncodingsT<StringPiece, std::wstring>( utf8, kNarrowStringEncoding, kWideStringEncoding); } @@ -148,7 +151,7 @@ std::string SysWideToNativeMB(const std::wstring& wide) { return SysWideToUTF8(wide); } -std::wstring SysNativeMBToWide(const std::string& native_mb) { +std::wstring SysNativeMBToWide(StringPiece native_mb) { return SysUTF8ToWide(native_mb); } diff --git a/base/sys_string_conversions_win.cc b/base/sys_string_conversions_win.cc index c736549..48e8f01 100644 --- a/base/sys_string_conversions_win.cc +++ b/base/sys_string_conversions_win.cc @@ -31,6 +31,8 @@ #include <windows.h> +#include "base/string_piece.h" + namespace base { // Do not assert in this function since it is used by the asssertion code! @@ -39,7 +41,7 @@ std::string SysWideToUTF8(const std::wstring& wide) { } // Do not assert in this function since it is used by the asssertion code! -std::wstring SysUTF8ToWide(const std::string& utf8) { +std::wstring SysUTF8ToWide(StringPiece utf8) { return SysMultiByteToWide(utf8, CP_UTF8); } @@ -47,16 +49,16 @@ std::string SysWideToNativeMB(const std::wstring& wide) { return SysWideToMultiByte(wide, CP_ACP); } -std::wstring SysNativeMBToWide(const std::string& native_mb) { +std::wstring SysNativeMBToWide(StringPiece native_mb) { return SysMultiByteToWide(native_mb, CP_ACP); } // Do not assert in this function since it is used by the asssertion code! -std::wstring SysMultiByteToWide(const std::string& mb, uint32 code_page) { - int mb_length = static_cast<int>(mb.length()); - if (mb_length == 0) +std::wstring SysMultiByteToWide(StringPiece mb, uint32 code_page) { + if (mb.empty()) return std::wstring(); + int mb_length = static_cast<int>(mb.length()); // Compute the length of the buffer. int charcount = MultiByteToWideChar(code_page, 0, mb.data(), mb_length, NULL, 0); diff --git a/base/sys_string_conversions_win_unittest.cc b/base/sys_string_conversions_win_unittest.cc index 70c8592..ecadf00 100644 --- a/base/sys_string_conversions_win_unittest.cc +++ b/base/sys_string_conversions_win_unittest.cc @@ -27,6 +27,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#include "base/string_piece.h" #include "base/sys_string_conversions.h" #include "testing/gtest/include/gtest/gtest.h" |