comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam@spam.com>
Subject: Re: Optimising string handling in application
Date: Tue, 01 Apr 2003 18:52:09 GMT
Date: 2003-04-01T18:52:09+00:00	[thread overview]
Message-ID: <3E89E035.9000103@spam.com> (raw)
In-Reply-To: 1049198066.356477@edh3

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




  reply	other threads:[~2003-04-01 18:52 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-04-01 11:54 Optimising string handling in application Frode Tenneboe
2003-04-01 18:52 ` Jeffrey Carter [this message]
2003-04-02  3:17 ` Steve
2003-04-02  4:10 ` tmoran
2003-04-02  7:15   ` Frode Tenneboe
2003-04-02 18:23     ` tmoran
2003-04-04 10:13       ` Frode Tenneboe
2003-04-04 13:26         ` Preben Randhol
2003-04-04 14:42           ` Frode Tenneboe
2003-04-04 21:48             ` Warren W. Gay VE3WWG
2003-04-05  7:24               ` Pascal Obry
2003-04-04 14:08         ` Frank J. Lhota
2003-04-04 15:26           ` Robert Spooner
2003-04-04 21:49             ` Warren W. Gay VE3WWG
2003-04-04 17:43         ` tmoran
2003-04-04 21:42           ` Frode Tennebø
2003-04-02 19:03     ` tmoran
2003-04-04 10:44       ` Frode Tenneboe
2003-04-05  3:28         ` tmoran
2003-04-05  0:08     ` Jeffrey Carter
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox