]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/matlab/src/util/unicode_conversion.cc
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / matlab / src / util / unicode_conversion.cc
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17
18 #include <locale> /* for std::wstring_convert */
19 #include <codecvt> /* for std::codecvt_utf8_utf16 */
20
21 #include "unicode_conversion.h"
22
23 namespace arrow {
24 namespace matlab {
25 namespace util {
26
27 mxArray* ConvertUTF8StringToUTF16CharMatrix(const std::string& utf8_string) {
28 // Get pointers to the start and end of the std::string data.
29 const char* string_start = utf8_string.c_str();
30 const char* string_end = string_start + utf8_string.length();
31
32 // Due to this issue on MSVC: https://stackoverflow.com/q/32055357 we cannot
33 // directly use a destination type of char16_t.
34 #if _MSC_VER >= 1900
35 using CharType = int16_t;
36 #else
37 using CharType = char16_t;
38 #endif
39 using ConverterType = std::codecvt_utf8_utf16<CharType>;
40 std::wstring_convert<ConverterType, CharType> code_converter{};
41
42 std::basic_string<CharType> utf16_string;
43 try {
44 utf16_string = code_converter.from_bytes(string_start, string_end);
45 } catch (...) {
46 // In the case that any error occurs, just try returning a string in the
47 // user's current locale instead.
48 return mxCreateString(string_start);
49 }
50
51 // Store the converter UTF-16 string in a mxCharMatrix and return it.
52 const mwSize dimensions[2] = {1, utf16_string.size()};
53 mxArray* character_matrix = mxCreateCharArray(2, dimensions);
54 mxChar* character_matrix_pointer = mxGetChars(character_matrix);
55 std::copy(utf16_string.data(), utf16_string.data() + utf16_string.size(),
56 character_matrix_pointer);
57
58 return character_matrix;
59 }
60
61 } // namespace util
62 } // namespace matlab
63 } // namespace arrow