| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . V A L _ U T I L -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1992-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 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. -- |
| 17 |
-- -- |
| 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. -- |
| 21 |
-- -- |
| 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/>. -- |
| 26 |
-- -- |
| 27 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
-- Ghost code, loop invariants and assertions in this unit are meant for |
| 33 |
-- analysis only, not for run-time checking, as it would be too costly |
| 34 |
-- otherwise. This is enforced by setting the assertion policy to Ignore. |
| 35 |
|
| 36 |
pragma Assertion_Policy (Ghost => Ignore, |
| 37 |
Loop_Invariant => Ignore, |
| 38 |
Assert => Ignore); |
| 39 |
|
| 40 |
with System.Case_Util; use System.Case_Util; |
| 41 |
|
| 42 |
package body System.Val_Util |
| 43 |
with SPARK_Mode |
| 44 |
is |
| 45 |
|
| 46 |
--------------- |
| 47 |
-- Bad_Value -- |
| 48 |
--------------- |
| 49 |
|
| 50 |
procedure Bad_Value (S : String) is |
| 51 |
pragma Annotate (GNATprove, Intentional, "exception might be raised", |
| 52 |
"Intentional exception from Bad_Value"); |
| 53 |
begin |
| 54 |
-- Bad_Value might be called with very long strings allocated on the |
| 55 |
-- heap. Limit the size of the message so that we avoid creating a |
| 56 |
-- Storage_Error during error handling. |
| 57 |
if S'Length > 127 then |
| 58 |
raise Constraint_Error with "bad input for 'Value: """ |
| 59 |
& S (S'First .. S'First + 127) & "..."""; |
| 60 |
else |
| 61 |
raise Constraint_Error with "bad input for 'Value: """ & S & '"'; |
| 62 |
end if; |
| 63 |
end Bad_Value; |
| 64 |
|
| 65 |
---------------------- |
| 66 |
-- Normalize_String -- |
| 67 |
---------------------- |
| 68 |
|
| 69 |
procedure Normalize_String |
| 70 |
(S : in out String; |
| 71 |
F, L : out Integer; |
| 72 |
To_Upper_Case : Boolean) |
| 73 |
is |
| 74 |
begin |
| 75 |
F := S'First; |
| 76 |
L := S'Last; |
| 77 |
|
| 78 |
-- Case of empty string |
| 79 |
|
| 80 |
if F > L then |
| 81 |
return; |
| 82 |
end if; |
| 83 |
|
| 84 |
-- Scan for leading spaces |
| 85 |
|
| 86 |
while F < L and then S (F) = ' ' loop |
| 87 |
pragma Loop_Invariant (F in S'First .. L - 1); |
| 88 |
pragma Loop_Invariant (for all J in S'First .. F => S (J) = ' '); |
| 89 |
pragma Loop_Variant (Increases => F); |
| 90 |
F := F + 1; |
| 91 |
end loop; |
| 92 |
|
| 93 |
-- Case of no nonspace characters found. Decrease L to ensure L < F |
| 94 |
-- without risking an overflow if F is Integer'Last. |
| 95 |
|
| 96 |
if S (F) = ' ' then |
| 97 |
L := L - 1; |
| 98 |
return; |
| 99 |
end if; |
| 100 |
|
| 101 |
-- Scan for trailing spaces |
| 102 |
|
| 103 |
while S (L) = ' ' loop |
| 104 |
pragma Loop_Invariant (L in F + 1 .. S'Last); |
| 105 |
pragma Loop_Invariant (for all J in L .. S'Last => S (J) = ' '); |
| 106 |
pragma Loop_Variant (Decreases => L); |
| 107 |
L := L - 1; |
| 108 |
end loop; |
| 109 |
|
| 110 |
-- Convert to upper case if requested and not a character literal |
| 111 |
|
| 112 |
if To_Upper_Case and then S (F) /= ''' then |
| 113 |
for J in F .. L loop |
| 114 |
S (J) := To_Upper (S (J)); |
| 115 |
pragma Loop_Invariant |
| 116 |
(for all K in F .. J => S (K) = To_Upper (S'Loop_Entry (K))); |
| 117 |
end loop; |
| 118 |
end if; |
| 119 |
end Normalize_String; |
| 120 |
|
| 121 |
------------------- |
| 122 |
-- Scan_Exponent -- |
| 123 |
------------------- |
| 124 |
|
| 125 |
procedure Scan_Exponent |
| 126 |
(Str : String; |
| 127 |
Ptr : not null access Integer; |
| 128 |
Max : Integer; |
| 129 |
Exp : out Integer; |
| 130 |
Real : Boolean := False) |
| 131 |
is |
| 132 |
P : Integer := Ptr.all; |
| 133 |
M : Boolean; |
| 134 |
X : Integer; |
| 135 |
|
| 136 |
begin |
| 137 |
if P >= Max |
| 138 |
or else (Str (P) /= 'E' and then Str (P) /= 'e') |
| 139 |
then |
| 140 |
Exp := 0; |
| 141 |
return; |
| 142 |
end if; |
| 143 |
pragma Annotate |
| 144 |
(CodePeer, False_Positive, "test always false", |
| 145 |
"the slice might be empty or not start with an 'e'"); |
| 146 |
|
| 147 |
-- We have an E/e, see if sign follows |
| 148 |
|
| 149 |
P := P + 1; |
| 150 |
|
| 151 |
if Str (P) = '+' then |
| 152 |
P := P + 1; |
| 153 |
|
| 154 |
if P > Max then |
| 155 |
Exp := 0; |
| 156 |
return; |
| 157 |
else |
| 158 |
M := False; |
| 159 |
end if; |
| 160 |
|
| 161 |
elsif Str (P) = '-' then |
| 162 |
P := P + 1; |
| 163 |
|
| 164 |
if P > Max or else not Real then |
| 165 |
Exp := 0; |
| 166 |
return; |
| 167 |
else |
| 168 |
M := True; |
| 169 |
end if; |
| 170 |
|
| 171 |
else |
| 172 |
M := False; |
| 173 |
end if; |
| 174 |
|
| 175 |
if Str (P) not in '0' .. '9' then |
| 176 |
Exp := 0; |
| 177 |
return; |
| 178 |
end if; |
| 179 |
|
| 180 |
-- Scan out the exponent value as an unsigned integer. Values larger |
| 181 |
-- than (Integer'Last / 10) are simply considered large enough here. |
| 182 |
-- This assumption is correct for all machines we know of (e.g. in the |
| 183 |
-- case of 16 bit integers it allows exponents up to 3276, which is |
| 184 |
-- large enough for the largest floating types in base 2.) |
| 185 |
|
| 186 |
X := 0; |
| 187 |
|
| 188 |
declare |
| 189 |
Rest : constant String := Str (P .. Max) with Ghost; |
| 190 |
Last : constant Natural := Sp.Last_Number_Ghost (Rest) with Ghost; |
| 191 |
|
| 192 |
begin |
| 193 |
pragma Assert (Sp.Is_Natural_Format_Ghost (Rest)); |
| 194 |
|
| 195 |
loop |
| 196 |
pragma Assert (Str (P) in '0' .. '9'); |
| 197 |
|
| 198 |
if X < (Integer'Last / 10) then |
| 199 |
X := X * 10 + (Character'Pos (Str (P)) - Character'Pos ('0')); |
| 200 |
end if; |
| 201 |
|
| 202 |
pragma Loop_Invariant (X >= 0); |
| 203 |
pragma Loop_Invariant (P in Rest'First .. Last); |
| 204 |
pragma Loop_Invariant (Str (P) in '0' .. '9'); |
| 205 |
pragma Loop_Invariant |
| 206 |
(Sp.Scan_Natural_Ghost (Rest, Rest'First, 0) |
| 207 |
= Sp.Scan_Natural_Ghost (Rest, P + 1, X)); |
| 208 |
|
| 209 |
P := P + 1; |
| 210 |
|
| 211 |
exit when P > Max; |
| 212 |
|
| 213 |
if Str (P) = '_' then |
| 214 |
Scan_Underscore (Str, P, Ptr, Max, False); |
| 215 |
else |
| 216 |
exit when Str (P) not in '0' .. '9'; |
| 217 |
end if; |
| 218 |
end loop; |
| 219 |
|
| 220 |
pragma Assert (P = Last + 1); |
| 221 |
end; |
| 222 |
|
| 223 |
if M then |
| 224 |
X := -X; |
| 225 |
end if; |
| 226 |
|
| 227 |
Ptr.all := P; |
| 228 |
Exp := X; |
| 229 |
end Scan_Exponent; |
| 230 |
|
| 231 |
-------------------- |
| 232 |
-- Scan_Plus_Sign -- |
| 233 |
-------------------- |
| 234 |
|
| 235 |
procedure Scan_Plus_Sign |
| 236 |
(Str : String; |
| 237 |
Ptr : not null access Integer; |
| 238 |
Max : Integer; |
| 239 |
Start : out Positive) |
| 240 |
is |
| 241 |
P : Integer := Ptr.all; |
| 242 |
|
| 243 |
begin |
| 244 |
if P > Max then |
| 245 |
Bad_Value (Str); |
| 246 |
end if; |
| 247 |
|
| 248 |
-- Scan past initial blanks |
| 249 |
|
| 250 |
while Str (P) = ' ' loop |
| 251 |
P := P + 1; |
| 252 |
|
| 253 |
pragma Loop_Invariant (Ptr.all = Ptr.all'Loop_Entry); |
| 254 |
pragma Loop_Invariant (P in Ptr.all .. Max); |
| 255 |
pragma Loop_Invariant (for some J in P .. Max => Str (J) /= ' '); |
| 256 |
pragma Loop_Invariant |
| 257 |
(for all J in Ptr.all .. P - 1 => Str (J) = ' '); |
| 258 |
|
| 259 |
if P > Max then |
| 260 |
Ptr.all := P; |
| 261 |
Bad_Value (Str); |
| 262 |
end if; |
| 263 |
end loop; |
| 264 |
|
| 265 |
Start := P; |
| 266 |
|
| 267 |
pragma Assert (Start = Sp.First_Non_Space_Ghost (Str, Ptr.all, Max)); |
| 268 |
|
| 269 |
-- Skip past an initial plus sign |
| 270 |
|
| 271 |
if Str (P) = '+' then |
| 272 |
P := P + 1; |
| 273 |
|
| 274 |
if P > Max then |
| 275 |
Ptr.all := Start; |
| 276 |
Bad_Value (Str); |
| 277 |
end if; |
| 278 |
end if; |
| 279 |
|
| 280 |
Ptr.all := P; |
| 281 |
end Scan_Plus_Sign; |
| 282 |
|
| 283 |
--------------- |
| 284 |
-- Scan_Sign -- |
| 285 |
--------------- |
| 286 |
|
| 287 |
procedure Scan_Sign |
| 288 |
(Str : String; |
| 289 |
Ptr : not null access Integer; |
| 290 |
Max : Integer; |
| 291 |
Minus : out Boolean; |
| 292 |
Start : out Positive) |
| 293 |
is |
| 294 |
P : Integer := Ptr.all; |
| 295 |
|
| 296 |
begin |
| 297 |
-- Deal with case of null string (all blanks). As per spec, we raise |
| 298 |
-- constraint error, with Ptr unchanged, and thus > Max. |
| 299 |
|
| 300 |
if P > Max then |
| 301 |
Bad_Value (Str); |
| 302 |
end if; |
| 303 |
|
| 304 |
-- Scan past initial blanks |
| 305 |
|
| 306 |
while Str (P) = ' ' loop |
| 307 |
P := P + 1; |
| 308 |
|
| 309 |
pragma Loop_Invariant (Ptr.all = Ptr.all'Loop_Entry); |
| 310 |
pragma Loop_Invariant (P in Ptr.all .. Max); |
| 311 |
pragma Loop_Invariant (for some J in P .. Max => Str (J) /= ' '); |
| 312 |
pragma Loop_Invariant |
| 313 |
(for all J in Ptr.all .. P - 1 => Str (J) = ' '); |
| 314 |
|
| 315 |
if P > Max then |
| 316 |
Ptr.all := P; |
| 317 |
Bad_Value (Str); |
| 318 |
end if; |
| 319 |
end loop; |
| 320 |
|
| 321 |
Start := P; |
| 322 |
|
| 323 |
pragma Assert (Start = Sp.First_Non_Space_Ghost (Str, Ptr.all, Max)); |
| 324 |
|
| 325 |
-- Remember an initial minus sign |
| 326 |
|
| 327 |
if Str (P) = '-' then |
| 328 |
Minus := True; |
| 329 |
P := P + 1; |
| 330 |
|
| 331 |
if P > Max then |
| 332 |
Ptr.all := Start; |
| 333 |
Bad_Value (Str); |
| 334 |
end if; |
| 335 |
|
| 336 |
-- Skip past an initial plus sign |
| 337 |
|
| 338 |
elsif Str (P) = '+' then |
| 339 |
Minus := False; |
| 340 |
P := P + 1; |
| 341 |
|
| 342 |
if P > Max then |
| 343 |
Ptr.all := Start; |
| 344 |
Bad_Value (Str); |
| 345 |
end if; |
| 346 |
|
| 347 |
else |
| 348 |
Minus := False; |
| 349 |
end if; |
| 350 |
|
| 351 |
Ptr.all := P; |
| 352 |
end Scan_Sign; |
| 353 |
|
| 354 |
-------------------------- |
| 355 |
-- Scan_Trailing_Blanks -- |
| 356 |
-------------------------- |
| 357 |
|
| 358 |
procedure Scan_Trailing_Blanks (Str : String; P : Positive) is |
| 359 |
begin |
| 360 |
for J in P .. Str'Last loop |
| 361 |
if Str (J) /= ' ' then |
| 362 |
Bad_Value (Str); |
| 363 |
end if; |
| 364 |
|
| 365 |
pragma Loop_Invariant (for all K in P .. J => Str (K) = ' '); |
| 366 |
end loop; |
| 367 |
end Scan_Trailing_Blanks; |
| 368 |
|
| 369 |
--------------------- |
| 370 |
-- Scan_Underscore -- |
| 371 |
--------------------- |
| 372 |
|
| 373 |
procedure Scan_Underscore |
| 374 |
(Str : String; |
| 375 |
P : in out Natural; |
| 376 |
Ptr : not null access Integer; |
| 377 |
Max : Integer; |
| 378 |
Ext : Boolean) |
| 379 |
is |
| 380 |
C : Character; |
| 381 |
|
| 382 |
begin |
| 383 |
P := P + 1; |
| 384 |
|
| 385 |
-- If underscore is at the end of string, then this is an error and we |
| 386 |
-- raise Constraint_Error, leaving the pointer past the underscore. This |
| 387 |
-- seems a bit strange. It means e.g. that if the field is: |
| 388 |
|
| 389 |
-- 345_ |
| 390 |
|
| 391 |
-- that Constraint_Error is raised. You might think that the RM in this |
| 392 |
-- case would scan out the 345 as a valid integer, leaving the pointer |
| 393 |
-- at the underscore, but the ACVC suite clearly requires an error in |
| 394 |
-- this situation (see for example CE3704M). |
| 395 |
|
| 396 |
if P > Max then |
| 397 |
Ptr.all := P; |
| 398 |
Bad_Value (Str); |
| 399 |
end if; |
| 400 |
|
| 401 |
-- Similarly, if no digit follows the underscore raise an error. This |
| 402 |
-- also catches the case of double underscore which is also an error. |
| 403 |
|
| 404 |
C := Str (P); |
| 405 |
|
| 406 |
if C in '0' .. '9' |
| 407 |
or else (Ext and then (C in 'A' .. 'F' or else C in 'a' .. 'f')) |
| 408 |
then |
| 409 |
return; |
| 410 |
else |
| 411 |
Ptr.all := P; |
| 412 |
Bad_Value (Str); |
| 413 |
end if; |
| 414 |
end Scan_Underscore; |
| 415 |
|
| 416 |
end System.Val_Util; |