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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,789b3fa522bdcc2a X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-04-01 10:53:59 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!peernews-us.colt.net!newsfeed.news2me.com!newsfeed2.earthlink.net!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread1.prod.itd.earthlink.net.POSTED!not-for-mail Message-ID: <3E89E035.9000103@spam.com> From: Jeffrey Carter Organization: jrcarter commercial-at acm [period | full stop] org User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.0.0) Gecko/20020530 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Optimising string handling in application References: <1049198066.356477@edh3> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Date: Tue, 01 Apr 2003 18:52:09 GMT NNTP-Posting-Host: 63.184.8.226 X-Complaints-To: abuse@earthlink.net X-Trace: newsread1.prod.itd.earthlink.net 1049223129 63.184.8.226 (Tue, 01 Apr 2003 10:52:09 PST) NNTP-Posting-Date: Tue, 01 Apr 2003 10:52:09 PST Xref: archiver1.google.com comp.lang.ada:35868 Date: 2003-04-01T18:52:09+00:00 List-Id: Frode Tenneboe wrote: > I have a small-ish procedure which only purpose is to take a > formatted string (see example) and replace the callsign(s) > inside with another. > > The strings look like: > > "ME000=F012;ME001=AAA,BBB,CCC;ME002=FOO...." > > I would like to replace "ME001=AAA,BBB,CCC" with "ME001=AAA". The > procedure below does just that. However, looking at the code, I don't > like the lumpyness (in lack of a better word). There must be a > more elegant way of doing this? Any ideas? Several. 1. Lose the Hungarian notation. > > C_Max_Msg_Size : constant Integer := 1450; > C_Max_Msg_Callsign_Length : constant Integer := 20; > > subtype T_Msg_Data_Str is String(1..C_Max_Msg_Size); > subtype T_Msg_Callsign is String(1..C_Max_Msg_Callsign_Length); > > procedure Replace_Callsign_In_Msg > (Msg : in out Dpu_Loc_Messages.T_Msg_Data_Str; > Msg_Length : in out T_Integer_32; > Callsign : in Dpu_Loc_Messages.T_Msg_Callsign; > Callsign_Length : in Positive) is 2. Pass slices and lose the _Length parameters. Then you can use attributes in here. Make this a function returning String. > > Data : Dpu_Loc_Messages.T_Msg_Data_Str; > Data_Length : Positive; > Delete_Start : Positive; > Delete_Stop : Natural; > > begin > > -- Prepare data string for new ME001 > Delete_Start := Ada.Strings.Fixed.Index > (Source => Msg(1..Msg_Length), > Pattern => "ME001=") + 6; > for I in Delete_Start .. Msg_Length loop > if Msg(I) = ';' then > Delete_Stop := I - 1; > exit; > end if; > end loop; 3. Use another call to Index to find the ';'. > > -- delete all callsigns > declare > Dummy : String := Ada.Strings.Fixed.Delete > (Source => Msg(1..Msg_Length), > From => Delete_Start, > Through => Delete_Stop); > begin > Data_Length := Dummy'Length; > Data(Data'First .. Data'First + Dummy'Length - 1) := Dummy; > end; > > -- insert new callsign > declare > Dummy : String := Ada.Strings.Fixed.Insert > (Source => Data(Data'First .. Data_Length), > Before => Delete_Start, > New_Item => Callsign(Callsign'First..Callsign'First + Callsign_Length -1)); > begin > Data_Length := Dummy'Length; > Msg(Msg'First .. Msg'First + Data_Length - 1) := Dummy; > end; 4. Replace all this with return Msg (1 .. Delete_Start - 1) & Callsign & Msg (Delete_Stop + 1 .. Msg'Last); > > end Replace_Callsign_In_Msg; If you really need the fixed-length subtype, you can grab the length of this return value and store it in your length value, then store the String into your variable: declare Var_Size : constant String := Replace_Callsign_In_Msg (Msg, Callsign); begin Length := Var_Size'Length; Msg (1 .. Var_Size'Length) := Var_Size; end; -- Jeff Carter "You couldn't catch clap in a brothel, silly English K...niggets." Monty Python & the Holy Grail