-- * If Drop=Error, then Strings.Length_Error is propagated.
function To_String (Source : Bounded_String) return String with
+ Post =>
+ To_String'Result'First = 1
+ and then To_String'Result'Length = Length (Source),
Global => null;
-- To_String returns the String value with lower bound 1
-- represented by Source. If B is a Bounded_String, then
return Result_Type (Source);
end;
- elsif From not in Source'Range
- or else Through > Source'Last
- then
- pragma Annotate
- (CodePeer, False_Positive,
- "test always false", "self fullfilling prophecy");
-
- -- In most cases this raises an exception, but the case of deleting
- -- a null string at the end of the current one is a special-case, and
- -- reflects the equivalence with Replace_String (RM A.4.3 (86/3)).
-
- if From = Source'Last + 1 and then From = Through then
- return Source;
- else
- raise Index_Error;
- end if;
-
else
declare
- Front : constant Integer := From - Source'First;
+ Front_Len : constant Integer :=
+ Integer'Max (0, From - Source'First);
+ -- Length of prefix of Source copied to result
+ Back_Len : constant Integer :=
+ Integer'Max (0, Source'Last - Through);
+ -- Length of suffix of Source copied to result
+
+ Result_Length : constant Integer := Front_Len + Back_Len;
+ -- Length of result
begin
- return Result : String (1 .. Source'Length - (Through - From + 1))
+ return Result : String (1 .. Result_Length)
with Relaxed_Initialization
do
- Result (1 .. Front) :=
+ Result (1 .. Front_Len) :=
Source (Source'First .. From - 1);
if Through < Source'Last then
- Result (Front + 1 .. Result'Last) :=
+ Result (Front_Len + 1 .. Result'Last) :=
Source (Through + 1 .. Source'Last);
end if;
end return;
From : Positive;
Through : Natural) return String
with
- Pre => (if From <= Through
- then (From in Source'Range
- and then Through <= Source'Last)),
+ Pre => From > Through
+ or else (From - 1 <= Source'Last
+ and then Through >= Source'First - 1),
-- Lower bound of the returned string is 1
-- Length of the returned string
- Delete'Result'Length = Source'Length - (Through - From + 1)
+ Delete'Result'Length =
+ Integer'Max (0, From - Source'First)
+ + Integer'Max (Source'Last - Through, 0)
-- Elements before From are preserved
and then
- Delete'Result (1 .. From - Source'First)
+ Delete'Result (1 .. Integer'Max (0, From - Source'First))
= Source (Source'First .. From - 1)
-- If there are remaining characters after Through, they are
and then
(if Through < Source'Last
- then Delete'Result
- (From - Source'First + 1 .. Delete'Result'Last)
- = Source (Through + 1 .. Source'Last)),
+ then
+ Delete'Result
+ (Integer'Max (0, From - Source'First) + 1
+ .. Delete'Result'Last)
+ = Source (Through + 1 .. Source'Last)),
-- Otherwise, the returned string is Source with lower bound 1