| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . V A L _ U T I L -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 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 |
-- This package provides some common utilities used by the s-valxxx files |
| 33 |
|
| 34 |
-- Preconditions in this unit are meant for analysis only, not for run-time |
| 35 |
-- checking, so that the expected exceptions are raised. This is enforced by |
| 36 |
-- setting the corresponding assertion policy to Ignore. Postconditions and |
| 37 |
-- contract cases should not be executed at runtime as well, in order not to |
| 38 |
-- slow down the execution of these functions. |
| 39 |
|
| 40 |
pragma Assertion_Policy (Pre => Ignore, |
| 41 |
Post => Ignore, |
| 42 |
Contract_Cases => Ignore, |
| 43 |
Ghost => Ignore); |
| 44 |
|
| 45 |
with System.Case_Util; |
| 46 |
with System.Val_Spec; |
| 47 |
|
| 48 |
package System.Val_Util |
| 49 |
with SPARK_Mode, Pure |
| 50 |
is |
| 51 |
pragma Unevaluated_Use_Of_Old (Allow); |
| 52 |
|
| 53 |
package Sp renames System.Val_Spec; |
| 54 |
|
| 55 |
procedure Bad_Value (S : String) |
| 56 |
with |
| 57 |
Always_Terminates, |
| 58 |
Depends => (null => S), |
| 59 |
Exceptional_Cases => (others => Standard.False); |
| 60 |
pragma No_Return (Bad_Value); |
| 61 |
-- Raises constraint error with message: bad input for 'Value: "xxx" |
| 62 |
|
| 63 |
procedure Normalize_String |
| 64 |
(S : in out String; |
| 65 |
F, L : out Integer; |
| 66 |
To_Upper_Case : Boolean) |
| 67 |
with |
| 68 |
Post => (if Sp.Only_Space_Ghost (S'Old, S'First, S'Last) then |
| 69 |
F > L |
| 70 |
else |
| 71 |
F >= S'First |
| 72 |
and then L <= S'Last |
| 73 |
and then F <= L |
| 74 |
and then Sp.Only_Space_Ghost (S'Old, S'First, F - 1) |
| 75 |
and then S'Old (F) /= ' ' |
| 76 |
and then S'Old (L) /= ' ' |
| 77 |
and then |
| 78 |
(if L < S'Last then |
| 79 |
Sp.Only_Space_Ghost (S'Old, L + 1, S'Last)) |
| 80 |
and then |
| 81 |
(if To_Upper_Case and then S'Old (F) /= ''' then |
| 82 |
(for all J in S'Range => |
| 83 |
(if J in F .. L then |
| 84 |
S (J) = System.Case_Util.To_Upper (S'Old (J)) |
| 85 |
else |
| 86 |
S (J) = S'Old (J))))); |
| 87 |
-- This procedure scans the string S setting F to be the index of the first |
| 88 |
-- non-blank character of S and L to be the index of the last non-blank |
| 89 |
-- character of S. If To_Upper_Case is True and S does not represent a |
| 90 |
-- character literal, then any lower case characters in S are changed to |
| 91 |
-- their upper case counterparts. If S consists of only blank characters |
| 92 |
-- (including when S = "") then we return with F > L. |
| 93 |
|
| 94 |
procedure Scan_Sign |
| 95 |
(Str : String; |
| 96 |
Ptr : not null access Integer; |
| 97 |
Max : Integer; |
| 98 |
Minus : out Boolean; |
| 99 |
Start : out Positive) |
| 100 |
with |
| 101 |
Pre => |
| 102 |
-- Ptr.all .. Max is either an empty range, or a valid range in Str |
| 103 |
(Ptr.all > Max or else (Ptr.all >= Str'First and then Max <= Str'Last)) |
| 104 |
and then not Sp.Only_Space_Ghost (Str, Ptr.all, Max) |
| 105 |
and then |
| 106 |
(declare |
| 107 |
F : constant Positive := |
| 108 |
Sp.First_Non_Space_Ghost (Str, Ptr.all, Max); |
| 109 |
begin |
| 110 |
(if Str (F) in '+' | '-' then |
| 111 |
F <= Max - 1 and then Str (F + 1) /= ' ')), |
| 112 |
Post => |
| 113 |
(declare |
| 114 |
F : constant Positive := |
| 115 |
Sp.First_Non_Space_Ghost (Str, Ptr.all'Old, Max); |
| 116 |
begin |
| 117 |
Minus = (Str (F) = '-') |
| 118 |
and then Ptr.all = (if Str (F) in '+' | '-' then F + 1 else F) |
| 119 |
and then Start = F); |
| 120 |
-- The Str, Ptr, Max parameters are as for the scan routines (Str is the |
| 121 |
-- string to be scanned starting at Ptr.all, and Max is the index of the |
| 122 |
-- last character in the string). Scan_Sign first scans out any initial |
| 123 |
-- blanks, raising Constraint_Error if the field is all blank. It then |
| 124 |
-- checks for and skips an initial plus or minus (Constraint_Error is |
| 125 |
-- raised if plus or minus appears at the end of the string). Minus is set |
| 126 |
-- True if a minus sign was skipped, and False otherwise. On exit Ptr.all |
| 127 |
-- points to the character after the sign, or to the first non-blank |
| 128 |
-- character if no sign is present. Start is set to the point to the first |
| 129 |
-- non-blank character. |
| 130 |
-- |
| 131 |
-- Note: if Str is null, i.e. if Max is less than Ptr, then this is a |
| 132 |
-- special case of an all-blank string, and Ptr is unchanged, and hence |
| 133 |
-- is greater than Max as required in this case. Constraint_Error is also |
| 134 |
-- raised in this case. |
| 135 |
-- |
| 136 |
-- This routine must not be called with Str'Last = Positive'Last. There is |
| 137 |
-- no check for this case, the caller must ensure this condition is met. |
| 138 |
|
| 139 |
procedure Scan_Plus_Sign |
| 140 |
(Str : String; |
| 141 |
Ptr : not null access Integer; |
| 142 |
Max : Integer; |
| 143 |
Start : out Positive) |
| 144 |
with |
| 145 |
Pre => |
| 146 |
-- Ptr.all .. Max is either an empty range, or a valid range in Str |
| 147 |
(Ptr.all > Max or else (Ptr.all >= Str'First and then Max <= Str'Last)) |
| 148 |
and then not Sp.Only_Space_Ghost (Str, Ptr.all, Max) |
| 149 |
and then |
| 150 |
(declare |
| 151 |
F : constant Positive := |
| 152 |
Sp.First_Non_Space_Ghost (Str, Ptr.all, Max); |
| 153 |
begin |
| 154 |
(if Str (F) = '+' then |
| 155 |
F <= Max - 1 and then Str (F + 1) /= ' ')), |
| 156 |
Post => |
| 157 |
(declare |
| 158 |
F : constant Positive := |
| 159 |
Sp.First_Non_Space_Ghost (Str, Ptr.all'Old, Max); |
| 160 |
begin |
| 161 |
Ptr.all = (if Str (F) = '+' then F + 1 else F) |
| 162 |
and then Start = F); |
| 163 |
-- Same as Scan_Sign, but allows only plus, not minus. This is used for |
| 164 |
-- modular types. |
| 165 |
|
| 166 |
procedure Scan_Exponent |
| 167 |
(Str : String; |
| 168 |
Ptr : not null access Integer; |
| 169 |
Max : Integer; |
| 170 |
Exp : out Integer; |
| 171 |
Real : Boolean := False) |
| 172 |
with |
| 173 |
Pre => |
| 174 |
-- Ptr.all .. Max is either an empty range, or a valid range in Str |
| 175 |
(Ptr.all > Max or else (Ptr.all >= Str'First and then Max <= Str'Last)) |
| 176 |
and then Max < Natural'Last |
| 177 |
and then Sp.Is_Opt_Exponent_Format_Ghost (Str (Ptr.all .. Max), Real), |
| 178 |
Post => |
| 179 |
(if Sp.Starts_As_Exponent_Format_Ghost (Str (Ptr.all'Old .. Max), Real) |
| 180 |
then Exp = Sp.Scan_Exponent_Ghost (Str (Ptr.all'Old .. Max), Real) |
| 181 |
and then |
| 182 |
(if Str (Ptr.all'Old + 1) in '-' | '+' then |
| 183 |
Ptr.all = Sp.Last_Number_Ghost (Str (Ptr.all'Old + 2 .. Max)) + 1 |
| 184 |
else |
| 185 |
Ptr.all = Sp.Last_Number_Ghost (Str (Ptr.all'Old + 1 .. Max)) + 1) |
| 186 |
else Exp = 0 and Ptr.all = Ptr.all'Old); |
| 187 |
-- Called to scan a possible exponent. Str, Ptr, Max are as described above |
| 188 |
-- for Scan_Sign. If Ptr.all < Max and Str (Ptr.all) = 'E' or 'e', then an |
| 189 |
-- exponent is scanned out, with the exponent value returned in Exp, and |
| 190 |
-- Ptr.all updated to point past the exponent. If the exponent field is |
| 191 |
-- incorrectly formed or not present, then Ptr.all is unchanged, and the |
| 192 |
-- returned exponent value is zero. Real indicates whether a minus sign |
| 193 |
-- is permitted (True = permitted). Very large exponents are handled by |
| 194 |
-- returning a suitable large value. If the base is zero, then any value |
| 195 |
-- is allowed, and otherwise the large value will either cause underflow |
| 196 |
-- or overflow during the scaling process which is fine. |
| 197 |
-- |
| 198 |
-- This routine must not be called with Str'Last = Positive'Last. There is |
| 199 |
-- no check for this case, the caller must ensure this condition is met. |
| 200 |
|
| 201 |
procedure Scan_Trailing_Blanks (Str : String; P : Positive) |
| 202 |
with |
| 203 |
Pre => P >= Str'First |
| 204 |
and then Sp.Only_Space_Ghost (Str, P, Str'Last); |
| 205 |
-- Checks that the remainder of the field Str (P .. Str'Last) is all |
| 206 |
-- blanks. Raises Constraint_Error if a non-blank character is found. |
| 207 |
|
| 208 |
pragma Warnings |
| 209 |
(GNATprove, Off, """Ptr"" is not modified", |
| 210 |
Reason => "Ptr is actually modified when raising an exception"); |
| 211 |
procedure Scan_Underscore |
| 212 |
(Str : String; |
| 213 |
P : in out Natural; |
| 214 |
Ptr : not null access Integer; |
| 215 |
Max : Integer; |
| 216 |
Ext : Boolean) |
| 217 |
with |
| 218 |
Pre => P in Str'Range |
| 219 |
and then Str (P) = '_' |
| 220 |
and then Max in Str'Range |
| 221 |
and then P < Max |
| 222 |
and then |
| 223 |
(if Ext then |
| 224 |
Str (P + 1) in '0' .. '9' | 'A' .. 'F' | 'a' .. 'f' |
| 225 |
else |
| 226 |
Str (P + 1) in '0' .. '9'), |
| 227 |
Post => |
| 228 |
P = P'Old + 1 |
| 229 |
and then Ptr.all'Old = Ptr.all; |
| 230 |
-- Called if an underscore is encountered while scanning digits. Str (P) |
| 231 |
-- contains the underscore. Ptr is the pointer to be returned to the |
| 232 |
-- ultimate caller of the scan routine, Max is the maximum subscript in |
| 233 |
-- Str, and Ext indicates if extended digits are allowed. In the case |
| 234 |
-- where the underscore is invalid, Constraint_Error is raised with Ptr |
| 235 |
-- set appropriately, otherwise control returns with P incremented past |
| 236 |
-- the underscore. |
| 237 |
-- |
| 238 |
-- This routine must not be called with Str'Last = Positive'Last. There is |
| 239 |
-- no check for this case, the caller must ensure this condition is met. |
| 240 |
pragma Warnings (GNATprove, On, """Ptr"" is not modified"); |
| 241 |
|
| 242 |
end System.Val_Util; |