Official Soldat Forums

Server Talk => Scripting Releases => Topic started by: CurryWurst on February 08, 2009, 05:52:09 am

Title: IXSplit
Post by: CurryWurst on February 08, 2009, 05:52:09 am
Script Name: IXSplit
Script Description Improved version of XSplit
Author: Dual (http://soldatcentral.com/index.php?page=profile&u=57)

Hosted by: Soldat Central - http://soldatcentral.com/ (http://soldatcentral.com/)
Core Version: 2.6.3
Compile Test: (http://soldatcentral.com/images/pass.gif) Passed

IXSplit() splits a string into an array of strings. SOURCE determines a string to be splitted by the Delimiter.
To put it in a nutshell, it's an improved version of the common used XSplit (http://forums.soldat.pl/index.php?topic=12742.msg142224#msg142224).

Code: [Select]
function IXSplit(const SOURCE: string; Delimiter: string): array of string;
var
  i, x, d: integer;
  s, b: string;
begin
  d:= length(Delimiter);
  i:= 1;
  SetArrayLength(Result, 0);
  while (i <= length(SOURCE)) do
  begin
    s:= Copy(SOURCE, i, d);
    if (s = Delimiter) then
    begin
      SetArrayLength(Result, x + 1);
      Result[x]:= b;
      Inc(i, d);
      Inc(x, 1);
      b:= '';
    end else
    begin
      b:= b + Copy(s, 1, 1);
      Inc(i, 1);
    end;
  end;
  if (b <> '') then
  begin
    SetArrayLength(Result, x + 1);
    Result[x]:= b;
  end;
end;

Parameters:

SOURCE: string
String to be splitted into an array of string.

Delimiter: string
Defines a string as split point.

Return Value:

Depending on the amount of delimiters the SOURCE string contains, the elements count of the string array will increase.

Note:

IXSplit() will not return any array element if SOURCE is blank.
In case the delimiter is not found in the string, IXSplit() will return one array element containing the whole SOURCE string.

If you intent to split a string which is read from a file I recommend to use this function:

function ReadFromFile(File_Name: string): string;
begin
  Result:= ReadFile(File_Name);
  Result:= Copy(Result, 0, length(Result) - 2);
end;

You find more information about this problem here (http://forums.soldat.pl/index.php?topic=32817).


(http://soldatcentral.com/images/download.gif) (http://soldatcentral.com/dl.php?id=100&act=1)
(Size 629 B)
- http://soldatcentral.com/index.php?page=script&f=100 -


** Script hosted by Soldat Central (http://soldatcentral.com/index.php?page=script&f=100)! Please visit the author's script page (http://soldatcentral.com/index.php?page=script&f=100) and Rate this script **
Title: Re: IXSplit
Post by: iDante on February 08, 2009, 10:34:39 am
SOURCE: array of string
Array of string to be splitted into an array of string.
ORLY?

On another note, I prefer to use this:
Pretty sure it was written by Curt.
Code: [Select]
function Explode(source, delim: string): array of string;
begin
source := source + delim;
while strpos(delim, source) <> 0 do begin
SetArrayLength(Result, GetArrayLength(Result) + 1);
Result[GetArrayLength(Result) - 1] := copy(source, 0, strpos(delim, source));
source := copy(source, strpos(delim, source) + 1, length(source));
end;
end;
Title: Re: IXSplit
Post by: CurryWurst on February 08, 2009, 10:49:53 am
SOURCE: array of string
Array of string to be splitted into an array of string.
ORLY?

Oops, thanks for hint :)
Actually it should be:

SOURCE: string
String to be splitted into an array of string.

Code: [Select]
function Explode(source, delim: string): array of string;
begin
source := source + delim;
while strpos(delim, source) <> 0 do begin
SetArrayLength(Result, GetArrayLength(Result) + 1);
Result[GetArrayLength(Result) - 1] := copy(source, 0, strpos(delim, source));
source := copy(source, strpos(delim, source) + 1, length(source));
end;
end;

Nice clear function. I've never seen it before.
Seems to work quite well. Good job Curt! :D
Title: Re: IXSplit
Post by: DorkeyDear on February 08, 2009, 11:00:04 am
Just to verify, yeah that is mine. (looks like slight change of looks though, lol)