Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [SIZE="5"][COLOR="red"]String Handling Commands[/COLOR][/SIZE][SIZE="0"] | [url=http://villavu.com/forum/showthread.php?t=104687]Point Commands[/url] | [url=http://villavu.com/forum/showthread.php?t=104649]Box Commands[/url] | [url=http://villavu.com/forum/showthread.php?t=104689]Integer Commands[/url][/SIZE]
- Here is a little contribution from me to the SRL community.
- After all these years, that I have been here, it is finally time to give something back too (I am very proud member of this great community, even though I haven't been all that active)! :)
- Sorry for not commenting any of my code - I still haven't learned to comment my code.
- ..and I doubt I ever will, sadly. :( Just not my strongest field.
- I added small examples for commands, just to show how they work (due to missing code comments), although most of the functions/procedures are pretty much self-explanatory?.
- [b]List of [color=blue]Functions[/color] & [color=blue]Procedures[/color]:[/b]
- [code][u]### / Date Added / Command Name[/u]
- 000 / 07-04-2013 / Position[Ex]()
- 001 / 06-25-2013 / StrContains()
- 002 / 06-25-2013 / StrContainsEx()
- 003 / 06-13-2013 / Left()
- 004 / 06-13-2013 / Right()
- 005 / 10-06-2012 / CopyEx()
- 006 / 04-29-2013 / BetweenEx()
- 007 / 08-28-2012 / Reverse()
- 008 / 05-15-2012 / StartsWith()
- 009 / 05-15-2012 / EndsWith()
- 010 / 04-29-2013 / StartWith()
- 011 / 04-29-2013 / EndWith()
- 012 / 05-15-2012 / TrimStart()
- 013 / 05-15-2012 / TrimEnd()
- 014 / 05-26-2013 / Behind()
- 015 / 05-26-2013 / Ahead()
- 016 / 05-13-2012 / After()
- 017 / 05-13-2012 / Before()
- 018 / 05-13-2012 / AfterEx()
- 019 / 05-13-2012 / BeforeEx()
- 020 / 05-13-2012 / LastAfter()
- 021 / 05-13-2012 / LastBefore()
- 022 / 06-25-2013 / Center()
- 023 / 06-25-2013 / CenterEx()
- 024 / 06-06-2013 / PosMultiID()
- 025 / 06-06-2013 / PosMultiIDEx()
- 026 / 06-06-2013 / PosMulti()
- 027 / 06-06-2013 / PosMultiEx()
- 028 / 05-13-2012 / LastPos()
- 029 / 08-07-2012 / LastPosEx()
- 030 / 08-13-2012 / ClosestPos()
- 031 / 08-13-2012 / FarthestPos()
- 032 / 08-29-2012 / Count()
- 033 / 05-22-2012 / CountEx()
- 034 / 05-26-2013 / CountMulti()
- 035 / 05-14-2012 / Stuff()
- 036 / 08-19-2013 / StuffEx()
- 037 / 06-15-2013 / WrapTextEx()
- 038 / 06-15-2013 / WrapText()
- 039 / 08-26-2012 / ReplaceEx()
- 040 / 05-14-2012 / Compare()
- 041 / 05-14-2012 / Compare2()
- 042 / 08-13-2012 / Expand()
- 043 / 04-18-2013 / ExpandEx()
- 044 / 04-29-2013 / StringOfString()
- 045 / 04-29-2013 / StringOfStringEx()
- 046 / 07-03-2013 / Line()
- 047 / 07-03-2013 / Lines()
- 048 / 07-03-2013 / LinePositions()
- 049 / 07-03-2013 / LineCount()
- 050 / 06-27-2013 / LineByPos()
- 051 / 06-27-2013 / PosByLine()
- 052 / 06-11-2013 / Indentation()
- 053 / 06-02-2013 / SplitEx()
- 054 / 06-02-2013 / Split()
- 055 / 06-13-2013 / Quotatation()
- 056 / 05-30-2013 / PregQuote()
- 057 / 05-30-2013 / PregQuoteEx()
- 058 / 06-04-2013 / Find() [**]
- 059 / 06-08-2013 / FindEx() [*****]
- 060 / 08-10-2012 / StringToHex() (by CynicRus)
- 061 / 08-10-2012 / HexToString() (by CynicRus)
- 062 / 08-10-2012 / WebCode() (by CynicRus)
- 063 / 08-10-2012 / WebDecode (by CynicRus)
- 064 / 05-01-2013 / ExplodeMulti()
- 065 / 05-13-2012 / PosAll()
- 066 / 04-29-2013 / PosAllEx()
- 067 / 05-26-2013 / PosAllMulti()
- 068 / 06-21-2013 / TraceStrsEx() [***]
- 069 / 06-21-2013 / TraceStrs() [***]
- 070 / 05-30-2013 / TraceStrPositions() [*]
- 071 / 05-30-2013 / TraceStrPositionsEx() [*]
- 072 / 05-13-2012 / AllBetween()
- 073 / 05-14-2012 / MultiBetween()
- 074 / 05-14-2012 / MultiBetweenEx()
- 075 / 07-16-2013 / GetWords()
- 076 / 07-16-2013 / GetWordsEx()
- 077 / 07-16-2013 / CountWords()
- 078 / 07-16-2013 / CountWordsEx()
- 079 / 04-28-2013 / TSAFill()
- 080 / 04-28-2013 / TSAFillEx()
- 081 / 04-29-2013 / TSAOfString()
- 082 / 04-29-2013 / TSAOfStringEx()
- 083 / 04-21-2013 / TSAClone()
- 084 / 07-14-2012 / TSACopy()
- 085 / 04-21-2013 / TSACopyEx()
- 086 / 04-28-2013 / TSAGet()
- 087 / 04-18-2013 / TSAAdd()
- 088 / 09-05-2012 / TSAReverse()
- 089 / 04-18-2013 / TSACombine()
- 090 / 08-13-2012 / TSAConcat()
- 091 / 04-28-2013 / TSAConcatEx()
- 092 / 06-10-2013 / TSATrimEx()
- 093 / 06-10-2013 / TSATrim()
- 094 / 05-14-2012 / TSAContains()
- 095 / 07-17-2013 / TSAContainsEx()
- 096 / 07-17-2013 / TSAContainsTSA()
- 097 / 05-14-2012 / TSADelete()
- 098 / 09-05-2012 / TSARemove()
- 099 / 05-14-2012 / TSAAppend()
- 100 / 05-14-2012 / TSAInsert()
- 101 / 04-19-2013 / TSAPlant()
- 102 / 04-28-2013 / TSAMove()
- 103 / 04-28-2013 / TSASame()
- 104 / 05-14-2012 / TSAUnique()
- 105 / 05-15-2012 / TSAAllUnique()
- 106 / 05-15-2012 / TSAAllSame()
- 107 / 04-28-2013 / TSAPos()
- 108 / 05-13-2012 / TSAPosAll()
- 109 / 07-17-2013 / TSAExtractStrings()
- 110 / 07-17-2013 / TSAFilterStrings()
- 111 / 10-06-2012 / TSAToParts() [*]
- 112 / 05-14-2012 / TSAToATSAByName()
- 113 / 05-14-2012 / TSAToATSAByLength()
- 114 / 05-15-2012 / ATSAMerge()
- [i]* = Amount of custom types required.[/i][/code]
- [spoiler="Position[Ex]"]
- [simba]function Position(subStr, s: string): Integer;
- var
- x, y, z: Integer;
- begin
- z := Length(s);
- y := Length(subStr);
- if ((z > 0) and (y > 0) and (y <= z)) then
- for Result := 1 to ((z - y) + 1) do
- begin
- for x := 1 to y do
- if (s[((Result + x) - 1)] <> subStr[x]) then
- Break;
- if (x > y) then
- Exit;
- end;
- Result := 0;
- end;
- function Position2(subStr, s: string): Integer;
- var
- a, b: Integer;
- begin
- a := Length(s);
- b := Length(subStr);
- if ((a > 0) and (b > 0) and (b <= a)) then
- for Result := 1 to ((a - b) + 1) do
- if (subStr = Copy(s, Result, b)) then
- Exit;
- Result := 0;
- end;
- function PositionEx(subStr, s: string; offset: Integer): Integer;
- var
- x, y, z: Integer;
- begin
- z := Length(s);
- y := Length(subStr);
- if ((z > 0) and (y > 0) and (y <= z) and (offset > 0) and (offset <= ((z - y) + 1))) then
- for Result := offset to ((z - y) + 1) do
- begin
- for x := 1 to y do
- if (s[((Result + x) - 1)] <> subStr[x]) then
- Break;
- if (x > y) then
- Exit;
- end;
- Result := 0;
- end;
- function PositionEx2(subStr, s: string; offset: Integer): Integer;
- var
- a, b: Integer;
- begin
- a := Length(s);
- b := Length(subStr);
- if ((a > 0) and (b > 0) and (b <= a) and (offset > 0) and (offset <= ((a - b) + 1))) then
- for Result := offset to ((a - b) + 1) do
- if (subStr = Copy(s, Result, b)) then
- Exit;
- Result := 0;
- end;
- begin
- WriteLn(Position('Test', 'Time for Testing... Test!'));
- WriteLn(Position2('Test', 'Time for Testing... Test!'));
- WriteLn(Pos('Test', 'Time for Testing... Test!'));
- WriteLn(PositionEx('Test', 'Time for Testing... Test!', 11));
- WriteLn(PositionEx2('Test', 'Time for Testing... Test!', 11));
- WriteLn(PosEx('Test', 'Time for Testing... Test!', 11));
- end.[/simba]
- [/spoiler]
- [spoiler="StrContains"]
- [simba]function StrContains(str, s: string): Boolean;
- begin
- if ((str <> '') and (s <> '')) then
- Result := (Pos(s, str) > 0)
- else
- Result := False;
- end;
- begin
- ClearDebug;
- if StrContains('StrContains()', 'Contains') then
- WriteLn('YAY!');
- end.[/simba]
- [/spoiler]
- [spoiler="StrContainsEx"]
- [simba]function StrContainsEx(str, s: string; cs: Boolean): Boolean;
- begin
- if ((str = '') or (s = '')) then
- Result := False
- else
- if not cs then
- Result := (Pos(Lowercase(s), Lowercase(str)) > 0)
- else
- Result := (Pos(s, str) > 0);
- end;
- begin
- ClearDebug;
- if StrContainsEx('StrContainsEx()', 'contains', True) then
- WriteLn('NAY!');
- if StrContainsEx('StrContainsEx()', 'contains', False) then
- WriteLn('YAY!');
- end.[/simba]
- [/spoiler]
- [spoiler="Left"]
- [simba]function Left(str: string; x: Integer): string;
- var
- l: Integer;
- begin
- l := Length(str);
- if ((x > 0) and (l > 0)) then
- Result := Copy(str, 1, x);
- end;
- var
- text: string;
- begin
- ClearDebug;
- text := 'Left() TESTING';
- WriteLn(text);
- WriteLn(Left(text, 6));
- end.[/simba]
- [/spoiler]
- [spoiler="Right"]
- [simba]function Right(str: string; x: Integer): string;
- var
- l: Integer;
- begin
- l := Length(str);
- if ((x > 0) and (l > 0)) then
- Result := Copy(str, (l - (x - 1)), x);
- end;
- var
- text: string;
- begin
- ClearDebug;
- text := 'TESTING Right()';
- WriteLn(text);
- WriteLn(Right(text, 7));
- end.[/simba]
- [/spoiler]
- [spoiler="CopyEx"]
- [simba]const
- TEST_STR = 'Janilabo is a nub! ;)';
- START_POS = 1;
- END_POS = 21;
- function CopyEx(str: string; pos1, pos2: Integer): string;
- var
- i, l: Integer;
- begin
- l := Length(str);
- if (l > 0) then
- begin
- if (pos1 < 1) then
- pos1 := 1;
- if (pos1 > l) then
- pos1 := l;
- if (pos2 < 1) then
- pos2 := 1;
- if (pos2 > l) then
- pos2 := l;
- if (pos1 < pos2) then
- Result := Copy(str, pos1, ((pos2 - pos1) + 1))
- else
- if (pos1 <> pos2) then
- begin
- SetLength(Result, ((pos1 - pos2) + 1));
- for i := pos1 downto pos2 do
- Result[((pos1 - i) + 1)] := Char(str[i]);
- end else
- Result := Copy(str, pos1, 1);
- end else
- Result := '';
- end;
- begin
- ClearDebug;
- WriteLn('CopyEx(''' + TEST_STR + ''', ' + IntToStr(START_POS) + ', ' + IntToStr(END_POS) + '): ' + #13#10 + CopyEx(TEST_STR, START_POS, END_POS) + ' [Low=>High]');
- WriteLn('');
- WriteLn('CopyEx(''' + TEST_STR + ''', ' + IntToStr(END_POS) + ', ' + IntToStr(START_POS) + '): ' + #13#10 + CopyEx(TEST_STR, END_POS, START_POS) + ' [High=>Low]');
- end.[/simba]
- [/spoiler]
- [spoiler="BetweenEx"]
- [simba]{==============================================================================]
- Explanation: Returns the string between pos1 and pos2 of str.
- [==============================================================================}
- function BetweenEx(str: string; pos1, pos2: Integer): string;
- var
- i, l: Integer;
- begin
- l := Length(str);
- if (pos1 < 0) then
- pos1 := 0;
- if (pos1 > (l + 1)) then
- pos1 := (l + 1);
- if (pos2 < 0) then
- pos2 := 0;
- if (pos2 > (l + 1)) then
- pos2 := (l + 1);
- if (pos1 < pos2) then
- Result := Copy(str, (pos1 + 1), ((pos2 - pos1) - 1))
- else
- if (pos1 <> pos2) then
- begin
- SetLength(Result, ((pos1 - pos2) - 1));
- for i := (pos1 - 1) downto (pos2 + 1) do
- Result[(pos1 - i)] := Char(str[i]);
- end else
- Result := '';
- end;
- var
- str: string;
- begin
- ClearDebug;
- str := 'Testing BetweenEx() Works!?';
- WriteLn(BetweenEx(str, Pos(' B', str), Pos('?', str)));
- WriteLn(BetweenEx(str, Pos('?', str), Pos(' B', str)));
- WriteLn('');
- WriteLn(BetweenEx(str, 0, (Length(str) + 1)));
- WriteLn(BetweenEx(str, (Length(str) + 1), 0));
- end.[/simba]
- [/spoiler]
- [spoiler="Reverse"]
- [simba]function Reverse(str: string): string;
- var
- i, l: Integer;
- begin
- l := Length(str);
- SetLength(Result, l);
- for i := l downto 1 do
- Result[i] := Char(str[((l - i) + 1)]);
- end;
- begin
- ClearDebug;
- WriteLn(Reverse('!boj eht od ot smees ,noitcnuf )(trevnI rof gnirts TSET a si sihT'));
- end.[/simba]
- [/spoiler]
- [spoiler="StartsWith"]
- [simba]function StartsWith(s, str: string): Boolean;
- begin
- if ((s <> '') and (str <> '')) then
- Result := (s = Copy(str, 1, Length(s)))
- else
- Result := False;
- end;
- begin
- ClearDebug;
- if StartsWith('TE', 'TEST') then
- WriteLn('YEP!')
- else
- WriteLn('NOPE.');
- end.[/simba]
- [/spoiler]
- [spoiler="StartWith"]
- [simba]{==============================================================================]
- Explanation: Returns true if suffix was found in s and it was set as the beginning of the s.
- [==============================================================================}
- function StartWith(suffix: string; var s: string): Boolean;
- var
- p, l: Integer;
- begin
- l := Length(suffix);
- if ((s <> '') and (l <= Length(s))) then
- begin
- p := Pos(suffix, s);
- Result := (p > 0);
- if Result then
- s := Copy(s, p, (Length(s) - (p - 1)));
- end else
- Result := False;
- end;
- var
- str: string;
- begin
- ClearDebug;
- str := 'Lets see that StartWith() Works!';
- if StartWith('StartWith', str) then
- WriteLn(str);
- end.[/simba]
- [/spoiler]
- [spoiler="EndsWith"]
- [simba]function EndsWith(s, str: string): Boolean;
- var
- sL: Integer;
- begin
- sL := Length(s);
- if ((s <> '') and (str <> '')) then
- Result := (s = Copy(str, (Length(str) - (sL - 1)), sL))
- else
- Result := False;
- end;
- begin
- ClearDebug;
- if EndsWith('ST', 'TEST') then
- WriteLn('YEP!')
- else
- WriteLn('NOPE.');
- end.[/simba]
- [/spoiler]
- [spoiler="EndWith"]
- [simba]{==============================================================================]
- Explanation: Returns true if suffix was found in s and it was set as the ending of the s.
- [==============================================================================}
- function EndWith(suffix: string; var s: string): Boolean;
- var
- t, p, l: Integer;
- begin
- l := Length(suffix);
- if ((s <> '') and (l <= Length(s))) then
- begin
- if (Length(suffix) <= l) then
- begin
- repeat
- t := PosEx(suffix, s, (p + 1));
- if (t > 0) then
- p := t;
- until (t <= 0);
- Result := (p > 0);
- if Result then
- s := Copy(s, 1, ((p + l) - 1));
- end else
- Result := False;
- end else
- Result := False;
- end;
- var
- str: string;
- begin
- ClearDebug;
- str := 'EndWith() Works! Right? We will see if it does...';
- if EndWith('Works!', str) then
- WriteLn(str);
- end.[/simba]
- [/spoiler]
- [spoiler="TrimStart"]
- [simba]function TrimStart(str: string): string;
- var
- i, l: Integer;
- begin
- if (str <> '') then
- begin
- l := Length(str);
- for i := 1 to l do
- if (str[i] <> ' ') then
- Break;
- Result := Copy(str, i, ((l + 1) - i));
- end else
- Result := '';
- end;
- var
- str: string;
- begin
- ClearDebug;
- str := ' TEST ';
- WriteLn('STR length before TrimStart(): ' + IntToStr(Length(str)) + ' ("' + str + '").');
- str := TrimStart(str);
- WriteLn('STR length after TrimStart(): ' + IntToStr(Length(str)) + ' ("' + str + '").');
- end.[/simba]
- [/spoiler]
- [spoiler="TrimEnd"]
- [simba]function TrimEnd(str: string): string;
- var
- i, l: Integer;
- begin
- if (str <> '') then
- begin
- l := Length(str);
- for i := l downto 1 do
- if (str[i] <> ' ') then
- Break;
- Result := Copy(str, 1, i);
- end else
- Result := '';
- end;
- var
- str: string;
- begin
- ClearDebug;
- str := ' TEST ';
- WriteLn('STR length before TrimEnd(): ' + IntToStr(Length(str)) + ' ("' + str + '").');
- str := TrimEnd(str);
- WriteLn('STR length after TrimEnd(): ' + IntToStr(Length(str)) + ' ("' + str + '").');
- end.[/simba]
- [/spoiler]
- [spoiler="Behind"]
- [simba]function Behind(data: string; position, size: Integer): string;
- var
- l, r: Integer;
- begin
- l := Length(data);
- if ((l > 0) and (position > 1) and (size > 0)) then
- begin
- if ((position - size) < 1) then
- size := ((position - size) + (size - 1));
- if (position > (l + 1)) then
- begin
- r := ((position - l) - 1);
- size := (size - r);
- end;
- Result := Copy(data, ((position - size) - r), size);
- end else
- Result := '';
- end;
- var
- str: string;
- p, c: Integer;
- begin
- ClearDebug;
- str := 'BEHIND <= TEST => AHEAD';
- p := Pos(' TEST', str);
- c := (p - 1);
- WriteLn('Behind(str, ' + IntToStr(p) + ', ' + IntToStr(c) + '): "' + Behind(str, p, c) + '"');
- end.[/simba]
- [/spoiler]
- [spoiler="Ahead"]
- [simba]function Ahead(data: string; position, size: Integer): string;
- var
- l: Integer;
- begin
- l := Length(data);
- if ((l > 0) and (position <= l) and (size > 0)) then
- begin
- if (position < 1) then
- begin
- {$IFNDEF Lape}
- size := (size - iAbs(position - 1));
- {$ELSE}
- size := (size - Abs(position - 1));
- {$ENDIF}
- position := 1;
- end;
- if ((size > 0) and ((position + size) > l)) then
- size := (size - (((position + size) - l) - 1));
- Result := Copy(data, position, size);
- end else
- Result := '';
- end;
- var
- str: string;
- p, c: Integer;
- begin
- ClearDebug;
- str := 'BEHIND <= TEST => AHEAD';
- p := Pos('=>', str);
- c := ((Length(str) - p) + 1);
- WriteLn('Ahead(str, ' + IntToStr(p) + ', ' + IntToStr(c) + '): "' + Ahead(str, p, c) + '"');
- end.[/simba]
- [/spoiler]
- [spoiler="After"]
- [simba]function After(s, str: string): string;
- var
- p, strL, sL: Integer;
- begin
- sL := Length(s);
- strL := Length(str);
- if (sL < strL) then
- begin
- p := Pos(s, str);
- if (p > 0) then
- Result := Copy(str, (p + sL), ((1 + strL) - (p + sL)));
- end else
- Result := '';
- end;
- var
- str: string;
- begin
- ClearDebug;
- str := 'What comes after this? After() WORKS!';
- WriteLn(After('this? ', str));
- end.[/simba]
- [/spoiler]
- [spoiler="Before"]
- [simba]function Before(s, str: string): string;
- var
- p: Integer;
- begin
- if (Length(s) < Length(str)) then
- begin
- p := Pos(s, str);
- if (p > 1) then
- Result := Copy(str, 1, (p - 1))
- else
- Result := '';
- end else
- Result := '';
- end;
- var
- str: string;
- begin
- ClearDebug;
- str := 'Before() WORKS! Test to see if Before() works correctly.';
- WriteLn(Before(' Test', str));
- end.[/simba]
- [/spoiler]
- [spoiler="AfterEx"]
- [simba]function AfterEx(s, str: string; offset: Integer): string;
- var
- p, sL, strL: Integer;
- begin
- sL := Length(s);
- strL := Length(str);
- if ((sL < strL) and (offset < strL)) then
- begin
- p := PosEx(s, str, offset);
- if (p > 0) then
- Result := Copy(str, (p + sL), ((1 + strL) - (p + sL)))
- else
- Result := '';
- end else
- Result := '';
- end;
- var
- o: Integer;
- str: string;
- begin
- ClearDebug;
- str := 'Hmmmph, testing AfterEx(), testing AfterEx() WORKS!';
- o := Pos('testing', str);
- WriteLn(AfterEx('testing ', str, (o + 1)));
- end.[/simba]
- [/spoiler]
- [spoiler="BeforeEx"]
- [simba]function BeforeEx(s, str: string; offset: Integer): string;
- var
- p, strL: Integer;
- begin
- strL := Length(str);
- if ((Length(s) < strL) and (offset < strL)) then
- begin
- p := PosEx(s, str, offset);
- if (p > 1) then
- Result := Copy(str, 1, (p - 1))
- else
- Result := '';
- end else
- Result := '';
- end;
- var
- o: Integer;
- str: string;
- begin
- ClearDebug;
- str := 'BeforeEx() WORKS! This is BeforeEx() test!';
- o := Pos('This', str);
- WriteLn(BeforeEx(' ', str, (o - 1)));
- end.[/simba]
- [/spoiler]
- [spoiler="LastAfter"]
- [simba]function LastAfter(s, str: string): string;
- var
- sL, l, p, lp: Integer;
- begin
- l := Length(str);
- sL := Length(s);
- if (sL <= l) then
- begin
- repeat
- p := PosEx(s, str, (p + 1));
- if (p > 0) then
- lp := p;
- until (p <= 0);
- if (lp > 0) then
- Result := Copy(str, (lp + sL), ((1 + l) - (lp + sL)))
- else
- Result := '';
- end else
- Result := '';
- end;
- var
- str: string;
- begin
- ClearDebug;
- str := 'This is LastAfter() test. YEP, let''s test. LastAfter() WORKS!';
- WriteLn(LastAfter('test. ', str));
- end.[/simba]
- [/spoiler]
- [spoiler="LastBefore"]
- [simba]function LastBefore(s, str: string): string;
- var
- l, p, lp: Integer;
- begin
- l := Length(str);
- if (Length(s) <= l) then
- begin
- repeat
- p := PosEx(s, str, (p + 1));
- if (p > 0) then
- lp := p;
- until (p <= 0);
- if (lp > 0) then
- Result := Copy(str, 1, (lp - 1))
- else
- Result := '';
- end else
- Result := '';
- end;
- var
- str: string;
- begin
- ClearDebug;
- str := 'LastBefore() WORKS! WORKS? Right?! ;)';
- WriteLn(LastBefore(' WORK', str));
- end.[/simba]
- [/spoiler]
- [spoiler="Center"]
- [simba]function Center(str: string; size: Integer): string;
- var
- l, p: Integer;
- begin
- l := Length(str);
- if (l >= size) then
- Result := str
- else
- if (l > 0) then
- begin
- p := (((size - l) div 2) + 1);
- Result := (StringOfChar(' ', (p - 1)) + str + StringOfChar(' ', ((size - (p + l)) + 1)));
- end else
- Result := StringOfChar(' ', size)
- end;
- var
- test: string;
- begin
- ClearDebug;
- test := 'Center()';
- WriteLn('"' + Center(test, (Length(test) * 2)) + '"');
- end.[/simba]
- [/spoiler]
- [spoiler="CenterEx"]
- [simba]function CenterEx(str: string; size: Integer; fill: Char): string;
- var
- l, p: Integer;
- begin
- l := Length(str);
- if (l >= size) then
- Result := str
- else
- if (l > 0) then
- begin
- p := (((size - l) div 2) + 1);
- Result := (StringOfChar(fill, (p - 1)) + str + StringOfChar(fill, ((size - (p + l)) + 1)));
- end else
- Result := StringOfChar(fill, size);
- end;
- var
- test: string;
- begin
- ClearDebug;
- test := 'CenterEx()';
- WriteLn('"' + CenterEx(test, (Length(test) * 2), '-') + '"');
- end.[/simba]
- [/spoiler]
- [spoiler="PosMultiID"]
- [simba]{==============================================================================]
- Explanation: Finds position from s items in str. Stores the ID of the found s item to index variable.
- The importance order for d items is from left to right (=>).
- So place the important ones first and then less important after those.
- [==============================================================================}
- function PosMultiID(s: TStringArray; str: string; var index: Integer): Integer;
- var
- h, i, p, t: Integer;
- begin
- Result := -1;
- index := -1;
- h := High(s);
- if ((h > -1) and (str <> '')) then
- begin
- t := (Length(str) + 1);
- Result := t;
- for i := 0 to h do
- begin
- p := Pos(s[i], str);
- if ((p > 0) and (p < Result)) then
- begin
- Result := p;
- index := i;
- end;
- end;
- if (Result = t) then
- Result := 0;
- end;
- end;
- var
- str: string;
- ID, position: Integer;
- items: TStringArray;
- begin
- ClearDebug;
- str := 'Remember, this string variable is just for testing purposes!';
- items := ['testing', 'just', 'string'];
- position := PosMultiID(items, str, ID);
- if (position > 0) then
- WriteLn('Found item[' + IntToStr(ID) + '] ("' + items[ID] + '") inside str at position ' + IntToStr(position))
- else
- WriteLn('Nothing was found. :(');
- SetLength(items, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="PosMultiIDEx"]
- [simba]{==============================================================================]
- Explanation: Finds position from s items in str. Stores the ID of the found s item to index variable.
- The importance order for d items is from left to right (=>).
- So place the important ones first and then less important after those.
- Contains field for offset.
- [==============================================================================}
- function PosMultiIDEx(s: TStringArray; str: string; var index: Integer; offset: Integer): Integer;
- var
- h, i, p, t: Integer;
- begin
- if (offset < 1) then
- offset := 1;
- Result := -1;
- index := -1;
- h := High(s);
- if ((h > -1) and (str <> '')) then
- begin
- t := (Length(str) + 1);
- Result := t;
- for i := 0 to h do
- begin
- p := PosEx(s[i], str, offset);
- if ((p > 0) and (p < Result)) then
- begin
- Result := p;
- index := i;
- end;
- end;
- if (Result = t) then
- Result := 0;
- end;
- end;
- var
- str: string;
- ID, position: Integer;
- items: TStringArray;
- begin
- ClearDebug;
- str := 'Remember, this string variable is just for testing purposes!';
- items := ['testing', 'just', 'string'];
- position := PosMultiIDEx(items, str, ID, (Pos('string', str) + 1));
- if (position > 0) then
- WriteLn('Found item[' + IntToStr(ID) + '] ("' + items[ID] + '") inside str at position ' + IntToStr(position))
- else
- WriteLn('Nothing was found. :(');
- SetLength(items, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="PosMulti"]
- [simba]{==============================================================================]
- Explanation: Finds position from s items in str.
- The importance order for d items is from left to right (=>).
- So place the important ones first and then less important after those.
- [==============================================================================}
- function PosMulti(s: TStringArray; str: string): Integer;
- var
- h, i, p, t: Integer;
- begin
- h := High(s);
- if ((h > -1) and (str <> '')) then
- begin
- t := (Length(str) + 1);
- Result := t;
- for i := 0 to h do
- begin
- p := Pos(s[i], str);
- if ((p > 0) and (p < Result)) then
- Result := p;
- end;
- if (Result = t) then
- Result := 0;
- end else
- Result := -1;
- end;
- var
- str: string;
- position: Integer;
- items: TStringArray;
- begin
- ClearDebug;
- str := 'Remember, this string variable is just for testing purposes!';
- items := ['testing', 'just', 'string'];
- position := PosMulti(items, str);
- if (position > 0) then
- WriteLn('Found some item from items inside str at position ' + IntToStr(position))
- else
- WriteLn('Nothing was found. :(');
- SetLength(items, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="PosMultiEx"]
- [simba]{==============================================================================]
- Explanation: Finds position from s items in str.
- The importance order for d items is from left to right (=>).
- So place the important ones first and then less important after those.
- Contains field for offset.
- [==============================================================================}
- function PosMultiEx(s: TStringArray; str: string; offset: Integer): Integer;
- var
- h, i, p, t: Integer;
- begin
- h := High(s);
- if ((h > -1) and (str <> '')) then
- begin
- if (offset < 1) then
- offset := 1;
- t := (Length(str) + 1);
- Result := t;
- for i := 0 to h do
- begin
- p := PosEx(s[i], str, offset);
- if ((p > 0) and (p < Result)) then
- Result := p;
- end;
- if (Result = t) then
- Result := 0;
- end else
- Result := -1;
- end;
- var
- str: string;
- position: Integer;
- items: TStringArray;
- begin
- ClearDebug;
- str := 'Remember, this string variable is just for testing purposes!';
- items := ['testing', 'just', 'string'];
- position := PosMultiEx(items, str, (Pos('string', str) + 1));
- if (position > 0) then
- WriteLn('Found some item from items inside str at position ' + IntToStr(position))
- else
- WriteLn('Nothing was found. :(');
- SetLength(items, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="LastPos"]
- [simba]function LastPos(s, str: string): Integer;
- var
- l, p: Integer;
- begin
- Result := 0;
- l := Length(str);
- if (Length(s) <= l) then
- repeat
- p := PosEx(s, str, (p + 1));
- if (p > 0) then
- Result := p;
- until (p <= 0);
- end;
- begin
- ClearDebug;
- WriteLn(LastPos('.', 'LastPos() test. The DOT is the result position. <= Over there'));
- end.[/simba]
- [/spoiler]
- [spoiler="LastPosEx"]
- [simba]function LastPosEx(s, str: string; offset: Integer): Integer;
- var
- l, p: Integer;
- begin
- Result := 0;
- l := Length(str);
- if ((Length(s) > l) or (offset > l) or (offset < 1)) then
- Exit;
- str := Copy(str, 1, offset);
- repeat
- p := PosEx(s, str, (p + 1));
- if (p > 0) then
- Result := p;
- until (p <= 0);
- end;
- var
- str: string;
- begin
- ClearDebug;
- str := 'LastPosEx() test.. The ".." is the result position... <= Over there! We ignore this: ..';
- WriteLn(LastPosEx('..', str, Pos('We ignore this', str)));
- end.[/simba]
- [/spoiler]
- [spoiler="ClosestPos"]
- [simba]// Returns the closest position of 's' found in 'str' based on 'trgt_pos'.
- function ClosestPos(trgt_pos: Integer; s, str: string): Integer;
- var
- strL, o, p: Integer;
- begin
- Result := 0;
- strL := Length(str);
- if ((Length(s) <= strL) and (trgt_pos >= 1) and (trgt_pos <= strL)) then
- repeat
- p := PosEx(s, str, (o + 1));
- if (p > 0) then
- begin
- if (Result = 0) then
- Result := p
- else
- {$IFNDEF Lape}
- if (iAbs(trgt_pos - p) < iAbs(trgt_pos - Result)) then
- {$ELSE}
- if (Abs(trgt_pos - p) < Abs(trgt_pos - Result)) then
- {$ENDIF}
- Result := p
- else
- Exit;
- o := p;
- end;
- until (p <= 0);
- end;
- var
- h, i, cp: Integer;
- str: string;
- TIA: TIntegerArray;
- TSA, cTSA: TStringArray;
- begin
- ClearDebug;
- str := '<@#%¤ APPLE ~$¤@&* ORANGE ~%@$€* BANANA ~£&#>';
- TSA := ['APPLE', 'ORANGE', 'BANANA'];
- TIA := [Pos(TSA[0], str), Pos(TSA[1], str), Pos(TSA[2], str)];
- cTSA := ['@', '#', '%', '¤', '~', '$', '&', '*', '€', '£'];
- WriteLn('Closest positions...' + #13#10);
- h := High(cTSA);
- for i := 0 to 2 do
- begin
- WriteLn(TSA[i] + ' (pos: ' + IntToStr(TIA[i]) + ')');
- for cp := 0 to h do
- WriteLn(cTSA[cp] + ': ' + IntToStr(ClosestPos(TIA[i], cTSA[cp], str)));
- if (i < 2) then
- WriteLn('');
- end;
- SetLength(TSA, 0);
- SetLength(TIA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="FarthestPos"]
- [simba]// Returns the farthest position of 's' found in 'str' based on 'trgt_pos'.
- function FarthestPos(trgt_pos: Integer; s, str: string): Integer;
- var
- strL, o, p: Integer;
- begin
- Result := 0;
- strL := Length(str);
- if ((Length(s) <= strL) and (trgt_pos >= 1) and (trgt_pos <= strL)) then
- repeat
- p := PosEx(s, str, (o + 1));
- if (p > 0) then
- begin
- if (Result = 0) then
- Result := p
- else
- {$IFNDEF Lape}
- if (IAbs(trgt_pos - p) > IAbs(trgt_pos - Result)) then
- {$ELSE}
- if (Abs(trgt_pos - p) > Abs(trgt_pos - Result)) then
- {$ENDIF}
- Result := p;
- o := p;
- end;
- until (p <= 0);
- end;
- var
- h, i, cp: Integer;
- str: string;
- TIA: TIntegerArray;
- TSA, cTSA: TStringArray;
- begin
- ClearDebug;
- str := '<@#%¤ APPLE ~$¤@&* ORANGE ~%@$€* BANANA ~£&#>';
- TSA := ['APPLE', 'ORANGE', 'BANANA'];
- TIA := [Pos(TSA[0], str), Pos(TSA[1], str), Pos(TSA[2], str)];
- cTSA := ['@', '#', '%', '¤', '~', '$', '&', '*', '€', '£'];
- WriteLn('Farthest positions...' + #13#10);
- h := High(cTSA);
- for i := 0 to 2 do
- begin
- WriteLn(TSA[i] + ' (pos: ' + IntToStr(TIA[i]) + ')');
- for cp := 0 to h do
- WriteLn(cTSA[cp] + ': ' + IntToStr(FarthestPos(TIA[i], cTSA[cp], str)));
- if (i < 2) then
- WriteLn('');
- end;
- SetLength(TSA, 0);
- SetLength(TIA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="Count"]
- [simba]function Count(s, str: string): Integer;
- var
- p, sL: Integer;
- begin
- Result := 0;
- sL := Length(s);
- p := (p - (sL - 1));
- if (sL <= Length(str)) then
- repeat
- p := PosEx(s, str, (p + sL));
- if (p > 0) then
- Inc(Result);
- until (p <= 0);
- end;
- var
- s, str: string;
- begin
- ClearDebug;
- str := 'TESTESTESTEST for Count()!';
- s := 'TEST';
- WriteLn('String: "' + str + '"');
- WriteLn('Count of "' + s + '" in String: ' + IntToStr(Count(s, str)) + '!');
- end.[/simba]
- [/spoiler]
- [spoiler="CountEx"]
- [simba]function CountEx(s, str: string; overlap: Boolean): Integer;
- var
- p, o: Integer;
- begin
- Result := 0;
- if not overlap then
- begin
- o := Length(s);
- p := (p - (o - 1));
- end else
- o := 1;
- if (o <= Length(str)) then
- repeat
- p := PosEx(s, str, (p + o));
- if (p > 0) then
- Inc(Result);
- until (p <= 0);
- end;
- var
- s, str: string;
- begin
- ClearDebug;
- str := 'TESTESTESTEST for CountEx()!';
- s := 'TEST';
- WriteLn('String: "' + str + '"');
- WriteLn('Count of "' + s + '" in String: ' + IntToStr(CountEx(s, str, True)) + ' (overlap = ON)!');
- WriteLn('Count of "' + s + '" in String: ' + IntToStr(CountEx(s, str, False)) + ' (overlap = OFF)!');
- end.[/simba]
- [/spoiler]
- [spoiler="CountMulti"]
- [simba]function CountMulti(s: TStringArray; str: string; overlap: Boolean): Integer;
- var
- h, l, p, o, i, t, d, x, y: Integer;
- begin
- Result := 0;
- h := High(s);
- if (h > -1) then
- begin
- o := 1;
- repeat
- p := 0;
- for x := 0 to h do
- begin
- t := PosEx(s[x], str, (l + o));
- if (t < 1) then
- begin
- for y := x to (h - 1) do
- s[y] := s[(y + 1)];
- SetLength(s, h);
- Dec(x);
- Dec(h);
- end else
- if ((p = 0) or (t < p)) then
- begin
- p := t;
- i := x;
- end;
- end;
- if (p > 0) then
- begin
- Inc(Result);
- l := p;
- if not overlap then
- o := Length(s[i]);
- end;
- until (p <= 0);
- end;
- end;
- var
- str: string;
- begin
- ClearDebug;
- str := 'HAHAHAH LOLOLOL!';
- WriteLn(ToStr(CountMulti(['HAH', 'LOL'], str, True)) + ' [overlap = ON]');
- WriteLn(ToStr(CountMulti(['HAH', 'LOL'], str, False)) + ' [overlap = OFF]');
- end.[/simba]
- [/spoiler]
- [spoiler="Stuff"]
- [simba]function Stuff(str, s: string; iFrom, iCount: Integer): string;
- begin
- Result := Copy(str, 1, Length(str));
- Delete(Result, iFrom, iCount);
- Insert(s, Result, iFrom);
- end;
- var
- str: string;
- begin
- ClearDebug;
- str := 'Stuff test to see this is working..!';
- WriteLn(Stuff(str, '() WORKS', 6, 30));
- end.[/simba]
- [/spoiler]
- [spoiler="StuffEx"]
- [simba]{==============================================================================]
- Explanation: Returns str stuffed with s - pos1=start position, pos2=finish position. With pos1 > pos2 s will be reversed.
- [==============================================================================}
- function StuffEx(str, s: string; pos1, pos2: Integer): string;
- var
- i, l: Integer;
- begin
- Result := str;
- l := Length(Result);
- if (pos1 < 1) then
- pos1 := 1;
- if (pos1 > l) then
- pos1 := l;
- if (pos2 < 1) then
- pos2 := 1;
- if (pos2 > l) then
- pos2 := l;
- if (pos1 > pos2) then
- begin
- Delete(Result, pos2, ((pos1 - pos2) + 1));
- l := Length(s);
- SetLength(str, l);
- for i := l downto 1 do
- str[i] := Char(s[((l - i) + 1)]);
- Insert(str, Result, pos2);
- end else
- begin
- Delete(Result, pos1, ((pos2 - pos1) + 1));
- Insert(s, Result, pos1);
- end;
- end;
- var
- str: string;
- begin
- ClearDebug;
- str := 'StuffEx test to see this is working..!';
- WriteLn(StuffEx(str, '() WORKS', 8, 37));
- WriteLn(StuffEx(str, 'SKROW )(', 37, 8));
- end.[/simba]
- [/spoiler]
- [spoiler="WrapTextEx"]
- [simba]function WrapTextEx(str, breakStr, breakChars: string; maxCol: Integer): string;
- var
- a, i, l, p, t, b, c: Integer;
- begin
- l := Length(str);
- Result := '';
- if (l > 0) then
- if ((breakStr <> '') and (breakChars <> '')) then
- begin
- if (maxCol < 1) then
- maxCol := 1;
- a := 1;
- while ((i + a) <= l) do
- begin
- IncEx(i, a);
- p := (i + maxCol);
- b := l;
- for t := i to l do
- if ((t >= p) and (b < l)) then
- Break
- else
- if (Pos(str[t], breakChars) > 0) then
- b := t;
- if (t > l) then
- b := l;
- for c := i to b do
- Result := (Result + str[c]);
- if (b < l) then
- Result := (Result + breakStr);
- a := ((b - i) + 1);
- if (a < 1) then
- a := 1;
- end;
- end else
- Result := str;
- end;
- var
- s: string;
- begin
- ClearDebug;
- s := 'This is quite a long string, at least 50 chars long'; // Example from delphibasics.co.uk
- WriteLn(WrapTextEx(s, #13#10, ' ', 10));
- end.[/simba]
- [/spoiler]
- [spoiler="WrapText"]
- [simba]{$IFNDEF Lape}
- function WrapText(str: string; maxCol: Integer): string;
- {$ELSE}
- function WrapText2(str: string; maxCol: Integer): string;
- {$ENDIF}
- var
- a, i, l, p, t, b, c: Integer;
- begin
- l := Length(str);
- Result := '';
- if (l > 0) then
- begin
- if (maxCol < 1) then
- maxCol := 1;
- a := 1;
- while ((i + a) <= l) do
- begin
- IncEx(i, a);
- p := (i + maxCol);
- b := l;
- for t := i to l do
- if ((t >= p) and (b < l)) then
- Break
- else
- if (Pos(str[t], ' '#9'-') > 0) then
- b := t;
- if (t > l) then
- b := l;
- for c := i to b do
- Result := (Result + str[c]);
- if (b < l) then
- Result := (Result + #13#10);
- a := ((b - i) + 1);
- if (a < 1) then
- a := 1;
- end;
- end;
- end;
- var
- s: string;
- begin
- ClearDebug;
- s := 'This is quite a long string, at least 50 chars long!'; // Example from delphibasics.co.uk
- {$IFNDEF Lape}
- WriteLn(WrapText(s, 10));
- {$ELSE}
- WriteLn(WrapText2(s, 10));
- {$ENDIF}
- end.[/simba]
- [/spoiler]
- [spoiler="ReplaceEx"]
- [simba]function ReplaceEx(Text, FindStr, ReplaceStr: string; Offset: Integer; Flags: TReplaceFlags): string;
- var
- tL: Integer;
- begin
- tL := Length(Text);
- if (Offset < 1) then
- Offset := 1;
- if ((Length(FindStr) <= tL) and (Offset <= tL)) then
- {$IFNDEF Lape}
- Result := (Copy(Text, 1, (Offset - 1)) + ReplaceWrap(Copy(Text, Offset, ((tL - Offset) + 1)), FindStr, ReplaceStr, Flags))
- {$ELSE}
- Result := (Copy(Text, 1, (Offset - 1)) + Replace(Copy(Text, Offset, ((tL - Offset) + 1)), FindStr, ReplaceStr, Flags))
- {$ENDIF}
- else
- Result := '';
- end;
- var
- str: string;
- begin
- ClearDebug;
- str := 'Janilabo is? N?WBI?! :)';
- WriteLn(ReplaceEx(str, '?', 'E', (Pos('?', str) + 1), [rfReplaceAll]));
- end.[/simba]
- [/spoiler]
- [spoiler="Compare"]
- [simba]function Compare(s1, s2: string): Integer;
- begin
- if (s1 = s2) then
- Result := 0
- else
- if (s1 > s2) then
- Result := 1
- else
- Result := -1;
- end;
- function GetRelation(s1, s2: string): string;
- var
- r: Integer;
- begin
- r := Compare(s1, s2);
- case r of
- -1: Result := ('"' + s1 + '" < "' + s2 + '"');
- 0: Result := ('"' + s1 + '" = "' + s2 + '"');
- 1: Result := ('"' + s1 + '" > "' + s2 + '"');
- end;
- end;
- begin
- ClearDebug;
- WriteLn(GetRelation('ABCD A', 'ABCD B'));
- WriteLn(GetRelation('ABCD B', 'ABCD B'));
- WriteLn(GetRelation('ABCD C', 'ABCD B'));
- end.[/simba]
- [/spoiler]
- [spoiler="Compare2"]
- [simba]function Compare2(s1, s2: string): Integer;
- var
- i, mL, s1L, s2L: Integer;
- begin
- Result := 0;
- if (s1 <> s2) then
- begin
- s1L := Length(s1);
- s2L := Length(s2);
- if ((s1L <= 0) or (s2L <= 0)) then
- if (s1L < s2L) then
- Result := -1
- else
- Result := 1;
- if (Result <> 0) then
- Exit;
- Result := -1;
- mL := Min(s1L, s2L);
- for i := 1 to mL do
- if (s1[i] <> s2[i]) then
- begin
- if (s1[i] > s2[i]) then
- Result := 1;
- Exit;
- end;
- if (s1L > s2L) then
- Result := 1;
- end;
- end;
- function GetRelation(s1, s2: string): string;
- var
- r: Integer;
- begin
- r := Compare2(s1, s2);
- case r of
- -1: Result := ('"' + s1 + '" < "' + s2 + '"');
- 0: Result := ('"' + s1 + '" = "' + s2 + '"');
- 1: Result := ('"' + s1 + '" > "' + s2 + '"');
- end;
- end;
- begin
- ClearDebug;
- WriteLn(GetRelation('ABCD A', 'ABCD B'));
- WriteLn(GetRelation('ABCD B', 'ABCD B'));
- WriteLn(GetRelation('ABCD C', 'ABCD B'));
- end.[/simba]
- [/spoiler]
- [spoiler="Expand"]
- [simba]function Expand(trgt_pos: Integer; s1, s2, str: string): string;
- var
- l, p, start, finish, s1L, s2L: Integer;
- tmp: string;
- begin
- Result := '';
- l := Length(str);
- s1L := Length(s1);
- s2L := Length(s2);
- if ((s1L <= l) and (s2L <= l) and (trgt_pos <= l) and (trgt_pos >= 1)) then
- begin
- finish := PosEx(s2, str, trgt_pos);
- if ((finish < 1) or (finish = trgt_pos)) then
- Exit;
- tmp := Copy(str, 1, trgt_pos);
- repeat
- p := PosEx(s1, tmp, (p + 1));
- if (p > 0) then
- start := p;
- until (p <= 0);
- if ((start <> trgt_pos) and (start > 0) and (finish > 0) and (start < finish)) then
- Result := Copy(str, (start + s1L), ((finish - start) - s2L));
- tmp := '';
- end;
- end;
- var
- str, tmp: string;
- id, id_pos: Integer;
- begin
- ClearDebug;
- str := '<item_list>' + #13#10 +
- ' <item_data>' + #13#10 +
- ' <item_name>Apple</item_name>' + #13#10 +
- ' <item_category>Fruits</item_category>' + #13#10 +
- ' <item_ID>2</item_ID>' + #13#10 +
- ' <item_description>Great fruit</item_description>' + #13#10 +
- ' </item_data>' + #13#10 +
- ' <item_data>' + #13#10 +
- ' <item_name>Strawberry</item_name>' + #13#10 +
- ' <item_category>Berries</item_category>' + #13#10 +
- ' <item_ID>0</item_ID>' + #13#10 +
- ' <item_description>Sweet berry</item_description>' + #13#10 +
- ' </item_data>' + #13#10 +
- ' <item_data>' + #13#10 +
- ' <item_name>Tomato</item_name>' + #13#10 +
- ' <item_category>Vegetables</item_category>' + #13#10 +
- ' <item_ID>1</item_ID>' + #13#10
- ' <item_description>Healthy vegetable</item_description>' + #13#10 +
- ' </item_data>' + #13#10 +
- '</item_list>';
- str := Between('<item_list>', '</item_list>', str);
- for id := 0 to 2 do
- begin
- id_pos := Pos(('<item_ID>' + IntToStr(id) + '</item_ID>'), str);
- if (id_pos > 0) then
- begin
- tmp := Expand(id_pos, '<item_data>', '</item_data>', str);
- WriteLn('Item nr.' + IntToStr(id) + ':' + #13#10 +
- 'Name: ' + Between('<item_name>', '</item_name>', tmp) + #13#10
- 'Description: ' + Between('<item_description>', '</item_description>', tmp) + #13#10
- 'Category: ' + Between('<item_category>', '</item_category>', tmp) + #13#10);
- tmp := '';
- end;
- end;
- end.[/simba]
- [/spoiler]
- [spoiler="ExpandEx"]
- [simba]function ExpandEx(trgt_pos: Integer; s1, s2, str: string; includeTags: Boolean): string;
- var
- l, p, start, finish, s1L, s2L, e: Integer;
- tmp: string;
- begin
- Result := '';
- l := Length(str);
- s1L := Length(s1);
- s2L := Length(s2);
- if ((s1L <= l) and (s2L <= l) and (trgt_pos <= l) and (trgt_pos >= 1)) then
- begin
- if includeTags then
- begin
- s1L := 0;
- e := s2L;
- s2L := 0;
- end;
- finish := PosEx(s2, str, trgt_pos);
- if ((finish < 1) or (finish = trgt_pos)) then
- Exit;
- tmp := Copy(str, 1, trgt_pos);
- repeat
- p := PosEx(s1, tmp, (p + 1));
- if (p > 0) then
- start := p;
- until (p <= 0);
- if ((start <> trgt_pos) and (start > 0) and (finish > 0) and (start < finish)) then
- Result := Copy(str, (start + s1L), (((finish - start) - S2L) + e));
- tmp := '';
- end;
- end;
- var
- str, tmp: string;
- id, id_pos: Integer;
- begin
- ClearDebug;
- str := 'Lets do this... {* function ExpandEx() *} It is... Magical!';
- WriteLn('ExpandEx(): "' + ExpandEx(Pos('ExpandEx', str), '{* ', ' *}', str, True) + '" [tags included]');
- WriteLn('ExpandEx(): "' + ExpandEx(Pos('ExpandEx', str), '{* ', ' *}', str, False) + '" [without tags]');
- end.[/simba]
- [/spoiler]
- [spoiler="StringOfString"]
- [simba]{==============================================================================]
- Explanation: Returns str * count times. Example: ('ha', 3) => 'hahaha'
- [==============================================================================}
- function StringOfString(str: string; count: Integer): string;
- var
- i: Integer;
- begin
- Result := '';
- if (count > 0) then
- for i := 1 to count do
- Result := (Result + string(str));
- end;
- begin
- ClearDebug;
- WriteLn(StringOfString('ha', 4));
- end.[/simba]
- [/spoiler]
- [spoiler="StringOfStringEx"]
- [simba]{==============================================================================]
- Explanation: Returns str * count times. Example: ('ha', 3, ' ') => 'ha ha ha'
- Places glue between the str positions to result.
- [==============================================================================}
- function StringOfStringEx(str: string; count: Integer; glue: string): string;
- var
- i: Integer;
- begin
- Result := '';
- if (count > 0) then
- if (count > 1) then
- begin
- for i := 1 to (count - 1) do
- Result := (Result + string(str) + string(glue));
- Result := (Result + string(str));
- end else
- Result := string(str);
- end;
- begin
- ClearDebug;
- WriteLn('"' + StringOfStringEx('hah', 3, '-') + '"');
- WriteLn('"' + StringOfStringEx('hah', 2, '-') + '"');
- WriteLn('"' + StringOfStringEx('hah', 1, '-') + '"');
- WriteLn('"' + StringOfStringEx('hah', 0, '-') + '"');
- end.[/simba]
- [/spoiler]
- [spoiler="Line"]
- [simba]{==============================================================================]
- Explanation: Returns the line text from str by number.
- NOTE: Line numbers start from 1!
- [==============================================================================}
- function Line(str: string; number: Integer): string;
- var
- p, h, i, x, o, m, l, y, z: Integer;
- d, t: TStringArray;
- begin
- if ((str <> '') and (number > 0)) then
- begin
- d := [#13#10, #13, #10];
- h := High(d);
- o := 1;
- SetLength(t, Length(str));
- repeat
- l := 0;
- for x := 0 to h do
- begin
- p := Pos(d[x], str);
- if (p < 1) then
- begin
- z := High(d);
- if ((x <= z) and (x > -1)) then
- begin
- for y := x to (z - 1) do
- d[y] := d[(y + 1)];
- SetLength(d, z);
- end;
- Dec(x);
- Dec(h);
- end else
- if ((l = 0) or (p < l)) then
- begin
- m := x;
- l := p;
- end;
- end;
- if (l > 0) then
- begin
- t[i] := Copy(str, 1, (l - 1));
- Delete(str, 1, ((l + Length(d[m])) - 1));
- Inc(i);
- if (i > number) then
- Break;
- end else
- t[i] := Copy(str, 1, Length(str));
- until (l = 0);
- if ((i + 1) >= number) then
- Result := string(t[(number - 1)]);
- SetLength(t, 0);
- SetLength(d, 0);
- end else
- Result := '';
- end;
- var
- str: string;
- i: Integer;
- begin
- ClearDebug;
- str := ('Line1' + #13#10 + 'Line2' + #13 + 'Line3' + #13#10 + 'Line4' + #10 + 'Line5' + #13#10 + 'TEST!');
- for i := 1 to 5 do
- WriteLn(Line(str, i));
- end.[/simba]
- [/spoiler]
- [spoiler="Lines"]
- [simba]{==============================================================================]
- Explanation: Returns the lines of str as TStringArray.
- [==============================================================================}
- function Lines(str: string): TStringArray;
- var
- p, h, i, x, o, m, l, y, z: Integer;
- d: TStringArray;
- begin
- d := [#13#10, #13, #10];
- h := High(d);
- if ((h > -1) and (str <> '')) then
- begin
- o := 1;
- SetLength(Result, Length(str));
- repeat
- l := 0;
- for x := 0 to h do
- begin
- p := Pos(d[x], str);
- if (p < 1) then
- begin
- z := High(d);
- if ((x <= z) and (x > -1)) then
- begin
- for y := x to (z - 1) do
- d[y] := d[(y + 1)];
- SetLength(d, z);
- end;
- Dec(x);
- Dec(h);
- end else
- if ((l = 0) or (p < l)) then
- begin
- m := x;
- l := p;
- end;
- end;
- if (l > 0) then
- begin
- Result[i] := Copy(str, 1, (l - 1));
- Delete(str, 1, ((l + Length(d[m])) - 1));
- Inc(i);
- end else
- Result[i] := Copy(str, 1, Length(str));
- until (l = 0);
- SetLength(Result, (i + 1));
- end else
- Result := [string(str)];
- end;
- var
- str: string;
- begin
- str := ('Line1' + #13#10 + 'Line2' + #13 + 'Line3' + #13#10 + 'Line4' + #10 + 'Line5');
- WriteLn(ToStr(Lines(str)));
- end.[/simba]
- [/spoiler]
- [spoiler="LinePositions"]
- [simba]function LinePositions(str: string): TIntegerArray;
- var
- s: TStringArray;
- c, i, l, h, p, m, t, o, r: Integer;
- begin
- if (str <> '') then
- begin
- r := 1;
- t := (Length(str) + 1);
- SetLength(Result, t);
- Result[c] := r;
- Inc(c);
- l := 1;
- s := [#13#10, #13, #10];
- h := High(s);
- repeat
- m := t;
- for i := 0 to h do
- begin
- p := PosEx(s[i], str, r);
- if ((p > 0) and (p < m)) then
- begin
- m := p;
- o := Length(s[i]);
- end;
- end;
- if (m < t) then
- begin
- r := (m + o);
- Result[c] := r;
- Inc(c);
- Inc(l);
- end;
- until (m = t);
- SetLength(Result, c);
- end else
- Result := [1];
- end;
- var
- i, h: Integer;
- test: string;
- positions: TIntegerArray;
- begin
- ClearDebug;
- test := 'Line0' + #10 +
- 'Line1' + #13#10 +
- 'Line2' + #13 +
- 'Line3' + #10 +
- 'Line4' + #13 +
- 'Line5' + #13#10 +
- 'Line6' + #10 +
- 'Line7' + #13#10 +
- 'Line8' + #13 +
- 'Line9';
- positions := LinePositions(test);
- h := High(positions);
- for i := 0 to h do
- WriteLn(Copy(test, positions[i], 5));
- end.[/simba]
- [/spoiler]
- [spoiler="LineCount"]
- [simba]function LineCount(str: string): Integer;
- var
- h, l, p, o, i, t, x, y: Integer;
- s: TStringArray;
- begin
- Result := 1;
- if (str <> '') then
- begin
- s := [#13#10, #13, #10];
- h := High(s);
- o := 1;
- repeat
- p := 0;
- for x := 0 to h do
- begin
- t := PosEx(s[x], str, (l + o));
- if (t < 1) then
- begin
- for y := x to (h - 1) do
- s[y] := s[(y + 1)];
- SetLength(s, h);
- Dec(x);
- Dec(h);
- end else
- if ((p = 0) or (t < p)) then
- begin
- p := t;
- i := x;
- end;
- end;
- if (p > 0) then
- begin
- Inc(Result);
- l := p;
- o := Length(s[i]);
- end;
- until (p <= 0);
- SetLength(s, 0);
- end;
- end;
- var
- c: Integer;
- test: string;
- begin
- ClearDebug;
- test := 'Line0' + #10 +
- 'Line1' + #13#10 +
- 'Line2' + #13 +
- 'Line3' + #10 +
- 'Line4' + #13 +
- 'Line5' + #13#10 +
- 'Line6' + #10 +
- 'Line7' + #13#10 +
- 'Line8' + #13 +
- 'Line9';
- c := LineCount(test);
- if (c > 1) then
- WriteLn('Variable "test" contains ' + IntToStr(c) + ' lines!')
- else
- WriteLn('Variable "test" contains ' + IntToStr(c) + ' line!');
- end.[/simba]
- [/spoiler]
- [spoiler="LineByPos"]
- [simba]// Returns line from str by position. Results as 0 with invalid position (<1)
- function LineByPos(str: string; position: Integer): Integer;
- var
- s: TStringArray;
- i, n, o: Integer;
- begin
- if (position > 0) then
- begin
- Result := 1;
- str := Copy(str, 1, position);
- s := [#13#10, #13, #10];
- for i := 0 to High(s) do
- begin
- o := Length(str);
- {$IFNDEF Lape}
- str := ReplaceWrap(str, s[i], '', [rfReplaceAll]);
- {$ELSE}
- str := Replace(str, s[i], '', [rfReplaceAll]);
- {$ENDIF}
- n := ((o - Length(str)) div Length(s[i]));
- IncEx(Result, n);
- end;
- end else
- Result := 0;
- end;
- var
- i: Integer;
- test: string;
- begin
- ClearDebug;
- test := 'Line1' + #13#10 +
- 'Line2' + #13 +
- 'Line3' + #10 +
- 'Line4' + #13 +
- 'Line5' + #13#10 +
- 'Line6' + #10 +
- 'Line7' + #13#10 +
- 'Line8' + #13 +
- 'Line9';
- for i := 0 to 10 do
- WriteLn(LineByPos(test, Pos('Line' + IntToStr(i), test)));
- end.[/simba]
- [/spoiler]
- [spoiler="PosByLine"]
- [simba]function PosByLine(str: string; line: Integer): Integer;
- var
- s: TStringArray;
- i, l, h, p, m, t, o: Integer;
- begin
- if (line > 0) then
- begin
- Result := 1;
- t := (Length(str) + 1);
- if ((t > 1) and (line > 1)) then
- begin
- l := 1;
- s := [#13#10, #13, #10];
- h := High(s);
- repeat
- m := t;
- for i := 0 to h do
- begin
- p := PosEx(s[i], str, Result);
- if ((p > 0) and (p < m)) then
- begin
- m := p;
- o := Length(s[i]);
- end;
- end;
- if (m < t) then
- begin
- Result := (m + o);
- Inc(l);
- end;
- until ((m = t) or (l >= line));
- if (l < line) then
- Result := 0;
- end;
- end else
- Result := 0;
- end;
- var
- p, n13, n10, m, i: Integer;
- test: string;
- begin
- ClearDebug;
- test := 'Line1' + #13#10 +
- 'Line2' + #13 +
- 'Line3' + #10 +
- 'Line4' + #13 +
- 'Line5' + #13#10 +
- 'Line6' + #10 +
- 'Line7' + #13#10 +
- 'Line8' + #13 +
- 'Line9';
- for i := 1 to 9 do
- begin
- p := PosByLine(test, i);
- n13 := PosEx(#13, test, p);
- if (n13 = 0) then
- n13 := Length(test);
- n10 := PosEx(#10, test, p);
- if (n10 = 0) then
- n10 := Length(test);
- m := Min(n13, n10);
- WriteLn('Line ' + IntToStr(i) + ': "' + Copy(test, p, ((m - p) + 1)) + '"');
- end;
- end.[/simba]
- [/spoiler]
- [spoiler="Indentation"]
- [simba]const
- TEXT = 'This' + #13#10 +
- ' is' + #13#10 +
- ' test' + #13#10 +
- ' for' + #13#10 +
- ' Indent()' + #13#10 +
- ' function' + #13#10 +
- '!';
- INDENT = -1; // Indent TEXT with spaces? Value can be positive (indent), 0 (no change) or negative (unindent)
- {==============================================================================]
- Explanation: Indents str with spaces (just like the feature in SCAR, Ctrl+Shift+[I/U]).
- shift is the amount of spaces. It can be positive value (indent), 0 (no change) or negative value (unindent).
- [==============================================================================}
- procedure Indentation(var str: string; shift: Integer);
- var
- d, tmp, nl: TStringArray;
- p, h, i, x, o, m, l, y, z: Integer;
- s: string;
- begin
- if ((str <> '') and (shift <> 0)) then
- begin
- d := [#13#10, #13, #10];
- h := High(d);
- o := 1;
- SetLength(tmp, Length(str));
- SetLength(nl, Length(str));
- repeat
- l := 0;
- for x := 0 to h do
- begin
- p := Pos(d[x], str);
- if (p < 1) then
- begin
- z := High(d);
- if ((x <= z) and (x > -1)) then
- begin
- for y := x to (z - 1) do
- d[y] := d[(y + 1)];
- SetLength(d, z);
- end;
- Dec(x);
- Dec(h);
- end else
- if ((l = 0) or (p < l)) then
- begin
- m := x;
- l := p;
- end;
- end;
- if (l > 0) then
- begin
- tmp[i] := Copy(str, 1, (l - 1));
- nl[i] := string(d[m]);
- Delete(str, 1, ((l + Length(d[m])) - 1));
- Inc(i);
- end else
- tmp[i] := Copy(str, 1, Length(str));
- until (l = 0);
- str := '';
- SetLength(tmp, (i + 1));
- SetLength(nl, i);
- if (shift > 0) then
- begin
- s := StringOfChar(' ', shift);
- for x := 0 to i do
- begin
- str := (str + (s + tmp[x]));
- if (x < i) then
- str := (str + nl[x]);
- end;
- end else
- begin
- {$IFNDEF Lape}
- shift := iAbs(shift);
- {$ELSE}
- shift := Abs(shift);
- {$ENDIF}
- for x := 0 to i do
- begin
- y := 0;
- l := Length(tmp[x]);
- while ((y < shift) and (y < l) and (tmp[x][(y + 1)] = ' ')) do
- Inc(y);
- if (y > 0) then
- Delete(tmp[x], 1, y);
- str := (str + tmp[x]);
- if (x < i) then
- str := (str + nl[x]);
- end;
- end;
- SetLength(tmp, 0);
- SetLength(nl, 0);
- end;
- end;
- var
- str: string;
- begin
- ClearDebug;
- str := TEXT;
- Indentation(str, INDENT);
- WriteLn(str);
- end.[/simba]
- [/spoiler]
- [spoiler="SplitEx"]
- [simba]{==============================================================================]
- Explanation: Splits string (str) from position stores the 2 halves to a and b variables. If includeCenter is set as true, char from position will be included to b variable.
- [==============================================================================}
- procedure SplitEx(str: string; var a, b: string; position: Integer; includeCenter: Boolean);
- var
- l, e: Integer;
- begin
- a := '';
- b := '';
- l := Length(str);
- if (l > 0) then
- begin
- if (position < 1) then
- position := 1;
- a := Copy(str, 1, (position - 1));
- if not includeCenter then
- e := 1;
- IncEx(position, e);
- b := Copy(str, position, ((l - position) + 1));
- end;
- end;
- var
- str, j, l: string;
- begin
- ClearDebug;
- str := 'Jani:labo';
- SplitEx(str, j, l, Pos(':', str), True);
- WriteLn('"' + j + '", "' + l + '"');
- SplitEx(str, j, l, Pos(':', str), False);
- WriteLn('"' + j + '", "' + l + '"');
- end.[/simba]
- [/spoiler]
- [spoiler="Split"]
- [simba]{==============================================================================]
- Explanation: Splits string (str) from position stores the 2 halves to a and b variables.
- NOTE: Does not include the char from position!
- [==============================================================================}
- procedure Split(str: string; var a, b: string; position: Integer);
- var
- l: Integer;
- begin
- a := '';
- b := '';
- l := Length(str);
- if (l > 0) then
- begin
- if (position < 1) then
- position := 1;
- a := Copy(str, 1, (position - 1));
- IncEx(position, 1);
- b := Copy(str, position, ((l - position) + 1));
- end;
- end;
- var
- str, j, l: string;
- begin
- ClearDebug;
- str := 'Jani:labo';
- Split(str, j, l, Pos(':', str));
- WriteLn('"' + j + '", "' + l + '"');
- end.[/simba]
- [/spoiler]
- [spoiler="Quotatation"]
- [simba]function Quotatation(str: string): string;
- begin
- {$IFNDEF Lape}
- Result := ('''' + ReplaceWrap(str, '''', '''''', [rfReplaceAll]) + '''');
- {$ELSE}
- Result := ('''' + Replace(str, '''', '''''', [rfReplaceAll]) + '''');
- {$ENDIF}
- end;
- var
- text: string;
- begin
- ClearDebug;
- text := 'It''s time to test Quotatation';
- WriteLn(text);
- WriteLn(Quotatation(text));
- end.[/simba]
- [/spoiler]
- [spoiler="PregQuote"]
- [simba]function PregQuote(str: string): string;
- var
- l, i: Integer;
- r: string;
- begin
- Result := str;
- r := '.\+*?[^]$(){}=!<>|:-';
- l := Length(str);
- if (l > 0) then
- for i := l downto 1 do
- if (Pos(str[i], r) > 0) then
- Insert('\', Result, i);
- end;
- var
- str: string;
- begin
- ClearDebug;
- str := 'C:\Simba\';
- writeln(PregQuote(str));
- end.[/simba]
- [/spoiler]
- [spoiler="PregQuoteEx"]
- [simba]function PregQuoteEx(str, regexChars: string): string;
- var
- l, i: Integer;
- begin
- Result := str;
- l := Length(str);
- if ((l > 0) and (regexChars <> '')) then
- for i := l downto 1 do
- if (Pos(str[i], regexChars) > 0) then
- Insert('\', Result, i);
- end;
- var
- str: string;
- begin
- ClearDebug;
- str := 'C:\Simba\';
- writeln(PregQuoteEx(str, '.\+*?[^]$(){}=!<>|:-'));
- end.[/simba]
- [/spoiler]
- [spoiler="Find"]
- [simba]const
- TEXT = 'TestesTESTTestest test testest1 2testest test3 TEST Test Test.' + #13#10 + 'TEST!' + #13#10 + 'TeSt';
- FIND_STR = 'test';
- {==============================================================================]
- Explanation: Important types for Find() function! Contains the string matching methods.
- [==============================================================================}
- type
- TMatchMethod = (mmAll, mmIgnoreCase, mmOverlap, mmWholeWords, mmStrictWW, mmGreedyRegex);
- TMatchMethods = set of TMatchMethod;
- var
- methods: array of TMatchMethods;
- procedure SetupMethods;
- begin
- SetLength(methods, 6);
- methods[0] := [];
- methods[1] := [mmIgnoreCase];
- methods[2] := [mmIgnoreCase, mmAll];
- methods[3] := [mmIgnoreCase, mmAll, mmOverlap];
- methods[4] := [mmIgnoreCase, mmAll, mmOverlap, mmWholeWords];
- methods[5] := [mmIgnoreCase, mmAll, mmOverlap, mmWholeWords, mmStrictWW];
- end;
- {==============================================================================]
- Explanation: Returns all the positions of found/matching strings (findStr) in text.
- Uses a set of TMatchMethod (methods) for string matching.
- Contains field for offset.
- [==============================================================================}
- function Find(text, findStr: string; methods: TMatchMethods; offset: Integer): TIntegerArray;
- var
- sb, sa: string;
- r, i, l, f, o, p, d, x, y, abL, abR, abX, abP, spA, spB, spH, spL, spI, spR, spD: Integer;
- re: TRegExp;
- ma, mb, a, s, ol: Boolean;
- c: TIntegerArray;
- t: T2DIntegerArray;
- begin
- l := Length(text);
- f := Length(findStr);
- if ((l > 0) and (f > 0) and (offset <= (l - f))) then
- begin
- if (offset < 1) then
- offset := 1;
- for i := f downto 1 do
- if (Pos(findStr[i], '.\+*?[^]$(){}=!<>|:-') > 0) then
- Insert('\', findStr, i);
- SetLength(Result, l);
- re := TRegExp.Create;
- re.InputString := text;
- re.Expression := findStr;
- if (mmIgnoreCase in methods) then
- re.ModifierI := True;
- re.ModifierM := True;
- a := (mmAll in methods);
- re.ModifierG := (mmGreedyRegex in methods);
- ol := (mmOverlap in methods);
- if not ol then
- o := (Length(findStr) - 1);
- Inc(o);
- p := offset;
- while re.ExecPos(p) do
- begin
- Result[r] := re.MatchPos[0];
- p := (Result[r] + o);
- Inc(r);
- end;
- p := Offset;
- re.Free;
- SetLength(Result, r);
- if ((r > 0) and (mmWholeWords in methods)) then
- begin
- s := (mmStrictWW in methods);
- if not s then
- c := [65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, // A-Z
- 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, // a-z
- 48, 49, 50, 51, 52, 53, 54, 55, 56, 57]; // 0-9
- if ol then
- begin
- spH := High(Result);
- if (spH > -1) then
- begin
- SetLength(t, (spH + 1));
- t[0] := [Integer(Result[0])];
- if (spH > 0) then
- begin
- spR := 1;
- for spI := 1 to spH do
- begin
- for spA := 0 to (spR - 1) do
- begin
- spL := Length(t[spA]);
- for spB := 0 to (spL - 1) do
- begin
- spD := IAbs(Result[spI] - t[spA][spB]);
- if (spD <= f) then
- begin
- SetLength(t[spA], (spL + 1));
- t[spA][spL] := Integer(Result[spI]);
- Break;
- end;
- end;
- if (spB < spL) then
- Break;
- end;
- if (spA >= spR) then
- begin
- t[spR] := [Integer(Result[spI])];
- Inc(spR);
- end;
- end;
- end;
- SetLength(t, spR);
- spH := High(t);
- for spI := spH downto 0 do
- begin
- spB := Low(t[spI]);
- spA := High(t[spI]);
- abX := 1;
- abP := t[spI][spB];
- abL := Length(text);
- if ((abL > 0) and (abP > 1)) then
- begin
- if ((abP - abX) < 1) then
- abX := ((abP - abX) + (abX - 1));
- if (abP > (abL + 1)) then
- begin
- abR := ((abP - abL) - 1);
- abX := (abX - abR);
- end;
- sb := Copy(text, ((abP - abX) - abR), abX);
- end else
- sb := '';
- abX := 1;
- abP := (t[spI][spA] + f);
- abL := Length(text);
- if ((abL > 0) and (abP <= abL)) then
- begin
- if (abP < 1) then
- begin
- abX := (abX - iAbs(abP - 1));
- abP := 1;
- end;
- if ((abX > 0) and ((abP + abX) > abL)) then
- abX := (abX - (((abP + abX) - abL) - 1));
- sa := Copy(text, abP, abX);
- end else
- sa := '';
- if s then
- begin
- mb := (sb = '');
- if not mb then
- mb := ((sb = ' ') or (sb = #13#10) or (sb = #13) or (sb = #10));
- ma := (sa = '');
- if not ma then
- ma := ((sa = ' ') or (sa = #13#10) or (sa = #13) or (sa = #10));
- end else
- begin
- mb := (sb = '');
- if not mb then
- mb := not InIntArray(c, Ord(sb[1]));
- ma := (sa = '');
- if not ma then
- ma := not InIntArray(c, Ord(sa[1]));
- end;
- if not (mb and ma) then
- begin
- for spD := spI to (spH - 1) do
- t[spD] := t[(spD + 1)];
- SetLength(t, spH);
- Dec(spH);
- end;
- end;
- spH := High(t);
- if (spH > -1) then
- begin
- for spI := 0 to spH do
- IncEx(spR, (High(t[spI]) + 1));
- SetLength(Result, spR);
- spR := 0;
- for spI := 0 to spH do
- begin
- spL := High(t[spI]);
- for spA := 0 to spL do
- begin
- Result[spR] := Integer(t[spI][spA]);
- Inc(spR);
- end;
- end;
- SetLength(Result, spR);
- end else
- SetLength(Result, 0);
- end else
- r := 0;
- end else
- begin
- for x := (r - 1) downto 0 do
- begin
- abX := 1;
- abP := Result[x];
- abL := Length(text);
- if ((abL > 0) and (abP > 1)) then
- begin
- if ((abP - abX) < 1) then
- abX := ((abP - abX) + (abX - 1));
- if (abP > (abL + 1)) then
- begin
- abR := ((abP - abL) - 1);
- abX := (abX - abR);
- end;
- sb := Copy(text, ((abP - abX) - abR), abX);
- end else
- sb := '';
- abX := 1;
- abP := (Result[x] + f);
- abL := Length(text);
- if ((abL > 0) and (abP <= abL)) then
- begin
- if (abP < 1) then
- begin
- abX := (abX - iAbs(abP - 1));
- abP := 1;
- end;
- if ((abX > 0) and ((abP + abX) > abL)) then
- abX := (abX - (((abP + abX) - abL) - 1));
- sa := Copy(text, abP, abX);
- end else
- sa := '';
- if s then
- begin
- mb := (sb = '');
- if not mb then
- mb := ((sb = ' ') or (sb = #13#10) or (sb = #13) or (sb = #10));
- ma := (sa = '');
- if not ma then
- ma := ((sa = ' ') or (sa = #13#10) or (sa = #13) or (sa = #10));
- end else
- begin
- mb := (sb = '');
- if not mb then
- mb := not InIntArray(c, Ord(sb[1]));
- ma := (sa = '');
- if not ma then
- ma := not InIntArray(c, Ord(sa[1]));
- end;
- if not (mb and ma) then
- begin
- y := (r - 1);
- for d := x to (y - 1) do
- Result[d] := Result[(d + 1)];
- SetLength(Result, y);
- Dec(r);
- end;
- end;
- end;
- end;
- if (not a and (r > 0)) then
- SetLength(Result, 1);
- end else
- SetLength(Result, 0);
- end;
- var
- h, i: Integer;
- begin
- ClearDebug;
- SetupMethods;
- h := High(methods);
- for i := 0 to h do
- WriteLn('Matching positions of FIND_STR found in TEXT using methods[' + IntToStr(i) + ']: ' + ToStr(Find(TEXT, FIND_STR, methods[i], 1)));
- end.[/simba]
- [/spoiler]
- [spoiler="FindEx"]
- [simba]const
- TEXT = 'TestesTESTTestest wwTest. -test! testest1 6test9 2testest test3 TEST Test test.' + #13#10 + ' TEST! ' + #13#10 + 'TeSt';
- FIND_STR = 'test((\!)|(\.))';
- USE_REGEX = True;
- {==============================================================================]
- Explanation: Important types for Find() function! Contains the string matching methods.
- [==============================================================================}
- type
- TMatchMethod = (mmAll, mmIgnoreCase, mmOverlap, mmWholeWords, mmStrictWW, mmGreedyRegex);
- TMatchMethods = set of TMatchMethod;
- TRegexMatch = record
- position, size: Integer;
- text: string;
- end;
- TRegexMatchArray = array of TRegexMatch;
- T2DRegexMatchArray = array of TRegexMatchArray;
- var
- methods: array of TMatchMethods;
- procedure SetupMethods;
- begin
- SetLength(methods, 6);
- methods[0] := [];
- methods[1] := [mmIgnoreCase];
- methods[2] := [mmIgnoreCase, mmAll];
- methods[3] := [mmIgnoreCase, mmAll, mmOverlap];
- methods[4] := [mmIgnoreCase, mmAll, mmOverlap, mmWholeWords];
- methods[5] := [mmIgnoreCase, mmAll, mmOverlap, mmWholeWords, mmStrictWW];
- end;
- {==============================================================================]
- Explanation: Returns all the positions of found/matching strings (findStr) in text.
- Uses a set of TMatchMethod (methods) for string matching.
- Contains field for offset.
- If regex field is set as true, then this function searches for the regex you use.
- [==============================================================================}
- function FindEx(text, findStr: string; methods: TMatchMethods; offset: Integer; regex: Boolean): TIntegerArray;
- var
- rmArr: TRegexMatchArray;
- rmArr2D: T2DRegexMatchArray;
- sb, sa: string;
- r, i, l, f, p, d, o, x, y, abL, abR, abX, abP, spA, spB, spH, spL, spI, spR, spD: Integer;
- re: TRegExp;
- ma, mb, a, s, ol: Boolean;
- c: TIntegerArray;
- t: T2DIntegerArray;
- begin
- l := Length(text);
- f := Length(findStr);
- if ((l > 0) and (f > 0) and (offset <= l)) then
- begin
- if (offset < 1) then
- offset := 1;
- if not regex then
- begin
- for i := f downto 1 do
- if (Pos(findStr[i], '.\+*?[^]$(){}=!<>|:-') > 0) then
- Insert('\', findStr, i);
- SetLength(Result, l);
- re := TRegExp.Create;
- re.InputString := text;
- re.Expression := findStr;
- if (mmIgnoreCase in methods) then
- re.ModifierI := True;
- re.ModifierM := True;
- a := (mmAll in methods);
- re.ModifierG := (mmGreedyRegex in methods);
- ol := (mmOverlap in methods);
- if not ol then
- o := (Length(findStr) - 1);
- Inc(o);
- p := offset;
- while re.ExecPos(p) do
- begin
- Result[r] := re.MatchPos[0];
- p := (Result[r] + o);
- Inc(r);
- end;
- p := Offset;
- re.Free;
- SetLength(Result, r);
- if ((r > 0) and (mmWholeWords in methods)) then
- begin
- s := (mmStrictWW in methods);
- if not s then
- c := [65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, // A-Z
- 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, // a-z
- 48, 49, 50, 51, 52, 53, 54, 55, 56, 57]; // 0-9
- if ol then
- begin
- spH := High(Result);
- if (spH > -1) then
- begin
- SetLength(t, (spH + 1));
- t[0] := [Integer(Result[0])];
- if (spH > 0) then
- begin
- spR := 1;
- for spI := 1 to spH do
- begin
- for spA := 0 to (spR - 1) do
- begin
- spL := Length(t[spA]);
- for spB := 0 to (spL - 1) do
- begin
- spD := IAbs(Result[spI] - t[spA][spB]);
- if (spD <= f) then
- begin
- SetLength(t[spA], (spL + 1));
- t[spA][spL] := Integer(Result[spI]);
- Break;
- end;
- end;
- if (spB < spL) then
- Break;
- end;
- if (spA >= spR) then
- begin
- t[spR] := [Integer(Result[spI])];
- Inc(spR);
- end;
- end;
- end;
- SetLength(t, spR);
- spH := High(t);
- for spI := spH downto 0 do
- begin
- spB := Low(t[spI]);
- spA := High(t[spI]);
- abX := 1;
- abP := t[spI][spB];
- abL := Length(text);
- if ((abL > 0) and (abP > 1)) then
- begin
- if ((abP - abX) < 1) then
- abX := ((abP - abX) + (abX - 1));
- if (abP > (abL + 1)) then
- begin
- abR := ((abP - abL) - 1);
- abX := (abX - abR);
- end;
- sb := Copy(text, ((abP - abX) - abR), abX);
- end else
- sb := '';
- abX := 1;
- abP := (t[spI][spA] + f);
- abL := Length(text);
- if ((abL > 0) and (abP <= abL)) then
- begin
- if (abP < 1) then
- begin
- abX := (abX - iAbs(abP - 1));
- abP := 1;
- end;
- if ((abX > 0) and ((abP + abX) > abL)) then
- abX := (abX - (((abP + abX) - abL) - 1));
- sa := Copy(text, abP, abX);
- end else
- sa := '';
- if s then
- begin
- mb := (sb = '');
- if not mb then
- mb := ((sb = ' ') or (sb = #13#10) or (sb = #13) or (sb = #10));
- ma := (sa = '');
- if not ma then
- ma := ((sa = ' ') or (sa = #13#10) or (sa = #13) or (sa = #10));
- end else
- begin
- mb := (sb = '');
- if not mb then
- mb := not InIntArray(c, Ord(sb[1]));
- ma := (sa = '');
- if not ma then
- ma := not InIntArray(c, Ord(sa[1]));
- end;
- if not (mb and ma) then
- begin
- for spD := spI to (spH - 1) do
- t[spD] := t[(spD + 1)];
- SetLength(t, spH);
- Dec(spH);
- end;
- end;
- spH := High(t);
- if (spH > -1) then
- begin
- for spI := 0 to spH do
- IncEx(spR, (High(t[spI]) + 1));
- SetLength(Result, spR);
- spR := 0;
- for spI := 0 to spH do
- begin
- spL := High(t[spI]);
- for spA := 0 to spL do
- begin
- Result[spR] := Integer(t[spI][spA]);
- Inc(spR);
- end;
- end;
- SetLength(Result, spR);
- end else
- SetLength(Result, 0);
- end else
- r := 0;
- end else
- begin
- for x := (r - 1) downto 0 do
- begin
- abX := 1;
- abP := Result[x];
- abL := Length(text);
- if ((abL > 0) and (abP > 1)) then
- begin
- if ((abP - abX) < 1) then
- abX := ((abP - abX) + (abX - 1));
- if (abP > (abL + 1)) then
- begin
- abR := ((abP - abL) - 1);
- abX := (abX - abR);
- end;
- sb := Copy(text, ((abP - abX) - abR), abX);
- end else
- sb := '';
- abX := 1;
- abP := (Result[x] + f);
- abL := Length(text);
- if ((abL > 0) and (abP <= abL)) then
- begin
- if (abP < 1) then
- begin
- abX := (abX - iAbs(abP - 1));
- abP := 1;
- end;
- if ((abX > 0) and ((abP + abX) > abL)) then
- abX := (abX - (((abP + abX) - abL) - 1));
- sa := Copy(text, abP, abX);
- end else
- sa := '';
- if s then
- begin
- mb := (sb = '');
- if not mb then
- mb := ((sb = ' ') or (sb = #13#10) or (sb = #13) or (sb = #10));
- ma := (sa = '');
- if not ma then
- ma := ((sa = ' ') or (sa = #13#10) or (sa = #13) or (sa = #10));
- end else
- begin
- mb := (sb = '');
- if not mb then
- mb := not InIntArray(c, Ord(sb[1]));
- ma := (sa = '');
- if not ma then
- ma := not InIntArray(c, Ord(sa[1]));
- end;
- if not (mb and ma) then
- begin
- y := (r - 1);
- for d := x to (y - 1) do
- Result[d] := Result[(d + 1)];
- SetLength(Result, y);
- Dec(r);
- end;
- end;
- end;
- end;
- if (not a and (r > 0)) then
- SetLength(Result, 1);
- end else
- begin
- SetLength(rmArr, l);
- re := TRegExp.Create;
- re.InputString := text;
- re.Expression := findStr;
- if (mmIgnoreCase in methods) then
- re.ModifierI := True;
- re.ModifierM := True;
- a := (mmAll in methods);
- re.ModifierG := (mmGreedyRegex in methods);
- ol := (mmOverlap in methods);
- p := offset;
- while re.ExecPos(p) do
- begin
- rmArr[r].position := re.MatchPos[0];
- rmArr[r].text := re.Match[0];
- rmArr[r].size := re.MatchLen[0];
- if ol then
- p := (rmArr[r].position + 1)
- else
- p := (rmArr[r].position + rmArr[r].size);
- Inc(r);
- end;
- p := Offset;
- re.Free;
- SetLength(rmArr, r);
- if ((r > 0) and (mmWholeWords in methods)) then
- begin
- s := (mmStrictWW in methods);
- if not s then
- c := [65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, // A-Z
- 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, // a-z
- 48, 49, 50, 51, 52, 53, 54, 55, 56, 57]; // 0-9
- if ol then
- begin
- spH := High(rmArr);
- if (spH > -1) then
- begin
- SetLength(rmArr2D, (spH + 1));
- rmArr2D[0] := [TRegexMatch(rmArr[0])];
- if (spH > 0) then
- begin
- spR := 1;
- for spI := 1 to spH do
- begin
- for spA := 0 to (spR - 1) do
- begin
- spL := Length(rmArr2D[spA]);
- for spB := 0 to (spL - 1) do
- begin
- spD := IAbs(rmArr[spI].position - rmArr2D[spA][spB].position);
- if (spD <= rmArr2D[spA][spB].size) then
- begin
- SetLength(rmArr2D[spA], (spL + 1));
- rmArr2D[spA][spL] := TRegexMatch(rmArr[spI]);
- Break;
- end;
- end;
- if (spB < spL) then
- Break;
- end;
- if (spA >= spR) then
- begin
- rmArr2D[spR] := [TRegexMatch(rmArr[spI])];
- Inc(spR);
- end;
- end;
- end;
- SetLength(rmArr2D, spR);
- spH := High(rmArr2D);
- for spI := spH downto 0 do
- begin
- spB := Low(rmArr2D[spI]);
- spA := High(rmArr2D[spI]);
- abX := 1;
- abP := rmArr2D[spI][spB].position;
- abL := Length(text);
- if ((abL > 0) and (abP > 1)) then
- begin
- if ((abP - abX) < 1) then
- abX := ((abP - abX) + (abX - 1));
- if (abP > (abL + 1)) then
- begin
- abR := ((abP - abL) - 1);
- abX := (abX - abR);
- end;
- sb := Copy(text, ((abP - abX) - abR), abX);
- end else
- sb := '';
- abX := 1;
- abP := (rmArr2D[spI][spA].position + rmArr2D[spI][spA].size);
- abL := Length(text);
- if ((abL > 0) and (abP <= abL)) then
- begin
- if (abP < 1) then
- begin
- abX := (abX - iAbs(abP - 1));
- abP := 1;
- end;
- if ((abX > 0) and ((abP + abX) > abL)) then
- abX := (abX - (((abP + abX) - abL) - 1));
- sa := Copy(text, abP, abX);
- end else
- sa := '';
- if s then
- begin
- mb := (sb = '');
- if not mb then
- mb := ((sb = ' ') or (sb = #13#10) or (sb = #13) or (sb = #10));
- ma := (sa = '');
- if not ma then
- ma := ((sa = ' ') or (sa = #13#10) or (sa = #13) or (sa = #10));
- end else
- begin
- mb := (sb = '');
- if not mb then
- mb := not InIntArray(c, Ord(sb[1]));
- ma := (sa = '');
- if not ma then
- ma := not InIntArray(c, Ord(sa[1]));
- end;
- if not (mb and ma) then
- begin
- for spD := spI to (spH - 1) do
- rmArr2D[spD] := rmArr2D[(spD + 1)];
- SetLength(rmArr2D, spH);
- Dec(spH);
- end;
- end;
- spH := High(rmArr2D);
- if (spH > -1) then
- begin
- for spI := 0 to spH do
- IncEx(spR, (High(rmArr2D[spI]) + 1));
- SetLength(rmArr, spR);
- spR := 0;
- for spI := 0 to spH do
- begin
- spL := High(rmArr2D[spI]);
- for spA := 0 to spL do
- begin
- rmArr[spR] := TRegexMatch(rmArr2D[spI][spA]);
- Inc(spR);
- end;
- end;
- SetLength(rmArr, spR);
- r := spR;
- end else
- SetLength(rmArr, 0);
- end else
- r := 0;
- end else
- begin
- for x := (r - 1) downto 0 do
- begin
- abX := 1;
- abP := rmArr[x].position;
- abL := Length(text);
- if ((abL > 0) and (abP > 1)) then
- begin
- if ((abP - abX) < 1) then
- abX := ((abP - abX) + (abX - 1));
- if (abP > (abL + 1)) then
- begin
- abR := ((abP - abL) - 1);
- abX := (abX - abR);
- end;
- sb := Copy(text, ((abP - abX) - abR), abX);
- end else
- sb := '';
- abX := 1;
- abP := (rmArr[x].position + rmArr[x].size);
- abL := Length(text);
- if ((abL > 0) and (abP <= abL)) then
- begin
- if (abP < 1) then
- begin
- abX := (abX - iAbs(abP - 1));
- abP := 1;
- end;
- if ((abX > 0) and ((abP + abX) > abL)) then
- abX := (abX - (((abP + abX) - abL) - 1));
- sa := Copy(text, abP, abX);
- end else
- sa := '';
- if s then
- begin
- mb := (sb = '');
- if not mb then
- mb := ((sb = ' ') or (sb = #13#10) or (sb = #13) or (sb = #10));
- ma := (sa = '');
- if not ma then
- ma := ((sa = ' ') or (sa = #13#10) or (sa = #13) or (sa = #10));
- end else
- begin
- mb := (sb = '');
- if not mb then
- mb := not InIntArray(c, Ord(sb[1]));
- ma := (sa = '');
- if not ma then
- ma := not InIntArray(c, Ord(sa[1]));
- end;
- if not (mb and ma) then
- begin
- y := (r - 1);
- for d := x to (y - 1) do
- rmArr[d] := rmArr[(d + 1)];
- SetLength(rmArr, y);
- Dec(r);
- end;
- end;
- end;
- end;
- r := Length(rmArr);
- if (r > 0) then
- begin
- if not a then
- r := 1;
- SetLength(Result, r);
- for i := 0 to (r - 1) do
- Result[i] := rmArr[i].position;
- end else
- SetLength(Result, 0);
- end;
- end else
- SetLength(Result, 0);
- end;
- var
- h, i: Integer;
- begin
- ClearDebug;
- SetupMethods;
- h := High(methods);
- for i := 0 to h do
- WriteLn('Matching positions of FIND_STR found in TEXT using methods[' + IntToStr(i) + ']: ' + ToStr(FindEx(TEXT, FIND_STR, methods[i], 1, USE_REGEX)));
- end.[/simba]
- [/spoiler]
- [spoiler="StringToHex"]
- [simba]// by CynicRus
- function StringToHex(s: string): string;
- var
- i: Integer;
- begin
- Result := '';
- for i := 1 to Length(s) do
- Result := Result + IntToHex(ord(s[i]));
- end;
- begin
- ClearDebug;
- WriteLn(StringToHex('That my string!'));
- end.[/simba]
- [/spoiler]
- [spoiler="HexToString"]
- [simba]// by CynicRus
- function HexToString(s: string): string;
- var
- i: Integer;
- begin
- Result := '';
- for i := 1 to Length(s) div 2 do
- try
- Result := Result + chr(StrToInt('$' + Copy(s, i*2-1, 2)));
- except
- Result := Result + '?';
- end;
- end;
- begin
- ClearDebug;
- WriteLn(HexToString('54686174206D7920737472696E6721'));
- end.[/simba]
- [/spoiler]
- [spoiler="WebCode"]
- [simba]// by CynicRus
- function WebCode(const Src: string): string;
- var
- i: Integer;
- begin
- Result := '';
- for i := 1 to Length(Src) do
- Result := (Result + '%' + IntToHex(Ord(Src[i])));
- end;
- begin
- ClearDebug;
- WriteLn(WebCode('that is string!'));
- end.[/simba]
- [/spoiler]
- [spoiler="WebDecode"]
- [simba]// by CynicRus
- function WebHexToInt(HexChar: Char): Integer;
- begin
- if (HexChar < '0') then
- Result := (Ord(HexChar) + 256 - Ord('0'))
- else
- if (HexChar <= Chr(Ord('A') - 1)) then
- Result := Ord(HexChar) - Ord('0')
- else
- if (HexChar <= Chr(Ord('a') - 1)) then
- Result := (Ord(HexChar) - Ord('A') + 10)
- else
- Result := (Ord(HexChar) - Ord('a') + 10);
- end;
- function WebDecode(const S: string): string;
- var
- i, idx, len, n_coded: Integer;
- begin
- len := 0;
- n_coded := 0;
- for i := 1 to Length(S) do
- if (n_coded >= 1) then
- begin
- n_coded := (n_coded + 1);
- if (n_coded >= 3) then
- n_coded := 0;
- end else
- begin
- len := (len + 1);
- if (S[i] = '%') then
- n_coded := 1;
- end;
- SetLength(Result, len);
- idx := 0;
- n_coded := 0;
- for i := 1 to Length(S) do
- if (n_coded >= 1) then
- begin
- n_coded := (n_coded + 1);
- if (n_coded >= 3) then
- begin
- Result[idx] := Chr((WebHexToInt(S[(i - 1)]) * 16 + WebHexToInt(S[i])) mod 256);
- n_coded := 0;
- end;
- end else
- begin
- idx := (idx + 1);
- if (S[i] = '%') then
- n_coded := 1;
- if (S[i] = '+') then
- Result[idx] := ' '
- else
- Result[idx] := S[i];
- end;
- end;
- begin
- ClearDebug;
- WriteLn(WebDecode('%74%68%61%74%20%69%73%20%73%74%72%69%6E%67%21'));
- end.[/simba]
- [/spoiler]
- [spoiler="ExplodeMulti"]
- [simba]{==============================================================================]
- Explanation: Explodes str with multiple separators/delimiters (d).
- The importance order for d items is from left to right (=>).
- So place the important ones first and then less important after those.
- [==============================================================================}
- function ExplodeMulti(d: TStringArray; str: string): TStringArray;
- var
- p, h, i, x, o, m, l, y, z: Integer;
- begin
- h := High(d);
- if ((h > -1) and (str <> '')) then
- begin
- o := 1;
- SetLength(Result, Length(str));
- repeat
- l := 0;
- for x := 0 to h do
- begin
- p := Pos(d[x], str);
- if (p < 1) then
- begin
- z := High(d);
- if ((x <= z) and (x > -1)) then
- begin
- for y := x to (z - 1) do
- d[y] := d[(y + 1)];
- SetLength(d, z);
- end;
- Dec(x);
- Dec(h);
- end else
- if ((l = 0) or (p < l)) then
- begin
- m := x;
- l := p;
- end;
- end;
- if (l > 0) then
- begin
- Result[i] := Copy(str, 1, (l - 1));
- Delete(str, 1, ((l + Length(d[m])) - 1));
- Inc(i);
- end else
- Result[i] := Copy(str, 1, Length(str));
- until (l = 0);
- SetLength(Result, (i + 1));
- end else
- Result := [string(str)];
- end;
- var
- str: string;
- begin
- str := ('Line1' + #13#10 + 'Line2' + #13 + 'Line3' + #13#10 + 'Line4' + #10 + 'Line5');
- WriteLn(ToStr(ExplodeMulti([#13#10, #13, #10], str)));
- end.[/simba]
- [/spoiler]
- [spoiler="PosAll"]
- [simba]function PosAll(s, str: string): TIntegerArray;
- var
- sL, strL, o, p, r: Integer;
- begin
- sL := Length(s);
- strL := Length(str);
- if (sL <= strL) then
- begin
- SetLength(Result, strL);
- repeat
- p := PosEx(s, str, (o + 1));
- if (p > 0) then
- begin
- Result[r] := p;
- o := p;
- Inc(r);
- end;
- until (p <= 0);
- end;
- SetLength(Result, r);
- end;
- var
- s, str: string;
- TIA: TIntegerArray;
- h, i: Integer;
- begin
- ClearDebug;
- str := '|||| PosAll() Test ||| *** ||| Should work pretty well. ||||';
- s := '||';
- TIA := PosAll(s, str);
- h := High(TIA);
- if (h < 0) then
- Exit;
- str := '"' + s + '" positions in str: ';
- for i := 0 to h do
- if (i < h) then
- str := str + IntToStr(TIA[i]) + ', '
- else
- str := str + IntToStr(TIA[i]) + '.';
- WriteLn(str);
- SetLength(TIA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="PosAllEx"]
- [simba]{==============================================================================]
- Explanation: Returns all the s positions in str.
- If overlap is set to true, strings can overlap.
- ('aa', 'baaaah', False) => [2,3,4]
- ('aa', 'baaaah', True) => [2,4]
- [==============================================================================}
- function PosAllEx(s, str: string; overlap: Boolean): TIntegerArray;
- var
- sL, strL, o, p, r: Integer;
- begin
- sL := Length(s);
- strL := Length(str);
- if (sL <= strL) then
- begin
- if not overlap then
- begin
- o := Length(s);
- p := (p - (o - 1));
- end else
- o := 1;
- SetLength(Result, ((strL div sL) + 1));
- repeat
- p := PosEx(s, str, (p + o));
- if (p > 0) then
- begin
- Result[r] := p;
- Inc(r);
- end;
- until (p <= 0);
- end;
- SetLength(Result, r);
- end;
- var
- s, str: string;
- begin
- ClearDebug;
- str := '|||| PosAllEx() Test ||| *** ||| Should work pretty well. ||||';
- s := '||';
- WriteLn('PosAll(s, str, True): ' + ToStr(PosAllEx('||', str, True)));
- WriteLn('PosAll(s, str, False): ' + ToStr(PosAllEx('||', str, False)));
- end.[/simba]
- [/spoiler]
- [spoiler="PosAllMulti"]
- [simba]{==============================================================================]
- Explanation: Returns all the positions by items from s array in str. Place s items in importance order (=>)
- If overlap is set to true, strings can overlap.
- (['aa'], 'baaaah', False) => [2,3,4]
- (['aa'], 'baaaah', True) => [2,4]
- [==============================================================================}
- function PosAllMulti(s: TStringArray; str: string; overlap: Boolean): TIntegerArray;
- var
- h, l, p, o, x, i, t, r, y, d: Integer;
- begin
- h := High(s);
- y := Length(str);
- if ((y > 0) and (h > -1)) then
- begin
- SetLength(Result, y);
- o := 1;
- repeat
- p := 0;
- for x := 0 to h do
- begin
- t := PosEx(s[x], str, (l + o));
- if (t < 1) then
- begin
- for d := x to (h - 1) do
- s[d] := s[(d + 1)];
- SetLength(s, h);
- Dec(x);
- Dec(h);
- end else
- if ((p = 0) or (t < p)) then
- begin
- p := t;
- i := x;
- end;
- end;
- if (p > 0) then
- begin
- Result[r] := p;
- Inc(r);
- l := p;
- if not overlap then
- o := Length(s[i]);
- end;
- until (p <= 0);
- end;
- SetLength(Result, r);
- end;
- var
- str: string;
- begin
- ClearDebug;
- str := 'HAHAHAH LOLOLOL!';
- WriteLn(ToStr(PosAllMulti(['HAH', 'LOL'], str, True)) + ' [overlap = ON]');
- WriteLn(ToStr(PosAllMulti(['HAH', 'LOL'], str, False)) + ' [overlap = OFF]');
- end.[/simba]
- [/spoiler]
- [spoiler="TraceStrsEx"]
- [simba]type
- TRange = record
- minimum, maximum: Integer;
- end;
- TRangeArray = array of TRange;
- TRegexModifier = (rm_G, rm_I, rm_M, rm_S, rm_X, rm_R);
- {==============================================================================]
- Explanation: Function for tracing findStr's from data.
- Contains also custom fields for regexTags and regexStrs;
- each tag performs a regex action, that is set to regexStrs, during the search.
- [==============================================================================}
- function TraceStrsEx(findStr: string; regexTags: array of Char; regexStrs: TStringArray; data: string; modifiers: set of TRegexModifier): TRangeArray;
- var
- re: TRegExp;
- p, l, r, s, c, h, t: Integer;
- q: string;
- begin
- l := Length(data);
- s := Length(findStr);
- if ((l > 0) and (s > 0)) then
- begin
- h := High(regexTags);
- if (High(regexStrs) < h) then
- SetLength(regexStrs, (h + 1));
- SetLength(Result, l);
- q := '.\+*?[^]$(){}=!<>|:-';
- for c := h downto 0 do
- begin
- t := Pos(q, regexTags[c]);
- if (t > 0) then
- Delete(q, t, 1);
- end;
- for c := s downto 1 do
- if (Pos(findStr[c], q) > 0) then
- Insert('\', findStr, c);
- for t := 0 to h do
- findStr := Replace(findStr, regexTags[t], regexStrs[t], [rfReplaceAll]);
- re := TRegExp.Create;
- re.ModifierI := (rm_I in modifiers);
- re.ModifierG := (rm_G in modifiers);
- re.ModifierM := (rm_M in modifiers);
- re.ModifierS := (rm_S in modifiers);
- re.ModifierX := (rm_X in modifiers);
- re.ModifierR := (rm_R in modifiers);
- re.Expression := findStr;
- re.InputString := data;
- p := 1;
- if re.ExecPos(p) then
- repeat
- if (re.Match[0] <> '') then
- begin
- p := (re.MatchPos[0] + 1);
- Result[r].minimum := re.MatchPos[0];
- Result[r].maximum := (Result[r].minimum + (re.MatchLen[0] - 1));
- Inc(r);
- end;
- until not re.ExecPos(p);
- SetLength(Result, r);
- re.Free;
- end else
- SetLength(Result, 0);
- end;
- var
- str: string;
- begin
- ClearDebug;
- str := ' MMouse ( x , y , 1, 1 );' + #13#10 + // Detects.
- 'mmouse(x, y, 1, 1);' + #13#10 + // Detects.
- 'MMOUSE ( x, y, 1, 1);' + #13#10 + // Detects.
- 'MMouse(x,y,1,1)' + #13#10 + // Detects.
- 'MMouse(x, y, 2, 1)' + #13#10 + // Doesn't detect, because now there is a higher value, than 1, involved (2).
- 'MMouse(x, y, 1 + Random(100), 1 + Random(100));' // Doesn't detect, for same reasons as above. :)
- WriteLn(ToStr(TraceStrsEx('@ ( x , y , 1 , 1 )', [' ', '@'], ['(\s*)', '(m)?mouse'], str, [rm_I])));
- WriteLn(Copy(str, 3, 32));
- WriteLn(Copy(str, 35, 1));
- end.[/simba]
- [/spoiler]
- [spoiler="TraceStrs"]
- [simba]type
- TRange = record
- minimum, maximum: Integer;
- end;
- TRangeArray = array of TRange;
- TRegexModifier = (rm_G, rm_I, rm_M, rm_S, rm_X, rm_R);
- {==============================================================================]
- Explanation: Function for tracing findStr's from data.
- Contains also custom fields for regexTag and regexStr;
- tag performs a regex action, that is set to regexStr, during the search.
- [==============================================================================}
- function TraceStrs(findStr: string; regexTag: Char; regexStr, data: string; modifiers: set of TRegexModifier): TRangeArray;
- var
- re: TRegExp;
- p, l, r, s, c, h, t: Integer;
- q: string;
- begin
- l := Length(data);
- s := Length(findStr);
- if ((l > 0) and (s > 0)) then
- begin
- SetLength(Result, l);
- q := '.\+*?[^]$(){}=!<>|:-';
- t := Pos(regexTag, q);
- if (t > 0) then
- Delete(q, t, 1);
- for c := s downto 1 do
- if (Pos(findStr[c], q) > 0) then
- Insert('\', findStr, c);
- for t := 0 to h do
- findStr := Replace(findStr, regexTag, regexStr, [rfReplaceAll]);
- re := TRegExp.Create;
- re.ModifierI := (rm_I in modifiers);
- re.ModifierG := (rm_G in modifiers);
- re.ModifierM := (rm_M in modifiers);
- re.ModifierS := (rm_S in modifiers);
- re.ModifierX := (rm_X in modifiers);
- re.ModifierR := (rm_R in modifiers);
- re.Expression := findStr;
- re.InputString := data;
- p := 1;
- if re.ExecPos(p) then
- repeat
- if (re.Match[0] <> '') then
- begin
- p := (re.MatchPos[0] + 1);
- Result[r].minimum := re.MatchPos[0];
- Result[r].maximum := (Result[r].minimum + (re.MatchLen[0] - 1));
- Inc(r);
- end;
- until not re.ExecPos(p);
- SetLength(Result, r);
- re.Free;
- end else
- SetLength(Result, 0);
- end;
- var
- str: string;
- begin
- ClearDebug;
- str := ' MMouse ( x , y , 1, 1 );' + #13#10 + // Detects.
- 'mmouse(x, y, 1, 1);' + #13#10 + // Detects.
- 'MMOUSE ( x, y, 1, 1);' + #13#10 + // Detects.
- 'MMouse(x,y,1,1)' + #13#10 + // Detects.
- 'MMouse(x, y, 2, 1)' + #13#10 + // Doesn't detect, because now there is a higher value, than 1, involved (2).
- 'MMouse(x, y, 1 + Random(100), 1 + Random(100));' // Doesn't detect, for same reasons as above. :)
- WriteLn(ToStr(TraceStrs('mmouse ( x , y , 1 , 1 )', ' ', '(\s*)', str, [rm_I])));
- WriteLn(Copy(str, 3, 32));
- WriteLn(Copy(str, 35, 1));
- end.[/simba]
- [/spoiler]
- [spoiler="TraceStrPositions"]
- [simba]type
- TRegexModifier = (rm_G, rm_I, rm_M, rm_S, rm_X, rm_R);
- {==============================================================================]
- Explanation: Function for tracing findStr's from data.
- Contains also custom fields for regexTag and regexStr;
- tag performs a regex action, that is set to regexStr, during the search.
- [==============================================================================}
- function TraceStrPositions(findStr: string; regexTag: Char; regexStr, data: string; modifiers: set of TRegexModifier): TIntegerArray;
- var
- re: TRegExp;
- p, l, r, s, c, h, t: Integer;
- q: string;
- begin
- l := Length(data);
- s := Length(findStr);
- if ((l > 0) and (s > 0)) then
- begin
- SetLength(Result, l);
- q := '.\+*?[^]$(){}=!<>|:-';
- t := Pos(regexTag, q);
- if (t > 0) then
- Delete(q, t, 1);
- for c := s downto 1 do
- if (Pos(findStr[c], q) > 0) then
- Insert('\', findStr, c);
- for t := 0 to h do
- findStr := Replace(findStr, regexTag, regexStr, [rfReplaceAll]);
- re := TRegExp.Create;
- re.ModifierI := (rm_I in modifiers);
- re.ModifierG := (rm_G in modifiers);
- re.ModifierM := (rm_M in modifiers);
- re.ModifierS := (rm_S in modifiers);
- re.ModifierX := (rm_X in modifiers);
- re.ModifierR := (rm_R in modifiers);
- re.Expression := findStr;
- re.InputString := data;
- p := 1;
- if re.ExecPos(p) then
- repeat
- if (re.Match[0] <> '') then
- begin
- p := (re.MatchPos[0] + 1);
- Result[r] := re.MatchPos[0];
- Inc(r);
- end;
- until not re.ExecPos(p);
- SetLength(Result, r);
- re.Free;
- end else
- SetLength(Result, 0);
- end;
- var
- str: string;
- begin
- ClearDebug;
- str := ' MMouse ( x , y , 1, 1 );' + #13#10 + // Detects.
- 'mmouse(x, y, 1, 1);' + #13#10 + // Detects.
- 'MMOUSE ( x, y, 1, 1);' + #13#10 + // Detects.
- 'MMouse(x,y,1,1)' + #13#10 + // Detects.
- 'MMouse(x, y, 2, 1)' + #13#10 + // Doesn't detect, because now there is a higher value, than 1, involved (2).
- 'MMouse(x, y, 1 + Random(100), 1 + Random(100));' // Doesn't detect, for same reasons as above. :)
- WriteLn(ToStr(TraceStrPositions('mmouse ( x , y , 1 , 1 )', ' ', '(\s*)', str, [rm_I])));
- end.[/simba]
- [/spoiler]
- [spoiler="TraceStrPositionsEx"]
- [simba]type
- TRegexModifier = (rm_G, rm_I, rm_M, rm_S, rm_X, rm_R);
- {==============================================================================]
- Explanation: Function for tracing findStr's from data.
- Contains also custom fields for regexTags and regexStrs;
- each tag performs a regex action, that is set to regexStrs, during the search.
- [==============================================================================}
- function TraceStrPositionsEx(findStr: string; regexTags: array of Char; regexStrs: TStringArray; data: string; modifiers: set of TRegexModifier): TIntegerArray;
- var
- re: TRegExp;
- p, l, r, s, c, h, t: Integer;
- q: string;
- begin
- l := Length(data);
- s := Length(findStr);
- if ((l > 0) and (s > 0)) then
- begin
- h := High(regexTags);
- if (High(regexStrs) < h) then
- SetLength(regexStrs, (h + 1));
- SetLength(Result, l);
- q := '.\+*?[^]$(){}=!<>|:-';
- for c := h downto 0 do
- begin
- t := Pos(q, regexTags[c]);
- if (t > 0) then
- Delete(q, t, 1);
- end;
- for c := s downto 1 do
- if (Pos(findStr[c], q) > 0) then
- Insert('\', findStr, c);
- for t := 0 to h do
- findStr := Replace(findStr, regexTags[t], regexStrs[t], [rfReplaceAll]);
- re := TRegExp.Create;
- re.ModifierI := (rm_I in modifiers);
- re.ModifierG := (rm_G in modifiers);
- re.ModifierM := (rm_M in modifiers);
- re.ModifierS := (rm_S in modifiers);
- re.ModifierX := (rm_X in modifiers);
- re.ModifierR := (rm_R in modifiers);
- re.Expression := findStr;
- re.InputString := data;
- p := 1;
- if re.ExecPos(p) then
- repeat
- if (re.Match[0] <> '') then
- begin
- p := (re.MatchPos[0] + 1);
- Result[r] := re.MatchPos[0];
- Inc(r);
- end;
- until not re.ExecPos(p);
- SetLength(Result, r);
- re.Free;
- end else
- SetLength(Result, 0);
- end;
- var
- str: string;
- begin
- ClearDebug;
- str := ' MMouse ( x , y , 1, 1 );' + #13#10 + // Detects.
- 'mmouse(x, y, 1, 1);' + #13#10 + // Detects.
- 'MMOUSE ( x, y, 1, 1);' + #13#10 + // Detects.
- 'MMouse(x,y,1,1)' + #13#10 + // Detects.
- 'MMouse(x, y, 2, 1)' + #13#10 + // Doesn't detect, because now there is a higher value, than 1, involved (2).
- 'MMouse(x, y, 1 + Random(100), 1 + Random(100));' // Doesn't detect, for same reasons as above. :)
- WriteLn(ToStr(TraceStrPositionsEx('mmouse ( x , y , 1 , 1 )', [' '], ['(\s*)'], str, [rm_I])));
- end.[/simba]
- [/spoiler]
- [spoiler="AllBetween"]
- [simba]function AllBetween(s1, s2, str: string): TStringArray;
- var
- s1L, s2L, sL, r, sp, start, fp, finish: Integer;
- tmp: string;
- begin
- sL := Length(str);
- s1L := Length(s1);
- s2L := Length(s2);
- if ((sL > 0) and (s1L > 0) and (s2L > 0) and (sL >= (s1L + s2L))) then
- begin
- SetLength(Result, (sL * (sL - (s1L + s2L))));
- repeat
- sp := PosEx(s1, str, (start + 1));
- if (sp > 0) then
- begin
- start := sp;
- finish := ((start + s1L) - 1);
- repeat
- fp := PosEx(s2, str, (finish + 1));
- if (fp > 0) then
- begin
- finish := fp;
- tmp := Copy(str, (start + s1L), (finish - (start + s1L)));
- if (tmp <> '') then
- begin
- Result[r] := tmp;
- Inc(r);
- tmp := '';
- end;
- end;
- until (fp <= 0);
- end;
- until (sp <= 0);
- end;
- SetLength(Result, r);
- end;
- var
- str: string;
- TSA: TStringArray;
- h, i: Integer;
- begin
- ClearDebug;
- str := '***AllBetween()**Test***';
- TSA := AllBetween('*', '*', str);
- h := High(TSA);
- if (h < 0) then
- Exit;
- for i := 0 to h do
- WriteLn('TSA[' + IntToStr(i) + ']: ' + TSA[i]);
- SetLength(TSA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="MultiBetween"]
- [simba]function MultiBetween(str, s1, s2: string): TStringArray;
- var
- strL, s1L, s2L, o, n, e, r: Integer;
- begin
- s1L := Length(s1);
- s2L := Length(s2);
- strL := Length(str);
- if ((s1 <> '') and (s2 <> '') and (strL > (s1L + s2L))) then
- begin
- SetLength(Result, (strL - (s1L + s2L)));
- repeat
- n := PosEx(s1, str, (n + 1));
- if (n < 1) then
- Break;
- e := PosEx(s2, str, (n + 1));
- if (e < 1) then
- Break;
- repeat
- o := n;
- n := PosEx(s1, str, (n + 1));
- until ((n >= e) or (n < 1));
- n := o;
- Result[r] := Between(s1, s2, Copy(str, n, (s1L + (e + s2L))));
- if (Result[r] <> '') then
- Inc(r);
- until ((e < 1) or (n < 1));
- end;
- SetLength(Result, r);
- end;
- var
- h, i: Integer;
- TSA: TStringArray;
- Text: string;
- begin
- ClearDebug;
- WriteLn('MultiBetween() Test1:');
- Text := '<tag>test1<tag>..<tag>test2dgtsgtsgdsgs<tag>....<tag>test3<tag>.....<tag>test4<tag>.....';
- TSA := MultiBetween(Text, '<tag>', '<tag>');
- h := High(TSA);
- for i := 0 to h do
- WriteLn('TSA[' + IntToStr(i) + ']: ' + TSA[i]);
- WriteLn('');
- SetLength(TSA, 0);
- WriteLn('MultiBetween() Test2:');
- Text := '<tag>test1</tag>....<tag>test2dgtsgtsgdsgs</tag>....<tag>test3</tag>.....<tag>test4</tag>.....';
- TSA := MultiBetween(Text, '<tag>', '</tag>');
- h := High(TSA);
- for i := 0 to h do
- WriteLn('TSA[' + IntToStr(i) + ']: ' + TSA[i]);
- WriteLn('');
- SetLength(TSA, 0);
- WriteLn('MultiBetween() Test3:');
- Text := '||*|*||<|<||[|]||>|>||*|*||';
- TSA := MultiBetween(Text, '|', '|');
- h := High(TSA);
- for i := 0 to h do
- WriteLn('TSA[' + IntToStr(i) + ']: ' + TSA[i]);
- SetLength(TSA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="MultiBetweenEx"]
- [simba]function MultiBetweenEx(str, s1, s2: string; method: Integer): TStringArray;
- var
- s1L, s2L, strL, s, o, n, e, r: Integer;
- begin
- strL := Length(str);
- s1L := Length(s1);
- if ((strL > Length(s1 + s2)) and (s1L >= 1) and (Length(s2) >= 1) and InRange(method, 0, 3)) then
- begin
- SetLength(Result, (strL - (s1L + s2L)));
- case method of
- 0:
- repeat
- n := PosEx(s1, str, (n + 1));
- if (n < 1) then
- Break;
- e := PosEx(s2, str, (n + 1));
- if (e < 1) then
- Break;
- repeat
- o := n;
- n := PosEx(s1, str, (n + 1));
- until ((n >= e) or (n < 1));
- n := o;
- Result[r] := Between(s1, s2, Copy(str, n, (s1L + (e + s2L))));
- if (Result[r] <> '') then
- Inc(r);
- until ((e < 1) or (n < 1));
- 1..3:
- repeat
- case method of
- 1, 2: s := PosEx(s1, str, (e + 1));
- 3: s := PosEx(s1, str, (s + 1));
- end;
- e := PosEx(s2, str, (s + 1));
- if ((s < 1) or (e < 1)) then
- Break;
- Result[r] := Copy(str, (s + s1L), (e - (s + s1L)));
- if (Result[r] <> '') then
- Inc(r)
- else
- if (method = 1) then
- e := ((s + s1L) - 1);
- until ((s < 1) or (e < 1));
- end;
- end;
- SetLength(Result, r);
- end;
- var
- m, h, i: Integer;
- TSA: TStringArray;
- Text: string;
- begin
- ClearDebug;
- for m := 0 to 3 do
- begin
- WriteLn('*** METHOD ' + IntToStr(m) + ' ***');
- WriteLn('MultiBetweenEx() Test1:');
- Text := '<tag>test1<tag>..<tag>test2dgtsgtsgdsgs<tag>....<tag>test3<tag>.....<tag>test4<tag>.....';
- TSA := MultiBetweenEx(Text, '<tag>', '<tag>', m);
- h := High(TSA);
- for i := 0 to h do
- WriteLn('TSA[' + IntToStr(i) + ']: ' + TSA[i]);
- WriteLn('');
- SetLength(TSA, 0);
- WriteLn('MultiBetweenEx() Test2:');
- Text := '<tag>test1</tag>....<tag>test2dgtsgtsgdsgs</tag>....<tag>test3</tag>.....<tag>test4</tag>.....';
- TSA := MultiBetweenEx(Text, '<tag>', '</tag>', m);
- h := High(TSA);
- for i := 0 to h do
- WriteLn('TSA[' + IntToStr(i) + ']: ' + TSA[i]);
- WriteLn('');
- SetLength(TSA, 0);
- WriteLn('MultiBetweenEx() Test3:');
- Text := '||*|*||<|<||[|]||>|>||*|*||';
- TSA := MultiBetweenEx(Text, '|', '|', m);
- h := High(TSA);
- for i := 0 to h do
- WriteLn('TSA[' + IntToStr(i) + ']: ' + TSA[i]);
- SetLength(TSA, 0);
- WriteLn('*** METHOD ' + IntToStr(m) + ' ***');
- if (m < 4) then
- WriteLn('');
- end;
- end.[/simba]
- [/spoiler]
- [spoiler="GetWords"]
- [simba]const // TEST_TEXT by slacky/warpie! ;)
- TEST_TEXT = 'Ost World1 Amf#World2,but hello there.Are we friends?';
- {==============================================================================]
- Explanation: Returns the words from text as TStringArray.
- [==============================================================================}
- function GetWords(text: string): TStringArray;
- var
- l, i, r: Integer;
- w: string;
- begin
- l := Length(text);
- if (l > 0) then
- begin
- w := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_';
- SetLength(Result, l);
- for i := 1 to l do
- if (Pos(text[i], w) > 0) then
- begin
- Result[r] := text[i];
- for i := (i + 1) to l do
- if (Pos(text[i], w) > 0) then
- Result[r] := (Result[r] + text[i])
- else
- Break;
- Inc(r);
- end;
- end;
- SetLength(Result, r);
- end;
- begin
- ClearDebug;
- WriteLn('GetWords(TEST_TEXT): ' + ToStr(GetWords(TEST_TEXT)));
- end.[/simba]
- [/spoiler]
- [spoiler="GetWordsEx"]
- [simba]const // TEST_TEXT by slacky/warpie! ;)
- TEST_TEXT = 'Ost World1 Amf#World2,but hello there.Are we friends?';
- CHARSET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_';
- {==============================================================================]
- Explanation: Returns the words from text as TStringArray.
- Supports custom character set (wordCharacters)
- [==============================================================================}
- function GetWordsEx(text, wordCharacters: string): TStringArray;
- var
- l, i, r: Integer;
- begin
- l := Length(text);
- if ((l > 0) and (wordCharacters <> '')) then
- begin
- SetLength(Result, l);
- for i := 1 to l do
- if (Pos(text[i], wordCharacters) > 0) then
- begin
- Result[r] := text[i];
- for i := (i + 1) to l do
- if (Pos(text[i], wordCharacters) > 0) then
- Result[r] := (Result[r] + text[i])
- else
- Break;
- Inc(r);
- end;
- end;
- SetLength(Result, r);
- end;
- begin
- ClearDebug;
- WriteLn('GetWordsEx(TEST_TEXT, CHARSET): ' + ToStr(GetWordsEx(TEST_TEXT, CHARSET)));
- end.[/simba]
- [/spoiler]
- [spoiler="CountWords"]
- [simba]const // TEST_TEXT by slacky/warpie! ;)
- TEST_TEXT = 'Ost World1 Amf#World2,but hello there.Are we friends?';
- {==============================================================================]
- Explanation: Returns count of words in text.
- [==============================================================================}
- function CountWords(text: string): Integer;
- var
- l, i: Integer;
- w: string;
- begin
- Result := 0;
- l := Length(text);
- if (l > 0) then
- begin
- w := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_';
- for i := 1 to l do
- if (Pos(text[i], w) > 0) then
- begin
- Inc(Result);
- for i := (i + 1) to l do
- if (Pos(text[i], w) < 1) then
- Break;
- end;
- end;
- end;
- begin
- ClearDebug;
- WriteLn('CountWords(TEST_TEXT): ' + ToStr(CountWords(TEST_TEXT)));
- end.[/simba]
- [/spoiler]
- [spoiler="CountWordsEx"]
- [simba]const // TEST_TEXT by slacky/warpie! ;)
- TEST_TEXT = 'Ost World1 Amf#World2,but hello there.Are we friends?';
- CHARSET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_';
- {==============================================================================]
- Explanation: Returns count of words in text.
- Supports custom character set (wordCharacters)
- [==============================================================================}
- function CountWordsEx(text, wordCharacters: string): Integer;
- var
- l, i: Integer;
- begin
- Result := 0;
- l := Length(text);
- if ((l > 0) and (wordCharacters <> '')) then
- for i := 1 to l do
- if (Pos(text[i], wordCharacters) > 0) then
- begin
- Inc(Result);
- for i := (i + 1) to l do
- if (Pos(text[i], wordCharacters) < 1) then
- Break;
- end;
- end;
- begin
- ClearDebug;
- WriteLn('CountWordsEx(TEST_TEXT, CHARSET): ' + ToStr(CountWordsEx(TEST_TEXT, CHARSET)));
- end.[/simba]
- [/spoiler]
- [spoiler="TSAFill"]
- [simba]{==============================================================================]
- Explanation: Fills TSA items with x.
- [==============================================================================}
- procedure TSAFill(var TSA: TStringArray; x: string);
- var
- i, h: Integer;
- begin
- h := High(TSA);
- for i := 0 to h do
- TSA[i] := string(x);
- end;
- var
- s: string;
- h, i: Integer;
- a: TStringArray;
- begin
- ClearDebug;
- a := ['Test0', 'Test1', 'Test2', 'Test3', 'Test4'];
- TSAFill(a, 'TEST');
- h := High(a);
- for i := 0 to h do
- s := (s + '"' + a[i] + '" ');
- WriteLn('a: ' + s);
- end.[/simba]
- [/spoiler]
- [spoiler="TSAFillEx"]
- [simba]{==============================================================================]
- Explanation: Fills TSA items with x.
- [==============================================================================}
- procedure TSAFillEx(var TSA: TStringArray; x: TStringArray);
- var
- i, h, l: Integer;
- begin
- h := High(TSA);
- l := Length(x);
- for i := 0 to h do
- TSA[i] := string(x[i mod l]);
- end;
- var
- s: string;
- h, i: Integer;
- a: TStringArray;
- begin
- ClearDebug;
- a := ['Test0', 'Test1', 'Test2', 'Test3', 'Test4', 'Test6', 'Test7', 'Test8', 'Test9'];
- TSAFillEx(a, ['Test1', 'Test2', 'Test3']);
- h := High(a);
- for i := 0 to h do
- s := (s + '"' + a[i] + '" ');
- WriteLn('a: ' + s);
- end.[/simba]
- [/spoiler]
- [spoiler="TSAOfString"]
- [simba]{==============================================================================]
- Explanation: Returns TSA of x, count being the size of the result.
- [==============================================================================}
- function TSAOfString(x: string; count: Integer): TStringArray;
- var
- i: Integer;
- begin
- if (count > 0) then
- begin
- SetLength(Result, count);
- for i := 0 to (count - 1) do
- Result[i] := string(x);
- end else
- SetLength(Result, 0);
- end;
- begin
- ClearDebug;
- WriteLn(ToStr(TSAOfString('Test', 4)));
- end.[/simba]
- [/spoiler]
- [spoiler="TSAOfStringEx"]
- [simba]{==============================================================================]
- Explanation: Returns TSA with x items, count being the size of the result.
- [==============================================================================}
- function TSAOfStringEx(x: TStringArray; count: Integer): TStringArray;
- var
- i, l: Integer;
- begin
- l := Length(x);
- if ((count > 0) and (l > 0)) then
- begin
- SetLength(Result, count);
- for i := 0 to (count - 1) do
- Result[i] := string(x[i mod l]);
- end else
- SetLength(Result, 0);
- end;
- begin
- ClearDebug;
- WriteLn(ToStr(TSAOfStringEx(['a', 'b', 'c'], 8)));
- end.[/simba]
- [/spoiler]
- [spoiler="TSAClone"]
- [simba]{==============================================================================]
- Explanation: Returns copy ("clone") of TSA safely
- [==============================================================================}
- function TSAClone(TSA: TStringArray): TStringArray;
- var
- i, l: Integer;
- begin
- l := Length(TSA);
- SetLength(Result, l);
- for i := 0 to (l - 1) do
- Result[i] := string(TSA[i]);
- end;
- var
- tmp: TStringArray;
- i: Integer;
- begin
- tmp := TSAClone(['Test1', 'Test2', 'Test3', 'Test4']);
- WriteLn(ToStr(tmp));
- SetLength(tmp, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="TSACopy"]
- [simba]function TSACopy(TSA: TStringArray; startIndex, count: Integer): TStringArray;
- var
- i, l, t: Integer;
- begin
- l := Length(TSA);
- if (startIndex < 0) then
- startIndex := 0;
- if ((l >= startIndex) and (count > 0)) then
- begin
- t := (l - startIndex);
- if (count > t) then
- count := t;
- SetLength(Result, count);
- for i := startIndex to ((startIndex + count) - 1) do
- Result[(i - startIndex)] := string(TSA[i]);
- end else
- SetLength(Result, 0);
- end;
- var
- TSA: TStringArray;
- i: Integer;
- begin
- ClearDebug;
- TSA := TSACopy(['TEST1', 'TEST2', 'TEST3', 'TEST4', 'TEST5', 'TEST6', 'TEST7'], 2, 3);
- WriteLn(ToStr(TSA));
- SetLength(TSA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="TSACopyEx"]
- [simba]{==============================================================================]
- Explanation: Copies TSA from pos1 to pos2. Reverse copies with pos1>pos2 (flexible!).
- [==============================================================================}
- function TSACopyEx(TSA: TStringArray; pos1, pos2: Integer): TStringArray;
- var
- i, l: Integer;
- begin
- l := Length(TSA);
- if (l > 0) then
- begin
- if (pos1 < 0) then
- pos1 := 0;
- if (pos1 > (l - 1)) then
- pos1 := (l - 1);
- if (pos2 < 0) then
- pos2 := 0;
- if (pos2 > (l - 1)) then
- pos2 := (l - 1);
- if (pos1 <> pos2) then
- begin
- {$IFNDEF Lape}
- SetLength(Result, (IAbs(pos1 - pos2) + 1));
- {$ELSE}
- SetLength(Result, (Abs(pos1 - pos2) + 1));
- {$ENDIF}
- if (pos1 < pos2) then
- begin
- for i := pos1 to pos2 do
- Result[(i - pos1)] := string(TSA[i]);
- end else
- for i := pos1 downto pos2 do
- Result[(pos1 - i)] := string(TSA[i]);
- end else
- Result := [string(TSA[pos1])];
- end else
- SetLength(Result, 0);
- end;
- var
- i: Integer;
- begin
- for i := 0 to 5 do
- WriteLn(ToStr(TSACopyEx(['A', 'B', 'C', 'D', 'E', 'F'], i, 5)));
- for i := 5 downto 0 do
- WriteLn(ToStr(TSACopyEx(['A', 'B', 'C', 'D', 'E', 'F'], 5, i)));
- end.[/simba]
- [/spoiler]
- [spoiler="TSAGet"]
- [simba]{==============================================================================]
- Explanation: Returns array of items from TSA by IDs. Ignores invalid ID's.
- [==============================================================================}
- function TSAGet(TSA: TStringArray; IDs: TIntegerArray): TStringArray;
- var
- i, h, h2, r: Integer;
- begin
- h := High(TSA);
- h2 := High(IDs);
- if ((h2 > -1) and (h > -1)) then
- begin
- SetLength(Result, (h2 + 1));
- for i := 0 to h2 do
- if ((IDs[i] <= h) and (IDs[i] > -1)) then
- begin
- Result[r] := string(TSA[IDs[i]]);
- Inc(r);
- end;
- end;
- SetLength(Result, r);
- end;
- var
- tmp: TStringArray;
- h, i: Integer;
- begin
- ClearDebug;
- tmp := TSAGet(['Test0', 'Test1', 'Test2', 'Test3', 'Test4', 'Test5', 'Test6', 'Test7', 'Test8', 'Test9'], [100000, 1, 2, -1, 3, 4, 999]);
- h := High(tmp);
- for i := 0 to h do
- WriteLn(tmp[i]);
- end.[/simba]
- [/spoiler]
- [spoiler="TSAAdd"]
- [simba]{==============================================================================]
- Explanation: Adds all addTSA items to TSA. Returns the highest index in the end.
- [==============================================================================}
- function TSAAdd(var TSA: TStringArray; addTSA: TStringArray): Integer;
- var
- h, l, i: Integer;
- begin
- h := High(addTSA);
- if (h > -1) then
- begin
- l := Length(TSA);
- SetLength(TSA, (l + (h + 1)));
- for i := 0 to h do
- TSA[(i + l)] := string(addTSA[i]);
- end;
- Result := High(TSA);
- end;
- var
- TSA: TStringArray;
- begin
- ClearDebug;
- TSAAdd(TSA, ['Test1', 'Test2', 'Test3']);
- WriteLn('TSA: ' + ToStr(TSA));
- TSAAdd(TSA, ['Test4', 'Test5', 'Test6', 'Test7']);
- WriteLn('TSA: ' + ToStr(TSA));
- TSAAdd(TSA, ['Test8', 'Test9']);
- WriteLn('TSA: ' + ToStr(TSA));
- end.[/simba]
- [/spoiler]
- [spoiler="TSAReverse"]
- [simba]procedure TSAReverse(var TSA: TStringArray);
- var
- g, h, i: Integer;
- begin
- h := High(TSA);
- if (h < 1) then
- Exit;
- g := (h div 2);
- for i := 0 to g do
- Swap(TSA[i], TSA[(h - i)]);
- end;
- var
- TSA: TStringArray;
- begin
- ClearDebug;
- TSA := ['Test10', 'Test9', 'Test8', 'Test7', 'Test6', 'Test5', 'Test4', 'Test3', 'Test2', 'Test1'];
- TSAReverse(TSA);
- WriteLn(TSA);
- SetLength(TSA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="TSACombine"]
- [simba]function TSACombine(TSA1, TSA2: TStringArray): TStringArray;
- var
- l1, l2, i: Integer;
- begin
- l1 := Length(TSA1);
- l2 := Length(TSA2);
- SetLength(Result, (l1 + l2));
- for i := 0 to (l1 - 1) do
- Result[i] := string(TSA1[i]);
- for i := 0 to (l2 - 1) do
- Result[(l1 + i)] := string(TSA2[i]);
- end;
- begin
- WriteLn(ToStr(TSACombine(['one', 'two'], ['three'])));
- end.[/simba]
- [/spoiler]
- [spoiler="TSAConcat"]
- [simba]function TSAConcat(TSA: TStringArray): string;
- var
- h, i: Integer;
- begin
- Result := '';
- h := High(TSA);
- for i := 0 to h do
- Result := (Result + string(TSA[i]));
- end;
- var
- TSA: TStringArray;
- begin
- TSA := ['Test1', ', ', 'Test2', ', ', 'Test3', '!'];
- ClearDebug;
- WriteLn(TSAConcat(TSA));
- SetLength(TSA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="TSAConcatEx"]
- [simba]{==============================================================================]
- Explanation: Returns string of all TSA items binded together. Places glue between the indexes.
- [==============================================================================}
- function TSAConcatEx(TSA: TStringArray; glue: string): string;
- var
- h, i: Integer;
- begin
- Result := '';
- h := High(TSA);
- if (h > -1) then
- begin
- for i := 0 to (h - 1) do
- Result := (Result + string(TSA[i]) + string(glue));
- Result := (Result + string(TSA[i]));
- end;
- end;
- begin
- ClearDebug;
- WriteLn(TSAConcatEx(['This', 'is', 'a', 'test!'], ' '));
- end.[/simba]
- [/spoiler]
- [spoiler="TSATrimEx"]
- [simba]{==============================================================================]
- Explanation: Trims all TSA items.
- If delEmpty is set as true, procedure will delete all empty items.
- [==============================================================================}
- procedure TSATrimEx(var TSA: TStringArray; delEmpty: Boolean);
- var
- l, i, r: Integer;
- tmp: TStringArray;
- begin
- l := Length(TSA);
- if (l > 0) then
- if delEmpty then
- begin
- SetLength(tmp, l);
- for i := 0 to (l - 1) do
- if (TSA[i] <> '') then
- begin
- tmp[r] := Trim(TSA[i]);
- if (tmp[r] <> '') then
- Inc(r);
- end;
- SetLength(tmp, r);
- SetLength(TSA, r);
- for i := 0 to (r - 1) do
- TSA[i] := string(tmp[i]);
- SetLength(tmp, 0);
- end else
- for i := 0 to (l - 1) do
- TSA[i] := Trim(TSA[i]);
- end;
- var
- TSA: TStringArray;
- begin
- ClearDebug;
- TSA := Explode(',', ' This , is , a , test ,, ! ');
- TSATrimEx(TSA, False);
- WriteLn(ToStr(TSA));
- TSA := Explode(',', ' This , is , a , test ,, ! ');
- TSATrimEx(TSA, True);
- WriteLn(ToStr(TSA));
- TSA := Explode(',', ' This , is , a , test ');
- WriteLn(ToStr(TSA));
- SetLength(TSA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="TSATrim"]
- [simba]{==============================================================================]
- Explanation: Trims all TSA items.
- [==============================================================================}
- procedure TSATrim(var TSA: TStringArray);
- var
- h, i: Integer;
- begin
- h := High(TSA);
- for i := 0 to h do
- TSA[i] := Trim(TSA[i]);
- end;
- var
- TSA: TStringArray;
- begin
- ClearDebug;
- TSA := Explode(',', ' This , is , a , test ');
- TSATrim(TSA);
- WriteLn(ToStr(TSA));
- TSA := Explode(',', ' This , is , a , test ');
- WriteLn(ToStr(TSA));
- SetLength(TSA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="TSAContains"]
- [simba]function TSAContains(TSA: TStringArray; x: string): Boolean;
- var
- i, h: Integer;
- begin
- h := High(TSA);
- for i := 0 to h do
- begin
- Result := (TSA[i] = x);
- if Result then
- Exit;
- end;
- Result := False;
- end;
- var
- i: Integer;
- s: string;
- TSA: TStringArray;
- begin
- ClearDebug;
- TSA := ['Test0', 'Test1', 'Test2', 'Test3', 'Test4',
- 'Test5', 'Test6', 'Test7', 'Test8', 'Test9'];
- for i := -1 to 10 do
- begin
- s := 'Test' + IntToStr(i);
- if TSAContains(TSA, s) then
- WriteLn('TSA contains "' + s + '"!')
- else
- WriteLn('TSA doesn''t contain "' + s + '".');
- end;
- SetLength(TSA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="TSAContainsEx"]
- [simba]{==============================================================================]
- Explanation: Returns true if TSA contains ANY string from strs.
- CASE-SENSITIVE.
- [==============================================================================}
- function TSAContainsEx(TSA, strs: TStringArray): Boolean;
- var
- a, b, x, y: Integer;
- begin
- b := High(TSA);
- y := High(strs);
- Result := True;
- if ((b > -1) and (y > -1)) then
- for a := 0 to b do
- for x := 0 to y do
- if (TSA[a] = strs[x]) then
- Exit;
- Result := False;
- end;
- begin
- ClearDebug;
- if TSAContainsEx(['Lets', 'test', 'TSAContainsEx()', 'function', '!'], ['TSAContainsEx()', '!']) then
- WriteLn('YEP!');
- end.[/simba]
- [/spoiler]
- [spoiler="TSAContainsTSA"]
- [simba]{==============================================================================]
- Explanation: Returns true if TSA contains WHOLE chain of strings (arr) in it.
- CASE-SENSITIVE.
- [==============================================================================}
- function TSAContainsTSA(TSA, arr: TStringArray): Boolean;
- var
- a, b, x, y, z: Integer;
- begin
- z := High(TSA);
- y := High(arr);
- if ((z >= y) and (z > -1)) then
- begin
- b := (z - y);
- for a := 0 to b do
- begin
- for x := 0 to y do
- if (TSA[(a + x)] <> arr[x]) then
- Break;
- if (x > y) then
- Break;
- end;
- Result := (a <= b);
- end else
- Result := False;
- end;
- begin
- ClearDebug;
- if TSAContainsTSA(['Lets', 'test', 'TSAContainsEx()', 'function', '!'], ['TSAContainsEx()', '!']) then
- WriteLn('NOOOO!');
- if TSAContainsTSA(['Lets', 'test', 'TSAContainsEx()', 'function', '!'], ['TSAContainsEx()', 'function', '!']) then
- WriteLn('YEEAH!');
- end.[/simba]
- [/spoiler]
- [spoiler="TSADelete"]
- [simba]procedure TSADelete(var TSA: TStringArray; x: Integer);
- var
- i, h: Integer;
- begin
- h := High(TSA);
- if ((x > h) or (x < 0)) then
- Exit;
- for i := x to (h - 1) do
- TSA[i] := TSA[(i + 1)];
- SetLength(TSA, h);
- end;
- var
- TSA: TStringArray;
- begin
- ClearDebug;
- TSA := ['ID0', 'ID1', 'ID5', 'ID2', 'ID3', 'ID8', 'ID4'];
- TSADelete(TSA, 5);
- TSADelete(TSA, 2);
- WriteLn(ToStr(TSA));
- end.[/simba]
- [/spoiler]
- [spoiler="TSARemove"]
- [simba]procedure TSARemove(var TSA: TStringArray; x: TIntegerArray);
- var
- i, i2, h, h2: Integer;
- begin
- h := High(TSA);
- h2 := High(x);
- if ((h < 0) or (h2 < 0)) then
- Exit;
- for i2 := 0 to h2 do
- if ((x[i2] <= h) and (x[i2] > -1)) then
- begin
- for i := x[i2] to (h - 1) do
- TSA[i] := TSA[(i + 1)];
- Dec(h);
- end;
- SetLength(TSA, (h + 1));
- end;
- var
- TSA: TStringArray;
- begin
- ClearDebug;
- TSA := ['ID0', 'ID1', 'ID5', 'ID2', 'ID3', 'ID8', 'ID4', 'ID5', 'ID999', 'ID6'];
- TSARemove(TSA, [8, 5, 2]);
- WriteLn(ToStr(TSA));
- SetLength(TSA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="TSAAppend"]
- [simba]procedure TSAAppend(var TSA: TStringArray; x: string);
- var
- aL: Integer;
- begin
- aL := (Length(TSA) + 1);
- SetLength(TSA, aL);
- TSA[(aL - 1)] := string(x);
- end;
- var
- TSA: TStringArray;
- begin
- ClearDebug;
- TSA := ['ID0', 'ID1', 'ID2', 'ID3', 'ID4'];
- TSAAppend(TSA, 'ID5');
- WriteLn(ToStr(TSA));
- end.[/simba]
- [/spoiler]
- [spoiler="TSAInsert"]
- [simba]procedure TSAInsert(var TSA: TStringArray; index: Integer; str: string);
- var
- i, l: Integer;
- begin
- l := Length(TSA);
- SetLength(TSA, (l + 1));
- if (index < 0) then
- index := 0;
- if (index > l) then
- index := l;
- if (l > index) then
- for i := (l - 1) downto index do
- TSA[(i + 1)] := string(TSA[i]);
- TSA[index] := string(str);
- end;
- var
- i: Integer;
- TSA: TStringArray;
- begin
- ClearDebug;
- for i := 0 to 10 do
- TSAInsert(TSA, Random(10 + 1), 'i = ' + IntToStr(i));
- WriteLn(ToStr(TSA));
- SetLength(TSA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="TSAPlant"]
- [simba]{==============================================================================]
- Explanation: Plants/places strs to index position in TSA. Like TSAInsert(), with an exception that this inserts array of strings.
- [==============================================================================}
- function TSAPlant(var TSA: TStringArray; index: Integer; strs: TStringArray): Integer;
- var
- i, l, h: Integer;
- begin
- h := High(strs);
- if (h > -1) then
- begin
- l := Length(TSA);
- SetLength(TSA, (l + (h + 1)));
- if (index < 0) then
- index := 0;
- if (index > l) then
- index := l;
- for i := (l + (h + 1) - 1) downto (index + (h + 1)) do
- TSA[i] := string(TSA[(i - (h + 1))]);
- for i := 0 to h do
- TSA[(i + index)] := string(strs[i]);
- end;
- Result := High(TSA);
- end;
- var
- str: string;
- TSA: TStringArray;
- h, i: Integer;
- begin
- ClearDebug;
- TSA := ['1', '2', '7'];
- TSAPlant(TSA, 2, ['3', '4', '5', '6']);
- TSAPlant(TSA, 7, ['8', '9']);
- TSAPlant(TSA, 0, ['0']);
- h := High(TSA);
- for i := 0 to h do
- str := (str + TSA[i]);
- SetLength(TSA, 0);
- WriteLn(str);
- end.[/simba]
- [/spoiler]
- [spoiler="TSAMove"]
- [simba]{==============================================================================]
- Explanation: Moves oldIndex to newIndex in TSA. Returns true, if movement was succesfully done!
- [==============================================================================}
- function TSAMove(var TSA: TStringArray; oldIndex, newIndex: Integer): Boolean;
- var
- h, i: Integer;
- begin
- h := High(TSA);
- Result := ((h > 0) and (oldIndex <> newIndex) and InRange(oldIndex, 0, h) and InRange(newIndex, 0, h));
- if Result then
- if (oldIndex > newIndex) then
- begin
- for i := oldIndex downto (newIndex + 1) do
- Swap(TSA[i], TSA[(i - 1)]);
- end else
- for i := oldIndex to (newIndex - 1) do
- Swap(TSA[i], TSA[(i + 1)]);
- end;
- var
- tmp: TStringArray;
- begin
- tmp := ['Test0', 'Test2', 'Test3', 'Test4', 'Test1', 'Test5'];
- TSAMove(tmp, 4, 1);
- WriteLn(ToStr(tmp));
- end.[/simba]
- [/spoiler]
- [spoiler="TSASame"]
- [simba]{==============================================================================]
- Explanation: Returns true if TSA1 is identical with TSA2.
- [==============================================================================}
- function TSASame(TSA1, TSA2: TStringArray): Boolean;
- var
- h, i: Integer;
- begin
- Result := False;
- h := High(TSA1);
- if (h = High(TSA2)) then
- begin
- for i := 0 to h do
- if (TSA1[i] <> TSA2[i]) then
- Exit;
- Result := True;
- end;
- end;
- begin
- ClearDebug;
- if TSASame(['a', 'b', 'c'], ['a', 'b', 'c', 'd']) then
- WriteLn('WTF!');
- if TSASame(['a', 'b', 'c', 'd'], ['a', 'b', 'c']) then
- WriteLn('WTF!');
- if TSASame(['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd']) then
- WriteLn('YAY!');
- end.[/simba]
- [/spoiler]
- [spoiler="TSAUnique"]
- [simba]procedure TSAUnique(var TSA: TStringArray);
- var
- h, h2, i, i2, i3, d: Integer;
- begin
- h := High(TSA);
- if (h < 1) then
- Exit;
- for i := (h - d) downto 1 do
- for i2 := (i - 1) downto 0 do
- if (TSA[i] = TSA[i2]) then
- begin
- h2 := High(TSA);
- for i3 := i to (h2 - 1) do
- TSA[i3] := TSA[(i3 + 1)];
- SetLength(TSA, h2);
- Inc(d);
- Break;
- end;
- end;
- var
- TSA: TStringArray;
- begin
- ClearDebug;
- TSA := ['Test0', 'Test1', 'Test1', 'Test2', 'Test0', 'Test3', 'Test3', 'Test2', 'Test4'];
- TSAUnique(TSA);
- WriteLn(ToStr(TSA));
- SetLength(TSA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="TSAAllUnique"]
- [simba]function TSAAllUnique(TSA: TStringArray): Boolean;
- var
- h, i, i2: Integer;
- begin
- h := High(TSA);
- if (h < 1) then
- begin
- Result := (h = 0);
- Exit;
- end;
- Result := False;
- for i := 0 to (h - 1) do
- for i2 := (i + 1) to h do
- if (i <> i2) then
- begin
- Result := (TSA[i] <> TSA[i2]);
- if not Result then
- Exit;
- end;
- end;
- var
- ATSA: T2DStringArray;
- h, i: Integer;
- begin
- ClearDebug;
- SetLength(ATSA, 9);
- ATSA[0] := ['Test0', 'Test1', 'Test2', 'Test3'];
- ATSA[1] := ['Test0', 'Test1', 'Test2', 'Test1'];
- ATSA[2] := ['Test0', 'Test1', 'Test2', 'Test2'];
- ATSA[3] := ['Test0', 'Test1', 'Test1', 'Test2'];
- ATSA[4] := ['Test0', 'Test1', 'Test1', 'Test1'];
- ATSA[5] := ['Test1', 'Test1', 'Test1', 'Test1'];
- ATSA[6] := ['Test1', 'Test2', 'Test3', 'Test4'];
- ATSA[7] := ['Test'];
- ATSA[8] := [];
- h := High(ATSA);
- for i := 0 to h do
- begin
- if TSAAllUnique(ATSA[i]) then
- WriteLn('ATSA[' + IntToSTr(i) + ']: ALL values are unique!')
- else
- WriteLn('ATSA[' + IntToStr(i) + ']: ALL values are NOT unique.');
- SetLength(ATSA[i], 0);
- end;
- SetLength(ATSA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="TSAAllSame"]
- [simba]function TSAAllSame(TSA: TStringArray): Boolean;
- var
- h, i: Integer;
- begin
- h := High(TSA);
- if (h < 1) then
- begin
- Result := (h = 0);
- Exit;
- end;
- Result := False;
- for i := 1 to h do
- if (TSA[i] <> TSA[0]) then
- Exit;
- Result := True;
- end;
- var
- ATSA: T2DStringArray;
- h, i: Integer;
- begin
- ClearDebug;
- SetLength(ATSA, 9);
- ATSA[0] := ['Test0', 'Test1', 'Test2', 'Test3'];
- ATSA[1] := ['Test0', 'Test1', 'Test2', 'Test1'];
- ATSA[2] := ['Test0', 'Test1', 'Test2', 'Test2'];
- ATSA[3] := ['Test0', 'Test1', 'Test1', 'Test2'];
- ATSA[4] := ['Test0', 'Test1', 'Test1', 'Test1'];
- ATSA[5] := ['Test1', 'Test1', 'Test1', 'Test1'];
- ATSA[6] := ['Test1', 'Test2', 'Test3', 'Test4'];
- ATSA[7] := ['Test'];
- ATSA[8] := [];
- h := High(ATSA);
- for i := 0 to h do
- begin
- if TSAAllSame(ATSA[i]) then
- WriteLn('ATSA[' + IntToSTr(i) + ']: ALL values are same!')
- else
- WriteLn('ATSA[' + IntToStr(i) + ']: ALL values are NOT same.');
- SetLength(ATSA[i], 0);
- end;
- SetLength(ATSA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="TSAPos"]
- [simba]{==============================================================================]
- Explanation: Returns index position of TSA which matched str.
- Returns -1 if any of TSA items doesnt match with str.
- [==============================================================================}
- function TSAPos(TSA: TStringArray; str: string): Integer;
- var
- h: Integer;
- begin
- h := High(TSA);
- for Result := 0 to h do
- if (TSA[Result] = str) then
- Exit;
- Result := -1;
- end;
- begin
- WriteLn(TSAPos(['a', 'b', 'c', 'd', 'e', 'f', 'g'], '_'));
- WriteLn(TSAPos(['a', 'b', 'c', 'd', 'e', 'f', 'g'], 'a'));
- WriteLn(TSAPos(['a', 'b', 'c', 'd', 'e', 'f', 'g'], 'b'));
- WriteLn(TSAPos(['a', 'b', 'c', 'd', 'e', 'f', 'g'], 'c'));
- WriteLn(TSAPos(['a', 'b', 'c', 'd', 'e', 'f', 'g'], 'd'));
- WriteLn(TSAPos(['a', 'b', 'c', 'd', 'e', 'f', 'g'], 'x'));
- end.[/simba]
- [/spoiler]
- [spoiler="TSAPosEx"]
- [simba]{==============================================================================]
- Explanation: Returns all positions of TSA which match with str.
- [==============================================================================}
- function TSAPosEx(TSA: TStringArray; str: string): TIntegerArray;
- var
- i, r, h: Integer;
- begin
- h := High(TSA);
- if (h > -1) then
- begin
- SetLength(Result, (h + 1));
- for i := 0 to h do
- if (TSA[i] = str) then
- begin
- Result[r] := i;
- Inc(r);
- end;
- end;
- SetLength(Result, r);
- end;
- begin
- WriteLn(ToStr(TSAPosEx(['a', 'b', 'c', 'd', 'c', 'b', 'a'], '_')));
- WriteLn(ToStr(TSAPosEx(['a', 'b', 'c', 'd', 'c', 'b', 'a'], 'a')));
- WriteLn(ToStr(TSAPosEx(['a', 'b', 'c', 'd', 'c', 'b', 'a'], 'b')));
- WriteLn(ToStr(TSAPosEx(['a', 'b', 'c', 'd', 'c', 'b', 'a'], 'c')));
- WriteLn(ToStr(TSAPosEx(['a', 'b', 'c', 'd', 'c', 'b', 'a'], 'd')));
- WriteLn(ToStr(TSAPosEx(['a', 'b', 'c', 'd', 'c', 'b', 'a'], 'x')));
- end.[/simba]
- [/spoiler]
- [spoiler="TSAPosAll"]
- [simba]function TSAPosAll(s: string; TSA: TStringArray): T2DIntegerArray;
- var
- h, i, l, p, r, sL, t: Integer;
- begin
- h := High(TSA);
- sL := Length(s);
- if ((h > -1) and (sL > 0)) then
- begin
- SetLength(Result, (h + 1));
- for i := 0 to h do
- begin
- l := Length(TSA[i]);
- if (l >= sL) then
- begin
- SetLength(Result[r], (l + 1));
- repeat
- p := PosEx(s, TSA[i], (p + 1));
- if (p > 0) then
- begin
- Result[r][(t + 1)] := p;
- Inc(t);
- end;
- until (p <= 0);
- if (t > 0) then
- begin
- Result[r][0] := i;
- SetLength(Result[r], (t + 1));
- Inc(r);
- t := 0;
- end else
- SetLength(Result[r], 0);
- end;
- end;
- end;
- SetLength(Result, r);
- end;
- var
- TSA: TStringArray;
- ATIA: T2DIntegerArray;
- h, h2, i, i2: Integer;
- str: string;
- begin
- ClearDebug;
- TSA := ['|', '| |', '| | |', '| | | |', '| | | | |',
- '||', '|| ||', '|| || ||', '|| || || ||',
- '|||', '||| |||', '||| ||| |||',
- '||||', '|||| ||||', '|||| |||| ||||'];
- ATIA := TSAPosAll('||', TSA);
- h := High(ATIA);
- for i := 0 to h do
- begin
- h2 := High(ATIA[i]);
- if (h2 > 0) then
- begin
- str := 'Positions in TSA[' + IntToStr(ATIA[i][0]) + ']: ';
- for i2 := 1 to h2 do
- if (i2 < h2) then
- str := (str + IntToStr(ATIA[i][i2]) + ', ')
- else
- str := (str + IntToStr(ATIA[i][i2]));
- WriteLn(str + ' (' + TSA[ATIA[i][0]] + ')');
- str := '';
- end;
- SetLength(ATIA[i], 0);
- end;
- SetLength(ATIA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="TSAExtractStrings"]
- [simba]{==============================================================================]
- Explanation: Keeps all strings in TSA that are found in strings.
- [==============================================================================}
- procedure TSAExtractStrings(var TSA: TStringArray; strings: TStringArray);
- var
- a, b, c, x, y: Integer;
- begin
- b := High(TSA);
- y := High(strings);
- if ((b > -1) and (y > -1)) then
- for a := 0 to b do
- begin
- for x := 0 to y do
- if (TSA[a] = strings[x]) then
- Break;
- if (x <= y) then
- begin
- TSA[c] := TSA[a];
- Inc(c);
- end;
- end;
- SetLength(TSA, c);
- end;
- var
- TSA, filter: TStringArray;
- begin
- ClearDebug;
- filter := ['Test2', 'Test3', 'Test4', 'Test5', 'Test6', 'Test7'];
- TSA := ['Test0', 'Test1', 'Test2', 'Test3', 'Test4', 'Test5', 'Test6', 'Test7', 'Test8', 'Test9'];
- TSAExtractStrings(TSA, filter);
- WriteLn(ToStr(TSA));
- end.[/simba]
- [/spoiler]
- [spoiler="TSAFilterStrings"]
- [simba]{==============================================================================]
- Explanation: Removes all strings from TSA that are found in strings.
- [==============================================================================}
- procedure TSAFilterStrings(var TSA: TStringArray; strings: TStringArray);
- var
- a, b, c, x, y: Integer;
- begin
- b := High(TSA);
- y := High(strings);
- if ((b > -1) and (y > -1)) then
- for a := 0 to b do
- begin
- for x := 0 to y do
- if (TSA[a] = strings[x]) then
- Break;
- if (x > y) then
- begin
- TSA[c] := TSA[a];
- Inc(c);
- end;
- end;
- SetLength(TSA, c);
- end;
- var
- TSA, filter: TStringArray;
- begin
- ClearDebug;
- filter := ['Test2', 'Test3', 'Test4', 'Test5', 'Test6', 'Test7'];
- TSA := ['Test0', 'Test1', 'Test2', 'Test3', 'Test4', 'Test5', 'Test6', 'Test7', 'Test8', 'Test9'];
- TSAFilterStrings(TSA, filter);
- WriteLn(ToStr(TSA));
- end.[/simba]
- [/spoiler]
- [spoiler="TSAToParts"]
- [simba]type
- TPartitionMethod = (pm_PartSize, pm_PartAmount);
- {==============================================================================]
- Explanation: Breaks TSA to parts (TSA => ATSA). Contains 2 methods:
- -pm_PartSize (Breaks TSA to ATSA by size of the parts) [x = size]
- -pm_PartAmount (Breaks TSA to ATSA by amount of the parts) [x = amount]
- [==============================================================================}
- function TSAToParts(TSA: TStringArray; method: TPartitionMethod; x: Integer): T2DStringArray;
- var
- a, e, h, h2, i, i2, p, z, l: Integer;
- f: Boolean;
- begin
- h := High(TSA);
- if ((h > -1) and (x > 0)) then
- begin
- case method of
- pm_PartSize:
- if (x <= h) then
- begin
- Inc(h);
- p := (h div x);
- if ((p * x) < h) then
- Inc(p);
- SetLength(Result, p);
- for i := 0 to (p - 1) do
- for i2 := 0 to (x - 1) do
- begin
- SetLength(Result[i], x);
- if (a < h) then
- begin
- Result[i][i2] := string(TSA[a]);
- Inc(a);
- end else
- begin
- SetLength(Result[i], i2);
- Exit;
- end;
- end;
- end else
- f := True;
- pm_PartAmount:
- if (h > -1) then
- begin
- if (h < (x - 1)) then
- x := (h + 1);
- p := Floor((h + 1) / x);
- if (p = 0) then
- p := 1;
- e := ((h + 1) - (p * x));
- if (e >= (h + 1)) then
- e := 0;
- SetLength(Result, x);
- for i := 0 to (x - 1) do
- begin
- if ((e >= (i + 1)) and (e > 0)) then
- SetLength(Result[i], (p + 1))
- else
- if (i <= h) then
- SetLength(Result[i], p);
- h2 := High(Result[i]);
- for i2 := 0 to h2 do
- begin
- Result[i][i2] := string(TSA[a]);
- Inc(a);
- end;
- end;
- end else
- f := True;
- end;
- if f then
- begin
- SetLength(Result, 1);
- l := Length(TSA);
- SetLength(Result[0], l);
- for z := 0 to (l - 1) do
- Result[0][z] := string(TSA[z]);
- end;
- end else
- SetLength(Result, 0);
- end;
- var
- str: string;
- TSA: TStringArray;
- ATSA: T2DStringArray;
- x, h, h2, i, i2: Integer;
- begin
- ClearDebug;
- TSA := ['Test0', 'Test1', 'Test2', 'Test3', 'Test4', 'Test5', 'Test6',
- 'Test7', 'Test8', 'Test9', 'Test10', 'Test11', 'Test12', 'Test13',
- 'Test14', 'Test15', 'Test16', 'Test17', 'Test18', 'Test19', 'Test20'];
- for x := 10 downto 3 do
- begin
- WriteLn('TSAToParts(TSA, pm_PartAmount, ' + IntToStr(x) + '):');
- ATSA := TSAToParts(TSA, pm_PartAmount, x);
- h := High(ATSA);
- for i := 0 to h do
- begin
- h2 := High(ATSA[i]);
- str := '';
- str := 'ATSA[' + IntToStr(i) + ']: ';
- for i2 := 0 to h2 do
- if (i2 < h2) then
- str := (str + '''' + ATSA[i][i2] + ''', ')
- else
- str := (str + '''' + ATSA[i][i2] + '''');
- WriteLn(str);
- SetLength(ATSA[i], 0);
- end;
- SetLength(ATSA, 0);
- WriteLn('');
- WriteLn('TSAToParts(TSA, pm_PartSize, ' + IntToStr(x) + '):');
- ATSA := TSAToParts(TSA, pm_PartSize, x);
- h := High(ATSA);
- for i := 0 to h do
- begin
- h2 := High(ATSA[i]);
- str := '';
- str := 'ATSA[' + IntToStr(i) + ']: ';
- for i2 := 0 to h2 do
- if (i2 < h2) then
- str := (str + '''' + ATSA[i][i2] + ''', ')
- else
- str := (str + '''' + ATSA[i][i2] + '''');
- WriteLn(str);
- SetLength(ATSA[i], 0);
- end;
- SetLength(ATSA, 0);
- WriteLn('');
- end;
- SetLength(TSA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="TSAToATSAByName"]
- [simba]function TSAToATSAByName(TSA: TStringArray): T2DStringArray;
- var
- i, i2, i3, h, h2, hR, l: Integer;
- begin
- SetLength(Result, 0);
- h := High(TSA);
- for i := 0 to h do
- begin
- hR := High(Result);
- for i2 := 0 to hR do
- begin
- h2 := High(Result[i2]);
- for i3 := 0 to h2 do
- if (TSA[i] = Result[i2][i3]) then
- Break;
- if (i3 <= h2) then
- begin
- l := Length(Result[i2]);
- SetLength(Result[i2], (l + 1));
- Result[i2][l] := string(TSA[i]);
- Break;
- end;
- end;
- if (i2 > hR) then
- begin
- SetLength(Result, (hR + 2));
- Result[(hR + 1)] := [string(TSA[i])];
- end;
- end;
- end;
- var
- TSA: TStringArray;
- ATSA: T2DStringArray;
- h, h2, i, i2: Integer;
- begin
- ClearDebug;
- TSA := ['Test1', 'Test1', 'Test2', 'Test0', 'Test1', 'Test3', 'Test0',
- 'Test4', 'Test2', 'Test2', 'Test4', 'Test10', 'Test5', 'Test9',
- 'Test5', 'Test8', 'Test8', 'Test8', 'Test6', 'Test7', 'Test0',
- 'Test10', 'Test1', 'Test5', 'Test6', 'Test3', 'Test'];
- ATSA := TSAToATSAByName(TSA);
- SetLength(TSA, 0);
- h := High(ATSA);
- for i := 0 to h do
- begin
- h2 := High(ATSA[i]);
- for i2 := 0 to h2 do
- WriteLn('ATSA[' + IntToStr(i) + '][' + IntToStr(i2) + ']: ' + ATSA[i][i2]);
- SetLength(ATSA[i], 0);
- end;
- SetLength(ATSA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="TSAToATSAByLength"]
- [simba]function TSAToATSAByLength(TSA: TStringArray): T2DStringArray;
- var
- i, i2, i3, h, h2, hR, l, t: Integer;
- begin
- SetLength(Result, 0);
- h := High(TSA);
- for i := 0 to h do
- begin
- t := Length(TSA[i]);
- hR := High(Result);
- for i2 := 0 to hR do
- begin
- h2 := High(Result[i2]);
- for i3 := 0 to h2 do
- if (t = Length(Result[i2][i3])) then
- Break;
- if (i3 <= h2) then
- begin
- l := Length(Result[i2]);
- SetLength(Result[i2], (l + 1));
- Result[i2][l] := string(TSA[i]);
- Break;
- end;
- end;
- if (i2 > hR) then
- begin
- SetLength(Result, (hR + 2));
- Result[(hR + 1)] := [string(TSA[i])];
- end;
- end;
- end;
- var
- TSA: TStringArray;
- ATSA: T2DStringArray;
- h, h2, i, i2: Integer;
- begin
- ClearDebug;
- TSA := ['Test1', 'Test1', 'Test2', 'Test0', 'Test1', 'Test3', 'Test0',
- 'Test4', 'Test2', 'Test2', 'Test4', 'Test10', 'Test5', 'Test9',
- 'Test5', 'Test8', 'Test8', 'Test8', 'Test6', 'Test7', 'Test0',
- 'Test10', 'Test1', 'Test5', 'Test6', 'Test3', 'Test'];
- ATSA := TSAToATSAByLength(TSA);
- SetLength(TSA, 0);
- h := High(ATSA);
- for i := 0 to h do
- begin
- h2 := High(ATSA[i]);
- for i2 := 0 to h2 do
- WriteLn('ATSA[' + IntToStr(i) + '][' + IntToStr(i2) + ']: ' + ATSA[i][i2]);
- SetLength(ATSA[i], 0);
- end;
- SetLength(ATSA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="ATSAMerge"]
- [simba]function ATSAMerge(ATSA: T2DStringArray): TStringArray;
- var
- i, i2, h, h2, r: Integer;
- begin
- h := High(ATSA);
- if (h > -1) then
- begin
- for i := 0 to h do
- IncEx(r, (High(ATSA[i]) + 1));
- SetLength(Result, r);
- r := 0;
- for i := 0 to h do
- begin
- h2 := High(ATSA[i]);
- for i2 := 0 to h2 do
- begin
- Result[r] := string(ATSA[i][i2]);
- Inc(r);
- end;
- end;
- end else
- SetLength(Result, 0);
- end;
- var
- ATSA: T2DStringArray;
- TSA: TStringArray;
- h, i: Integer;
- begin
- ClearDebug;
- SetLength(ATSA, 4);
- ATSA[0] := ['Test0'];
- ATSA[1] := ['Test1', 'Test2'];
- ATSA[2] := ['Test3', 'Test4', 'Test5'];
- ATSA[3] := ['Test6', 'Test7', 'Test8', 'Test9'];
- TSA := ATSAMerge(ATSA);
- SetLength(ATSA, 0);
- h := High(TSA);
- for i := 0 to h do
- WriteLn('TSA[' + IntToStr(i) + ']: ' + TSA[i]);
- SetLength(TSA, 0);
- end.[/simba]
- [/spoiler]
- More to come later... :stirthepot:
- Feel free to use/take/improve/anything - it's all open-source - I don't even need credit.
- Enjoy!
- -Jani
Advertisement
Add Comment
Please, Sign In to add comment