1c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// found in the LICENSE file.
4c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#ifndef BASE_STRINGS_UTF_STRING_CONVERSIONS_H_
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#define BASE_STRINGS_UTF_STRING_CONVERSIONS_H_
7c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include <string>
9c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/base_export.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string16.h"
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/strings/string_piece.h"
13c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace base {
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
16c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// These convert between UTF-8, -16, and -32 strings. They are potentially slow,
17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// so avoid unnecessary conversions. The low-level versions return a boolean
18c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// indicating whether the conversion was 100% valid. In this case, it will still
19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// do the best it can and put the result in the output buffer. The versions that
20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// return strings ignore this error and just return the best conversion
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// possible.
22c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT bool WideToUTF8(const wchar_t* src, size_t src_len,
23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            std::string* output);
24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT std::string WideToUTF8(const std::wstring& wide);
25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT bool UTF8ToWide(const char* src, size_t src_len,
26c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            std::wstring* output);
27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT std::wstring UTF8ToWide(const StringPiece& utf8);
28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT bool WideToUTF16(const wchar_t* src, size_t src_len,
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             string16* output);
31c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT string16 WideToUTF16(const std::wstring& wide);
32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT bool UTF16ToWide(const char16* src, size_t src_len,
33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             std::wstring* output);
34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT std::wstring UTF16ToWide(const string16& utf16);
35c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
36c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT bool UTF8ToUTF16(const char* src, size_t src_len, string16* output);
37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT string16 UTF8ToUTF16(const StringPiece& utf8);
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT bool UTF16ToUTF8(const char16* src, size_t src_len,
39c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             std::string* output);
40c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT std::string UTF16ToUTF8(const string16& utf16);
41c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// These convert an ASCII string, typically a hardcoded constant, to a
43c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// UTF16/Wide string.
44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT std::wstring ASCIIToWide(const StringPiece& ascii);
45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)BASE_EXPORT string16 ASCIIToUTF16(const StringPiece& ascii);
46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Converts to 7-bit ASCII by truncating. The result must be known to be ASCII
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// beforehand.
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)BASE_EXPORT std::string UTF16ToASCII(const string16& utf16);
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
51c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace base
52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif  // BASE_STRINGS_UTF_STRING_CONVERSIONS_H_
54