WebKit roll 98705:98715
[chromium-blink-merge.git] / base / sys_string_conversions.h
blobd2f4d1b5be6b3d142c892d70f87eb8d327ff27ba
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_SYS_STRING_CONVERSIONS_H_
6 #define BASE_SYS_STRING_CONVERSIONS_H_
7 #pragma once
9 // Provides system-dependent string type conversions for cases where it's
10 // necessary to not use ICU. Generally, you should not need this in Chrome,
11 // but it is used in some shared code. Dependencies should be minimal.
13 #include <string>
15 #include "base/base_export.h"
16 #include "base/basictypes.h"
17 #include "base/string16.h"
19 #if defined(OS_MACOSX)
20 #include <CoreFoundation/CoreFoundation.h>
21 #ifdef __OBJC__
22 @class NSString;
23 #else
24 class NSString;
25 #endif
26 #endif // OS_MACOSX
28 namespace base {
30 class StringPiece;
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 BASE_EXPORT std::wstring SysUTF8ToWide(const StringPiece& utf8);
37 // Converts between wide and the system multi-byte representations of a string.
38 // DANGER: This will lose information and can change (on Windows, this can
39 // change between reboots).
40 BASE_EXPORT std::string SysWideToNativeMB(const std::wstring& wide);
41 BASE_EXPORT std::wstring SysNativeMBToWide(const StringPiece& native_mb);
43 // Windows-specific ------------------------------------------------------------
45 #if defined(OS_WIN)
47 // Converts between 8-bit and wide strings, using the given code page. The
48 // code page identifier is one accepted by the Windows function
49 // MultiByteToWideChar().
50 BASE_EXPORT std::wstring SysMultiByteToWide(const StringPiece& mb,
51 uint32 code_page);
52 BASE_EXPORT std::string SysWideToMultiByte(const std::wstring& wide,
53 uint32 code_page);
55 #endif // defined(OS_WIN)
57 // Mac-specific ----------------------------------------------------------------
59 #if defined(OS_MACOSX)
61 // Converts between STL strings and CFStringRefs/NSStrings.
63 // Creates a string, and returns it with a refcount of 1. You are responsible
64 // for releasing it. Returns NULL on failure.
65 BASE_EXPORT CFStringRef SysUTF8ToCFStringRef(const std::string& utf8);
66 BASE_EXPORT CFStringRef SysUTF16ToCFStringRef(const string16& utf16);
67 BASE_EXPORT CFStringRef SysWideToCFStringRef(const std::wstring& wide);
69 // Same, but returns an autoreleased NSString.
70 BASE_EXPORT NSString* SysUTF8ToNSString(const std::string& utf8);
71 BASE_EXPORT NSString* SysUTF16ToNSString(const string16& utf16);
72 BASE_EXPORT NSString* SysWideToNSString(const std::wstring& wide);
74 // Converts a CFStringRef to an STL string. Returns an empty string on failure.
75 BASE_EXPORT std::string SysCFStringRefToUTF8(CFStringRef ref);
76 BASE_EXPORT string16 SysCFStringRefToUTF16(CFStringRef ref);
77 BASE_EXPORT std::wstring SysCFStringRefToWide(CFStringRef ref);
79 // Same, but accepts NSString input. Converts nil NSString* to the appropriate
80 // string type of length 0.
81 BASE_EXPORT std::string SysNSStringToUTF8(NSString* ref);
82 BASE_EXPORT string16 SysNSStringToUTF16(NSString* ref);
83 BASE_EXPORT std::wstring SysNSStringToWide(NSString* ref);
85 #endif // defined(OS_MACOSX)
87 } // namespace base
89 #endif // BASE_SYS_STRING_CONVERSIONS_H_