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 17:04:34 +0100
Date: 2015-12-04T17:04:34+01:00	[thread overview]
Message-ID: <17lytwwdndg6u.1o21jn6ox2sd4$.dlg@40tude.net> (raw)
In-Reply-To: e28c35a7-5f41-4beb-afb3-eb4fc9da9384@googlegroups.com

On Fri, 4 Dec 2015 07:49:28 -0800 (PST), Laurent wrote:

> I have a problem with unbounded strings. When I append a String(1..10) to
> my Buffer which is an unbounded string it works. 

You should not use Ubounded_String for I/O and parsing.
 
> procedure Read_Message (File         : in Ada.Text_IO.File_Type;
>                                            Buffer       : out Ub_S;
>                                       Has_Finished : out Boolean)

Pass a String buffer inside. Read into that buffer (Get_Line procedure, not
function). Handle buffer overrun (Last = Buffer'Last) as a protocol error. 

Don't make any dynamic allocations inside. If you have to return the
payload data, do that through the indices to the beginning and end of the
data in the buffer. Better yet to call a semantic callback with the buffer
slice.

>          begin -- declare
>             Line (1 .. Line'Last) := S (38 .. S'Last);

Don't copy anything, use indices in the raw buffer or pass a slice
downward. The only case where copying comes in question is when decoding
data.
             
>             elsif Line (1 .. 5) = ETX
>               or Line (1 .. 5) = ACK
>               or Line (1 .. 5) = ENQ
>               or Line (1 .. 5) = STX
>               or Line (1 .. 4) = GS

Always use a case statement instead of cascaded IFs. (I have no idea how
ETX could be 5 characters long...)

P.S. Ubounded_String Append certainly works OK. You just don't use it.

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


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