Commit 4962dc44 by Ed Schonberg Committed by Pierre-Marie de Rodat

[Ada] Semantics of Delete for fixed strings

This patch corrects a bug in the implementation of Delete in an unusual
boundary case: the RM describes the semantics of Delete as equivalent to
that of Replace_String with a null argument. As a result, deleting a
null string that starts past the end of its argument is a noop and must
not raise Index_Error.

2019-07-08  Ed Schonberg  <schonberg@adacore.com>

gcc/ada/

	* libgnat/a-strfix.adb (Delete): The RM describes the semantics
	of Delete as equivalent to that of Replace_String with a null
	argument. As a result, deleting a null string that starts past
	the end of its argument is a noop and must not raise
	Index_Error.

gcc/testsuite/

	* gnat.dg/fixed_delete.adb: New testcase.

From-SVN: r273205
parent 4a0e6ac1
2019-07-08 Ed Schonberg <schonberg@adacore.com>
* libgnat/a-strfix.adb (Delete): The RM describes the semantics
of Delete as equivalent to that of Replace_String with a null
argument. As a result, deleting a null string that starts past
the end of its argument is a noop and must not raise
Index_Error.
2019-07-08 Javier Miranda <miranda@adacore.com> 2019-07-08 Javier Miranda <miranda@adacore.com>
* exp_disp.adb (Register_Primitive): When registering a * exp_disp.adb (Register_Primitive): When registering a
......
...@@ -192,7 +192,15 @@ package body Ada.Strings.Fixed is ...@@ -192,7 +192,15 @@ package body Ada.Strings.Fixed is
elsif From not in Source'Range elsif From not in Source'Range
or else Through > Source'Last or else Through > Source'Last
then then
-- 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; raise Index_Error;
end if;
else else
declare declare
......
2019-07-08 Ed Schonberg <schonberg@adacore.com>
* gnat.dg/fixed_delete.adb: New testcase.
2019-07-08 Javier Miranda <miranda@adacore.com> 2019-07-08 Javier Miranda <miranda@adacore.com>
* gnat.dg/interface9.adb, gnat.dg/interface9_root-child.ads, * gnat.dg/interface9.adb, gnat.dg/interface9_root-child.ads,
......
-- { dg-do run }
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
procedure Fixed_Delete is
Str : String := "a";
Str1 : String := Replace_Slice (Str, 2, 2, "");
Str2 : String := Delete (Str, 2, 2);
begin
if Str1 /= "a" then
raise Program_Error;
end if;
if Str2 /= "a" then
raise Program_Error;
end if;
end Fixed_Delete;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment