1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . V A L _ U T I L --
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 System.Case_Util; use System.Case_Util;
34 package body System.Val_Util is
36 ----------------------
37 -- Normalize_String --
38 ----------------------
40 procedure Normalize_String
48 -- Scan for leading spaces
50 while F <= L and then S (F) = ' ' loop
54 -- Check for case when the string contained no characters
57 raise Constraint_Error;
60 -- Scan for trailing spaces
62 while S (L) = ' ' loop
66 -- Except in the case of a character literal, convert to upper case
70 S (J) := To_Upper (S (J));
79 function Scan_Exponent
81 Ptr : not null access Integer;
83 Real : Boolean := False) return Integer
85 P : Natural := Ptr.all;
91 or else (Str (P) /= 'E' and then Str (P) /= 'e')
96 -- We have an E/e, see if sign follows
100 if Str (P) = '+' then
109 elsif Str (P) = '-' then
112 if P > Max or else not Real then
122 if Str (P) not in '0' .. '9' then
126 -- Scan out the exponent value as an unsigned integer. Values larger
127 -- than (Integer'Last / 10) are simply considered large enough here.
128 -- This assumption is correct for all machines we know of (e.g. in
129 -- the case of 16 bit integers it allows exponents up to 3276, which
130 -- is large enough for the largest floating types in base 2.)
135 if X < (Integer'Last / 10) then
136 X := X * 10 + (Character'Pos (Str (P)) - Character'Pos ('0'));
143 if Str (P) = '_' then
144 Scan_Underscore (Str, P, Ptr, Max, False);
146 exit when Str (P) not in '0' .. '9';
162 procedure Scan_Plus_Sign
164 Ptr : not null access Integer;
166 Start : out Positive)
168 P : Natural := Ptr.all;
172 raise Constraint_Error;
175 -- Scan past initial blanks
177 while Str (P) = ' ' loop
182 raise Constraint_Error;
188 -- Skip past an initial plus sign
190 if Str (P) = '+' then
195 raise Constraint_Error;
208 Ptr : not null access Integer;
211 Start : out Positive)
213 P : Natural := Ptr.all;
216 -- Deal with case of null string (all blanks!). As per spec, we
217 -- raise constraint error, with Ptr unchanged, and thus > Max.
220 raise Constraint_Error;
223 -- Scan past initial blanks
225 while Str (P) = ' ' loop
230 raise Constraint_Error;
236 -- Remember an initial minus sign
238 if Str (P) = '-' then
244 raise Constraint_Error;
247 -- Skip past an initial plus sign
249 elsif Str (P) = '+' then
255 raise Constraint_Error;
265 --------------------------
266 -- Scan_Trailing_Blanks --
267 --------------------------
269 procedure Scan_Trailing_Blanks (Str : String; P : Positive) is
271 for J in P .. Str'Last loop
272 if Str (J) /= ' ' then
273 raise Constraint_Error;
276 end Scan_Trailing_Blanks;
278 ---------------------
279 -- Scan_Underscore --
280 ---------------------
282 procedure Scan_Underscore
285 Ptr : not null access Integer;
294 -- If underscore is at the end of string, then this is an error and
295 -- we raise Constraint_Error, leaving the pointer past the underscore.
296 -- This seems a bit strange. It means e.g. that if the field is:
300 -- that Constraint_Error is raised. You might think that the RM in
301 -- this case would scan out the 345 as a valid integer, leaving the
302 -- pointer at the underscore, but the ACVC suite clearly requires
303 -- an error in this situation (see for example CE3704M).
307 raise Constraint_Error;
310 -- Similarly, if no digit follows the underscore raise an error. This
311 -- also catches the case of double underscore which is also an error.
317 (Ext and then (C in 'A' .. 'F' or else C in 'a' .. 'f'))
322 raise Constraint_Error;