1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . V A L _ U T I L --
9 -- Copyright (C) 1992-2006, 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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with System
.Case_Util
; use System
.Case_Util
;
36 package body System
.Val_Util
is
38 ----------------------
39 -- Normalize_String --
40 ----------------------
42 procedure Normalize_String
50 -- Scan for leading spaces
52 while F
<= L
and then S
(F
) = ' ' loop
56 -- Check for case when the string contained no characters
59 raise Constraint_Error
;
62 -- Scan for trailing spaces
64 while S
(L
) = ' ' loop
68 -- Except in the case of a character literal, convert to upper case
72 S
(J
) := To_Upper
(S
(J
));
82 function Scan_Exponent
86 Real
: Boolean := False) return Integer
88 P
: Natural := Ptr
.all;
94 or else (Str
(P
) /= 'E' and then Str
(P
) /= 'e')
99 -- We have an E/e, see if sign follows
103 if Str
(P
) = '+' then
112 elsif Str
(P
) = '-' then
115 if P
> Max
or else not Real
then
125 if Str
(P
) not in '0' .. '9' then
129 -- Scan out the exponent value as an unsigned integer. Values larger
130 -- than (Integer'Last / 10) are simply considered large enough here.
131 -- This assumption is correct for all machines we know of (e.g. in
132 -- the case of 16 bit integers it allows exponents up to 3276, which
133 -- is large enough for the largest floating types in base 2.)
138 if X
< (Integer'Last / 10) then
139 X
:= X
* 10 + (Character'Pos (Str
(P
)) - Character'Pos ('0'));
146 if Str
(P
) = '_' then
147 Scan_Underscore
(Str
, P
, Ptr
, Max
, False);
149 exit when Str
(P
) not in '0' .. '9';
166 procedure Scan_Plus_Sign
168 Ptr
: access Integer;
170 Start
: out Positive)
172 P
: Natural := Ptr
.all;
176 raise Constraint_Error
;
179 -- Scan past initial blanks
181 while Str
(P
) = ' ' loop
186 raise Constraint_Error
;
192 -- Skip past an initial plus sign
194 if Str
(P
) = '+' then
199 raise Constraint_Error
;
212 Ptr
: access Integer;
215 Start
: out Positive)
217 P
: Natural := Ptr
.all;
220 -- Deal with case of null string (all blanks!). As per spec, we
221 -- raise constraint error, with Ptr unchanged, and thus > Max.
224 raise Constraint_Error
;
227 -- Scan past initial blanks
229 while Str
(P
) = ' ' loop
234 raise Constraint_Error
;
240 -- Remember an initial minus sign
242 if Str
(P
) = '-' then
248 raise Constraint_Error
;
251 -- Skip past an initial plus sign
253 elsif Str
(P
) = '+' then
259 raise Constraint_Error
;
269 --------------------------
270 -- Scan_Trailing_Blanks --
271 --------------------------
273 procedure Scan_Trailing_Blanks
(Str
: String; P
: Positive) is
275 for J
in P
.. Str
'Last loop
276 if Str
(J
) /= ' ' then
277 raise Constraint_Error
;
280 end Scan_Trailing_Blanks
;
282 ---------------------
283 -- Scan_Underscore --
284 ---------------------
286 procedure Scan_Underscore
289 Ptr
: access Integer;
298 -- If underscore is at the end of string, then this is an error and
299 -- we raise Constraint_Error, leaving the pointer past the undescore.
300 -- This seems a bit strange. It means e,g, that if the field is:
304 -- that Constraint_Error is raised. You might think that the RM in
305 -- this case would scan out the 345 as a valid integer, leaving the
306 -- pointer at the underscore, but the ACVC suite clearly requires
307 -- an error in this situation (see for example CE3704M).
311 raise Constraint_Error
;
314 -- Similarly, if no digit follows the underscore raise an error. This
315 -- also catches the case of double underscore which is also an error.
321 (Ext
and then (C
in 'A' .. 'F' or else C
in 'a' .. 'f'))
326 raise Constraint_Error
;