1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . V A L _ U T I L --
9 -- Copyright (C) 1992-2013, 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
40 procedure Bad_Value (S : String) is
42 raise Constraint_Error with "bad input for 'Value: """ & S & '"';
45 ----------------------
46 -- Normalize_String --
47 ----------------------
49 procedure Normalize_String
57 -- Scan for leading spaces
59 while F <= L and then S (F) = ' ' loop
63 -- Check for case when the string contained no characters
69 -- Scan for trailing spaces
71 while S (L) = ' ' loop
75 -- Except in the case of a character literal, convert to upper case
79 S (J) := To_Upper (S (J));
88 function Scan_Exponent
90 Ptr : not null access Integer;
92 Real : Boolean := False) return Integer
94 P : Natural := Ptr.all;
100 or else (Str (P) /= 'E' and then Str (P) /= 'e')
105 -- We have an E/e, see if sign follows
109 if Str (P) = '+' then
118 elsif Str (P) = '-' then
121 if P > Max or else not Real then
131 if Str (P) not in '0' .. '9' then
135 -- Scan out the exponent value as an unsigned integer. Values larger
136 -- than (Integer'Last / 10) are simply considered large enough here.
137 -- This assumption is correct for all machines we know of (e.g. in the
138 -- case of 16 bit integers it allows exponents up to 3276, which is
139 -- large enough for the largest floating types in base 2.)
144 if X < (Integer'Last / 10) then
145 X := X * 10 + (Character'Pos (Str (P)) - Character'Pos ('0'));
152 if Str (P) = '_' then
153 Scan_Underscore (Str, P, Ptr, Max, False);
155 exit when Str (P) not in '0' .. '9';
171 procedure Scan_Plus_Sign
173 Ptr : not null access Integer;
175 Start : out Positive)
177 P : Natural := Ptr.all;
184 -- Scan past initial blanks
186 while Str (P) = ' ' loop
197 -- Skip past an initial plus sign
199 if Str (P) = '+' then
217 Ptr : not null access Integer;
220 Start : out Positive)
222 P : Natural := Ptr.all;
225 -- Deal with case of null string (all blanks). As per spec, we raise
226 -- constraint error, with Ptr unchanged, and thus > Max.
232 -- Scan past initial blanks
234 while Str (P) = ' ' loop
245 -- Remember an initial minus sign
247 if Str (P) = '-' then
256 -- Skip past an initial plus sign
258 elsif Str (P) = '+' then
274 --------------------------
275 -- Scan_Trailing_Blanks --
276 --------------------------
278 procedure Scan_Trailing_Blanks (Str : String; P : Positive) is
280 for J in P .. Str'Last loop
281 if Str (J) /= ' ' then
285 end Scan_Trailing_Blanks;
287 ---------------------
288 -- Scan_Underscore --
289 ---------------------
291 procedure Scan_Underscore
294 Ptr : not null access Integer;
303 -- If underscore is at the end of string, then this is an error and we
304 -- raise Constraint_Error, leaving the pointer past the underscore. This
305 -- seems a bit strange. It means e.g. that if the field is:
309 -- that Constraint_Error is raised. You might think that the RM in this
310 -- case would scan out the 345 as a valid integer, leaving the pointer
311 -- at the underscore, but the ACVC suite clearly requires an error in
312 -- this situation (see for example CE3704M).
319 -- Similarly, if no digit follows the underscore raise an error. This
320 -- also catches the case of double underscore which is also an error.
325 or else (Ext and then (C in 'A' .. 'F' or else C in 'a' .. 'f'))