PR c++/3637
[official-gcc.git] / gcc / ada / s-wchstw.adb
blobad9d095e68817548cac60825b3d90300cc1dfadb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUNTIME COMPONENTS --
4 -- --
5 -- S Y S T E M . W C H _ S T W --
6 -- --
7 -- B o d y --
8 -- --
9 -- $Revision: 1.14 $
10 -- --
11 -- Copyright (C) 1992-2000 Free Software Foundation, Inc. --
12 -- --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
23 -- --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
30 -- --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 -- --
34 ------------------------------------------------------------------------------
36 with Interfaces; use Interfaces;
37 with System.WCh_Con; use System.WCh_Con;
38 with System.WCh_JIS; use System.WCh_JIS;
40 package body System.WCh_StW is
42 ---------------------------
43 -- String_To_Wide_String --
44 ---------------------------
46 function String_To_Wide_String
47 (S : String;
48 EM : WC_Encoding_Method)
49 return Wide_String
51 R : Wide_String (1 .. S'Length);
52 RP : Natural;
53 SP : Natural;
54 U1 : Unsigned_16;
55 U2 : Unsigned_16;
56 U3 : Unsigned_16;
57 U : Unsigned_16;
59 Last : constant Natural := S'Last;
61 function Get_Hex (C : Character) return Unsigned_16;
62 -- Converts character from hex digit to value in range 0-15. The
63 -- input must be in 0-9, A-F, or a-f, and no check is needed.
65 procedure Get_Hex_4;
66 -- Translates four hex characters starting at S (SP) to a single
67 -- wide character. Used in WCEM_Hex and WCEM_Brackets mode. SP
68 -- is not modified by the call. The resulting wide character value
69 -- is stored in R (RP). RP is not modified by the call.
71 function Get_Hex (C : Character) return Unsigned_16 is
72 begin
73 if C in '0' .. '9' then
74 return Character'Pos (C) - Character'Pos ('0');
75 elsif C in 'A' .. 'F' then
76 return Character'Pos (C) - Character'Pos ('A') + 10;
77 else
78 return Character'Pos (C) - Character'Pos ('a') + 10;
79 end if;
80 end Get_Hex;
82 procedure Get_Hex_4 is
83 begin
84 R (RP) := Wide_Character'Val (
85 Get_Hex (S (SP + 3)) + 16 *
86 (Get_Hex (S (SP + 2)) + 16 *
87 (Get_Hex (S (SP + 1)) + 16 *
88 (Get_Hex (S (SP + 0))))));
89 end Get_Hex_4;
91 -- Start of processing for String_To_Wide_String
93 begin
94 SP := S'First;
95 RP := 0;
97 case EM is
99 -- ESC-Hex representation
101 when WCEM_Hex =>
102 while SP <= Last - 4 loop
103 RP := RP + 1;
105 if S (SP) = ASCII.ESC then
106 SP := SP + 1;
107 Get_Hex_4;
108 SP := SP + 4;
109 else
110 R (RP) := Wide_Character'Val (Character'Pos (S (SP)));
111 SP := SP + 1;
112 end if;
113 end loop;
115 -- Upper bit shift, internal code = external code
117 when WCEM_Upper =>
118 while SP < Last loop
119 RP := RP + 1;
121 if S (SP) >= Character'Val (16#80#) then
122 U1 := Character'Pos (S (SP));
123 U2 := Character'Pos (S (SP + 1));
124 R (RP) := Wide_Character'Val (256 * U1 + U2);
125 SP := SP + 2;
126 else
127 R (RP) := Wide_Character'Val (Character'Pos (S (SP)));
128 SP := SP + 1;
129 end if;
130 end loop;
132 -- Upper bit shift, shift-JIS
134 when WCEM_Shift_JIS =>
135 while SP < Last loop
136 RP := RP + 1;
138 if S (SP) >= Character'Val (16#80#) then
139 R (RP) := Shift_JIS_To_JIS (S (SP), S (SP + 1));
140 SP := SP + 2;
141 else
142 R (RP) := Wide_Character'Val (Character'Pos (S (SP)));
143 SP := SP + 1;
144 end if;
145 end loop;
147 -- Upper bit shift, EUC
149 when WCEM_EUC =>
150 while SP < Last loop
151 RP := RP + 1;
153 if S (SP) >= Character'Val (16#80#) then
154 R (RP) := EUC_To_JIS (S (SP), S (SP + 1));
155 SP := SP + 2;
156 else
157 R (RP) := Wide_Character'Val (Character'Pos (S (SP)));
158 SP := SP + 1;
159 end if;
160 end loop;
162 -- Upper bit shift, UTF-8
164 when WCEM_UTF8 =>
165 while SP < Last loop
166 RP := RP + 1;
168 if S (SP) >= Character'Val (16#80#) then
169 U1 := Character'Pos (S (SP));
170 U2 := Character'Pos (S (SP + 1));
172 U := Shift_Left (U1 and 2#00011111#, 6) +
173 (U2 and 2#00111111#);
174 SP := SP + 2;
176 if U1 >= 2#11100000# then
177 U3 := Character'Pos (S (SP));
178 U := Shift_Left (U, 6) + (U3 and 2#00111111#);
179 SP := SP + 1;
180 end if;
182 R (RP) := Wide_Character'Val (U);
184 else
185 R (RP) := Wide_Character'Val (Character'Pos (S (SP)));
186 SP := SP + 1;
187 end if;
188 end loop;
190 -- Brackets representation
192 when WCEM_Brackets =>
193 while SP <= Last - 7 loop
194 RP := RP + 1;
196 if S (SP) = '['
197 and then S (SP + 1) = '"'
198 and then S (SP + 2) /= '"'
199 then
200 SP := SP + 2;
201 Get_Hex_4;
202 SP := SP + 6;
204 else
205 R (RP) := Wide_Character'Val (Character'Pos (S (SP)));
206 SP := SP + 1;
207 end if;
208 end loop;
210 end case;
212 while SP <= Last loop
213 RP := RP + 1;
214 R (RP) := Wide_Character'Val (Character'Pos (S (SP)));
215 SP := SP + 1;
216 end loop;
218 return R (1 .. RP);
219 end String_To_Wide_String;
221 end System.WCh_StW;