[Ada] Ada.Strings.Unbounded.Aux.Set_String

Message ID 20210503092945.GA77647@adacore.com
State New
Headers show
Series
  • [Ada] Ada.Strings.Unbounded.Aux.Set_String
Related show

Commit Message

Pierre-Marie de Rodat May 3, 2021, 9:29 a.m.
The current version of Set_String is no longer used and not very useful
and even unsafe with the introduction of a-stunau__shared.adb.

Replace it with a more useful and safer version taking a callback to set
the string.

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

gcc/ada/

	* libgnat/a-stunau.ads, libgnat/a-stunau.adb,
	libgnat/a-stunau__shared.adb (Set_String): Remove old version,
	replace by a new version taking a callback to set the string.

Patch

diff --git a/gcc/ada/libgnat/a-stunau.adb b/gcc/ada/libgnat/a-stunau.adb
--- a/gcc/ada/libgnat/a-stunau.adb
+++ b/gcc/ada/libgnat/a-stunau.adb
@@ -52,11 +52,17 @@  package body Ada.Strings.Unbounded.Aux is
    -- Set_String --
    ----------------
 
-   procedure Set_String (UP : in out Unbounded_String; S : String_Access) is
+   procedure Set_String
+     (U      : out Unbounded_String;
+      Length : Positive;
+      Set    : not null access procedure (S : out String))
+   is
+      Old : String_Access := U.Reference;
    begin
-      Finalize (UP);
-      UP.Reference := S;
-      UP.Last := UP.Reference'Length;
+      U.Last := Length;
+      U.Reference := new String (1 .. Length);
+      Set (U.Reference.all);
+      Free (Old);
    end Set_String;
 
 end Ada.Strings.Unbounded.Aux;


diff --git a/gcc/ada/libgnat/a-stunau.ads b/gcc/ada/libgnat/a-stunau.ads
--- a/gcc/ada/libgnat/a-stunau.ads
+++ b/gcc/ada/libgnat/a-stunau.ads
@@ -56,22 +56,24 @@  package Ada.Strings.Unbounded.Aux is
       S : out Big_String_Access;
       L : out Natural);
    pragma Inline (Get_String);
-   --  This procedure returns the internal string pointer used in the
-   --  representation of an unbounded string as well as the actual current
-   --  length (which may be less than S.all'Length because in general there
-   --  can be extra space assigned). The characters of this string may be
-   --  not be modified via the returned pointer,  and are valid only as
-   --  long as the original unbounded string is not accessed or modified.
+   --  Return the internal string pointer used in the representation of an
+   --  unbounded string as well as the actual current length (which may be less
+   --  than S.all'Length because in general there can be extra space assigned).
+   --  The characters of this string may be not be modified via the returned
+   --  pointer, and are valid only as long as the original unbounded string is
+   --  not accessed or modified.
    --
    --  This procedure is much more efficient than the use of To_String
    --  since it avoids the need to copy the string. The lower bound of the
    --  referenced string returned by this call is always one, so the actual
    --  string data is always accessible as S (1 .. L).
 
-   procedure Set_String (UP : in out Unbounded_String; S : String_Access);
+   procedure Set_String
+     (U      : out Unbounded_String;
+      Length : Positive;
+      Set    : not null access procedure (S : out String));
    pragma Inline (Set_String);
-   --  This version of Set_Unbounded_String takes a string access value, rather
-   --  than a string. The lower bound of the string value is required to be
-   --  one, and this requirement is not checked.
+   --  Create an unbounded string U with the given Length, using Set to fill
+   --  the contents of U.
 
 end Ada.Strings.Unbounded.Aux;


diff --git a/gcc/ada/libgnat/a-stunau__shared.adb b/gcc/ada/libgnat/a-stunau__shared.adb
--- a/gcc/ada/libgnat/a-stunau__shared.adb
+++ b/gcc/ada/libgnat/a-stunau__shared.adb
@@ -51,12 +51,30 @@  package body Ada.Strings.Unbounded.Aux is
    -- Set_String --
    ----------------
 
-   procedure Set_String (UP : in out Unbounded_String; S : String_Access) is
-      X : String_Access := S;
-
+   procedure Set_String
+     (U      : out Unbounded_String;
+      Length : Positive;
+      Set    : not null access procedure (S : out String))
+   is
+      TR : constant Shared_String_Access := U.Reference;
+      DR : Shared_String_Access;
    begin
-      Set_Unbounded_String (UP, S.all);
-      Free (X);
+      --  Try to reuse existing shared string
+
+      if Can_Be_Reused (TR, Length) then
+         Reference (TR);
+         DR := TR;
+
+      --  Otherwise allocate new shared string
+
+      else
+         DR := Allocate (Length);
+         U.Reference := DR;
+      end if;
+
+      Set (DR.Data (1 .. Length));
+      DR.Last := Length;
+      Unreference (TR);
    end Set_String;
 
 end Ada.Strings.Unbounded.Aux;