]>
git.proxmox.com Git - ceph.git/blob - 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
9 // http://www.apache.org/licenses/LICENSE-2.0
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
18 #include <locale> /* for std::wstring_convert */
19 #include <codecvt> /* for std::codecvt_utf8_utf16 */
21 #include "unicode_conversion.h"
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();
32 // Due to this issue on MSVC: https://stackoverflow.com/q/32055357 we cannot
33 // directly use a destination type of char16_t.
35 using CharType
= int16_t;
37 using CharType
= char16_t
;
39 using ConverterType
= std::codecvt_utf8_utf16
<CharType
>;
40 std::wstring_convert
<ConverterType
, CharType
> code_converter
{};
42 std::basic_string
<CharType
> utf16_string
;
44 utf16_string
= code_converter
.from_bytes(string_start
, string_end
);
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
);
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
);
58 return character_matrix
;