1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . V A L _ U N S --
9 -- Copyright (C) 1992-2014, 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_Uns
is
37 -----------------------
38 -- Scan_Raw_Unsigned --
39 -----------------------
41 function Scan_Raw_Unsigned
43 Ptr
: not null access Integer;
44 Max
: Integer) return Unsigned
47 -- Local copy of the pointer
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
: Unsigned
:= 10;
62 -- Base value (reset in based case)
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 := (Unsigned
'Last - 9) / 10;
84 -- Max value which cannot overflow on accumulating next digit
86 Umax10
: constant := 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 Unsigned
:= (Unsigned
'Last - Base
+ 1) / Base
;
152 -- Max value which cannot overflow on accumulating next digit
154 UmaxB
: constant Unsigned
:= Unsigned
'Last / Base
;
155 -- Numbers bigger than UmaxB overflow if multiplied by base
158 -- Loop to scan out based integer value
161 -- We require a digit at this stage
163 if Str
(P
) in '0' .. '9' then
164 Digit
:= Character'Pos (Str
(P
)) - Character'Pos ('0');
166 elsif Str
(P
) in 'A' .. 'F' then
168 Character'Pos (Str
(P
)) - (Character'Pos ('A') - 10);
170 elsif Str
(P
) in 'a' .. 'f' then
172 Character'Pos (Str
(P
)) - (Character'Pos ('a') - 10);
174 -- If we don't have a digit, then this is not a based number
175 -- after all, so we use the value we scanned out as the base
176 -- (now in Base), and the pointer to the base character was
177 -- already stored in Ptr.all.
184 -- If digit is too large, just signal overflow and continue.
185 -- The idea here is to keep scanning as long as the input is
186 -- syntactically valid, even if we have detected overflow
188 if Digit
>= Base
then
191 -- Here we accumulate the value, checking overflow
193 elsif Uval
<= Umax
then
194 Uval
:= Base
* Uval
+ Digit
;
196 elsif Uval
> UmaxB
then
200 Uval
:= Base
* Uval
+ Digit
;
207 -- If at end of string with no base char, not a based number
208 -- but we signal Constraint_Error and set the pointer past
209 -- the end of the field, since this is what the ACVC tests
210 -- seem to require, see CE3704N, line 204.
219 -- If terminating base character, we are done with loop
221 if Str
(P
) = Base_Char
then
225 -- Deal with underscore
227 elsif Str
(P
) = '_' then
228 Scan_Underscore
(Str
, P
, Ptr
, Max
, True);
235 -- Come here with scanned unsigned value in Uval. The only remaining
236 -- required step is to deal with exponent if one is present.
238 Expon
:= Scan_Exponent
(Str
, Ptr
, Max
);
240 if Expon
/= 0 and then Uval
/= 0 then
242 -- For non-zero value, scale by exponent value. No need to do this
243 -- efficiently, since use of exponent in integer literals is rare,
244 -- and in any case the exponent cannot be very large.
247 UmaxB
: constant Unsigned
:= Unsigned
'Last / Base
;
248 -- Numbers bigger than UmaxB overflow if multiplied by base
251 for J
in 1 .. Expon
loop
262 -- Return result, dealing with sign and overflow
269 end Scan_Raw_Unsigned
;
275 function Scan_Unsigned
277 Ptr
: not null access Integer;
278 Max
: Integer) return Unsigned
281 -- Save location of first non-blank character
284 Scan_Plus_Sign
(Str
, Ptr
, Max
, Start
);
286 if Str
(Ptr
.all) not in '0' .. '9' then
291 return Scan_Raw_Unsigned
(Str
, Ptr
, Max
);
298 function Value_Unsigned
(Str
: String) return Unsigned
is
300 -- We have to special case Str'Last = Positive'Last because the normal
301 -- circuit ends up setting P to Str'Last + 1 which is out of bounds. We
302 -- deal with this by converting to a subtype which fixes the bounds.
304 if Str
'Last = Positive'Last then
306 subtype NT
is String (1 .. Str
'Length);
308 return Value_Unsigned
(NT
(Str
));
311 -- Normal case where Str'Last < Positive'Last
316 P
: aliased Integer := Str
'First;
318 V
:= Scan_Unsigned
(Str
, P
'Access, Str
'Last);
319 Scan_Trailing_Blanks
(Str
, P
);