1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . V A L _ L L U --
9 -- Copyright (C) 1992-2015, 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
.Unsigned_Types
; use System
.Unsigned_Types
;
33 with System
.Val_Util
; use System
.Val_Util
;
35 package body System
.Val_LLU
is
37 ---------------------------------
38 -- Scan_Raw_Long_Long_Unsigned --
39 ---------------------------------
41 function Scan_Raw_Long_Long_Unsigned
43 Ptr
: not null access Integer;
44 Max
: Integer) return Long_Long_Unsigned
47 -- Local copy of the pointer
49 Uval
: Long_Long_Unsigned
;
50 -- Accumulated unsigned integer result
55 Overflow
: Boolean := False;
56 -- Set True if overflow is detected at any point
58 Base_Char
: Character;
59 -- Base character (# or :) in based case
61 Base
: Long_Long_Unsigned
:= 10;
62 -- Base value (reset in based case)
64 Digit
: Long_Long_Unsigned
;
68 -- We do not tolerate strings with Str'Last = Positive'Last
70 if Str
'Last = Positive'Last then
71 raise Program_Error
with
72 "string upper bound is Positive'Last, not supported";
76 Uval
:= Character'Pos (Str
(P
)) - Character'Pos ('0');
79 -- Scan out digits of what is either the number or the base.
80 -- In either case, we are definitely scanning out in base 10.
83 Umax
: constant := (Long_Long_Unsigned
'Last - 9) / 10;
84 -- Max value which cannot overflow on accumulating next digit
86 Umax10
: constant := Long_Long_Unsigned
'Last / 10;
87 -- Numbers bigger than Umax10 overflow if multiplied by 10
90 -- Loop through decimal digits
94 Digit
:= Character'Pos (Str
(P
)) - Character'Pos ('0');
96 -- Non-digit encountered
100 Scan_Underscore
(Str
, P
, Ptr
, Max
, False);
105 -- Accumulate result, checking for overflow
109 Uval
:= 10 * Uval
+ Digit
;
111 elsif Uval
> Umax10
then
115 Uval
:= 10 * Uval
+ Digit
;
117 if Uval
< Umax10
then
129 -- Deal with based case. We recognize either the standard '#' or the
130 -- allowed alternative replacement ':' (see RM J.2(3)).
132 if P
< Max
and then (Str
(P
) = '#' or else Str
(P
) = ':') then
133 Base_Char
:= Str
(P
);
138 -- Check base value. Overflow is set True if we find a bad base, or
139 -- a digit that is out of range of the base. That way, we scan out
140 -- the numeral that is still syntactically correct, though illegal.
141 -- We use a safe base of 16 for this scan, to avoid zero divide.
143 if Base
not in 2 .. 16 then
148 -- Scan out based integer
151 Umax
: constant Long_Long_Unsigned
:=
152 (Long_Long_Unsigned
'Last - Base
+ 1) / Base
;
153 -- Max value which cannot overflow on accumulating next digit
155 UmaxB
: constant Long_Long_Unsigned
:=
156 Long_Long_Unsigned
'Last / Base
;
157 -- Numbers bigger than UmaxB overflow if multiplied by base
160 -- Loop to scan out based integer value
163 -- We require a digit at this stage
165 if Str
(P
) in '0' .. '9' then
166 Digit
:= Character'Pos (Str
(P
)) - Character'Pos ('0');
168 elsif Str
(P
) in 'A' .. 'F' then
170 Character'Pos (Str
(P
)) - (Character'Pos ('A') - 10);
172 elsif Str
(P
) in 'a' .. 'f' then
174 Character'Pos (Str
(P
)) - (Character'Pos ('a') - 10);
176 -- If we don't have a digit, then this is not a based number
177 -- after all, so we use the value we scanned out as the base
178 -- (now in Base), and the pointer to the base character was
179 -- already stored in Ptr.all.
186 -- If digit is too large, just signal overflow and continue.
187 -- The idea here is to keep scanning as long as the input is
188 -- syntactically valid, even if we have detected overflow
190 if Digit
>= Base
then
193 -- Here we accumulate the value, checking overflow
195 elsif Uval
<= Umax
then
196 Uval
:= Base
* Uval
+ Digit
;
198 elsif Uval
> UmaxB
then
202 Uval
:= Base
* Uval
+ Digit
;
209 -- If at end of string with no base char, not a based number
210 -- but we signal Constraint_Error and set the pointer past
211 -- the end of the field, since this is what the ACVC tests
212 -- seem to require, see CE3704N, line 204.
221 -- If terminating base character, we are done with loop
223 if Str
(P
) = Base_Char
then
227 -- Deal with underscore
229 elsif Str
(P
) = '_' then
230 Scan_Underscore
(Str
, P
, Ptr
, Max
, True);
237 -- Come here with scanned unsigned value in Uval. The only remaining
238 -- required step is to deal with exponent if one is present.
240 Expon
:= Scan_Exponent
(Str
, Ptr
, Max
);
242 if Expon
/= 0 and then Uval
/= 0 then
244 -- For non-zero value, scale by exponent value. No need to do this
245 -- efficiently, since use of exponent in integer literals is rare,
246 -- and in any case the exponent cannot be very large.
249 UmaxB
: constant Long_Long_Unsigned
:=
250 Long_Long_Unsigned
'Last / Base
;
251 -- Numbers bigger than UmaxB overflow if multiplied by base
254 for J
in 1 .. Expon
loop
265 -- Return result, dealing with sign and overflow
272 end Scan_Raw_Long_Long_Unsigned
;
274 -----------------------------
275 -- Scan_Long_Long_Unsigned --
276 -----------------------------
278 function Scan_Long_Long_Unsigned
280 Ptr
: not null access Integer;
281 Max
: Integer) return Long_Long_Unsigned
284 -- Save location of first non-blank character
287 Scan_Plus_Sign
(Str
, Ptr
, Max
, Start
);
289 if Str
(Ptr
.all) not in '0' .. '9' then
291 raise Constraint_Error
;
294 return Scan_Raw_Long_Long_Unsigned
(Str
, Ptr
, Max
);
295 end Scan_Long_Long_Unsigned
;
297 ------------------------------
298 -- Value_Long_Long_Unsigned --
299 ------------------------------
301 function Value_Long_Long_Unsigned
302 (Str
: String) return Long_Long_Unsigned
305 -- We have to special case Str'Last = Positive'Last because the normal
306 -- circuit ends up setting P to Str'Last + 1 which is out of bounds. We
307 -- deal with this by converting to a subtype which fixes the bounds.
309 if Str
'Last = Positive'Last then
311 subtype NT
is String (1 .. Str
'Length);
313 return Value_Long_Long_Unsigned
(NT
(Str
));
316 -- Normal case where Str'Last < Positive'Last
320 V
: Long_Long_Unsigned
;
321 P
: aliased Integer := Str
'First;
323 V
:= Scan_Long_Long_Unsigned
(Str
, P
'Access, Str
'Last);
324 Scan_Trailing_Blanks
(Str
, P
);
328 end Value_Long_Long_Unsigned
;