1b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Use of this source code is governed by a BSD-style license that can be
3b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// found in the LICENSE file.
4b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
5b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/strings/utf_string_conversions.h"
6b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
7cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko#include <stdint.h>
8cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko
9b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/strings/string_piece.h"
10b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/strings/string_util.h"
11b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/strings/utf_string_conversion_utils.h"
12cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko#include "build/build_config.h"
13b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
14b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratnamespace base {
15b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
16b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratnamespace {
17b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
18b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Generalized Unicode converter -----------------------------------------------
19b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
20b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Converts the given source Unicode character type to the given destination
21b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Unicode character type as a STL string. The given input buffer and size
22b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// determine the source, and the given output STL string will be replaced by
23b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// the result.
24b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erattemplate<typename SRC_CHAR, typename DEST_STRING>
25b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratbool ConvertUnicode(const SRC_CHAR* src,
26b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                    size_t src_len,
27b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                    DEST_STRING* output) {
28b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // ICU requires 32-bit numbers.
29b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  bool success = true;
30cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko  int32_t src_len32 = static_cast<int32_t>(src_len);
31cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko  for (int32_t i = 0; i < src_len32; i++) {
32cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko    uint32_t code_point;
33b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    if (ReadUnicodeCharacter(src, src_len32, &i, &code_point)) {
34b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat      WriteUnicodeCharacter(code_point, output);
35b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    } else {
36b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat      WriteUnicodeCharacter(0xFFFD, output);
37b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat      success = false;
38b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    }
39b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  }
40b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
41b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return success;
42b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
43b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
44b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}  // namespace
45b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
46b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// UTF-8 <-> Wide --------------------------------------------------------------
47b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
48b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratbool WideToUTF8(const wchar_t* src, size_t src_len, std::string* output) {
49b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (IsStringASCII(std::wstring(src, src_len))) {
50b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    output->assign(src, src + src_len);
51b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    return true;
52b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  } else {
53b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    PrepareForUTF8Output(src, src_len, output);
54b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    return ConvertUnicode(src, src_len, output);
55b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  }
56b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
57b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
58b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratstd::string WideToUTF8(const std::wstring& wide) {
59b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (IsStringASCII(wide)) {
60b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    return std::string(wide.data(), wide.data() + wide.length());
61b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  }
62b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
63b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  std::string ret;
64b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  PrepareForUTF8Output(wide.data(), wide.length(), &ret);
65b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  ConvertUnicode(wide.data(), wide.length(), &ret);
66b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return ret;
67b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
68b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
69b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratbool UTF8ToWide(const char* src, size_t src_len, std::wstring* output) {
70b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (IsStringASCII(StringPiece(src, src_len))) {
71b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    output->assign(src, src + src_len);
72b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    return true;
73b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  } else {
74b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    PrepareForUTF16Or32Output(src, src_len, output);
75b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    return ConvertUnicode(src, src_len, output);
76b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  }
77b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
78b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
79cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenkostd::wstring UTF8ToWide(StringPiece utf8) {
80b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (IsStringASCII(utf8)) {
81b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    return std::wstring(utf8.begin(), utf8.end());
82b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  }
83b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
84b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  std::wstring ret;
85b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  PrepareForUTF16Or32Output(utf8.data(), utf8.length(), &ret);
86b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  ConvertUnicode(utf8.data(), utf8.length(), &ret);
87b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return ret;
88b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
89b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
90b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// UTF-16 <-> Wide -------------------------------------------------------------
91b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
92b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#if defined(WCHAR_T_IS_UTF16)
93b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
94b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// When wide == UTF-16, then conversions are a NOP.
95b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratbool WideToUTF16(const wchar_t* src, size_t src_len, string16* output) {
96b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  output->assign(src, src_len);
97b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return true;
98b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
99b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
100b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratstring16 WideToUTF16(const std::wstring& wide) {
101b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return wide;
102b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
103b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
104b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratbool UTF16ToWide(const char16* src, size_t src_len, std::wstring* output) {
105b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  output->assign(src, src_len);
106b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return true;
107b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
108b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
109b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratstd::wstring UTF16ToWide(const string16& utf16) {
110b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return utf16;
111b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
112b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
113b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#elif defined(WCHAR_T_IS_UTF32)
114b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
115b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratbool WideToUTF16(const wchar_t* src, size_t src_len, string16* output) {
116b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  output->clear();
117b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Assume that normally we won't have any non-BMP characters so the counts
118b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // will be the same.
119b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  output->reserve(src_len);
120b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return ConvertUnicode(src, src_len, output);
121b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
122b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
123b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratstring16 WideToUTF16(const std::wstring& wide) {
124b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  string16 ret;
125b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  WideToUTF16(wide.data(), wide.length(), &ret);
126b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return ret;
127b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
128b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
129b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratbool UTF16ToWide(const char16* src, size_t src_len, std::wstring* output) {
130b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  output->clear();
131b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Assume that normally we won't have any non-BMP characters so the counts
132b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // will be the same.
133b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  output->reserve(src_len);
134b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return ConvertUnicode(src, src_len, output);
135b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
136b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
137b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratstd::wstring UTF16ToWide(const string16& utf16) {
138b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  std::wstring ret;
139b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  UTF16ToWide(utf16.data(), utf16.length(), &ret);
140b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return ret;
141b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
142b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
143b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#endif  // defined(WCHAR_T_IS_UTF32)
144b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
145b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// UTF16 <-> UTF8 --------------------------------------------------------------
146b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
147b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#if defined(WCHAR_T_IS_UTF32)
148b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
149b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratbool UTF8ToUTF16(const char* src, size_t src_len, string16* output) {
150b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (IsStringASCII(StringPiece(src, src_len))) {
151b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    output->assign(src, src + src_len);
152b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    return true;
153b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  } else {
154b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    PrepareForUTF16Or32Output(src, src_len, output);
155b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    return ConvertUnicode(src, src_len, output);
156b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  }
157b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
158b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
159cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenkostring16 UTF8ToUTF16(StringPiece utf8) {
160b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (IsStringASCII(utf8)) {
161b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    return string16(utf8.begin(), utf8.end());
162b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  }
163b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
164b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  string16 ret;
165b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  PrepareForUTF16Or32Output(utf8.data(), utf8.length(), &ret);
166b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Ignore the success flag of this call, it will do the best it can for
167b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // invalid input, which is what we want here.
168b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  ConvertUnicode(utf8.data(), utf8.length(), &ret);
169b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return ret;
170b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
171b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
172b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratbool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) {
173b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (IsStringASCII(StringPiece16(src, src_len))) {
174b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    output->assign(src, src + src_len);
175b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    return true;
176b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  } else {
177b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    PrepareForUTF8Output(src, src_len, output);
178b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    return ConvertUnicode(src, src_len, output);
179b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  }
180b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
181b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
182cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenkostd::string UTF16ToUTF8(StringPiece16 utf16) {
183b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (IsStringASCII(utf16)) {
184b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    return std::string(utf16.begin(), utf16.end());
185b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  }
186b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
187b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  std::string ret;
188b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Ignore the success flag of this call, it will do the best it can for
189b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // invalid input, which is what we want here.
190b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  UTF16ToUTF8(utf16.data(), utf16.length(), &ret);
191b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return ret;
192b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
193b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
194b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#elif defined(WCHAR_T_IS_UTF16)
195b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Easy case since we can use the "wide" versions we already wrote above.
196b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
197b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratbool UTF8ToUTF16(const char* src, size_t src_len, string16* output) {
198b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return UTF8ToWide(src, src_len, output);
199b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
200b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
201cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenkostring16 UTF8ToUTF16(StringPiece utf8) {
202b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return UTF8ToWide(utf8);
203b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
204b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
205b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratbool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) {
206b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return WideToUTF8(src, src_len, output);
207b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
208b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
209cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenkostd::string UTF16ToUTF8(StringPiece16 utf16) {
210cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko  if (IsStringASCII(utf16))
211cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko    return std::string(utf16.data(), utf16.data() + utf16.length());
212cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko
213cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko  std::string ret;
214cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko  PrepareForUTF8Output(utf16.data(), utf16.length(), &ret);
215cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko  ConvertUnicode(utf16.data(), utf16.length(), &ret);
216cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko  return ret;
217b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
218b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
219b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#endif
220b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
221cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenkostring16 ASCIIToUTF16(StringPiece ascii) {
222b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  DCHECK(IsStringASCII(ascii)) << ascii;
223b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return string16(ascii.begin(), ascii.end());
224b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
225b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
226cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenkostd::string UTF16ToASCII(StringPiece16 utf16) {
227b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  DCHECK(IsStringASCII(utf16)) << UTF16ToUTF8(utf16);
228b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return std::string(utf16.begin(), utf16.end());
229b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
230b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
231b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}  // namespace base
232