2009-07-17 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / gcc / ada / s-valwch.adb
blobb2db5005adc6b8b57d722404b64af355891d3922
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S Y S T E M . V A L _ W C H A R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2009, 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 ------------------------------------------------------------------------------
32 with Interfaces; use Interfaces;
33 with System.Val_Util; use System.Val_Util;
34 with System.WCh_Cnv; use System.WCh_Cnv;
35 with System.WCh_Con; use System.WCh_Con;
37 package body System.Val_WChar is
39 --------------------------
40 -- Value_Wide_Character --
41 --------------------------
43 function Value_Wide_Character
44 (Str : String;
45 EM : System.WCh_Con.WC_Encoding_Method) return Wide_Character
47 WC : constant Wide_Wide_Character := Value_Wide_Wide_Character (Str, EM);
48 WV : constant Unsigned_32 := Wide_Wide_Character'Pos (WC);
49 begin
50 if WV > 16#FFFF# then
51 raise Constraint_Error with
52 "out of range character for Value attribute";
53 else
54 return Wide_Character'Val (WV);
55 end if;
56 end Value_Wide_Character;
58 -------------------------------
59 -- Value_Wide_Wide_Character --
60 -------------------------------
62 function Value_Wide_Wide_Character
63 (Str : String;
64 EM : System.WCh_Con.WC_Encoding_Method) return Wide_Wide_Character
66 F : Natural;
67 L : Natural;
68 S : String (Str'Range) := Str;
70 begin
71 Normalize_String (S, F, L);
73 -- Character literal case
75 if S (F) = ''' and then S (L) = ''' then
77 -- Must be at least three characters
79 if L - F < 2 then
80 raise Constraint_Error;
82 -- If just three characters, simple character case
84 elsif L - F = 2 then
85 return Wide_Wide_Character'Val (Character'Pos (S (F + 1)));
87 -- Only other possibility for quoted string is wide char sequence
89 else
90 declare
91 P : Natural;
92 W : Wide_Wide_Character;
94 function In_Char return Character;
95 -- Function for instantiations of Char_Sequence_To_UTF_32
97 -------------
98 -- In_Char --
99 -------------
101 function In_Char return Character is
102 begin
103 P := P + 1;
105 if P = Str'Last then
106 raise Constraint_Error;
107 end if;
109 return Str (P);
110 end In_Char;
112 function UTF_32 is
113 new Char_Sequence_To_UTF_32 (In_Char);
115 begin
116 P := F + 1;
118 -- Brackets encoding
120 if S (F + 1) = '[' then
121 W := Wide_Wide_Character'Val (UTF_32 ('[', WCEM_Brackets));
122 else
123 W := Wide_Wide_Character'Val (UTF_32 (S (F + 1), EM));
124 end if;
126 if P /= L - 1 then
127 raise Constraint_Error;
128 end if;
130 return W;
131 end;
132 end if;
134 -- Deal with Hex_hhhhhhhh cases for wide_[wide_]character cases
136 elsif Str'Length = 12
137 and then Str (Str'First .. Str'First + 3) = "Hex_"
138 then
139 declare
140 W : Unsigned_32 := 0;
142 begin
143 for J in Str'First + 4 .. Str'First + 11 loop
144 W := W * 16 + Character'Pos (Str (J));
146 if Str (J) in '0' .. '9' then
147 W := W - Character'Pos ('0');
148 elsif Str (J) in 'A' .. 'F' then
149 W := W - Character'Pos ('A') + 10;
150 elsif Str (J) in 'a' .. 'f' then
151 W := W - Character'Pos ('a') + 10;
152 else
153 raise Constraint_Error;
154 end if;
155 end loop;
157 if W > 16#7FFF_FFFF# then
158 raise Constraint_Error;
159 else
160 return Wide_Wide_Character'Val (W);
161 end if;
162 end;
164 -- Otherwise must be one of the special names for Character
166 else
167 return
168 Wide_Wide_Character'Val (Character'Pos (Character'Value (Str)));
169 end if;
171 exception
172 when Constraint_Error =>
173 raise Constraint_Error with "invalid string for value attribute";
174 end Value_Wide_Wide_Character;
176 end System.Val_WChar;