| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . V A L U E _ I -- |
| 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 contains routines for scanning signed integer values for use |
| 33 |
-- in Text_IO.Integer_IO, and the Value attribute. |
| 34 |
|
| 35 |
pragma Assertion_Policy (Pre => Ignore, |
| 36 |
Post => Ignore, |
| 37 |
Contract_Cases => Ignore, |
| 38 |
Ghost => Ignore, |
| 39 |
Subprogram_Variant => Ignore); |
| 40 |
|
| 41 |
with System.Val_Spec; use System.Val_Spec; |
| 42 |
with System.Value_I_Spec; |
| 43 |
with System.Value_U_Spec; |
| 44 |
|
| 45 |
generic |
| 46 |
|
| 47 |
type Int is range <>; |
| 48 |
|
| 49 |
type Uns is mod <>; |
| 50 |
|
| 51 |
with procedure Scan_Raw_Unsigned |
| 52 |
(Str : String; |
| 53 |
Ptr : not null access Integer; |
| 54 |
Max : Integer; |
| 55 |
Res : out Uns); |
| 56 |
|
| 57 |
-- Additional parameters for ghost subprograms used inside contracts |
| 58 |
|
| 59 |
with package U_Spec is new System.Value_U_Spec (Uns => Uns) with Ghost; |
| 60 |
with package Spec is new System.Value_I_Spec |
| 61 |
(Int => Int, Uns => Uns, U_Spec => U_Spec) |
| 62 |
with Ghost; |
| 63 |
|
| 64 |
package System.Value_I is |
| 65 |
pragma Preelaborate; |
| 66 |
|
| 67 |
procedure Scan_Integer |
| 68 |
(Str : String; |
| 69 |
Ptr : not null access Integer; |
| 70 |
Max : Integer; |
| 71 |
Res : out Int) |
| 72 |
with |
| 73 |
Pre => Str'Last /= Positive'Last |
| 74 |
-- Ptr.all .. Max is either an empty range, or a valid range in Str |
| 75 |
and then (Ptr.all > Max |
| 76 |
or else (Ptr.all >= Str'First and then Max <= Str'Last)) |
| 77 |
and then not Only_Space_Ghost (Str, Ptr.all, Max) |
| 78 |
and then |
| 79 |
(declare |
| 80 |
Non_Blank : constant Positive := First_Non_Space_Ghost |
| 81 |
(Str, Ptr.all, Max); |
| 82 |
Fst_Num : constant Positive := |
| 83 |
(if Str (Non_Blank) in '+' | '-' then Non_Blank + 1 |
| 84 |
else Non_Blank); |
| 85 |
begin |
| 86 |
U_Spec.Is_Raw_Unsigned_Format_Ghost (Str (Fst_Num .. Max)) |
| 87 |
and then U_Spec.Raw_Unsigned_No_Overflow_Ghost |
| 88 |
(Str, Fst_Num, Max) |
| 89 |
and then Spec.Uns_Is_Valid_Int |
| 90 |
(Minus => Str (Non_Blank) = '-', |
| 91 |
Uval => U_Spec.Scan_Raw_Unsigned_Ghost |
| 92 |
(Str, Fst_Num, Max))), |
| 93 |
Post => |
| 94 |
(declare |
| 95 |
Non_Blank : constant Positive := First_Non_Space_Ghost |
| 96 |
(Str, Ptr.all'Old, Max); |
| 97 |
Fst_Num : constant Positive := |
| 98 |
(if Str (Non_Blank) in '+' | '-' then Non_Blank + 1 |
| 99 |
else Non_Blank); |
| 100 |
Uval : constant Uns := |
| 101 |
U_Spec.Scan_Raw_Unsigned_Ghost (Str, Fst_Num, Max); |
| 102 |
begin |
| 103 |
Spec.Is_Int_Of_Uns (Minus => Str (Non_Blank) = '-', |
| 104 |
Uval => Uval, |
| 105 |
Val => Res) |
| 106 |
and then Ptr.all = U_Spec.Raw_Unsigned_Last_Ghost |
| 107 |
(Str, Fst_Num, Max)); |
| 108 |
-- This procedure scans the string starting at Str (Ptr.all) for a valid |
| 109 |
-- integer according to the syntax described in (RM 3.5(43)). The substring |
| 110 |
-- scanned extends no further than Str (Max). There are three cases for the |
| 111 |
-- return: |
| 112 |
-- |
| 113 |
-- If a valid integer is found after scanning past any initial spaces, then |
| 114 |
-- Ptr.all is updated past the last character of the integer (but trailing |
| 115 |
-- spaces are not scanned out). |
| 116 |
-- |
| 117 |
-- If no valid integer is found, then Ptr.all points either to an initial |
| 118 |
-- non-digit character, or to Max + 1 if the field is all spaces and the |
| 119 |
-- exception Constraint_Error is raised. |
| 120 |
-- |
| 121 |
-- If a syntactically valid integer is scanned, but the value is out of |
| 122 |
-- range, or, in the based case, the base value is out of range or there |
| 123 |
-- is an out of range digit, then Ptr.all points past the integer, and |
| 124 |
-- Constraint_Error is raised. |
| 125 |
-- |
| 126 |
-- Note: these rules correspond to the requirements for leaving the pointer |
| 127 |
-- positioned in Text_Io.Get |
| 128 |
-- |
| 129 |
-- Note: if Str is null, i.e. if Max is less than Ptr, then this is a |
| 130 |
-- special case of an all-blank string, and Ptr is unchanged, and hence |
| 131 |
-- is greater than Max as required in this case. |
| 132 |
|
| 133 |
function Value_Integer (Str : String) return Int |
| 134 |
with |
| 135 |
Pre => not Only_Space_Ghost (Str, Str'First, Str'Last) |
| 136 |
and then Str'Length /= Positive'Last |
| 137 |
and then Spec.Is_Integer_Ghost (Spec.Slide_If_Necessary (Str)), |
| 138 |
Post => Spec.Is_Value_Integer_Ghost |
| 139 |
(Spec.Slide_If_Necessary (Str), Value_Integer'Result), |
| 140 |
Subprogram_Variant => (Decreases => Str'First); |
| 141 |
-- Used in computing X'Value (Str) where X is a signed integer type whose |
| 142 |
-- base range does not exceed the base range of Integer. Str is the string |
| 143 |
-- argument of the attribute. Constraint_Error is raised if the string is |
| 144 |
-- malformed, or if the value is out of range. |
| 145 |
|
| 146 |
end System.Value_I; |