Arcane  v4.1.0.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
BasicTranscoder.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2025 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* BasicTranscoder.cc (C) 2000-2025 */
9/* */
10/* Conversions entre utf8 et utf16. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arccore/base/CoreArray.h"
15#include "arccore/base/BasicTranscoder.h"
16
17#ifdef ARCCORE_HAS_GLIB
18#include <glib.h>
19#else
20#include <cwctype>
21#endif
22
23#include <iostream>
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28namespace
29{
30using namespace Arcane;
31
32bool _isSpace(Int32 wc)
33{
34#ifdef ARCCORE_HAS_GLIB
35 return g_unichar_isspace(wc);
36#else
37 return std::iswspace(wc);
38#endif
39}
40Int32 _toUpper(Int32 wc)
41{
42#ifdef ARCCORE_HAS_GLIB
43 return g_unichar_toupper(wc);
44#else
45 return std::towupper(wc);
46#endif
47}
48Int32 _toLower(Int32 wc)
49{
50#ifdef ARCCORE_HAS_GLIB
51 return g_unichar_tolower(wc);
52#else
53 return std::towlower(wc);
54#endif
55}
56
57int _invalidChar(Int32 pos, Int32& wc)
58{
59 std::cout << "WARNING: Invalid sequence '" << wc << "' in conversion input (position=" << pos << ")\n";
60 wc = '?';
61 return 1;
62}
63
64int _notEnoughChar(Int32& wc)
65{
66 std::cout << "WARNING: Invalid sequence '" << wc << "' in conversion input (unexpected eof)\n";
67 wc = '?';
68 return 1;
69}
70
71/*---------------------------------------------------------------------------*/
72/*---------------------------------------------------------------------------*/
84void ucs4_to_utf8(Int32 wc, Impl::CoreArray<Byte>& utf8)
85{
86 Int32 r[6];
87 int count;
88 if (wc < 0x80)
89 count = 1;
90 else if (wc < 0x800)
91 count = 2;
92 else if (wc < 0x10000)
93 count = 3;
94 else if (wc < 0x200000)
95 count = 4;
96 else if (wc < 0x4000000)
97 count = 5;
98 else
99 count = 6;
100 switch (count) { /* note: code falls through cases! */
101 case 6:
102 r[5] = 0x80 | (wc & 0x3f);
103 wc = wc >> 6;
104 wc |= 0x4000000;
105 [[fallthrough]];
106 case 5:
107 r[4] = 0x80 | (wc & 0x3f);
108 wc = wc >> 6;
109 wc |= 0x200000;
110 [[fallthrough]];
111 case 4:
112 r[3] = 0x80 | (wc & 0x3f);
113 wc = wc >> 6;
114 wc |= 0x10000;
115 [[fallthrough]];
116 case 3:
117 r[2] = 0x80 | (wc & 0x3f);
118 wc = wc >> 6;
119 wc |= 0x800;
120 [[fallthrough]];
121 case 2:
122 r[1] = 0x80 | (wc & 0x3f);
123 wc = wc >> 6;
124 wc |= 0xc0;
125 [[fallthrough]];
126 case 1:
127 r[0] = wc;
128 }
129 for (int i = 0; i < count; ++i)
130 utf8.add((Byte)r[i]);
131}
132/*---------------------------------------------------------------------------*/
133/*---------------------------------------------------------------------------*/
146Int64 utf8_to_ucs4(Span<const Byte> uchar, Int64 index, Int32& wc)
147{
148 const Byte* s = uchar.data() + index;
149 unsigned char c = s[0];
150 Int64 n = uchar.size() - index;
151 if (c < 0x80) {
152 wc = c;
153 return 1;
154 }
155
156 if (c < 0xc2)
157 return _invalidChar(1, wc);
158
159 if (c < 0xe0) {
160 if (n < 2)
161 return _notEnoughChar(wc);
162 if (!((s[1] ^ 0x80) < 0x40))
163 return _invalidChar(2, wc);
164 wc = ((Int32)(c & 0x1f) << 6) | (Int32)(s[1] ^ 0x80);
165 return 2;
166 }
167
168 if (c < 0xf0) {
169 if (n < 3)
170 return _notEnoughChar(wc);
171 if (!((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40 && (c >= 0xe1 || s[1] >= 0xa0)))
172 return _invalidChar(4, wc);
173 wc = ((Int32)(c & 0x0f) << 12) | ((Int32)(s[1] ^ 0x80) << 6) | (Int32)(s[2] ^ 0x80);
174 return 3;
175 }
176
177 if (c < 0xf8) {
178 if (n < 4)
179 return _notEnoughChar(wc);
180 if (!((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40 && (s[3] ^ 0x80) < 0x40 && (c >= 0xf1 || s[1] >= 0x90)))
181 return _invalidChar(5, wc);
182 wc = ((Int32)(c & 0x07) << 18) | ((Int32)(s[1] ^ 0x80) << 12) | ((Int32)(s[2] ^ 0x80) << 6) | (Int32)(s[3] ^ 0x80);
183 return 4;
184 }
185
186 // On ne devrait jamais arriver ici
187 // car il n'y a plus (depuis la norme 2003) de caractères UTF-8
188 // encodés sur 5 ou 6 octets.
189
190 if (c < 0xfc) {
191 if (n < 5)
192 return _notEnoughChar(wc);
193 if (!((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40 && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40 && (c >= 0xf9 || s[1] >= 0x88)))
194 return _invalidChar(7, wc);
195 wc = ((Int32)(c & 0x03) << 24) | ((Int32)(s[1] ^ 0x80) << 18) | ((Int32)(s[2] ^ 0x80) << 12) | ((Int32)(s[3] ^ 0x80) << 6) | (Int32)(s[4] ^ 0x80);
196 return 5;
197 }
198 if (c < 0xfe) {
199 if (n < 6)
200 return _notEnoughChar(wc);
201 if (!((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40 && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40 && (s[5] ^ 0x80) < 0x40 && (c >= 0xfd || s[1] >= 0x84)))
202 return _invalidChar(8, wc);
203 wc = ((Int32)(c & 0x01) << 30) | ((Int32)(s[1] ^ 0x80) << 24) | ((Int32)(s[2] ^ 0x80) << 18) | ((Int32)(s[3] ^ 0x80) << 12) | ((Int32)(s[4] ^ 0x80) << 6) | (Int32)(s[5] ^ 0x80);
204 return 6;
205 }
206 return _invalidChar(9, wc);
207}
208
209/*---------------------------------------------------------------------------*/
210/*---------------------------------------------------------------------------*/
223Int64 utf16_to_ucs4(Span<const UChar> uchar, Int64 index, Int32& wc)
224{
225 wc = uchar[index];
226 if (wc >= 0xd800 && wc < 0xdc00) {
227 if ((index + 1) == uchar.size()) {
228 std::cout << "WARNING: utf16_to_ucs4(): Invalid sequence in conversion input (unexpected eof)\n";
229 wc = 0x1A;
230 return 1;
231 }
232 Int32 wc2 = uchar[index + 1];
233 if (!(wc2 >= 0xdc00 && wc2 < 0xe000)) {
234 std::cout << "WARNING: utf16_to_ucs4(): Invalid sequence (1) '" << wc2 << "' in conversion input\n";
235 wc = 0x1A;
236 return 1;
237 }
238 wc = (0x10000 + ((wc - 0xd800) << 10) + (wc2 - 0xdc00));
239 return 2;
240 }
241 else if (wc >= 0xdc00 && wc < 0xe0000) {
242 std::cout << "WARNING: utf16_to_ucs4(): Invalid sequence (2) '" << wc << "' in conversion input\n";
243 wc = 0x1A;
244 return 1;
245 }
246 return 1;
247}
248
249/*---------------------------------------------------------------------------*/
250/*---------------------------------------------------------------------------*/
251
252} // namespace
253
254/*---------------------------------------------------------------------------*/
255/*---------------------------------------------------------------------------*/
267void
268ucs4_to_utf16(Int32 wc,Impl::CoreArray<UChar>& uchar)
269{
270 if (wc < 0xd800){
271 uchar.add((UChar)wc);
272 return;
273 }
274 if (wc < 0xe000){
275 std::cout << "WARNING: ucs4_to_utf16(): Invalid sequence in conversion input\n";
276 uchar.add(0x1A);
277 return;
278 }
279 if (wc < 0x10000){
280 uchar.add((UChar)wc);
281 return;
282 }
283 if (wc < 0x110000){
284 uchar.add( (UChar) ((wc - 0x10000) / 0x400 + 0xd800) );
285 uchar.add( (UChar) ((wc - 0x10000) % 0x400 + 0xdc00) );
286 return;
287 }
288 std::cerr << "WARNING: ucs4_to_utf16(): Invalid sequence in conversion input\n";
289 uchar.add(0x1A);
290}
291
292/*---------------------------------------------------------------------------*/
293/*---------------------------------------------------------------------------*/
294
295namespace Arcane::Impl
296{
297
298/*---------------------------------------------------------------------------*/
299/*---------------------------------------------------------------------------*/
300
301Int64 BasicTranscoder::
302stringLen(const UChar* ustr)
303{
304 if (!ustr || ustr[0] == 0)
305 return 0;
306 const UChar* u = ustr + 1;
307 while ((*u) != 0)
308 ++u;
309 return arccoreCheckLargeArraySize((std::size_t)(u - ustr));
310}
311
312/*---------------------------------------------------------------------------*/
313/*---------------------------------------------------------------------------*/
314
316void BasicTranscoder::
317transcodeFromUtf16ToUtf8(Span<const UChar> utf16, CoreArray<Byte>& utf8)
318{
319 for (Int64 i = 0, n = utf16.size(); i < n;) {
320 Int32 wc;
321 i += utf16_to_ucs4(utf16, i, wc);
322 ucs4_to_utf8(wc, utf8);
323 }
324}
325
326/*---------------------------------------------------------------------------*/
327/*---------------------------------------------------------------------------*/
328
329void BasicTranscoder::
330transcodeFromUtf8ToUtf16(Span<const Byte> utf8, CoreArray<UChar>& utf16)
331{
332 for (Int64 i = 0, n = utf8.size(); i < n;) {
333 Int32 wc;
334 i += utf8_to_ucs4(utf8, i, wc);
335 ucs4_to_utf16(wc, utf16);
336 }
337}
338
339/*---------------------------------------------------------------------------*/
340/*---------------------------------------------------------------------------*/
341
342void BasicTranscoder::
343replaceWS(CoreArray<Byte>& out_utf8)
344{
345 Impl::CoreArray<Byte> copy_utf8(out_utf8);
346 Span<const Byte> utf8(copy_utf8);
347 out_utf8.clear();
348 for (Int64 i = 0, n = utf8.size(); i < n;) {
349 Int32 wc;
350 i += utf8_to_ucs4(utf8, i, wc);
351 if (_isSpace(wc))
352 out_utf8.add(' ');
353 else
354 ucs4_to_utf8(wc, out_utf8);
355 }
356}
357
358/*---------------------------------------------------------------------------*/
359/*---------------------------------------------------------------------------*/
360
361void BasicTranscoder::
362collapseWS(CoreArray<Byte>& out_utf8)
363{
364 Impl::CoreArray<Byte> copy_utf8(out_utf8);
365 Span<const Byte> utf8(copy_utf8);
366 out_utf8.clear();
367 Int64 i = 0;
368 Int64 n = utf8.size();
369 // Si la chaîne est vide, retourne une chaîne vide.
370 if (n == 1) {
371 out_utf8.add('\0');
372 return;
373 }
374 bool old_is_space = true;
375 bool has_spaces_only = true;
376 for (; i < n;) {
377 if (utf8[i] == 0)
378 break;
379 Int32 wc;
380 i += utf8_to_ucs4(utf8, i, wc);
381 if (_isSpace(wc)) {
382 if (!old_is_space)
383 out_utf8.add(' ');
384 old_is_space = true;
385 }
386 else {
387 old_is_space = false;
388 ucs4_to_utf8(wc, out_utf8);
389 has_spaces_only = false;
390 }
391 }
392 if (old_is_space && (!has_spaces_only)) {
393 if (out_utf8.size() > 0)
394 out_utf8.back() = 0;
395 }
396 else {
397 if (has_spaces_only)
398 out_utf8.add(' ');
399 out_utf8.add(0);
400 }
401}
402
403/*---------------------------------------------------------------------------*/
404/*---------------------------------------------------------------------------*/
405
406void BasicTranscoder::
407upperCase(CoreArray<Byte>& out_utf8)
408{
409 CoreArray<Byte> copy_utf8(out_utf8);
410 Span<const Byte> utf8(copy_utf8.view());
411 out_utf8.clear();
412 for (Int64 i = 0, n = utf8.size(); i < n;) {
413 Int32 wc;
414 i += utf8_to_ucs4(utf8, i, wc);
415 Int32 upper_wc = _toUpper(wc);
416 ucs4_to_utf8(upper_wc, out_utf8);
417 }
418}
419
420/*---------------------------------------------------------------------------*/
421/*---------------------------------------------------------------------------*/
422
423void BasicTranscoder::
424lowerCase(CoreArray<Byte>& out_utf8)
425{
426 CoreArray<Byte> copy_utf8(out_utf8);
427 Span<const Byte> utf8(copy_utf8.view());
428 out_utf8.clear();
429 for (Int64 i = 0, n = utf8.size(); i < n;) {
430 Int32 wc;
431 i += utf8_to_ucs4(utf8, i, wc);
432 Int32 upper_wc = _toLower(wc);
433 ucs4_to_utf8(upper_wc, out_utf8);
434 }
435}
436
437/*---------------------------------------------------------------------------*/
438/*---------------------------------------------------------------------------*/
439
440void BasicTranscoder::
441substring(CoreArray<Byte>& out_utf8, Span<const Byte> utf8, Int64 pos, Int64 len)
442{
443 // Copie les \a len caractères unicodes de \a utf8 à partir de la position \a pos
444 Int64 current_pos = 0;
445 for (Int64 i = 0, n = utf8.size(); i < n;) {
446 Int32 wc;
447 i += utf8_to_ucs4(utf8, i, wc);
448 if (current_pos >= pos && current_pos < (pos + len)) {
449 // Pour être sur de ne pas ajouter le 0 terminal
450 if (wc != 0)
451 ucs4_to_utf8(wc, out_utf8);
452 }
453 ++current_pos;
454 }
455 // Ajoute le 0 terminal
456 ucs4_to_utf8(0, out_utf8);
457}
458
459/*---------------------------------------------------------------------------*/
460/*---------------------------------------------------------------------------*/
461
462} // namespace Arcane::Impl
463
464/*---------------------------------------------------------------------------*/
465/*---------------------------------------------------------------------------*/
constexpr __host__ __device__ pointer data() const noexcept
Pointeur sur le début de la vue.
Definition Span.h:516
constexpr __host__ __device__ SizeType size() const noexcept
Retourne la taille du tableau.
Definition Span.h:304
Vue d'un tableau d'éléments de type T.
Definition Span.h:612
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
std::int64_t Int64
Type entier signé sur 64 bits.
constexpr __host__ __device__ Int64 arccoreCheckLargeArraySize(size_t size)
Vérifie que size peut être converti dans un 'Int64' pour servir de taille à un tableau.
unsigned char Byte
Type d'un octet.
Definition BaseTypes.h:43
unsigned short UChar
Type d'un caractère unicode.
Definition BaseTypes.h:47
std::int32_t Int32
Type entier signé sur 32 bits.