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 21:34:50 +0100 Organization: cbb software GmbH Message-ID: <19i08yxoly6r5.lk9snum67ier.dlg@40tude.net> References: <17lytwwdndg6u.1o21jn6ox2sd4$.dlg@40tude.net> <2c489db5-0d14-4a1e-9883-cf018c3a0b83@googlegroups.com> 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:28641 Date: 2015-12-04T21:34:50+01:00 List-Id: 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 . To lazy to rewrite the <>. So ETX : String > constant := "";... 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