Flutter Engine
string_conversion.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/shell/platform/windows/string_conversion.h"
6 
7 #include <Windows.h>
8 
9 namespace flutter {
10 
11 std::string Utf8FromUtf16(const std::wstring& utf16_string) {
12  if (utf16_string.empty()) {
13  return std::string();
14  }
15  int target_length = ::WideCharToMultiByte(
16  CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string.data(),
17  static_cast<int>(utf16_string.length()), nullptr, 0, nullptr, nullptr);
18  if (target_length == 0) {
19  return std::string();
20  }
21  std::string utf8_string;
22  utf8_string.resize(target_length);
23  int converted_length = ::WideCharToMultiByte(
24  CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string.data(),
25  static_cast<int>(utf16_string.length()), utf8_string.data(),
26  target_length, nullptr, nullptr);
27  if (converted_length == 0) {
28  return std::string();
29  }
30  return utf8_string;
31 }
32 
33 std::wstring Utf16FromUtf8(const std::string& utf8_string) {
34  if (utf8_string.empty()) {
35  return std::wstring();
36  }
37  int target_length =
38  ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8_string.data(),
39  static_cast<int>(utf8_string.length()), nullptr, 0);
40  if (target_length == 0) {
41  return std::wstring();
42  }
43  std::wstring utf16_string;
44  utf16_string.resize(target_length);
45  int converted_length =
46  ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8_string.data(),
47  static_cast<int>(utf8_string.length()),
48  utf16_string.data(), target_length);
49  if (converted_length == 0) {
50  return std::wstring();
51  }
52  return utf16_string;
53 }
54 
55 } // namespace flutter
std::string Utf8FromUtf16(const std::wstring &utf16_string)
std::wstring Utf16FromUtf8(const std::string &utf8_string)