comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Problem with Unbounded Strings
Date: Fri, 4 Dec 2015 21:34:50 +0100
Date: 2015-12-04T21:34:50+01:00	[thread overview]
Message-ID: <19i08yxoly6r5.lk9snum67ier.dlg@40tude.net> (raw)
In-Reply-To: 2c489db5-0d14-4a1e-9883-cf018c3a0b83@googlegroups.com

On Fri, 4 Dec 2015 08:53:54 -0800 (PST), in comp.lang.ada you wrote:

> I don't use append?

No. There is nothing that is not in the buffer read already. Why would you
append anything?

In some protocols payload data are accumulated from several packets.
Unbounded_String is not used for that either. You want to limit the size of
the accumulated data to prevent DoS attacks unless the protocol limits that
already, as most protocols do. Simply keep the buffer in the communication
object, that is all.

> What is this then doing? Where U_B = ada.strings.unbound...
> 
>                  U_B.Append (Source   => Buffer, 
>                             New_Item => Line (5 .. Line'Last)); 
> 
> ETX is as the other commands 5 chars long because in the txt file I read
> from, it is  used as <ETX> . To lazy to rewrite the <>. So ETX : String
> constant := "<ETX>";...

In such cases I am usually match the current string prefix against a table
of tokens (table-driven recursive descent).

But if you want an explicit form it is like this (a recursive descent
parser):

   case Buffer (Index) is -- Index is the current position
      when '<' =>
         Index := Index + 1;
         case Buffer (Index) is
            when 'A' =>
               Index := Index + 1;
               ...
            when 'E' =>
               Index := Index + 1;
               ...
            when 'G' =>
               Index := Index + 1;
               ...
            when 'R' =>
               Index := Index + 1;
               ...
            when 'S' =>
               Index := Index + 1;
               ...
            when others =>
               raise Protocol_Error with
                     "ACK, ETX, ENQ, STX or GS is expected at" &
                     Integer'Image (Index);
      when others =>
         raise Protocol_Error with
                "< is expected at" & Integer'Image (Index);
   end case;

The point is to visit each source character just once and prevent excessive
copying. That is also the reason why you practically never need or should
not use Unbounded_String for I/O. It won't make your life easier anyway.
You would have to convert it to String anytime you would use its contents
as your code demonstrates. The code will be cleaner, safer and many times
faster.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

  parent reply	other threads:[~2015-12-04 20:34 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 [this message]
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
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