OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "base/strings/utf_string_conversions.h" | 5 #include "base/strings/utf_string_conversions.h" |
6 | 6 |
7 #include "base/strings/string_piece.h" | 7 #include "base/strings/string_piece.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/utf_string_conversion_utils.h" | 9 #include "base/strings/utf_string_conversion_utils.h" |
10 | 10 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 | 49 |
50 std::string WideToUTF8(const std::wstring& wide) { | 50 std::string WideToUTF8(const std::wstring& wide) { |
51 std::string ret; | 51 std::string ret; |
52 // Ignore the success flag of this call, it will do the best it can for | 52 // Ignore the success flag of this call, it will do the best it can for |
53 // invalid input, which is what we want here. | 53 // invalid input, which is what we want here. |
54 WideToUTF8(wide.data(), wide.length(), &ret); | 54 WideToUTF8(wide.data(), wide.length(), &ret); |
55 return ret; | 55 return ret; |
56 } | 56 } |
57 | 57 |
58 bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output) { | 58 bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output) { |
59 PrepareForUTF16Or32Output(src, src_len, output); | 59 if (IsStringASCII(StringPiece(src, src_len))) { |
60 return ConvertUnicode(src, src_len, output); | 60 output->assign(src, src + src_len); |
| 61 return true; |
| 62 } else { |
| 63 PrepareForUTF16Or32Output(src, src_len, output); |
| 64 return ConvertUnicode(src, src_len, output); |
| 65 } |
61 } | 66 } |
62 | 67 |
63 std::wstring UTF8ToWide(const StringPiece& utf8) { | 68 std::wstring UTF8ToWide(const StringPiece& utf8) { |
| 69 if (IsStringASCII(utf8)) { |
| 70 return std::wstring(utf8.begin(), utf8.end()); |
| 71 } |
| 72 |
64 std::wstring ret; | 73 std::wstring ret; |
65 UTF8ToWide(utf8.data(), utf8.length(), &ret); | 74 PrepareForUTF16Or32Output(utf8.data(), utf8.length(), &ret); |
| 75 ConvertUnicode(utf8.data(), utf8.length(), &ret); |
66 return ret; | 76 return ret; |
67 } | 77 } |
68 | 78 |
69 // UTF-16 <-> Wide ------------------------------------------------------------- | 79 // UTF-16 <-> Wide ------------------------------------------------------------- |
70 | 80 |
71 #if defined(WCHAR_T_IS_UTF16) | 81 #if defined(WCHAR_T_IS_UTF16) |
72 | 82 |
73 // When wide == UTF-16, then conversions are a NOP. | 83 // When wide == UTF-16, then conversions are a NOP. |
74 bool WideToUTF16(const wchar_t* src, size_t src_len, string16* output) { | 84 bool WideToUTF16(const wchar_t* src, size_t src_len, string16* output) { |
75 output->assign(src, src_len); | 85 output->assign(src, src_len); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 return ret; | 129 return ret; |
120 } | 130 } |
121 | 131 |
122 #endif // defined(WCHAR_T_IS_UTF32) | 132 #endif // defined(WCHAR_T_IS_UTF32) |
123 | 133 |
124 // UTF16 <-> UTF8 -------------------------------------------------------------- | 134 // UTF16 <-> UTF8 -------------------------------------------------------------- |
125 | 135 |
126 #if defined(WCHAR_T_IS_UTF32) | 136 #if defined(WCHAR_T_IS_UTF32) |
127 | 137 |
128 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { | 138 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { |
129 PrepareForUTF16Or32Output(src, src_len, output); | 139 if (IsStringASCII(StringPiece(src, src_len))) { |
130 return ConvertUnicode(src, src_len, output); | 140 output->assign(src, src + src_len); |
| 141 return true; |
| 142 } else { |
| 143 PrepareForUTF16Or32Output(src, src_len, output); |
| 144 return ConvertUnicode(src, src_len, output); |
| 145 } |
131 } | 146 } |
132 | 147 |
133 string16 UTF8ToUTF16(const StringPiece& utf8) { | 148 string16 UTF8ToUTF16(const StringPiece& utf8) { |
| 149 if (IsStringASCII(utf8)) { |
| 150 return string16(utf8.begin(), utf8.end()); |
| 151 } |
| 152 |
134 string16 ret; | 153 string16 ret; |
| 154 PrepareForUTF16Or32Output(utf8.data(), utf8.length(), &ret); |
135 // Ignore the success flag of this call, it will do the best it can for | 155 // Ignore the success flag of this call, it will do the best it can for |
136 // invalid input, which is what we want here. | 156 // invalid input, which is what we want here. |
137 UTF8ToUTF16(utf8.data(), utf8.length(), &ret); | 157 ConvertUnicode(utf8.data(), utf8.length(), &ret); |
138 return ret; | 158 return ret; |
139 } | 159 } |
140 | 160 |
141 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) { | 161 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) { |
142 PrepareForUTF8Output(src, src_len, output); | 162 PrepareForUTF8Output(src, src_len, output); |
143 return ConvertUnicode(src, src_len, output); | 163 return ConvertUnicode(src, src_len, output); |
144 } | 164 } |
145 | 165 |
146 std::string UTF16ToUTF8(const string16& utf16) { | 166 std::string UTF16ToUTF8(const string16& utf16) { |
147 std::string ret; | 167 std::string ret; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 DCHECK(IsStringASCII(ascii)) << ascii; | 201 DCHECK(IsStringASCII(ascii)) << ascii; |
182 return string16(ascii.begin(), ascii.end()); | 202 return string16(ascii.begin(), ascii.end()); |
183 } | 203 } |
184 | 204 |
185 std::string UTF16ToASCII(const string16& utf16) { | 205 std::string UTF16ToASCII(const string16& utf16) { |
186 DCHECK(IsStringASCII(utf16)) << UTF16ToUTF8(utf16); | 206 DCHECK(IsStringASCII(utf16)) << UTF16ToUTF8(utf16); |
187 return std::string(utf16.begin(), utf16.end()); | 207 return std::string(utf16.begin(), utf16.end()); |
188 } | 208 } |
189 | 209 |
190 } // namespace base | 210 } // namespace base |
OLD | NEW |