OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_UTF_STRING_CONVERSIONS_H_ | 5 #ifndef BASE_UTF_STRING_CONVERSIONS_H_ |
6 #define BASE_UTF_STRING_CONVERSIONS_H_ | 6 #define BASE_UTF_STRING_CONVERSIONS_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/string16.h" | 11 #include "base/string16.h" |
12 | 12 #include "base/string_piece.h" |
13 namespace base { | |
14 class StringPiece; | |
15 } | |
16 | 13 |
17 // These convert between UTF-8, -16, and -32 strings. They are potentially slow, | 14 // These convert between UTF-8, -16, and -32 strings. They are potentially slow, |
18 // so avoid unnecessary conversions. The low-level versions return a boolean | 15 // so avoid unnecessary conversions. The low-level versions return a boolean |
19 // indicating whether the conversion was 100% valid. In this case, it will still | 16 // indicating whether the conversion was 100% valid. In this case, it will still |
20 // do the best it can and put the result in the output buffer. The versions that | 17 // do the best it can and put the result in the output buffer. The versions that |
21 // return strings ignore this error and just return the best conversion | 18 // return strings ignore this error and just return the best conversion |
22 // possible. | 19 // possible. |
23 bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output); | 20 bool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output); |
24 std::string WideToUTF8(const std::wstring& wide); | 21 std::string WideToUTF8(const std::wstring& wide); |
25 bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output); | 22 bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output); |
26 std::wstring UTF8ToWide(const base::StringPiece& utf8); | 23 std::wstring UTF8ToWide(const base::StringPiece& utf8); |
27 | 24 |
28 bool WideToUTF16(const wchar_t* src, size_t src_len, string16* output); | 25 bool WideToUTF16(const wchar_t* src, size_t src_len, string16* output); |
29 string16 WideToUTF16(const std::wstring& wide); | 26 string16 WideToUTF16(const std::wstring& wide); |
30 bool UTF16ToWide(const char16* src, size_t src_len, std::wstring* output); | 27 bool UTF16ToWide(const char16* src, size_t src_len, std::wstring* output); |
31 std::wstring UTF16ToWide(const string16& utf16); | 28 std::wstring UTF16ToWide(const string16& utf16); |
32 | 29 |
33 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output); | 30 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output); |
34 string16 UTF8ToUTF16(const std::string& utf8); | 31 string16 UTF8ToUTF16(const base::StringPiece& utf8); |
35 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output); | 32 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output); |
36 std::string UTF16ToUTF8(const string16& utf16); | 33 std::string UTF16ToUTF8(const string16& utf16); |
37 | 34 |
38 // We are trying to get rid of wstring as much as possible, but it's too big | 35 // We are trying to get rid of wstring as much as possible, but it's too big |
39 // a mess to do it all at once. These conversions should be used when we | 36 // a mess to do it all at once. These conversions should be used when we |
40 // really should just be passing a string16 around, but we haven't finished | 37 // really should just be passing a string16 around, but we haven't finished |
41 // porting whatever module uses wstring and the conversion is being used as a | 38 // porting whatever module uses wstring and the conversion is being used as a |
42 // stopcock. This makes it easy to grep for the ones that should be removed. | 39 // stopcock. This makes it easy to grep for the ones that should be removed. |
43 #if defined(OS_WIN) | 40 #if defined(OS_WIN) |
44 # define WideToUTF16Hack | 41 # define WideToUTF16Hack |
45 # define UTF16ToWideHack | 42 # define UTF16ToWideHack |
46 #else | 43 #else |
47 # define WideToUTF16Hack WideToUTF16 | 44 # define WideToUTF16Hack WideToUTF16 |
48 # define UTF16ToWideHack UTF16ToWide | 45 # define UTF16ToWideHack UTF16ToWide |
49 #endif | 46 #endif |
50 | 47 |
51 // These convert an ASCII string, typically a hardcoded constant, to a | 48 // These convert an ASCII string, typically a hardcoded constant, to a |
52 // UTF16/Wide string. | 49 // UTF16/Wide string. |
53 // | 50 std::wstring ASCIIToWide(const base::StringPiece& ascii); |
54 // Note that this doesn't use StringPiece because it's very common to need | 51 string16 ASCIIToUTF16(const base::StringPiece& ascii); |
55 // ASCIIToUTF16("foo"), and either we have to include it in this file, or | |
56 // forward declare it and force all callers to include string_piece.h. Given | |
57 // that string_piece brings in complicated stuff like <algorithm>, it's | |
58 // easier to just duplicate these very simple definitions for the two calling | |
59 // cases we actually use. | |
60 std::wstring ASCIIToWide(const char* ascii); | |
61 std::wstring ASCIIToWide(const std::string& ascii); | |
62 string16 ASCIIToUTF16(const char* ascii); | |
63 string16 ASCIIToUTF16(const std::string& ascii); | |
64 | 52 |
65 #endif // BASE_UTF_STRING_CONVERSIONS_H_ | 53 #endif // BASE_UTF_STRING_CONVERSIONS_H_ |
OLD | NEW |