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=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI autolearn=unavailable autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,fff7eeabc7a7bc9c X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-08-22 09:24:29 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!newsfeeds.belnet.be!news.belnet.be!opentransit.net!jussieu.fr!enst!enst.fr!not-for-mail From: "David C. Hoos" Newsgroups: comp.lang.ada Subject: Re: String manupulation Date: Wed, 22 Aug 2001 11:23:36 -0500 Organization: ENST, France Sender: comp.lang.ada-admin@ada.eu.org Message-ID: References: <9lt2pe$lrm$1@snipp.uninett.no> <9lu7r2$3vp$1@news.online-isp.com> <9lvq0p$cpm$1@snipp.uninett.no> <0AQg7.10582$2u.75569@www.newsranger.com> Reply-To: comp.lang.ada@ada.eu.org NNTP-Posting-Host: marvin.enst.fr Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Trace: avanie.enst.fr 998497436 36399 137.194.161.2 (22 Aug 2001 16:23:56 GMT) X-Complaints-To: usenet@enst.fr NNTP-Posting-Date: Wed, 22 Aug 2001 16:23:56 +0000 (UTC) Cc: "Ted Dennison" , To: Return-Path: X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 Errors-To: comp.lang.ada-admin@ada.eu.org X-BeenThere: comp.lang.ada@ada.eu.org X-Mailman-Version: 2.0.4 Precedence: bulk List-Help: List-Post: List-Subscribe: , List-Id: comp.lang.ada mail<->news gateway List-Unsubscribe: , List-Archive: Errors-To: comp.lang.ada-admin@ada.eu.org X-BeenThere: comp.lang.ada@ada.eu.org Xref: archiver1.google.com comp.lang.ada:12251 Date: 2001-08-22T11:23:36-05:00 Some additional style comments: 1. One might wish to extend the "Space" object to be "Whitespace" with the appropriate set of characters, in case one is reading a file with tabs or other "whitespace" characters. 2. The use of a construct like FS'Length would better be replaced with FS'Last to cover cases like the string being parsed is an input parameter to the subprogram, and the caller calls with a slice not starting with index 1. ----- Original Message ----- From: "Ted Dennison" Newsgroups: comp.lang.ada To: Sent: Wednesday, August 22, 2001 10:53 AM Subject: Re: String manupulation > In article <9lvq0p$cpm$1@snipp.uninett.no>, Reinert Korsnes says... > > > >Thanks to all of you. Is this the (dirty ?) way to do it (?) : > > Some style comments: > > o Anything that never changes after the declaration should be declared > "constant". > > o There's already an instantiation of Integer_IO for Integer. Its > Ada.Integer_Text_IO. > > o Unless it won't work for some reason, I generally prefer to use 'Image rather > than a numeric Text_IO instantiation. > > o You don't need to "with" a package if you already "with" one of its children. > > o Except in extreme quick-n-dirty code, I always specify the parameter names in > multi-parameter subprogram calls. > > o (Contraversial) I prefer to aviod the "use" clause. > > o There's no indication where the "1" you initialize those variables to comes > from or why its there. Instead, only initialize the one(s) you need to, and do > it from the array it is using to index through. > > Also, while transforming that appropriately, I noticed that you have a bug where > it doesn't handle a final substring of length 1 properly. That's the kind of > thing you are more apt to notice the more closely you tie your indices to your > array. > > Also, Find_Token returns 0 for Last when no more spaces are found. If you supply > a string that doesn't end in a space, your code will loop endlessly. The > *proper* way to terminate your "Find_Token" based loop is to terminate when no > further token is found (Last = 0). > > Given all that, I'd transform it to: > ------------------------ > with Ada.Text_IO; > with Ada.Strings.Fixed; > with Ada.Strings.Maps; > > procedure Rtest1 is > FS : constant String := " This is a test to split a string "; > C1 : constant String := "123456789012345678901234"; > Space : constant Ada.Strings.Maps.Character_Set := Ada.Strings.Maps.To_Set(" "); > > First : Natural := Fs'First; > Last : Natural; > begin > Ada.Text_IO.Put_Line (C1); > Ada.Text_IO.Put_Line (FS); > > loop > Ada.Strings.Fixed.Find_Token > (Source => FS(First..FS'Length), > Set => Space, > Test => Ada.Strings.Outside, > First => First, > Last => Last > ); > > exit when Last = 0; > > Ada.Text_IO.Put_Line > (Integer'Image(First) & Integer'Image(Last) & " " & Fs (First..Last)); > > First := Last + 1; > end loop; > end Rtest1; > ------------- > > To satify those who don't agree with the "no use" bit, I'll also include a > version with "use"s. Obviously *I* don't think it looks better, but some here > surely will: > > ------------- > with Ada.Text_IO; > with Ada.Strings.Fixed; > with Ada.Strings.Maps; > > use Ada.Text_IO; > use Ada.Strings.Fixed; > use Ada.Strings.Maps; > > procedure Rtest1 is > FS : constant String := " This is a test to split a string "; > C1 : constant String := "123456789012345678901234"; > Space : constant Character_Set := To_Set(" "); > > First : Natural := Fs'First; > Last : Natural; > begin > Put_Line (C1); > Put_Line (FS); > > loop > Find_Token > (Source => FS(First..FS'Length), > Set => Space, > Test => Ada.Strings.Outside, > First => First, > Last => Last > ); > > exit when Last = 0; > > Put_Line > (Integer'Image(First) & Integer'Image(Last) & " " & Fs (First..Last)); > > First := Last + 1; > end loop; > end Rtest1; > ------------- > > --- > T.E.D. homepage - http://www.telepath.com/dennison/Ted/TED.html > home email - mailto:dennison@telepath.com > _______________________________________________ > comp.lang.ada mailing list > comp.lang.ada@ada.eu.org > http://ada.eu.org/mailman/listinfo/comp.lang.ada >