Solved

optimizing code

Posted on 2001-07-31
5
183 Views
Last Modified: 2010-04-06
can someone help me tell me how to optimize this code ?

(this is just a test, trying to wrap some IRC command)
:)


procedure TForm1.Button1Click(Sender: TObject);
var
Position1,Position2: Integer;
nick,command, temp : string;

begin
  nick := ':Berzerk1!~Berzerk1@ti13a80-0306.bb.online.no JOIN :#skien';
  temp := nick;
  command := temp;
  delete(temp,1,lastdelimiter(':',temp));
  delete(command,1,pos(' ',command));
  delete(command,pos(' ',command),length(command));
  Position1 := Pos(':',nick) + 1;
  Position2 := Pos('!',nick);
  nick := Copy(nick,Position1, Position2 - Position1);
  ShowMessage(nick + ':' + temp + '+' + command);
end;
0
Comment
Question by:joepezt
  • 3
5 Comments
 
LVL 6

Accepted Solution

by:
MannSoft earned 50 total points
ID: 6338915
Here is some shorter code:

S := 'Berzerk1!~Berzerk1@ti13a80-0306.bb.online.no JOIN :#skien'
Nick := Copy(S, 1, Pos('!', S) - 1);
Delete(S, Pos(' ', S);
Command := Copy(S, 1, Pos(' ', S) - 1);
Delete(S, Pos(' ', S + 1);
Channel := S;

Of course that will only work specifically for the JOIN command.  I wrote an IRC program awhile ago.  Here is how I handled server messages (just ignore if you are not interested in writing a client of your own :-) )

1) I called a function that split the string into a TStringList (split at each space of course).
2) I checked element 1 to see what the command/numeric was
3) I called another function to combine the last X elements of the stringlist.  The value of X depended on which command/numeric it was.  The leading : was also stripped by this function
4) If required, I called yet another function with element 0 as a paramter, and it returned everything up to the ! (to get the nick of the sender)

By using functions for these things it helps seperate your code and (IMO) makes things much easier to work with.
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6339092
Umm...

procedure Test(InputString: string);
var
  StringList: TStringList;
  Nick, Command, Channel: string;
begin
  InputString := StringReplace(InputString,
    ' ', #13, [rfReplaceAll]);
  InputString := StringReplace(InputString,
    '!', #13, [rfReplaceAll]);
  StringList := TStringList.Create;
  try
    StringList.Text := InputString;
    Nick := StringList.Strings[0];
    Command := StringList.Strings[2];
    Channel := StringList.Strings[3];
  finally
    StringList.Free;
  end;
end;


:)
0
 
LVL 2

Author Comment

by:joepezt
ID: 6340064
will test some of these :)

i have an open source IRC component i was starting on a while ago, http://www.berzerk.net


but some of you might know more about this thing ?

http://www.delphi3d.net/articles/viewarticle.php?article=console.htm



0
 
LVL 2

Author Comment

by:joepezt
ID: 6340261
will test some of these :)

i have an open source IRC component i was starting on a while ago, http://www.berzerk.net


but some of you might know more about this thing ?

http://www.delphi3d.net/articles/viewarticle.php?article=console.htm



0
 
LVL 2

Author Comment

by:joepezt
ID: 6375139
nice ..
well, if you are interrested, you CAN if you want to check out the component i made..

http://vortex.berzerk.net
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
delphi prevent click fast 2 212
Delphi Yen format 3 31
how to resize animated Gif image in delphi ? 1 28
scroll down TListBox component in Delphi 1 6
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now