SRL Archive - Research & Development Lounge

Function ReplaceRegex2 and Debug


mixster

2008-01-25 05:23:48

I decided to make a quick and easy general debugging function that (at the moment) returns the begins, end, repeats and untils in a script as an integer in an array (with begin 0, end 1, repeat 2 and until 3). Also, it includes ReplaceRegex2, which, as far as I know, works in the same way replace regex, except it returns how many times it replaced something (in the variable count).

ReplaceRegex2:

function ReplaceRegex2(Txt,Regex,ReplaceStr: string; var count: integer): string;
var
RPos,RLen: integer;

begin
repeat
RLen:= Length(Regex);
RPos:= RegexPos(Txt,Regex);
if(RPos = -1) then
Break;
Delete(Txt,Rpos,RLen-1);
Insert(ReplaceStr,Txt,RPos);
Inc(Count);
until(count >= 1000);
Result:= Txt;
end;

Debug:

function Debug(fileloc: string; var output: array of integer):Boolean;
var
iFile: Integer;
sFile: String;

begin
SetArrayLength(output,4);
iFile:= OpenFile(fileloc,false);
ReadFileString(iFile,sFile,filesize(iFile));
CloseFile(iFile);
sFile:= Lowercase(sFile);
ReplaceRegex2(sFile,'begin','<egi>',Output[0]);
ReplaceRegex2(sFile,'end','<n>',Output[1]);
ReplaceRegex2(sFile,'repeat','<epea>',Output[2]);
ReplaceRegex2(sFile,'until','<nti>',Output[3]);
end;

You would use replaceregex2 the exact same way, excpet you put an integer to store the replaces in.
E.g.
Writeln(ReplaceRegex2('Moo Quack Cluck','Moo','Quack',Moos)+' and '+IntToStr(Moos)+' moo/s');
would give you: 'Quack Quack Cluck and 1 moo/s' in the debug log.

Debug would be used as:
Debug(ScriptPath+'newscript.scar',debugArray);
Writeln('You have '+IntToStr(debugArray[0])+' begin/s');
or
Writeln('You have '+IntToStr(debugArray[1]-debugArray[0])+' more end/s than begins';

That should explain the general idea of them. If you have any question, feel free to ask.

BobboHobbo

2008-01-25 06:39:46

Interesting..

Negaal

2008-01-25 12:25:19

Mhhh…

Hmm…

Nice dude, I'd love to see you in members…

Your functions seems nice, but it's useful only peoples who can't script, or forgot keywords all the time:p

By the way, standards, repeat and until is same as begin & end

Where did you find out those commands?

mixster

2008-01-25 16:10:05

:o Put together ReplaceRegex2 for Debug, so only added it because I thought someone might find it useful (will fix it after this).
I'll hopefully be entering a script soon, only problem is I don't know what to do. I was going to make a powercutter, but there's a lot of them already and I want to make an 'original' script.
Also, what commands are you talking about? OpenFile etc. I learned from a tutorial and found the RegexPos etc. in the scar help when checking if there was already a Regex counter.