1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . V A L _ L L U --
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
.Unsigned_Types
; use System
.Unsigned_Types
;
35 with System
.Val_Util
; use System
.Val_Util
;
37 package body System
.Val_LLU
is
39 ---------------------------------
40 -- Scan_Raw_Long_Long_Unsigned --
41 ---------------------------------
43 function Scan_Raw_Long_Long_Unsigned
46 Max
: Integer) return Long_Long_Unsigned
49 -- Local copy of the pointer
51 Uval
: Long_Long_Unsigned
;
52 -- Accumulated unsigned integer result
57 Overflow
: Boolean := False;
58 -- Set True if overflow is detected at any point
60 Base_Char
: Character;
61 -- Base character (# or :) in based case
63 Base
: Long_Long_Unsigned
:= 10;
64 -- Base value (reset in based case)
66 Digit
: Long_Long_Unsigned
;
71 Uval
:= Character'Pos (Str
(P
)) - Character'Pos ('0');
74 -- Scan out digits of what is either the number or the base.
75 -- In either case, we are definitely scanning out in base 10.
78 Umax
: constant := (Long_Long_Unsigned
'Last - 9) / 10;
79 -- Max value which cannot overflow on accumulating next digit
81 Umax10
: constant := Long_Long_Unsigned
'Last / 10;
82 -- Numbers bigger than Umax10 overflow if multiplied by 10
85 -- Loop through decimal digits
89 Digit
:= Character'Pos (Str
(P
)) - Character'Pos ('0');
91 -- Non-digit encountered
95 Scan_Underscore
(Str
, P
, Ptr
, Max
, False);
100 -- Accumulate result, checking for overflow
104 Uval
:= 10 * Uval
+ Digit
;
106 elsif Uval
> Umax10
then
110 Uval
:= 10 * Uval
+ Digit
;
112 if Uval
< Umax10
then
124 -- Deal with based case
126 if P
< Max
and then (Str
(P
) = ':' or else Str
(P
) = '#') then
127 Base_Char
:= Str
(P
);
132 -- Check base value. Overflow is set True if we find a bad base, or
133 -- a digit that is out of range of the base. That way, we scan out
134 -- the numeral that is still syntactically correct, though illegal.
135 -- We use a safe base of 16 for this scan, to avoid zero divide.
137 if Base
not in 2 .. 16 then
142 -- Scan out based integer
145 Umax
: constant Long_Long_Unsigned
:=
146 (Long_Long_Unsigned
'Last - Base
+ 1) / Base
;
147 -- Max value which cannot overflow on accumulating next digit
149 UmaxB
: constant Long_Long_Unsigned
:=
150 Long_Long_Unsigned
'Last / Base
;
151 -- Numbers bigger than UmaxB overflow if multiplied by base
154 -- Loop to scan out based integer value
157 -- We require a digit at this stage
159 if Str
(P
) in '0' .. '9' then
160 Digit
:= Character'Pos (Str
(P
)) - Character'Pos ('0');
162 elsif Str
(P
) in 'A' .. 'F' then
164 Character'Pos (Str
(P
)) - (Character'Pos ('A') - 10);
166 elsif Str
(P
) in 'a' .. 'f' then
168 Character'Pos (Str
(P
)) - (Character'Pos ('a') - 10);
170 -- If we don't have a digit, then this is not a based number
171 -- after all, so we use the value we scanned out as the base
172 -- (now in Base), and the pointer to the base character was
173 -- already stored in Ptr.all.
180 -- If digit is too large, just signal overflow and continue.
181 -- The idea here is to keep scanning as long as the input is
182 -- syntactically valid, even if we have detected overflow
184 if Digit
>= Base
then
187 -- Here we accumulate the value, checking overflow
189 elsif Uval
<= Umax
then
190 Uval
:= Base
* Uval
+ Digit
;
192 elsif Uval
> UmaxB
then
196 Uval
:= Base
* Uval
+ Digit
;
203 -- If at end of string with no base char, not a based number
204 -- but we signal Constraint_Error and set the pointer past
205 -- the end of the field, since this is what the ACVC tests
206 -- seem to require, see CE3704N, line 204.
212 raise Constraint_Error
;
215 -- If terminating base character, we are done with loop
217 if Str
(P
) = Base_Char
then
221 -- Deal with underscore
223 elsif Str
(P
) = '_' then
224 Scan_Underscore
(Str
, P
, Ptr
, Max
, True);
231 -- Come here with scanned unsigned value in Uval. The only remaining
232 -- required step is to deal with exponent if one is present.
234 Expon
:= Scan_Exponent
(Str
, Ptr
, Max
);
236 if Expon
/= 0 and then Uval
/= 0 then
238 -- For non-zero value, scale by exponent value. No need to do this
239 -- efficiently, since use of exponent in integer literals is rare,
240 -- and in any case the exponent cannot be very large.
243 UmaxB
: constant Long_Long_Unsigned
:=
244 Long_Long_Unsigned
'Last / Base
;
245 -- Numbers bigger than UmaxB overflow if multiplied by base
248 for J
in 1 .. Expon
loop
259 -- Return result, dealing with sign and overflow
262 raise Constraint_Error
;
266 end Scan_Raw_Long_Long_Unsigned
;
268 -----------------------------
269 -- Scan_Long_Long_Unsigned --
270 -----------------------------
272 function Scan_Long_Long_Unsigned
274 Ptr
: access Integer;
275 Max
: Integer) return Long_Long_Unsigned
278 -- Save location of first non-blank character
281 Scan_Plus_Sign
(Str
, Ptr
, Max
, Start
);
283 if Str
(Ptr
.all) not in '0' .. '9' then
285 raise Constraint_Error
;
288 return Scan_Raw_Long_Long_Unsigned
(Str
, Ptr
, Max
);
289 end Scan_Long_Long_Unsigned
;
291 ------------------------------
292 -- Value_Long_Long_Unsigned --
293 ------------------------------
295 function Value_Long_Long_Unsigned
296 (Str
: String) return Long_Long_Unsigned
298 V
: Long_Long_Unsigned
;
299 P
: aliased Integer := Str
'First;
301 V
:= Scan_Long_Long_Unsigned
(Str
, P
'Access, Str
'Last);
302 Scan_Trailing_Blanks
(Str
, P
);
304 end Value_Long_Long_Unsigned
;