2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / ada / s-wchwts.adb
blobc9894f7c03821ed735a1657889dbfed4d9919f6b
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUNTIME COMPONENTS --
4 -- --
5 -- S Y S T E M . W C H _ W T S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2000 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 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
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. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 with Interfaces; use Interfaces;
35 with System.WCh_Con; use System.WCh_Con;
36 with System.WCh_JIS; use System.WCh_JIS;
38 package body System.WCh_WtS is
40 ---------------------------
41 -- Wide_String_To_String --
42 ---------------------------
44 function Wide_String_To_String
45 (S : Wide_String;
46 EM : WC_Encoding_Method)
47 return String
49 R : String (1 .. 5 * S'Length); -- worst case length!
50 RP : Natural;
51 C1 : Character;
52 C2 : Character;
54 begin
55 RP := 0;
57 for SP in S'Range loop
58 declare
59 C : constant Wide_Character := S (SP);
60 CV : constant Unsigned_16 := Wide_Character'Pos (C);
61 Hex : constant array (Unsigned_16 range 0 .. 15) of Character :=
62 "0123456789ABCDEF";
64 begin
65 if CV <= 127 then
66 RP := RP + 1;
67 R (RP) := Character'Val (CV);
69 else
70 case EM is
72 -- Hex ESC sequence encoding
74 when WCEM_Hex =>
75 if CV <= 16#FF# then
76 RP := RP + 1;
77 R (RP) := Character'Val (CV);
79 else
80 R (RP + 1) := ASCII.ESC;
81 R (RP + 2) := Hex (Shift_Right (CV, 12));
82 R (RP + 3) := Hex (Shift_Right (CV, 8) and 16#000F#);
83 R (RP + 4) := Hex (Shift_Right (CV, 4) and 16#000F#);
84 R (RP + 5) := Hex (CV and 16#000F#);
85 RP := RP + 5;
86 end if;
88 -- Upper bit shift (internal code = external code)
90 when WCEM_Upper =>
91 R (RP + 1) := Character'Val (Shift_Right (CV, 8));
92 R (RP + 2) := Character'Val (CV and 16#FF#);
93 RP := RP + 2;
95 -- Upper bit shift (EUC)
97 when WCEM_EUC =>
98 JIS_To_EUC (C, C1, C2);
99 R (RP + 1) := C1;
100 R (RP + 2) := C2;
101 RP := RP + 2;
103 -- Upper bit shift (Shift-JIS)
105 when WCEM_Shift_JIS =>
106 JIS_To_Shift_JIS (C, C1, C2);
107 R (RP + 1) := C1;
108 R (RP + 2) := C2;
109 RP := RP + 2;
111 -- Upper bit shift (UTF-8)
113 -- 16#0080#-16#07ff#: 2#110xxxxx# 2#10xxxxxx#
114 -- 16#0800#-16#ffff#: 2#1110xxxx# 2#10xxxxxx# 2#10xxxxxx#
116 when WCEM_UTF8 =>
117 if CV < 16#0800# then
118 R (RP + 1) :=
119 Character'Val (2#11000000# or Shift_Right (CV, 6));
120 R (RP + 2) :=
121 Character'Val (2#10000000# or (CV and 2#00111111#));
122 RP := RP + 2;
124 else
125 R (RP + 1) :=
126 Character'Val (2#11100000# or Shift_Right (CV, 12));
127 R (RP + 2) :=
128 Character'Val (2#10000000# or
129 (Shift_Right (CV, 6) and
130 2#00111111#));
131 R (RP + 3) :=
132 Character'Val (2#10000000# or (CV and 2#00111111#));
133 RP := RP + 3;
134 end if;
136 -- Brackets encoding
138 when WCEM_Brackets =>
139 if CV <= 16#FF# then
140 RP := RP + 1;
141 R (RP) := Character'Val (CV);
143 else
144 R (RP + 1) := '[';
145 R (RP + 2) := '"';
146 R (RP + 3) := Hex (Shift_Right (CV, 12));
147 R (RP + 4) := Hex (Shift_Right (CV, 8) and 16#000F#);
148 R (RP + 5) := Hex (Shift_Right (CV, 4) and 16#000F#);
149 R (RP + 6) := Hex (CV and 16#000F#);
150 R (RP + 7) := '"';
151 R (RP + 8) := ']';
152 RP := RP + 8;
153 end if;
155 end case;
156 end if;
157 end;
158 end loop;
160 return R (1 .. RP);
161 end Wide_String_To_String;
163 end System.WCh_WtS;