File: | /rootdir/_build/../src/icu-converter.cc |
Warning: | line 121, column 9 Value stored to 'target_size' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* |
2 | * Copyright © 2019 Christian Persch |
3 | * |
4 | * This library is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Lesser General Public |
6 | * License as published by the Free Software Foundation; either |
7 | * version 3 of the License, or (at your option) any later version. |
8 | * |
9 | * This library is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Lesser General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
16 | */ |
17 | |
18 | #include "config.h" |
19 | |
20 | #include "icu-converter.hh" |
21 | |
22 | #include <cassert> |
23 | #include <memory> |
24 | |
25 | #include <unicode/errorcode.h> |
26 | |
27 | #include "debug.h" |
28 | #include "icu-glue.hh" |
29 | |
30 | namespace bte::base { |
31 | |
32 | std::unique_ptr<ICUConverter> |
33 | ICUConverter::make(char const* charset, |
34 | GError** error) |
35 | { |
36 | if (bte::base::get_icu_charset_is_ecma35(charset)) |
37 | return {}; |
38 | |
39 | auto charset_converter = bte::base::make_icu_converter(charset, error); |
40 | if (!charset_converter) |
41 | return {}; |
42 | |
43 | auto u32_converter = bte::base::make_icu_converter("utf32platformendian", error); |
44 | if (!u32_converter) |
45 | return {}; |
46 | |
47 | auto u8_converter = bte::base::make_icu_converter("utf8", error); |
48 | if (!u8_converter) |
49 | return {}; |
50 | |
51 | return std::make_unique<ICUConverter>(charset, charset_converter, u32_converter, u8_converter); |
52 | } |
53 | |
54 | std::string |
55 | ICUConverter::convert(std::string_view const& data) |
56 | { |
57 | /* We can't use ucnv_convertEx since that doesn't support preflighting. |
58 | * Instead, convert to UTF-16 first, and the to the target, with |
59 | * preflighting both times. This is slow, but this is the legacy |
60 | * code path, so we don't care. |
61 | */ |
62 | |
63 | if (data.size() == 0) |
64 | return {}; |
65 | |
66 | ucnv_resetToUnicodeucnv_resetToUnicode_72(m_u8_converter.get()); |
67 | |
68 | auto err = icu::ErrorCode{}; |
69 | auto u16_size = ucnv_toUCharsucnv_toUChars_72(m_u8_converter.get(), |
70 | nullptr, 0, |
71 | data.data(), data.size(), |
72 | err); |
73 | if (err.isFailure() && (err.get() != U_BUFFER_OVERFLOW_ERROR)) { |
74 | _bte_debug_print(BTE_DEBUG_CONVERSION,do { } while(0) |
75 | "Error converting from UTF-8 to UTF-16 in preflight: %s\n",do { } while(0) |
76 | err.errorName())do { } while(0); |
77 | return {}; |
78 | } |
79 | |
80 | auto u16_buffer = std::u16string{}; |
81 | if ((size_t)u16_size > u16_buffer.max_size()) // prevent exceptions |
82 | return {}; |
83 | u16_buffer.resize(u16_size); |
84 | |
85 | err.reset(); |
86 | u16_size = ucnv_toUCharsucnv_toUChars_72(m_u8_converter.get(), |
87 | u16_buffer.data(), |
88 | u16_buffer.size(), |
89 | data.data(), |
90 | data.size(), |
91 | err); |
92 | if (err.isFailure()) { |
93 | _bte_debug_print(BTE_DEBUG_CONVERSION,do { } while(0) |
94 | "Error converting from UTF-8 to UTF-16: %s\n",do { } while(0) |
95 | err.errorName())do { } while(0); |
96 | return {}; |
97 | } |
98 | |
99 | /* Now convert to target */ |
100 | ucnv_resetFromUnicodeucnv_resetFromUnicode_72(m_charset_converter.get()); |
101 | err.reset(); |
102 | auto target_size = ucnv_fromUCharsucnv_fromUChars_72(m_charset_converter.get(), |
103 | nullptr, 0, |
104 | u16_buffer.data(), |
105 | u16_size, |
106 | err); |
107 | if (err.isFailure() && (err.get() != U_BUFFER_OVERFLOW_ERROR)) { |
108 | _bte_debug_print(BTE_DEBUG_CONVERSION,do { } while(0) |
109 | "Error converting from UTF-8 to %s in preflight: %s\n",do { } while(0) |
110 | m_charset.c_str(),do { } while(0) |
111 | err.errorName())do { } while(0); |
112 | return {}; |
113 | } |
114 | |
115 | auto target_buffer = std::string{}; |
116 | if ((size_t)target_size > target_buffer.max_size()) // prevent exceptions |
117 | return {}; |
118 | target_buffer.resize(target_size); |
119 | |
120 | err.reset(); |
121 | target_size = ucnv_fromUCharsucnv_fromUChars_72(m_charset_converter.get(), |
Value stored to 'target_size' is never read | |
122 | target_buffer.data(), |
123 | target_buffer.capacity(), |
124 | u16_buffer.data(), |
125 | u16_size, |
126 | err); |
127 | if (err.isFailure()) { |
128 | _bte_debug_print(BTE_DEBUG_CONVERSION,do { } while(0) |
129 | "Error converting from UTF-16 to %s: %s\n",do { } while(0) |
130 | m_charset.c_str(),do { } while(0) |
131 | err.errorName())do { } while(0); |
132 | return {}; |
133 | } |
134 | |
135 | return target_buffer; |
136 | } |
137 | |
138 | } // namespace bte::base |