//The "original" StrCSpn-C-Implementation --> corresponds to "PosSetEx" in StrUtils
//throws the whole Source-String char-by-char at "Reject", checking if it appears there
//if yes, return that found position --> WTF??
//Looking at the Source-code of PosSetEx in strutils.pp it's doing the same
//If the Source-String is really long, and the first occurence of a "Reject"-Character
//is very late........ talk about wasted performance
//Note: StrCSpn is "one off" compared to PosSetEx
function StrCSpn(const Source:String;const Reject:String):LongWord;
Var
i:LongWord;
f:LongWord=0; //Counter for "found rejects"
a:LongWord;
ls:LongWord;
lr:LongWord;
r:Array Of LongWord;
Function Min(Const AArray:Array Of LongWord):LongWord;
Var
i:LongWord;
Begin
//If there is only one Member, return it immediately
If Length(AArray)=1 Then Exit(AArray[0]);
//Setting Result to failure
//To conform with PosSetEx the next line should read:
//Result:=ls;
Result:=ls+1;
For i:=Low(AArray) To High(AArray) Do
If AArray[i]<Result Then Result:=AArray[i];
end;
begin
//Sanity-Check: Whyever would you pass an empty Source or Reject-String??
ls:=Length(Source);
lr:=Length(Reject);
If ls=0 Then Exit(0);
If lr=0 Then Exit(ls);
//We're only interested in the first occurence of each "Reject"-Character
//So the Max for "first occurences" is the length of Reject -->
//each character in reject appears at least once
SetLength(r,lr);
For i:=1 To lr Do
Begin
//Getting the first Position of each "Reject"-Character
//There might be "0" in there if a "Reject"-Character wasn't found.
//We're excluding them
a:=Pos(Reject[i],Source);
If a>0 Then
Begin
r[f]:=a;
Inc(f);
end;
end;
//Cutting off unneeded members, since they're initialized with "0"
SetLength(r,f);
//Looking for the lowest Value, We already excluded the "not found"
//Only makes sense to send at "Min" if at least 1 Reject-Character has been found
//Otherwise return length of Source
//To conform with PosSetEx the next line should read:
//If Length(r)>0 Then a:=Min(r) Else a:=0;
If Length(r)>0 Then a:=Min(r)-1 Else a:=ls;
Result:=a;
end;