blob: a96a687f6cf8dd3dbdc2ad8f57719fc888e6dc84 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.
[email protected]15af80e2008-08-07 03:11:424
5#ifndef BASE_SYS_STRING_CONVERSIONS_H_
6#define BASE_SYS_STRING_CONVERSIONS_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]15af80e2008-08-07 03:11:428
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.
12
13#include <string>
14#include "base/basictypes.h"
[email protected]f224d572009-02-18 21:39:2315#include "base/string16.h"
[email protected]15af80e2008-08-07 03:11:4216
[email protected]d2a10d12008-08-22 19:55:2617#if defined(OS_MACOSX)
18#include <CoreFoundation/CoreFoundation.h>
[email protected]03d95ac2008-10-08 21:02:5619#ifdef __OBJC__
20@class NSString;
21#else
22class NSString;
[email protected]d2a10d12008-08-22 19:55:2623#endif
[email protected]03d95ac2008-10-08 21:02:5624#endif // OS_MACOSX
[email protected]d2a10d12008-08-22 19:55:2625
[email protected]15af80e2008-08-07 03:11:4226namespace base {
27
[email protected]8a16266e2009-09-10 21:08:3928class StringPiece;
29
[email protected]15af80e2008-08-07 03:11:4230// Converts between wide and UTF-8 representations of a string. On error, the
31// result is system-dependent.
32std::string SysWideToUTF8(const std::wstring& wide);
[email protected]6c82baf2008-08-20 11:17:0033std::wstring SysUTF8ToWide(const StringPiece& utf8);
[email protected]15af80e2008-08-07 03:11:4234
35// Converts between wide and the system multi-byte representations of a string.
36// DANGER: This will lose information and can change (on Windows, this can
[email protected]39be4242008-08-07 18:31:4037// change between reboots).
[email protected]15af80e2008-08-07 03:11:4238std::string SysWideToNativeMB(const std::wstring& wide);
[email protected]6c82baf2008-08-20 11:17:0039std::wstring SysNativeMBToWide(const StringPiece& native_mb);
[email protected]15af80e2008-08-07 03:11:4240
41// Windows-specific ------------------------------------------------------------
42
[email protected]39be4242008-08-07 18:31:4043#if defined(OS_WIN)
[email protected]15af80e2008-08-07 03:11:4244
[email protected]47944fd2008-08-07 19:31:1645// Converts between 8-bit and wide strings, using the given code page. The
46// code page identifier is one accepted by the Windows function
[email protected]15af80e2008-08-07 03:11:4247// MultiByteToWideChar().
[email protected]6c82baf2008-08-20 11:17:0048std::wstring SysMultiByteToWide(const StringPiece& mb, uint32 code_page);
[email protected]15af80e2008-08-07 03:11:4249std::string SysWideToMultiByte(const std::wstring& wide, uint32 code_page);
50
[email protected]39be4242008-08-07 18:31:4051#endif // defined(OS_WIN)
[email protected]15af80e2008-08-07 03:11:4252
[email protected]d2a10d12008-08-22 19:55:2653// Mac-specific ----------------------------------------------------------------
[email protected]52a261f2009-03-03 15:01:1254
[email protected]d2a10d12008-08-22 19:55:2655#if defined(OS_MACOSX)
[email protected]52a261f2009-03-03 15:01:1256
[email protected]03d95ac2008-10-08 21:02:5657// Converts between STL strings and CFStringRefs/NSStrings.
[email protected]d2a10d12008-08-22 19:55:2658
59// Creates a string, and returns it with a refcount of 1. You are responsible
60// for releasing it. Returns NULL on failure.
61CFStringRef SysUTF8ToCFStringRef(const std::string& utf8);
[email protected]f224d572009-02-18 21:39:2362CFStringRef SysUTF16ToCFStringRef(const string16& utf16);
[email protected]d2a10d12008-08-22 19:55:2663CFStringRef SysWideToCFStringRef(const std::wstring& wide);
64
[email protected]03d95ac2008-10-08 21:02:5665// Same, but returns an autoreleased NSString.
66NSString* SysUTF8ToNSString(const std::string& utf8);
[email protected]f224d572009-02-18 21:39:2367NSString* SysUTF16ToNSString(const string16& utf16);
[email protected]03d95ac2008-10-08 21:02:5668NSString* SysWideToNSString(const std::wstring& wide);
69
[email protected]d2a10d12008-08-22 19:55:2670// Converts a CFStringRef to an STL string. Returns an empty string on failure.
71std::string SysCFStringRefToUTF8(CFStringRef ref);
[email protected]f224d572009-02-18 21:39:2372string16 SysCFStringRefToUTF16(CFStringRef ref);
[email protected]d2a10d12008-08-22 19:55:2673std::wstring SysCFStringRefToWide(CFStringRef ref);
74
[email protected]a2494cb2009-11-08 19:04:5475// Same, but accepts NSString input. Converts nil NSString* to the appropriate
76// string type of length 0.
[email protected]03d95ac2008-10-08 21:02:5677std::string SysNSStringToUTF8(NSString* ref);
[email protected]f224d572009-02-18 21:39:2378string16 SysNSStringToUTF16(NSString* ref);
[email protected]03d95ac2008-10-08 21:02:5679std::wstring SysNSStringToWide(NSString* ref);
80
[email protected]d2a10d12008-08-22 19:55:2681#endif // defined(OS_MACOSX)
[email protected]52a261f2009-03-03 15:01:1282
[email protected]15af80e2008-08-07 03:11:4283} // namespace base
84
85#endif // BASE_SYS_STRING_CONVERSIONS_H_