1 /* Copyright 2020 Google Inc. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/core/platform/windows/wide_char.h"
17 
18 #include <Windows.h>
19 
20 #include <string>
21 
22 namespace tensorflow {
23 
Utf8ToWideChar(const std::string & utf8str)24 std::wstring Utf8ToWideChar(const std::string& utf8str) {
25   int size_required = MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(),
26                                           (int)utf8str.size(), NULL, 0);
27   std::wstring ws_translated_str(size_required, 0);
28   MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(), (int)utf8str.size(),
29                       &ws_translated_str[0], size_required);
30   return ws_translated_str;
31 }
32 
WideCharToUtf8(const std::wstring & wstr)33 std::string WideCharToUtf8(const std::wstring& wstr) {
34   if (wstr.empty()) return std::string();
35   int size_required = WideCharToMultiByte(
36       CP_UTF8, 0, wstr.c_str(), (int)wstr.size(), NULL, 0, NULL, NULL);
37   std::string utf8_translated_str(size_required, 0);
38   WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), (int)wstr.size(),
39                       &utf8_translated_str[0], size_required, NULL, NULL);
40   return utf8_translated_str;
41 }
42 
43 }  // namespace tensorflow
44