Debian Codegraph @

gcc/ada/libgnat/s-wchcnv.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- S Y S T E M . W C H _ C N V --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2025, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 with Interfaces; use Interfaces;
33 with System.WCh_Con; use System.WCh_Con;
34 with System.WCh_JIS; use System.WCh_JIS;
35
36 package body System.WCh_Cnv is
37
38 -----------------------------
39 -- Char_Sequence_To_UTF_32 --
40 -----------------------------
41
42 function Char_Sequence_To_UTF_32
43 (C : Character;
44 EM : System.WCh_Con.WC_Encoding_Method) return UTF_32_Code
45 is
46 B1 : Unsigned_32;
47 C1 : Character;
48 U : Unsigned_32;
49 W : Unsigned_32;
50
51 procedure Get_Hex (N : Character);
52 -- If N is a hex character, then set B1 to 16 * B1 + character N.
53 -- Raise Constraint_Error if character N is not a hex character.
54
55 procedure Get_UTF_Byte;
56 pragma Inline (Get_UTF_Byte);
57 -- Used to interpret a 2#10xxxxxx# continuation byte in UTF-8 mode.
58 -- Reads a byte, and raises CE if the first two bits are not 10.
59 -- Otherwise shifts W 6 bits left and or's in the 6 xxxxxx bits.
60
61 -------------
62 -- Get_Hex --
63 -------------
64
65 procedure Get_Hex (N : Character) is
66 B2 : constant Unsigned_32 := Character'Pos (N);
67 begin
68 if B2 in Character'Pos ('0') .. Character'Pos ('9') then
69 B1 := B1 * 16 + B2 - Character'Pos ('0');
70 elsif B2 in Character'Pos ('A') .. Character'Pos ('F') then
71 B1 := B1 * 16 + B2 - (Character'Pos ('A') - 10);
72 elsif B2 in Character'Pos ('a') .. Character'Pos ('f') then
73 B1 := B1 * 16 + B2 - (Character'Pos ('a') - 10);
74 else
75 raise Constraint_Error;
76 end if;
77 end Get_Hex;
78
79 ------------------
80 -- Get_UTF_Byte --
81 ------------------
82
83 procedure Get_UTF_Byte is
84 begin
85 U := Unsigned_32 (Character'Pos (In_Char));
86
87 if (U and 2#11000000#) /= 2#10_000000# then
88 raise Constraint_Error;
89 end if;
90
91 W := Shift_Left (W, 6) or (U and 2#00111111#);
92 end Get_UTF_Byte;
93
94 -- Start of processing for Char_Sequence_To_UTF_32
95
96 begin
97 case EM is
98 when WCEM_Hex =>
99 if C /= ASCII.ESC then
100 return Character'Pos (C);
101
102 else
103 B1 := 0;
104 Get_Hex (In_Char);
105 Get_Hex (In_Char);
106 Get_Hex (In_Char);
107 Get_Hex (In_Char);
108
109 return UTF_32_Code (B1);
110 end if;
111
112 when WCEM_Upper =>
113 if C > ASCII.DEL then
114 return 256 * Character'Pos (C) + Character'Pos (In_Char);
115 else
116 return Character'Pos (C);
117 end if;
118
119 when WCEM_Shift_JIS =>
120 if C > ASCII.DEL then
121 return Wide_Character'Pos (Shift_JIS_To_JIS (C, In_Char));
122 else
123 return Character'Pos (C);
124 end if;
125
126 when WCEM_EUC =>
127 if C > ASCII.DEL then
128 return Wide_Character'Pos (EUC_To_JIS (C, In_Char));
129 else
130 return Character'Pos (C);
131 end if;
132
133 when WCEM_UTF8 =>
134
135 -- Note: for details of UTF8 encoding see RFC 3629
136
137 U := Unsigned_32 (Character'Pos (C));
138
139 -- 16#00_0000#-16#00_007F#: 0xxxxxxx
140
141 if (U and 2#10000000#) = 2#00000000# then
142 return Character'Pos (C);
143
144 -- 16#00_0080#-16#00_07FF#: 110xxxxx 10xxxxxx
145
146 elsif (U and 2#11100000#) = 2#110_00000# then
147 W := U and 2#00011111#;
148 Get_UTF_Byte;
149 return UTF_32_Code (W);
150
151 -- 16#00_0800#-16#00_ffff#: 1110xxxx 10xxxxxx 10xxxxxx
152
153 elsif (U and 2#11110000#) = 2#1110_0000# then
154 W := U and 2#00001111#;
155 Get_UTF_Byte;
156 Get_UTF_Byte;
157 return UTF_32_Code (W);
158
159 -- 16#01_0000#-16#10_FFFF#: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
160
161 elsif (U and 2#11111000#) = 2#11110_000# then
162 W := U and 2#00000111#;
163
164 for K in 1 .. 3 loop
165 Get_UTF_Byte;
166 end loop;
167
168 return UTF_32_Code (W);
169
170 -- 16#0020_0000#-16#03FF_FFFF#: 111110xx 10xxxxxx 10xxxxxx
171 -- 10xxxxxx 10xxxxxx
172
173 elsif (U and 2#11111100#) = 2#111110_00# then
174 W := U and 2#00000011#;
175
176 for K in 1 .. 4 loop
177 Get_UTF_Byte;
178 end loop;
179
180 return UTF_32_Code (W);
181
182 -- 16#0400_0000#-16#7FFF_FFFF#: 1111110x 10xxxxxx 10xxxxxx
183 -- 10xxxxxx 10xxxxxx 10xxxxxx
184
185 elsif (U and 2#11111110#) = 2#1111110_0# then
186 W := U and 2#00000001#;
187
188 for K in 1 .. 5 loop
189 Get_UTF_Byte;
190 end loop;
191
192 return UTF_32_Code (W);
193
194 else
195 raise Constraint_Error;
196 end if;
197
198 when WCEM_Brackets =>
199 if C /= '[' then
200 return Character'Pos (C);
201 end if;
202
203 if In_Char /= '"' then
204 raise Constraint_Error;
205 end if;
206
207 B1 := 0;
208 Get_Hex (In_Char);
209 Get_Hex (In_Char);
210
211 C1 := In_Char;
212
213 if C1 /= '"' then
214 Get_Hex (C1);
215 Get_Hex (In_Char);
216
217 C1 := In_Char;
218
219 if C1 /= '"' then
220 Get_Hex (C1);
221 Get_Hex (In_Char);
222
223 C1 := In_Char;
224
225 if C1 /= '"' then
226 Get_Hex (C1);
227 Get_Hex (In_Char);
228
229 if B1 > Unsigned_32 (UTF_32_Code'Last) then
230 raise Constraint_Error;
231 end if;
232
233 if In_Char /= '"' then
234 raise Constraint_Error;
235 end if;
236 end if;
237 end if;
238 end if;
239
240 if In_Char /= ']' then
241 raise Constraint_Error;
242 end if;
243
244 return UTF_32_Code (B1);
245 end case;
246 end Char_Sequence_To_UTF_32;
247
248 --------------------------------
249 -- Char_Sequence_To_Wide_Char --
250 --------------------------------
251
252 function Char_Sequence_To_Wide_Char
253 (C : Character;
254 EM : System.WCh_Con.WC_Encoding_Method) return Wide_Character
255 is
256 function Char_Sequence_To_UTF is new Char_Sequence_To_UTF_32 (In_Char);
257
258 U : constant UTF_32_Code := Char_Sequence_To_UTF (C, EM);
259
260 begin
261 if U > 16#FFFF# then
262 raise Constraint_Error;
263 else
264 return Wide_Character'Val (U);
265 end if;
266 end Char_Sequence_To_Wide_Char;
267
268 -----------------------------
269 -- UTF_32_To_Char_Sequence --
270 -----------------------------
271
272 procedure UTF_32_To_Char_Sequence
273 (Val : UTF_32_Code;
274 EM : System.WCh_Con.WC_Encoding_Method)
275 is
276 Hexc : constant array (UTF_32_Code range 0 .. 15) of Character :=
277 "0123456789ABCDEF";
278
279 C1, C2 : Character;
280 U : Unsigned_32;
281
282 begin
283 -- Raise CE for invalid UTF_32_Code
284
285 if not Val'Valid then
286 raise Constraint_Error;
287 end if;
288
289 -- Processing depends on encoding mode
290
291 case EM is
292 when WCEM_Hex =>
293 if Val < 256 then
294 Out_Char (Character'Val (Val));
295 elsif Val <= 16#FFFF# then
296 Out_Char (ASCII.ESC);
297 Out_Char (Hexc (Val / (16**3)));
298 Out_Char (Hexc ((Val / (16**2)) mod 16));
299 Out_Char (Hexc ((Val / 16) mod 16));
300 Out_Char (Hexc (Val mod 16));
301 else
302 raise Constraint_Error;
303 end if;
304
305 when WCEM_Upper =>
306 if Val < 128 then
307 Out_Char (Character'Val (Val));
308 elsif Val < 16#8000# or else Val > 16#FFFF# then
309 raise Constraint_Error;
310 else
311 Out_Char (Character'Val (Val / 256));
312 Out_Char (Character'Val (Val mod 256));
313 end if;
314
315 when WCEM_Shift_JIS =>
316 if Val < 128 then
317 Out_Char (Character'Val (Val));
318 elsif Val <= 16#FFFF# then
319 JIS_To_Shift_JIS (Wide_Character'Val (Val), C1, C2);
320 Out_Char (C1);
321 Out_Char (C2);
322 else
323 raise Constraint_Error;
324 end if;
325
326 when WCEM_EUC =>
327 if Val < 128 then
328 Out_Char (Character'Val (Val));
329 elsif Val <= 16#FFFF# then
330 JIS_To_EUC (Wide_Character'Val (Val), C1, C2);
331 Out_Char (C1);
332 Out_Char (C2);
333 else
334 raise Constraint_Error;
335 end if;
336
337 when WCEM_UTF8 =>
338
339 -- Note: for details of UTF8 encoding see RFC 3629
340
341 U := Unsigned_32 (Val);
342
343 -- 16#00_0000#-16#00_007F#: 0xxxxxxx
344
345 if U <= 16#00_007F# then
346 Out_Char (Character'Val (U));
347
348 -- 16#00_0080#-16#00_07FF#: 110xxxxx 10xxxxxx
349
350 elsif U <= 16#00_07FF# then
351 Out_Char (Character'Val (2#11000000# or Shift_Right (U, 6)));
352 Out_Char (Character'Val (2#10000000# or (U and 2#00111111#)));
353
354 -- 16#00_0800#-16#00_FFFF#: 1110xxxx 10xxxxxx 10xxxxxx
355
356 elsif U <= 16#00_FFFF# then
357 Out_Char (Character'Val (2#11100000# or Shift_Right (U, 12)));
358 Out_Char (Character'Val (2#10000000# or (Shift_Right (U, 6)
359 and 2#00111111#)));
360 Out_Char (Character'Val (2#10000000# or (U and 2#00111111#)));
361
362 -- 16#01_0000#-16#10_FFFF#: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
363
364 elsif U <= 16#10_FFFF# then
365 Out_Char (Character'Val (2#11110000# or Shift_Right (U, 18)));
366 Out_Char (Character'Val (2#10000000# or (Shift_Right (U, 12)
367 and 2#00111111#)));
368 Out_Char (Character'Val (2#10000000# or (Shift_Right (U, 6)
369 and 2#00111111#)));
370 Out_Char (Character'Val (2#10000000# or (U and 2#00111111#)));
371
372 -- 16#0020_0000#-16#03FF_FFFF#: 111110xx 10xxxxxx 10xxxxxx
373 -- 10xxxxxx 10xxxxxx
374
375 elsif U <= 16#03FF_FFFF# then
376 Out_Char (Character'Val (2#11111000# or Shift_Right (U, 24)));
377 Out_Char (Character'Val (2#10000000# or (Shift_Right (U, 18)
378 and 2#00111111#)));
379 Out_Char (Character'Val (2#10000000# or (Shift_Right (U, 12)
380 and 2#00111111#)));
381 Out_Char (Character'Val (2#10000000# or (Shift_Right (U, 6)
382 and 2#00111111#)));
383 Out_Char (Character'Val (2#10000000# or (U and 2#00111111#)));
384
385 -- 16#0400_0000#-16#7FFF_FFFF#: 1111110x 10xxxxxx 10xxxxxx
386 -- 10xxxxxx 10xxxxxx 10xxxxxx
387
388 elsif U <= 16#7FFF_FFFF# then
389 Out_Char (Character'Val (2#11111100# or Shift_Right (U, 30)));
390 Out_Char (Character'Val (2#10000000# or (Shift_Right (U, 24)
391 and 2#00111111#)));
392 Out_Char (Character'Val (2#10000000# or (Shift_Right (U, 18)
393 and 2#00111111#)));
394 Out_Char (Character'Val (2#10000000# or (Shift_Right (U, 12)
395 and 2#00111111#)));
396 Out_Char (Character'Val (2#10000000# or (Shift_Right (U, 6)
397 and 2#00111111#)));
398 Out_Char (Character'Val (2#10000000# or (U and 2#00111111#)));
399
400 else
401 raise Constraint_Error;
402 end if;
403
404 when WCEM_Brackets =>
405
406 -- Values in the range 0-255 are directly output. Note that there
407 -- is an issue with [ (16#5B#) since this will cause confusion
408 -- if the resulting string is interpreted using brackets encoding.
409
410 -- One possibility would be to always output [ as ["5B"] but in
411 -- practice this is undesirable, since for example normal use of
412 -- Wide_Text_IO for output (much more common than input), really
413 -- does want to be able to say something like
414
415 -- Put_Line ("Start of output [first run]");
416
417 -- and have it come out as intended, rather than contaminated by
418 -- a ["5B"] sequence in place of the left bracket.
419
420 if Val < 256 then
421 Out_Char (Character'Val (Val));
422
423 -- Otherwise use brackets notation for vales greater than 255
424
425 else
426 Out_Char ('[');
427 Out_Char ('"');
428
429 if Val > 16#FFFF# then
430 if Val > 16#00FF_FFFF# then
431 Out_Char (Hexc (Val / 16 ** 7));
432 Out_Char (Hexc ((Val / 16 ** 6) mod 16));
433 end if;
434
435 Out_Char (Hexc ((Val / 16 ** 5) mod 16));
436 Out_Char (Hexc ((Val / 16 ** 4) mod 16));
437 end if;
438
439 Out_Char (Hexc ((Val / 16 ** 3) mod 16));
440 Out_Char (Hexc ((Val / 16 ** 2) mod 16));
441 Out_Char (Hexc ((Val / 16) mod 16));
442 Out_Char (Hexc (Val mod 16));
443
444 Out_Char ('"');
445 Out_Char (']');
446 end if;
447 end case;
448 end UTF_32_To_Char_Sequence;
449
450 --------------------------------
451 -- Wide_Char_To_Char_Sequence --
452 --------------------------------
453
454 procedure Wide_Char_To_Char_Sequence
455 (WC : Wide_Character;
456 EM : System.WCh_Con.WC_Encoding_Method)
457 is
458 procedure UTF_To_Char_Sequence is new UTF_32_To_Char_Sequence (Out_Char);
459 begin
460 UTF_To_Char_Sequence (Wide_Character'Pos (WC), EM);
461 end Wide_Char_To_Char_Sequence;
462
463 end System.WCh_Cnv;