From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Problem with Unbounded Strings Date: Fri, 4 Dec 2015 17:04:34 +0100 Organization: cbb software GmbH Message-ID: <17lytwwdndg6u.1o21jn6ox2sd4$.dlg@40tude.net> References: Reply-To: mailbox@dmitry-kazakov.de NNTP-Posting-Host: Sfz0eNwKWh4Uq03iti+GMw.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: 40tude_Dialog/2.0.15.1 X-Notice: Filtered by postfilter v. 0.8.2 Xref: news.eternal-september.org comp.lang.ada:28632 Date: 2015-12-04T17:04:34+01:00 List-Id: 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