diff options
author | avi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-18 21:39:23 +0000 |
---|---|---|
committer | avi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-18 21:39:23 +0000 |
commit | f224d57a86d8ed1ed6c4bca3c8a7150bf3c0ed14 (patch) | |
tree | f6305290a1c4781138d7d5a4f410915846ae6766 | |
parent | 7a0220b27d9d9a9e5b870411c6a18edca1981767 (diff) | |
download | chromium_src-f224d57a86d8ed1ed6c4bca3c8a7150bf3c0ed14.zip chromium_src-f224d57a86d8ed1ed6c4bca3c8a7150bf3c0ed14.tar.gz chromium_src-f224d57a86d8ed1ed6c4bca3c8a7150bf3c0ed14.tar.bz2 |
Now that we deal with string16s, provide some useful conversions for them.
Review URL: http://codereview.chromium.org/21473
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9970 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/sys_string_conversions.h | 5 | ||||
-rw-r--r-- | base/sys_string_conversions_mac.mm | 27 |
2 files changed, 24 insertions, 8 deletions
diff --git a/base/sys_string_conversions.h b/base/sys_string_conversions.h index 7b56a21..dbaa5a4 100644 --- a/base/sys_string_conversions.h +++ b/base/sys_string_conversions.h @@ -11,6 +11,7 @@ #include <string> #include "base/basictypes.h" +#include "base/string16.h" #if defined(OS_MACOSX) #include <CoreFoundation/CoreFoundation.h> @@ -57,18 +58,22 @@ std::string SysWideToMultiByte(const std::wstring& wide, uint32 code_page); // Creates a string, and returns it with a refcount of 1. You are responsible // for releasing it. Returns NULL on failure. CFStringRef SysUTF8ToCFStringRef(const std::string& utf8); +CFStringRef SysUTF16ToCFStringRef(const string16& utf16); CFStringRef SysWideToCFStringRef(const std::wstring& wide); // Same, but returns an autoreleased NSString. NSString* SysUTF8ToNSString(const std::string& utf8); +NSString* SysUTF16ToNSString(const string16& utf16); NSString* SysWideToNSString(const std::wstring& wide); // Converts a CFStringRef to an STL string. Returns an empty string on failure. std::string SysCFStringRefToUTF8(CFStringRef ref); +string16 SysCFStringRefToUTF16(CFStringRef ref); std::wstring SysCFStringRefToWide(CFStringRef ref); // Same, but accepts NSString input. std::string SysNSStringToUTF8(NSString* ref); +string16 SysNSStringToUTF16(NSString* ref); std::wstring SysNSStringToWide(NSString* ref); #endif // defined(OS_MACOSX) diff --git a/base/sys_string_conversions_mac.mm b/base/sys_string_conversions_mac.mm index 0da99f1..d0b2b235c 100644 --- a/base/sys_string_conversions_mac.mm +++ b/base/sys_string_conversions_mac.mm @@ -115,17 +115,11 @@ static CFStringRef STLStringToCFStringWithEncodingsT( // when strings don't carry BOMs, as they typically won't. static const CFStringEncoding kNarrowStringEncoding = kCFStringEncodingUTF8; #ifdef __BIG_ENDIAN__ -#if defined(WCHAR_T_IS_UTF16) -static const CFStringEncoding kWideStringEncoding = kCFStringEncodingUTF16BE; -#elif defined(WCHAR_T_IS_UTF32) +static const CFStringEncoding kMediumStringEncoding = kCFStringEncodingUTF16BE; static const CFStringEncoding kWideStringEncoding = kCFStringEncodingUTF32BE; -#endif // WCHAR_T_IS_UTF32 #elif defined(__LITTLE_ENDIAN__) -#if defined(WCHAR_T_IS_UTF16) -static const CFStringEncoding kWideStringEncoding = kCFStringEncodingUTF16LE; -#elif defined(WCHAR_T_IS_UTF32) +static const CFStringEncoding kMediumStringEncoding = kCFStringEncodingUTF16LE; static const CFStringEncoding kWideStringEncoding = kCFStringEncodingUTF32LE; -#endif // WCHAR_T_IS_UTF32 #endif // __LITTLE_ENDIAN__ } // namespace @@ -154,6 +148,10 @@ CFStringRef SysUTF8ToCFStringRef(const std::string& utf8) { return STLStringToCFStringWithEncodingsT(utf8, kNarrowStringEncoding); } +CFStringRef SysUTF16ToCFStringRef(const string16& utf16) { + return STLStringToCFStringWithEncodingsT(utf16, kMediumStringEncoding); +} + CFStringRef SysWideToCFStringRef(const std::wstring& wide) { return STLStringToCFStringWithEncodingsT(wide, kWideStringEncoding); } @@ -162,6 +160,10 @@ NSString* SysUTF8ToNSString(const std::string& utf8) { return CFTypeRefToNSObjectAutorelease(SysUTF8ToCFStringRef(utf8)); } +NSString* SysUTF16ToNSString(const string16& utf16) { + return CFTypeRefToNSObjectAutorelease(SysUTF16ToCFStringRef(utf16)); +} + NSString* SysWideToNSString(const std::wstring& wide) { return CFTypeRefToNSObjectAutorelease(SysWideToCFStringRef(wide)); } @@ -171,6 +173,11 @@ std::string SysCFStringRefToUTF8(CFStringRef ref) { kNarrowStringEncoding); } +string16 SysCFStringRefToUTF16(CFStringRef ref) { + return CFStringToSTLStringWithEncodingT<string16>(ref, + kMediumStringEncoding); +} + std::wstring SysCFStringRefToWide(CFStringRef ref) { return CFStringToSTLStringWithEncodingT<std::wstring>(ref, kWideStringEncoding); @@ -180,6 +187,10 @@ std::string SysNSStringToUTF8(NSString* nsstring) { return SysCFStringRefToUTF8(reinterpret_cast<CFStringRef>(nsstring)); } +string16 SysNSStringToUTF16(NSString* nsstring) { + return SysCFStringRefToUTF16(reinterpret_cast<CFStringRef>(nsstring)); +} + std::wstring SysNSStringToWide(NSString* nsstring) { return SysCFStringRefToWide(reinterpret_cast<CFStringRef>(nsstring)); } |