comp.lang.ada
 help / color / mirror / Atom feed
From: Laurent <lutgenl@icloud.com>
Subject: Re: Problem with Unbounded Strings
Date: Fri, 4 Dec 2015 13:21:55 -0800 (PST)
Date: 2015-12-04T13:21:55-08:00	[thread overview]
Message-ID: <274dc49b-b7cd-4eef-b333-34bae665c992@googlegroups.com> (raw)
In-Reply-To: <n3sklk$fpv$1@dont-email.me>

On Friday, 4 December 2015 19:08:11 UTC+1, Jeffrey R. Carter  wrote:
> I would ignore Kazakov's comments. While he prefers to avoid unbounded strings,
> many others don't, so if the characteristics of Unbounded_String are suitable
> for your project there is no reason not to use them.
> 
> You can't use a case statement because you're comparing strings, and case
> statements can only be used for discrete types.
> 
> On 12/04/2015 08:49 AM, Laurent wrote:
> > procedure Read_Message (File         : in Ada.Text_IO.File_Type;
> >  ...
> >       while not Ada.Text_IO.End_Of_File (Log_File) loop
> 
> What is Log_File?
> 
> >                  U_B.Append (Source   => Buffer,
> >                             New_Item => Line (5 .. Line'Last));
> 
> I see nothing wrong with this use of append. If it's possible that 5 > Line'Last
> then you would be appending a null string, which would not change the value of
> Buffer. It would be nice if you could reduce your example to eliminate the
> irrelevant parts and provide some input and corresponding output (and expected
> output) that demonstrates your problem. Otherwise I'm unable to to offer any
> advice. Certainly I've made extensive use of Unbounded_String and never
> encountered any problem with Append, so you probably have a logic problem.

Ok I have reduced the whole thing to the minimum and still get the same behaviour + an additional glitch

The Log_File I have reduced it to the minimum too:

0123456789
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ

I expect my code to do this:

1st read: 0123456789
append to the buffer which is empty so content of buffer is: 0123456789

2nd read: abcdefghijklmnopqrstuvwxyz
append to buffer so it becomes: 0123456789abcdefghijklmnopqrstuvwxyz

3rd read: ABCDEFGHIJKLMNOPQRSTUVWXYZ
append to buffer so it would be: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

But I get this:

1st read:
Line: 0123456789
Buffer after append: 0123456789

2nd read:
Line: abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz56789

3rd read:
Line: ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ56789

I first thought that I could have inverted source and new_item. But there are only 2 appends in Unbound_String. One which accepts only Unbound_Strings and one where source:Unbound_String and New_Item: String. So I can't inverse them, the compiler would complain. But the output looks a bit like inverted. So no idea.

And as a bonus after the first loop 
Ada.Text_IO.Put_Line ("Buffer after append: " & (+Buffer)); does no longer print the "Buffer after append: " part. No idea why.

So the reduced code which compiles. 

with Ada.Text_IO;
with Ada.Strings.Unbounded;

procedure Test_UB is
   
   package U_B renames Ada.Strings.Unbounded;
   subtype Ub_S is U_B.Unbounded_String;

   function "+" (Right : U_B.Unbounded_String) return String
                 renames U_B.To_String;

   function "+" (Right : String) return U_B.Unbounded_String
   is (U_B.To_Unbounded_String (Right));

   Log_File    : Ada.Text_IO.File_Type;
   Message_Buf : Ub_S;

   ------------------
   -- Read_Message --
   ------------------
   procedure Read_Message (File         : in Ada.Text_IO.File_Type;
                           Buffer       : out Ub_S)
   is
   begin -- Read_Message

      while not Ada.Text_IO.End_Of_File (File) loop
         declare
            Line     : String := Ada.Text_IO.Get_Line (File => File);

         begin -- declare

            Ada.Text_IO.Put_Line ("Line: " & Line);

            U_B.Append (Source   => Buffer,
                        New_Item => Line);

            Ada.Text_IO.Put_Line ("Buffer after append: " & (+Buffer));
            Ada.Text_IO.New_Line;
            
         end;
      end loop;
   end Read_Message;

begin -- Main

   Ada.Text_IO.Open (File => Log_File,
                     Mode => Ada.Text_IO.In_File, Name => "Test.log");

   Read_Message (File         => Log_File,
                 Buffer       => Message_Buf);

   Ada.Text_IO.Close (File => Log_File);

end Test_UB;

  reply	other threads:[~2015-12-04 21:21 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-04 15:49 Problem with Unbounded Strings Laurent
2015-12-04 16:04 ` Dmitry A. Kazakov
2015-12-04 16:53   ` Laurent
2015-12-04 18:00     ` Niklas Holsti
2015-12-04 21:05       ` Laurent
2015-12-04 20:34     ` Dmitry A. Kazakov
2015-12-04 21:43       ` Laurent
2015-12-04 22:23         ` Dmitry A. Kazakov
2015-12-05 10:22           ` Laurent
2015-12-05 12:19             ` Dmitry A. Kazakov
2015-12-05 13:15               ` Laurent
2015-12-04 18:08 ` Jeffrey R. Carter
2015-12-04 21:21   ` Laurent [this message]
2015-12-04 21:59     ` Simon Wright
2015-12-04 23:19       ` Laurent
2015-12-04 22:02     ` Dmitry A. Kazakov
2015-12-04 22:28     ` Jeffrey R. Carter
2015-12-04 23:35       ` Laurent
2015-12-04 23:59         ` Jeffrey R. Carter
2015-12-05 10:13           ` Laurent
2015-12-08  1:53           ` Randy Brukardt
2015-12-04 23:00     ` Ben Bacarisse
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox