michael@0: // Copyright (c) 2011 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_STRINGS_UTF_STRING_CONVERSIONS_H_ michael@0: #define BASE_STRINGS_UTF_STRING_CONVERSIONS_H_ michael@0: michael@0: #include michael@0: michael@0: #include "base/base_export.h" michael@0: #include "base/strings/string16.h" michael@0: #include "base/strings/string_piece.h" michael@0: michael@0: namespace base { michael@0: michael@0: // These convert between UTF-8, -16, and -32 strings. They are potentially slow, michael@0: // so avoid unnecessary conversions. The low-level versions return a boolean michael@0: // indicating whether the conversion was 100% valid. In this case, it will still michael@0: // do the best it can and put the result in the output buffer. The versions that michael@0: // return strings ignore this error and just return the best conversion michael@0: // possible. michael@0: BASE_EXPORT bool WideToUTF8(const wchar_t* src, size_t src_len, michael@0: std::string* output); michael@0: BASE_EXPORT std::string WideToUTF8(const std::wstring& wide); michael@0: BASE_EXPORT bool UTF8ToWide(const char* src, size_t src_len, michael@0: std::wstring* output); michael@0: BASE_EXPORT std::wstring UTF8ToWide(const StringPiece& utf8); michael@0: michael@0: BASE_EXPORT bool WideToUTF16(const wchar_t* src, size_t src_len, michael@0: string16* output); michael@0: BASE_EXPORT string16 WideToUTF16(const std::wstring& wide); michael@0: BASE_EXPORT bool UTF16ToWide(const char16* src, size_t src_len, michael@0: std::wstring* output); michael@0: BASE_EXPORT std::wstring UTF16ToWide(const string16& utf16); michael@0: michael@0: BASE_EXPORT bool UTF8ToUTF16(const char* src, size_t src_len, string16* output); michael@0: BASE_EXPORT string16 UTF8ToUTF16(const StringPiece& utf8); michael@0: BASE_EXPORT bool UTF16ToUTF8(const char16* src, size_t src_len, michael@0: std::string* output); michael@0: BASE_EXPORT std::string UTF16ToUTF8(const string16& utf16); michael@0: michael@0: // We are trying to get rid of wstring as much as possible, but it's too big michael@0: // a mess to do it all at once. These conversions should be used when we michael@0: // really should just be passing a string16 around, but we haven't finished michael@0: // porting whatever module uses wstring and the conversion is being used as a michael@0: // stopcock. This makes it easy to grep for the ones that should be removed. michael@0: #if defined(OS_WIN) michael@0: # define WideToUTF16Hack michael@0: # define UTF16ToWideHack michael@0: #else michael@0: # define WideToUTF16Hack WideToUTF16 michael@0: # define UTF16ToWideHack UTF16ToWide michael@0: #endif michael@0: michael@0: // These convert an ASCII string, typically a hardcoded constant, to a michael@0: // UTF16/Wide string. michael@0: BASE_EXPORT std::wstring ASCIIToWide(const StringPiece& ascii); michael@0: BASE_EXPORT string16 ASCIIToUTF16(const StringPiece& ascii); michael@0: michael@0: } // namespace base michael@0: michael@0: // TODO(brettw) remove these when callers are fixed up. michael@0: using base::WideToUTF8; michael@0: using base::UTF8ToWide; michael@0: using base::WideToUTF16; michael@0: using base::UTF16ToWide; michael@0: using base::UTF8ToUTF16; michael@0: using base::UTF16ToUTF8; michael@0: using base::ASCIIToWide; michael@0: using base::ASCIIToUTF16; michael@0: michael@0: #endif // BASE_STRINGS_UTF_STRING_CONVERSIONS_H_