public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Pierre-Marie de Rodat <derodat@adacore.com>
To: gcc-patches@gcc.gnu.org
Cc: Ed Schonberg <schonberg@adacore.com>
Subject: [Ada] Semantics of Delete for fixed strings
Date: Mon, 08 Jul 2019 08:19:00 -0000	[thread overview]
Message-ID: <20190708081857.GA80632@adacore.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 741 bytes --]

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.

Tested on x86_64-pc-linux-gnu, committed on trunk

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.

[-- Attachment #2: patch.diff --]
[-- Type: text/x-diff, Size: 1173 bytes --]

--- gcc/ada/libgnat/a-strfix.adb
+++ gcc/ada/libgnat/a-strfix.adb
@@ -192,7 +192,15 @@ package body Ada.Strings.Fixed is
       elsif From not in Source'Range
         or else Through > Source'Last
       then
-         raise Index_Error;
+         --  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

--- /dev/null
new file mode 100644
+++ gcc/testsuite/gnat.dg/fixed_delete.adb
@@ -0,0 +1,17 @@
+--  { 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;


                 reply	other threads:[~2019-07-08  8:19 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190708081857.GA80632@adacore.com \
    --to=derodat@adacore.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=schonberg@adacore.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).