1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . V A L _ U T I L --
10 -- Copyright (C) 1992-2001, Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
35 with GNAT
.Case_Util
; use GNAT
.Case_Util
;
37 package body System
.Val_Util
is
39 ----------------------
40 -- Normalize_String --
41 ----------------------
43 procedure Normalize_String
51 -- Scan for leading spaces
53 while F
<= L
and then S
(F
) = ' ' loop
57 -- Check for case when the string contained no characters
60 raise Constraint_Error
;
63 -- Scan for trailing spaces
65 while S
(L
) = ' ' loop
69 -- Except in the case of a character literal, convert to upper case
73 S
(J
) := To_Upper
(S
(J
));
83 function Scan_Exponent
87 Real
: Boolean := False)
90 P
: Natural := Ptr
.all;
96 or else (Str
(P
) /= 'E' and then Str
(P
) /= 'e')
101 -- We have an E/e, see if sign follows
105 if Str
(P
) = '+' then
114 elsif Str
(P
) = '-' then
117 if P
> Max
or else not Real
then
127 if Str
(P
) not in '0' .. '9' then
131 -- Scan out the exponent value as an unsigned integer. Values larger
132 -- than (Integer'Last / 10) are simply considered large enough here.
133 -- This assumption is correct for all machines we know of (e.g. in
134 -- the case of 16 bit integers it allows exponents up to 3276, which
135 -- is large enough for the largest floating types in base 2.)
140 if X
< (Integer'Last / 10) then
141 X
:= X
* 10 + (Character'Pos (Str
(P
)) - Character'Pos ('0'));
148 if Str
(P
) = '_' then
149 Scan_Underscore
(Str
, P
, Ptr
, Max
, False);
151 exit when Str
(P
) not in '0' .. '9';
170 Ptr
: access Integer;
173 Start
: out Positive)
175 P
: Natural := Ptr
.all;
178 -- Deal with case of null string (all blanks!). As per spec, we
179 -- raise constraint error, with Ptr unchanged, and thus > Max.
182 raise Constraint_Error
;
185 -- Scan past initial blanks
187 while Str
(P
) = ' ' loop
192 raise Constraint_Error
;
198 -- Remember an initial minus sign
200 if Str
(P
) = '-' then
206 raise Constraint_Error
;
209 -- Skip past an initial plus sign
211 elsif Str
(P
) = '+' then
217 raise Constraint_Error
;
227 --------------------------
228 -- Scan_Trailing_Blanks --
229 --------------------------
231 procedure Scan_Trailing_Blanks
(Str
: String; P
: Positive) is
233 for J
in P
.. Str
'Last loop
234 if Str
(J
) /= ' ' then
235 raise Constraint_Error
;
238 end Scan_Trailing_Blanks
;
240 ---------------------
241 -- Scan_Underscore --
242 ---------------------
244 procedure Scan_Underscore
247 Ptr
: access Integer;
256 -- If underscore is at the end of string, then this is an error and
257 -- we raise Constraint_Error, leaving the pointer past the undescore.
258 -- This seems a bit strange. It means e,g, that if the field is:
262 -- that Constraint_Error is raised. You might think that the RM in
263 -- this case would scan out the 345 as a valid integer, leaving the
264 -- pointer at the underscore, but the ACVC suite clearly requires
265 -- an error in this situation (see for example CE3704M).
269 raise Constraint_Error
;
272 -- Similarly, if no digit follows the underscore raise an error. This
273 -- also catches the case of double underscore which is also an error.
279 (Ext
and then (C
in 'A' .. 'F' or else C
in 'a' .. 'f'))
284 raise Constraint_Error
;