| 6 | #define BASE_STRINGS_SYS_STRING_CONVERSIONS_H_ |
| 7 |
| 8 | // Provides system-dependent string type conversions for cases where it's |
| 9 | // necessary to not use ICU. Generally, you should not need this in Chrome, |
| 10 | // but it is used in some shared code. Dependencies should be minimal. |
| 11 |
| 12 | #include <stdint.h> |
| 13 |
| 14 | #include <string> |
| 15 |
| 16 | #include "base/base_export.h" |
| 17 | #include "base/strings/string16.h" |
| 18 | #include "base/strings/string_piece.h" |
| 19 | #include "build/build_config.h" |
| 20 |
| 21 | #if defined(OS_MACOSX) |
| 22 | #include <CoreFoundation/CoreFoundation.h> |
| 23 | #ifdef __OBJC__ |
| 24 | @class NSString; |
| 25 | #else |
| 26 | class NSString; |
| 27 | #endif |
| 28 | #endif // OS_MACOSX |
| 29 |
| 30 | namespace base { |
| 31 |
| 32 | // Converts between wide and UTF-8 representations of a string. On error, the |
| 33 | // result is system-dependent. |
| 34 | BASE_EXPORT std::string SysWideToUTF8(const std::wstring& wide) |
| 35 | WARN_UNUSED_RESULT; |
| 36 | BASE_EXPORT std::wstring SysUTF8ToWide(StringPiece utf8) WARN_UNUSED_RESULT; |
| 37 |
| 38 | // Converts between wide and the system multi-byte representations of a string. |
| 39 | // DANGER: This will lose information and can change (on Windows, this can |
| 40 | // change between reboots). |
| 41 | BASE_EXPORT std::string SysWideToNativeMB(const std::wstring& wide) |
| 42 | WARN_UNUSED_RESULT; |
| 43 | BASE_EXPORT std::wstring SysNativeMBToWide(StringPiece native_mb) |
| 44 | WARN_UNUSED_RESULT; |
| 45 |
| 46 | // Windows-specific ------------------------------------------------------------ |
| 47 |
| 48 | #if defined(OS_WIN) |
| 49 |
| 50 | // Converts between 8-bit and wide strings, using the given code page. The |
| 51 | // code page identifier is one accepted by the Windows function |
| 52 | // MultiByteToWideChar(). |
| 53 | BASE_EXPORT std::wstring SysMultiByteToWide(StringPiece mb, uint32_t code_page) |
| 54 | WARN_UNUSED_RESULT; |
| 55 | BASE_EXPORT std::string SysWideToMultiByte(const std::wstring& wide, |
| 56 | uint32_t code_page) |
| 57 | WARN_UNUSED_RESULT; |
| 58 |
| 59 | #endif // defined(OS_WIN) |
| 60 |
| 61 | // Mac-specific ---------------------------------------------------------------- |
| 62 |
| 63 | #if defined(OS_MACOSX) |
| 64 |
| 65 | // Converts between STL strings and CFStringRefs/NSStrings. |
| 66 |
| 67 | // Creates a string, and returns it with a refcount of 1. You are responsible |
| 68 | // for releasing it. Returns NULL on failure. |
| 69 | BASE_EXPORT CFStringRef SysUTF8ToCFStringRef(StringPiece utf8) |
| 70 | WARN_UNUSED_RESULT; |
| 71 | BASE_EXPORT CFStringRef SysUTF16ToCFStringRef(StringPiece16 utf16) |
| 72 | WARN_UNUSED_RESULT; |
| 73 |
| 74 | // Same, but returns an autoreleased NSString. |
| 75 | BASE_EXPORT NSString* SysUTF8ToNSString(StringPiece utf8) WARN_UNUSED_RESULT; |
| 76 | BASE_EXPORT NSString* SysUTF16ToNSString(StringPiece16 utf16) |
| 77 | WARN_UNUSED_RESULT; |
| 78 |
| 79 | // Converts a CFStringRef to an STL string. Returns an empty string on failure. |
| 80 | BASE_EXPORT std::string SysCFStringRefToUTF8(CFStringRef ref) |
| 81 | WARN_UNUSED_RESULT; |
| 82 | BASE_EXPORT string16 SysCFStringRefToUTF16(CFStringRef ref) WARN_UNUSED_RESULT; |
| 83 |
| 84 | // Same, but accepts NSString input. Converts nil NSString* to the appropriate |
| 85 | // string type of length 0. |
| 86 | BASE_EXPORT std::string SysNSStringToUTF8(NSString* ref) WARN_UNUSED_RESULT; |
| 87 | BASE_EXPORT string16 SysNSStringToUTF16(NSString* ref) WARN_UNUSED_RESULT; |
| 88 |
| 89 | #endif // defined(OS_MACOSX) |
| 90 |
| 91 | } // namespace base |
| 92 |
| 93 | #endif // BASE_STRINGS_SYS_STRING_CONVERSIONS_H_ |
| 94 |