12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file. 42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef BASE_STRINGS_SYS_STRING_CONVERSIONS_H_ 62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define BASE_STRINGS_SYS_STRING_CONVERSIONS_H_ 72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Provides system-dependent string type conversions for cases where it's 92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// necessary to not use ICU. Generally, you should not need this in Chrome, 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// but it is used in some shared code. Dependencies should be minimal. 112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string> 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/base_export.h" 152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/basictypes.h" 16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string16.h" 17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/strings/string_piece.h" 182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(OS_MACOSX) 202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <CoreFoundation/CoreFoundation.h> 212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifdef __OBJC__ 222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)@class NSString; 232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#else 242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class NSString; 252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif 262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif // OS_MACOSX 272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace base { 292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Converts between wide and UTF-8 representations of a string. On error, the 312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// result is system-dependent. 322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT std::string SysWideToUTF8(const std::wstring& wide); 332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT std::wstring SysUTF8ToWide(const StringPiece& utf8); 342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Converts between wide and the system multi-byte representations of a string. 362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// DANGER: This will lose information and can change (on Windows, this can 372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// change between reboots). 382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT std::string SysWideToNativeMB(const std::wstring& wide); 392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT std::wstring SysNativeMBToWide(const StringPiece& native_mb); 402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Windows-specific ------------------------------------------------------------ 422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(OS_WIN) 442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Converts between 8-bit and wide strings, using the given code page. The 462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// code page identifier is one accepted by the Windows function 472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// MultiByteToWideChar(). 482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT std::wstring SysMultiByteToWide(const StringPiece& mb, 492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) uint32 code_page); 502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT std::string SysWideToMultiByte(const std::wstring& wide, 512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) uint32 code_page); 522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif // defined(OS_WIN) 542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Mac-specific ---------------------------------------------------------------- 562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(OS_MACOSX) 582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Converts between STL strings and CFStringRefs/NSStrings. 602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Creates a string, and returns it with a refcount of 1. You are responsible 622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// for releasing it. Returns NULL on failure. 632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT CFStringRef SysUTF8ToCFStringRef(const std::string& utf8); 642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT CFStringRef SysUTF16ToCFStringRef(const string16& utf16); 652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Same, but returns an autoreleased NSString. 672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT NSString* SysUTF8ToNSString(const std::string& utf8); 682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT NSString* SysUTF16ToNSString(const string16& utf16); 692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Converts a CFStringRef to an STL string. Returns an empty string on failure. 712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT std::string SysCFStringRefToUTF8(CFStringRef ref); 722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT string16 SysCFStringRefToUTF16(CFStringRef ref); 732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Same, but accepts NSString input. Converts nil NSString* to the appropriate 752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// string type of length 0. 762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT std::string SysNSStringToUTF8(NSString* ref); 772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT string16 SysNSStringToUTF16(NSString* ref); 782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif // defined(OS_MACOSX) 802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} // namespace base 822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif // BASE_STRINGS_SYS_STRING_CONVERSIONS_H_ 84