1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . W C H _ C N V --
9 -- Copyright (C) 1992-2007, Free Software Foundation, Inc. --
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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 pragma Warnings (Off);
38 with Interfaces; use Interfaces;
39 with System.WCh_Con; use System.WCh_Con;
40 with System.WCh_JIS; use System.WCh_JIS;
42 package body System.WCh_Cnv is
44 -----------------------------
45 -- Char_Sequence_To_UTF_32 --
46 -----------------------------
48 function Char_Sequence_To_UTF_32
50 EM : System.WCh_Con.WC_Encoding_Method) return UTF_32_Code
57 procedure Get_Hex (N : Character);
58 -- If N is a hex character, then set B1 to 16 * B1 + character N.
59 -- Raise Constraint_Error if character N is not a hex character.
61 procedure Get_UTF_Byte;
62 pragma Inline (Get_UTF_Byte);
63 -- Used to interpret a 2#10xxxxxx# continuation byte in UTF-8 mode.
64 -- Reads a byte, and raises CE if the first two bits are not 10.
65 -- Otherwise shifts W 6 bits left and or's in the 6 xxxxxx bits.
71 procedure Get_Hex (N : Character) is
72 B2 : constant Unsigned_32 := Character'Pos (N);
74 if B2 in Character'Pos ('0') .. Character'Pos ('9') then
75 B1 := B1 * 16 + B2 - Character'Pos ('0');
76 elsif B2 in Character'Pos ('A') .. Character'Pos ('F') then
77 B1 := B1 * 16 + B2 - (Character'Pos ('A') - 10);
78 elsif B2 in Character'Pos ('a') .. Character'Pos ('f') then
79 B1 := B1 * 16 + B2 - (Character'Pos ('a') - 10);
81 raise Constraint_Error;
89 procedure Get_UTF_Byte is
91 U := Unsigned_32 (Character'Pos (In_Char));
93 if (U and 2#11000000#) /= 2#10_000000# then
94 raise Constraint_Error;
97 W := Shift_Left (W, 6) or (U and 2#00111111#);
100 -- Start of processing for Char_Sequence_To_Wide
106 if C /= ASCII.ESC then
107 return Character'Pos (C);
116 return UTF_32_Code (B1);
120 if C > ASCII.DEL then
121 return 256 * Character'Pos (C) + Character'Pos (In_Char);
123 return Character'Pos (C);
126 when WCEM_Shift_JIS =>
127 if C > ASCII.DEL then
128 return Wide_Character'Pos (Shift_JIS_To_JIS (C, In_Char));
130 return Character'Pos (C);
134 if C > ASCII.DEL then
135 return Wide_Character'Pos (EUC_To_JIS (C, In_Char));
137 return Character'Pos (C);
142 -- Note: for details of UTF8 encoding see RFC 3629
144 U := Unsigned_32 (Character'Pos (C));
146 -- 16#00_0000#-16#00_007F#: 0xxxxxxx
148 if (U and 2#10000000#) = 2#00000000# then
149 return Character'Pos (C);
151 -- 16#00_0080#-16#00_07FF#: 110xxxxx 10xxxxxx
153 elsif (U and 2#11100000#) = 2#110_00000# then
154 W := U and 2#00011111#;
156 return UTF_32_Code (W);
158 -- 16#00_0800#-16#00_ffff#: 1110xxxx 10xxxxxx 10xxxxxx
160 elsif (U and 2#11110000#) = 2#1110_0000# then
161 W := U and 2#00001111#;
164 return UTF_32_Code (W);
166 -- 16#01_0000#-16#10_FFFF#: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
168 elsif (U and 2#11111000#) = 2#11110_000# then
169 W := U and 2#00000111#;
175 return UTF_32_Code (W);
177 -- 16#0020_0000#-16#03FF_FFFF#: 111110xx 10xxxxxx 10xxxxxx
180 elsif (U and 2#11111100#) = 2#111110_00# then
181 W := U and 2#00000011#;
187 return UTF_32_Code (W);
189 -- 16#0400_0000#-16#7FFF_FFFF#: 1111110x 10xxxxxx 10xxxxxx
190 -- 10xxxxxx 10xxxxxx 10xxxxxx
192 elsif (U and 2#11111110#) = 2#1111110_0# then
193 W := U and 2#00000001#;
199 return UTF_32_Code (W);
202 raise Constraint_Error;
205 when WCEM_Brackets =>
207 return Character'Pos (C);
210 if In_Char /= '"' then
211 raise Constraint_Error;
236 if B1 > Unsigned_32 (UTF_32_Code'Last) then
237 raise Constraint_Error;
240 if In_Char /= '"' then
241 raise Constraint_Error;
247 if In_Char /= ']' then
248 raise Constraint_Error;
251 return UTF_32_Code (B1);
254 end Char_Sequence_To_UTF_32;
256 --------------------------------
257 -- Char_Sequence_To_Wide_Char --
258 --------------------------------
260 function Char_Sequence_To_Wide_Char
262 EM : System.WCh_Con.WC_Encoding_Method) return Wide_Character
264 function Char_Sequence_To_UTF is new Char_Sequence_To_UTF_32 (In_Char);
266 U : constant UTF_32_Code := Char_Sequence_To_UTF (C, EM);
270 raise Constraint_Error;
272 return Wide_Character'Val (U);
274 end Char_Sequence_To_Wide_Char;
276 -----------------------------
277 -- UTF_32_To_Char_Sequence --
278 -----------------------------
280 procedure UTF_32_To_Char_Sequence
282 EM : System.WCh_Con.WC_Encoding_Method)
284 Hexc : constant array (UTF_32_Code range 0 .. 15) of Character :=
295 Out_Char (Character'Val (Val));
296 elsif Val <= 16#FFFF# then
297 Out_Char (ASCII.ESC);
298 Out_Char (Hexc (Val / (16**3)));
299 Out_Char (Hexc ((Val / (16**2)) mod 16));
300 Out_Char (Hexc ((Val / 16) mod 16));
301 Out_Char (Hexc (Val mod 16));
303 raise Constraint_Error;
308 Out_Char (Character'Val (Val));
309 elsif Val < 16#8000# or else Val > 16#FFFF# then
310 raise Constraint_Error;
312 Out_Char (Character'Val (Val / 256));
313 Out_Char (Character'Val (Val mod 256));
316 when WCEM_Shift_JIS =>
318 Out_Char (Character'Val (Val));
319 elsif Val <= 16#FFFF# then
320 JIS_To_Shift_JIS (Wide_Character'Val (Val), C1, C2);
324 raise Constraint_Error;
329 Out_Char (Character'Val (Val));
330 elsif Val <= 16#FFFF# then
331 JIS_To_EUC (Wide_Character'Val (Val), C1, C2);
335 raise Constraint_Error;
340 -- Note: for details of UTF8 encoding see RFC 3629
342 U := Unsigned_32 (Val);
344 -- 16#00_0000#-16#00_007F#: 0xxxxxxx
346 if U <= 16#00_007F# then
347 Out_Char (Character'Val (U));
349 -- 16#00_0080#-16#00_07FF#: 110xxxxx 10xxxxxx
351 elsif U <= 16#00_07FF# then
352 Out_Char (Character'Val (2#11000000# or Shift_Right (U, 6)));
353 Out_Char (Character'Val (2#10000000# or (U and 2#00111111#)));
355 -- 16#00_0800#-16#00_FFFF#: 1110xxxx 10xxxxxx 10xxxxxx
357 elsif U <= 16#00_FFFF# then
358 Out_Char (Character'Val (2#11100000# or Shift_Right (U, 12)));
359 Out_Char (Character'Val (2#10000000# or (Shift_Right (U, 6)
361 Out_Char (Character'Val (2#10000000# or (U and 2#00111111#)));
363 -- 16#01_0000#-16#10_FFFF#: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
365 elsif U <= 16#10_FFFF# then
366 Out_Char (Character'Val (2#11110000# or Shift_Right (U, 18)));
367 Out_Char (Character'Val (2#10000000# or (Shift_Right (U, 12)
369 Out_Char (Character'Val (2#10000000# or (Shift_Right (U, 6)
371 Out_Char (Character'Val (2#10000000# or (U and 2#00111111#)));
373 -- 16#0020_0000#-16#03FF_FFFF#: 111110xx 10xxxxxx 10xxxxxx
376 elsif U <= 16#03FF_FFFF# then
377 Out_Char (Character'Val (2#11111000# or Shift_Right (U, 24)));
378 Out_Char (Character'Val (2#10000000# or (Shift_Right (U, 18)
380 Out_Char (Character'Val (2#10000000# or (Shift_Right (U, 12)
382 Out_Char (Character'Val (2#10000000# or (Shift_Right (U, 6)
384 Out_Char (Character'Val (2#10000000# or (U and 2#00111111#)));
386 -- 16#0400_0000#-16#7FFF_FFFF#: 1111110x 10xxxxxx 10xxxxxx
387 -- 10xxxxxx 10xxxxxx 10xxxxxx
389 elsif U <= 16#7FFF_FFFF# then
390 Out_Char (Character'Val (2#11111100# or Shift_Right (U, 30)));
391 Out_Char (Character'Val (2#10000000# or (Shift_Right (U, 24)
393 Out_Char (Character'Val (2#10000000# or (Shift_Right (U, 18)
395 Out_Char (Character'Val (2#10000000# or (Shift_Right (U, 12)
397 Out_Char (Character'Val (2#10000000# or (Shift_Right (U, 6)
399 Out_Char (Character'Val (2#10000000# or (U and 2#00111111#)));
402 raise Constraint_Error;
405 when WCEM_Brackets =>
407 -- Values in the range 0-255 are directly output. Note that there
408 -- is some issue with [ (16#5B#] since this will cause confusion
409 -- if the resulting string is interpreted using brackets encoding.
411 -- One possibility would be to always output [ as ["5B"] but in
412 -- practice this is undesirable, since for example normal use of
413 -- Wide_Text_IO for output (much more common than input), really
414 -- does want to be able to say something like
416 -- Put_Line ("Start of output [first run]");
418 -- and have it come out as intended, rather than contaminated by
419 -- a ["5B"] sequence in place of the left bracket.
422 Out_Char (Character'Val (Val));
424 -- Otherwise use brackets notation for vales greater than 255
430 if Val > 16#FFFF# then
431 if Val > 16#00FF_FFFF# then
432 if Val > 16#7FFF_FFFF# then
433 raise Constraint_Error;
436 Out_Char (Hexc (Val / 16 ** 7));
437 Out_Char (Hexc ((Val / 16 ** 6) mod 16));
440 Out_Char (Hexc ((Val / 16 ** 5) mod 16));
441 Out_Char (Hexc ((Val / 16 ** 4) mod 16));
444 Out_Char (Hexc ((Val / 16 ** 3) mod 16));
445 Out_Char (Hexc ((Val / 16 ** 2) mod 16));
446 Out_Char (Hexc ((Val / 16) mod 16));
447 Out_Char (Hexc (Val mod 16));
453 end UTF_32_To_Char_Sequence;
455 --------------------------------
456 -- Wide_Char_To_Char_Sequence --
457 --------------------------------
459 procedure Wide_Char_To_Char_Sequence
460 (WC : Wide_Character;
461 EM : System.WCh_Con.WC_Encoding_Method)
463 procedure UTF_To_Char_Sequence is new UTF_32_To_Char_Sequence (Out_Char);
465 UTF_To_Char_Sequence (Wide_Character'Pos (WC), EM);
466 end Wide_Char_To_Char_Sequence;