1 // Copyright (c) 2006-2008 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_
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.
13 #include "base/basictypes.h"
14 #include "base/string16.h"
16 #if defined(OS_MACOSX)
17 #include <CoreFoundation/CoreFoundation.h>
29 // Converts between wide and UTF-8 representations of a string. On error, the
30 // result is system-dependent.
31 std::string
SysWideToUTF8(const std::wstring
& wide
);
32 std::wstring
SysUTF8ToWide(const StringPiece
& utf8
);
34 // Converts between wide and the system multi-byte representations of a string.
35 // DANGER: This will lose information and can change (on Windows, this can
36 // change between reboots).
37 std::string
SysWideToNativeMB(const std::wstring
& wide
);
38 std::wstring
SysNativeMBToWide(const StringPiece
& native_mb
);
40 // Windows-specific ------------------------------------------------------------
44 // Converts between 8-bit and wide strings, using the given code page. The
45 // code page identifier is one accepted by the Windows function
46 // MultiByteToWideChar().
47 std::wstring
SysMultiByteToWide(const StringPiece
& mb
, uint32 code_page
);
48 std::string
SysWideToMultiByte(const std::wstring
& wide
, uint32 code_page
);
50 #endif // defined(OS_WIN)
52 // Mac-specific ----------------------------------------------------------------
54 #if defined(OS_MACOSX)
56 // Converts between STL strings and CFStringRefs/NSStrings.
58 // Creates a string, and returns it with a refcount of 1. You are responsible
59 // for releasing it. Returns NULL on failure.
60 CFStringRef
SysUTF8ToCFStringRef(const std::string
& utf8
);
61 CFStringRef
SysUTF16ToCFStringRef(const string16
& utf16
);
62 CFStringRef
SysWideToCFStringRef(const std::wstring
& wide
);
64 // Same, but returns an autoreleased NSString.
65 NSString
* SysUTF8ToNSString(const std::string
& utf8
);
66 NSString
* SysUTF16ToNSString(const string16
& utf16
);
67 NSString
* SysWideToNSString(const std::wstring
& wide
);
69 // Converts a CFStringRef to an STL string. Returns an empty string on failure.
70 std::string
SysCFStringRefToUTF8(CFStringRef ref
);
71 string16
SysCFStringRefToUTF16(CFStringRef ref
);
72 std::wstring
SysCFStringRefToWide(CFStringRef ref
);
74 // Same, but accepts NSString input. Converts nil NSString* to the appropriate
75 // string type of length 0.
76 std::string
SysNSStringToUTF8(NSString
* ref
);
77 string16
SysNSStringToUTF16(NSString
* ref
);
78 std::wstring
SysNSStringToWide(NSString
* ref
);
80 #endif // defined(OS_MACOSX)
84 #endif // BASE_SYS_STRING_CONVERSIONS_H_