1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . V A L _ W C H A R --
9 -- Copyright (C) 1992-2009, 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 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. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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
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);
51 raise Constraint_Error with
52 "out of range character for Value attribute";
54 return Wide_Character'Val (WV);
56 end Value_Wide_Character;
58 -------------------------------
59 -- Value_Wide_Wide_Character --
60 -------------------------------
62 function Value_Wide_Wide_Character
64 EM : System.WCh_Con.WC_Encoding_Method) return Wide_Wide_Character
68 S : String (Str'Range) := Str;
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
80 raise Constraint_Error;
82 -- If just three characters, simple character case
85 return Wide_Wide_Character'Val (Character'Pos (S (F + 1)));
87 -- Only other possibility for quoted string is wide char sequence
92 W : Wide_Wide_Character;
94 function In_Char return Character;
95 -- Function for instantiations of Char_Sequence_To_UTF_32
101 function In_Char return Character is
106 raise Constraint_Error;
113 new Char_Sequence_To_UTF_32 (In_Char);
120 if S (F + 1) = '[' then
121 W := Wide_Wide_Character'Val (UTF_32 ('[', WCEM_Brackets));
124 W := Wide_Wide_Character'Val (UTF_32 (S (F + 1), EM));
128 raise Constraint_Error;
135 -- Deal with Hex_hhhhhhhh cases for wide_[wide_]character cases
137 elsif Str'Length = 12
138 and then Str (Str'First .. Str'First + 3) = "Hex_"
141 W : Unsigned_32 := 0;
144 for J in Str'First + 4 .. Str'First + 11 loop
145 W := W * 16 + Character'Pos (Str (J));
147 if Str (J) in '0' .. '9' then
148 W := W - Character'Pos ('0');
149 elsif Str (J) in 'A' .. 'F' then
150 W := W - Character'Pos ('A') + 10;
151 elsif Str (J) in 'a' .. 'f' then
152 W := W - Character'Pos ('a') + 10;
154 raise Constraint_Error;
158 if W > 16#7FFF_FFFF# then
159 raise Constraint_Error;
161 return Wide_Wide_Character'Val (W);
165 -- Otherwise must be one of the special names for Character
169 Wide_Wide_Character'Val (Character'Pos (Character'Value (Str)));
173 when Constraint_Error =>
174 raise Constraint_Error with "invalid string for value attribute";
175 end Value_Wide_Wide_Character;
177 end System.Val_WChar;