1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "utf.h"
18 
19 #include <cstring>
20 #include <iterator>
21 #include <string>
22 
23 #include "unchecked.h"
24 
25 namespace pdfClient {
26 
Utf8ToUtf32(std::string_view utf8)27 std::u32string Utf8ToUtf32(std::string_view utf8) {
28     std::u32string result;
29     unchecked::utf8to32(utf8.begin(), utf8.end(), std::back_inserter(result));
30     return result;
31 }
32 
Utf8ToUtf32(const char * utf8)33 std::u32string Utf8ToUtf32(const char* utf8) {
34     std::u32string result;
35     unchecked::utf8to32(utf8, utf8 + strlen(utf8), std::back_inserter(result));
36     return result;
37 }
38 
Utf16ToUtf8(const std::u16string & utf16)39 std::string Utf16ToUtf8(const std::u16string& utf16) {
40     std::string result;
41     unchecked::utf16to8(utf16.begin(), utf16.end(), std::back_inserter(result));
42     return result;
43 }
44 
AppendCodepointAsUtf8(const char32_t codepoint,std::string * output)45 void AppendCodepointAsUtf8(const char32_t codepoint, std::string* output) {
46     unchecked::append(codepoint, std::back_inserter(*output));
47 }
48 
EraseTrailingNulls(std::string * str)49 void EraseTrailingNulls(std::string* str) {
50     str->erase(str->find_last_not_of('\0') + 1);
51 }
52 
53 }  // namespace pdfClient