Branch data TLA Line data Source code
1 : : // Copyright (c) 2014-2022 Thomas Fussell
2 : : // Copyright (c) 2024-2025 xlnt-community
3 : : //
4 : : // Permission is hereby granted, free of charge, to any person obtaining a copy
5 : : // of this software and associated documentation files (the "Software"), to deal
6 : : // in the Software without restriction, including without limitation the rights
7 : : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : : // copies of the Software, and to permit persons to whom the Software is
9 : : // furnished to do so, subject to the following conditions:
10 : : //
11 : : // The above copyright notice and this permission notice shall be included in
12 : : // all copies or substantial portions of the Software.
13 : : //
14 : : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : : // THE SOFTWARE
21 : : //
22 : : // @license: http://www.opensource.org/licenses/mit-license.php
23 : : // @author: see AUTHORS file
24 : :
25 : : #pragma clang diagnostic push
26 : : #pragma clang diagnostic ignored "-Wdeprecated"
27 : : #pragma clang diagnostic ignored "-Wweak-vtables"
28 : : #pragma clang diagnostic ignored "-Wsign-conversion"
29 : : #pragma clang diagnostic ignored "-Wsuggest-override"
30 : : #include <utf8.h>
31 : : #pragma clang diagnostic pop
32 : :
33 : : #include <xlnt/utils/exceptions.hpp>
34 : : #include <detail/unicode.hpp>
35 : :
36 : : namespace xlnt {
37 : : namespace detail {
38 : :
39 :CBC 27 : std::u16string utf8_to_utf16(const std::string &utf8_string)
40 : : {
41 : 27 : std::u16string result;
42 [ + + ]: 27 : utf8::utf8to16(utf8_string.begin(), utf8_string.end(), std::back_inserter(result));
43 : 27 : return result;
44 :UBC 0 : }
45 : :
46 :CBC 1 : std::u32string utf8_to_utf32(const std::string &utf8_string)
47 : : {
48 : 1 : std::u32string result;
49 [ + + ]: 1 : utf8::utf8to32(utf8_string.begin(), utf8_string.end(), std::back_inserter(result));
50 : 1 : return result;
51 :UBC 0 : }
52 : :
53 :CBC 279 : std::string utf16_to_utf8(const std::u16string &utf16_string)
54 : : {
55 : 279 : std::string result;
56 [ + + ]: 279 : utf8::utf16to8(utf16_string.begin(), utf16_string.end(), std::back_inserter(result));
57 : 279 : return result;
58 :UBC 0 : }
59 : :
60 :CBC 1 : std::string utf32_to_utf8(const std::u32string &utf32_string)
61 : : {
62 : 1 : std::string result;
63 [ + + ]: 1 : utf8::utf32to8(utf32_string.begin(), utf32_string.end(), std::back_inserter(result));
64 : 1 : return result;
65 :UBC 0 : }
66 : :
67 : 0 : std::string latin1_to_utf8(const std::string &latin1)
68 : : {
69 : 0 : std::string utf8;
70 : :
71 [ # # ]: 0 : for (auto character : latin1)
72 : : {
73 [ # # ]: 0 : if (character >= 0)
74 : : {
75 [ # ]: 0 : utf8.push_back(character);
76 : : }
77 : : else
78 : : {
79 [ # ]: 0 : utf8.push_back(static_cast<char>(0xc0 + (character >> 6)));
80 [ # ]: 0 : utf8.push_back(static_cast<char>(0x80 + (character & 0x3f)));
81 : : }
82 : : }
83 : :
84 : 0 : return utf8;
85 : 0 : }
86 : :
87 :CBC 29 : size_t string_length(const std::string &utf8_string)
88 : : {
89 [ + ]: 29 : auto end_it = utf8::find_invalid(utf8_string.begin(), utf8_string.end());
90 [ + + ]: 29 : if (end_it != utf8_string.end())
91 : : {
92 [ + + ]: 3 : throw xlnt::exception("Invalid UTF-8 encoding detected");
93 : : }
94 : :
95 [ + ]: 56 : return static_cast<std::size_t>(utf8::distance(utf8_string.begin(), end_it));
96 : : }
97 : :
98 : : #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
99 : 12 : std::u16string utf8_to_utf16(std::u8string_view utf8_string)
100 : : {
101 : 12 : std::u16string result;
102 [ + + ]: 12 : utf8::utf8to16(utf8_string.begin(), utf8_string.end(), std::back_inserter(result));
103 : 12 : return result;
104 :UBC 0 : }
105 : :
106 :CBC 1 : std::u32string utf8_to_utf32(std::u8string_view utf8_string)
107 : : {
108 : 1 : std::u32string result;
109 [ + + ]: 1 : utf8::utf8to32(utf8_string.begin(), utf8_string.end(), std::back_inserter(result));
110 : 1 : return result;
111 :UBC 0 : }
112 : :
113 :CBC 1 : std::u8string utf16_to_utf8_u8(std::u16string_view utf16_string)
114 : : {
115 : 1 : std::u8string result;
116 [ + + ]: 1 : utf8::utf16to8(utf16_string.begin(), utf16_string.end(), std::back_inserter(result));
117 : 1 : return result;
118 :UBC 0 : }
119 : :
120 :CBC 1 : std::u8string utf32_to_utf8_u8(std::u32string_view utf32_string)
121 : : {
122 : 1 : std::u8string result;
123 [ + + ]: 1 : utf8::utf32to8(utf32_string.begin(), utf32_string.end(), std::back_inserter(result));
124 : 1 : return result;
125 :UBC 0 : }
126 : : #endif
127 : :
128 : : } // namespace detail
129 : : } // namespace xlnt
|