1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618 |
- {%mainunit syshelpers.pp}
- { ---------------------------------------------------------------------
- TGUIDHelper
- ---------------------------------------------------------------------}
- Procedure NotImplemented(S : String);
- begin
- Raise Exception.Create('Not yet implemented : '+S);
- end;
- Class function TGUIDHelper.Create(const Data; BigEndian: Boolean): TGUID; overload; static;
- Const
- GUIDSize = SizeOf(TGUID);
- Var
- B : Array[1..GUIDSize] of Byte;
- begin
- Move(Data,B,GUIDSize);
- Result:=Create(B,0,BigEndian);
- end;
- class function TGUIDHelper.Create(const Data: array of Byte; AStartIndex: Cardinal; BigEndian: Boolean): TGUID; overload; static;
- Var
- A : Cardinal;
- B,C : Word;
- begin
- if ((System.Length(Data)-AStartIndex)<16) then
- raise EArgumentException.CreateFmt('The length of a GUID array must be at least %d',[]);
- Move(Data[AStartIndex],A,SizeOf(Cardinal));
- Move(Data[AStartIndex+4],B,SizeOf(Word));
- Move(Data[AStartIndex+6],C,SizeOf(Word));
- // Writeln('BigEndian : ',BigEndian,', CPU bigendian : ',(CPUendian=TEndian.Big));
- if BigEndian<>(CPUendian=TEndian.Big) then
- begin
- // Writeln('Swapping');
- A:=SwapEndian(A);
- B:=SwapEndian(B);
- C:=SwapEndian(C);
- end;
- Result:=Create(A,B,C,Data[AStartIndex+8],Data[AStartIndex+9],Data[AStartIndex+10],Data[AStartIndex+11],Data[AStartIndex+12],Data[AStartIndex+13],Data[AStartIndex+14],Data[AStartIndex+15]);
- end;
- Class Function TGUIDHelper.Create(const Data; DataEndian: TEndian = CPUEndian): TGUID; overload; static; inline;
- begin
- Result:=Create(Data,DataEndian=TEndian.Big)
- end;
- Class Function TGUIDHelper.Create(const B: TBytes; DataEndian: TEndian = CPUEndian): TGUID; overload; static; inline;
- begin
- Result:=Create(B,0,DataEndian);
- end;
- Class Function TGUIDHelper.Create(const B: TBytes; AStartIndex: Cardinal; DataEndian: TEndian = CPUEndian): TGUID; overload; static;
- begin
- if ((System.Length(B)-AStartIndex)<16) then
- raise EArgumentException.CreateFmt('The length of a GUID array must be at least %d',[]);
- Result:=Create(B,AStartIndex,DataEndian=TEndian.Big);
- end;
- Class Function TGUIDHelper.Create(const S: string): TGUID; overload; static;
- begin
- Result:=StringToGUID(S);
- end;
- Class Function TGUIDHelper.Create(A: Integer; B: SmallInt; C: SmallInt; const D: TBytes): TGUID; overload; static;
- begin
- if (System.Length(D)<>8) then
- raise EArgumentException.CreateFmt('The length of a GUID array must be %d',[]);
- Result:=Create(Cardinal(A),Word(B),Word(C),D[0],D[1],D[2],D[3],D[4],D[5],D[6],D[7]);
- end;
- Class Function TGUIDHelper.Create(A: Integer; B: SmallInt; C: SmallInt; D, E, F, G, H, I, J, K: Byte): TGUID; overload; static;
- begin
- Result:=Create(Cardinal(A),Word(B),Word(C),D,E,F,G,H,I,J,K);
- end;
- Class Function TGUIDHelper.Create(A: Cardinal; B: Word; C: Word; D, E, F, G, H, I, J, K: Byte): TGUID; overload; static;
- begin
- Result.D1 := Cardinal(A);
- Result.D2 := Word(B);
- Result.D3 := Word(C);
- Result.D4[0] := D;
- Result.D4[1] := E;
- Result.D4[2] := F;
- Result.D4[3] := G;
- Result.D4[4] := H;
- Result.D4[5] := I;
- Result.D4[6] := J;
- Result.D4[7] := K;
- end;
- Class Function TGUIDHelper.NewGuid: TGUID; static;
- begin
- CreateGUID(Result)
- end;
- Function TGUIDHelper.ToByteArray(DataEndian: TEndian = CPUEndian): TBytes;
- begin
- SetLength(Result, 16);
- if DataEndian<>CPUEndian then
- begin
- PCardinal(@Result[0])^ := SwapEndian(D1);
- PWord(@Result[4])^ := SwapEndian(D2);
- PWord(@Result[6])^ := SwapEndian(D3);
- Move(D4, Result[8], 8);
- end
- else
- Move(D1, Result[0], SizeOf(Self));
- end;
- Function TGUIDHelper.ToString(SkipBrackets : Boolean = False): string;
- begin
- Result:=GuidToString(Self);
- If SkipBrackets then
- Result:=Copy(Result,2,Length(Result)-2);
- end;
- { ---------------------------------------------------------------------
- TStringHelper
- ---------------------------------------------------------------------}
- Function HaveChar(AChar : Char; const AList: array of Char) : Boolean;
- Var
- I : Integer;
- begin
- I:=0;
- Result:=False;
- While (Not Result) and (I<Length(AList)) do
- begin
- Result:=(AList[i]=AChar);
- Inc(I);
- end;
- end;
- function TStringHelper.GetChar(AIndex: Integer): Char;
- begin
- Result:=Self[AIndex+1];
- end;
- function TStringHelper.GetLength: Integer;
- begin
- Result:=System.Length(Self);
- end;
- class function TStringHelper.Compare(const A: string; const B: string): Integer;
- begin
- Result:=Compare(A,0,B,0,System.Length(B),[]);
- end;
- class function TStringHelper.Compare(const A: string; const B: string;
- IgnoreCase: Boolean): Integer; //deprecated 'Use same with TCompareOptions';
- begin
- if IgnoreCase then
- Result:=Compare(A,B,[coIgnoreCase])
- else
- Result:=Compare(A,B,[]);
- end;
- class function TStringHelper.Compare(const A: string; const B: string;
- Options: TCompareOptions): Integer;
- begin
- Result:=Compare(A,0,B,0,System.Length(B),Options);
- end;
- class function TStringHelper.Compare(const A: string; IndexA: Integer;
- const B: string; IndexB: Integer; ALen: Integer): Integer;
- begin
- Result:=Compare(A,IndexA,B,IndexB,ALen,[]);
- end;
- class function TStringHelper.Compare(const A: string; IndexA: Integer;
- const B: string; IndexB: Integer; ALen: Integer; IgnoreCase: Boolean
- ): Integer; //deprecated 'Use same with TCompareOptions';
- begin
- if IgnoreCase then
- Result:=Compare(A,IndexA,B,IndexB,ALen,[coIgnoreCase])
- else
- Result:=Compare(A,IndexA,B,IndexB,ALen,[])
- end;
- class function TStringHelper.Compare(const A: string; IndexA: Integer;
- const B: string; IndexB: Integer; ALen: Integer; Options: TCompareOptions
- ): Integer;
- Var
- l : Integer;
- begin
- L:=ALen;
- If (L>system.Length(A)-IndexA) then
- L:=system.Length(A)-IndexA;
- If (L>system.Length(B)-IndexB) then
- L:=system.Length(B)-IndexB;
- if (coIgnoreCase in Options) then
- begin
- Result:=strlicomp(PAnsiChar(@A[IndexA+1]),PAnsiChar(@B[IndexB+1]),L)
- end
- else
- Result:=strlcomp(PAnsiChar(@A[IndexA+1]),PAnsiChar(@B[IndexB+1]),L);
- end;
- class function TStringHelper.CompareOrdinal(const A: string; const B: string
- ): Integer;
- Var
- L : Integer;
- begin
- L:=System.Length(B);
- if L>System.Length(A) then
- L:=System.Length(A);
- Result:=CompareOrdinal(A,0,B,0,L);
- end;
- class function TStringHelper.CompareOrdinal(const A: string; IndexA: Integer;
- const B: string; IndexB: Integer; ALen: Integer): Integer;
- begin
- Result:=StrLComp(PAnsiChar(@A[IndexA+1]), PAnsiChar(@B[IndexB+1]), ALen);
- end;
- class function TStringHelper.CompareText(const A: string; const B: string
- ): Integer;
- begin
- Result:=Sysutils.CompareText(A,B);
- end;
- class function TStringHelper.Copy(const Str: string): string;
- begin
- Result:=Str;
- UniqueString(Result);
- end;
- class function TStringHelper.Create(AChar: Char; ACount: Integer): string;
- begin
- Result:=StringOfChar(AChar,ACount);
- end;
- class function TStringHelper.Create(const AValue: array of Char): string;
- begin
- Result:=Create(AValue,0,System.Length(AValue));
- end;
- class function TStringHelper.Create(const AValue: array of Char;
- StartIndex: Integer; ALen: Integer): string;
- begin
- SetLength(Result,ALen);
- if ALen>0 then
- Move(AValue[StartIndex],Result[1],ALen);
- end;
- class function TStringHelper.EndsText(const ASubText, AText: string): Boolean;
- begin
- Result:=(ASubText<>'') and (sysutils.CompareText(System.Copy(AText,System.Length(AText)-System.Length(ASubText)+1,System.Length(ASubText)),ASubText)=0);
- end;
- class function TStringHelper.Equals(const a: string; const b: string): Boolean;
- begin
- Result:=A=B;
- end;
- class function TStringHelper.Format(const AFormat: string;
- const args: array of const): string;
- begin
- Result:=Sysutils.Format(AFormat,Args);
- end;
- class function TStringHelper.IsNullOrEmpty(const AValue: string): Boolean;
- begin
- Result:=system.Length(AValue)=0;
- end;
- class function TStringHelper.IsNullOrWhiteSpace(const AValue: string): Boolean;
- begin
- Result:=system.Length(SysUtils.Trim(AValue))=0;
- end;
- class function TStringHelper.Join(const Separator: string;
- const Values: array of const): string;
- Var
- SValues : Array of string;
- I,L : Integer;
- S : String;
- P : ^TVarRec;
- begin
- L:=System.Length(Values);
- SetLength(SValues,L);
- Dec(L);
- for I:=0 to L do
- begin
- S:='';
- P:=@Values[I];
- Case P^.VType of
- vtInteger : S:=IntToStr(P^.VInteger);
- vtBoolean : S:=BoolToStr(P^.VBoolean, True);
- vtChar : S:=P^.VChar;
- vtPChar : S:= string(P^.VPChar);
- {$ifndef FPUNONE}
- vtExtended : S:=FloatToStr(P^.VExtended^);
- {$endif}
- vtObject : S:=TObject(P^.VObject).Classname;
- vtClass : S:=P^.VClass.Classname;
- vtCurrency : S:=CurrToStr(P^.VCurrency^);
- vtVariant : S:=(P^.VVariant^);
- vtInt64 : S:=IntToStr(PInt64(P^.VInt64)^);
- vtQword : S:=IntToStr(PQWord(P^.VQword)^);
- vtWideChar : S:=WideString(P^.VWideChar);
- vtPWideChar : S:=WideString(P^.VPWideChar);
- vtUnicodeString : S:=UnicodeString(P^.VUnicodeString);
- vtAnsiString : S:=Ansistring(P^.VAnsiString);
- else
- S:=Format('Unknown type: %d',[P^.VType]);
- end;
- SValues[I]:=S;
- end;
- Result:=Join(Separator,SValues);
- end;
- class function TStringHelper.Join(const Separator: string;
- const Values: array of string): string;
- begin
- Result:=Join(Separator,Values,0,System.Length(Values));
- end;
- class function TStringHelper.Join(const Separator: string;
- const Values: array of string; StartIndex: Integer; ACount: Integer): string;
- Var
- I,L,VLen : integer;
- begin
- VLen:=High(Values);
- If (ACount<0) or ((StartIndex>0) and (StartIndex>VLen)) then
- raise ERangeError.Create(SRangeError);
- If (ACount=0) or (VLen<0) then
- Result:=''
- else
- begin
- L:=StartIndex+ACount-1;
- if L>Vlen then
- L:=VLen;
- Result:=Values[StartIndex];
- For I:=StartIndex+1 to L do
- Result:=Result+Separator+Values[I];
- end;
- end;
- class function TStringHelper.LowerCase(const S: string): string;
- begin
- Result:=sysutils.Lowercase(S);
- end;
- class function TStringHelper.Parse(const AValue: Boolean): string;
- begin
- Result:=BoolToStr(AValue);
- end;
- class function TStringHelper.Parse(const AValue: Extended): string;
- begin
- Result:=FloatToStr(AValue);
- end;
- class function TStringHelper.Parse(const AValue: Int64): string;
- begin
- Result:=IntToStr(AValue);
- end;
- class function TStringHelper.Parse(const AValue: Integer): string;
- begin
- Result:=IntToStr(AValue);
- end;
- class function TStringHelper.ToBoolean(const S: string): Boolean;
- begin
- Result:=StrToBool(S);
- end;
- class function TStringHelper.ToDouble(const S: string): Double;
- begin
- Result:=StrToFloat(S);
- end;
- class function TStringHelper.ToExtended(const S: string): Extended;
- begin
- Result:=StrToFloat(S);
- end;
- class function TStringHelper.ToInt64(const S: string): Int64;
- begin
- Result:=StrToInt64(S);
- end;
- class function TStringHelper.ToInteger(const S: string): Integer;
- begin
- Result:=StrToInt(S);
- end;
- class function TStringHelper.ToSingle(const S: string): Single;
- begin
- Result:=StrToFloat(S);
- end;
- class function TStringHelper.UpperCase(const S: string): string;
- begin
- Result:=sysutils.Uppercase(S);
- end;
- function TStringHelper.CompareTo(const B: string): Integer;
- begin
- // Order is important
- Result:=sysUtils.StrComp(PChar(Self),PChar(B));
- end;
- procedure TStringHelper.CopyTo(SourceIndex: Integer; var destination: array of Char; DestinationIndex: Integer; ACount: Integer);
- Var
- P1,P2 : PChar;
- begin
- // Writeln('((',DestinationIndex,'+',ACount,')<',System.Length(Destination),') : ', ((DestinationIndex+ACount)<System.Length(Destination)));
- if ((DestinationIndex+ACount)<=System.Length(Destination)) then
- begin
- // Writeln('AHA');
- P1:=@Self[SourceIndex+1];
- P2:=@Destination[DestinationIndex];
- Move(P1^,P2^,ACount*SizeOf(Char));
- end;
- end;
- function TStringHelper.Contains(const AValue: string): Boolean;
- begin
- Result:=Pos(AValue,Self)>0;
- end;
- function TStringHelper.CountChar(const C: Char): Integer;
- Var
- S : Char;
- begin
- Result:=0;
- For S in Self do
- if (S=C) then
- Inc(Result);
- end;
- function TStringHelper.DeQuotedString: string;
- begin
- Result:=DeQuotedString('''');
- end;
- function TStringHelper.DeQuotedString(const AQuoteChar: Char): string;
- var
- L,I : Integer;
- Res : Array of Char;
- PS,PD : PChar;
- IsQuote : Boolean;
- begin
- L:=System.Length(Self);
- if (L<2) or Not ((Self[1]=AQuoteChar) and (Self[L]=AQuoteChar)) then
- Exit(Self);
- SetLength(Res,L);
- IsQuote:=False;
- PS:=@Self[2];
- PD:=@Res[0];
- For I:=2 to L-1 do
- begin
- if (PS^=AQuoteChar) then
- begin
- IsQuote:=Not IsQuote;
- if Not IsQuote then
- begin
- PD^:=PS^;
- Inc(PD);
- end;
- end
- else
- begin
- if IsQuote then
- IsQuote:=false;
- PD^:=PS^;
- Inc(PD);
- end;
- Inc(PS);
- end;
- SetString(Result,@Res[0],PD-@Res[0]);
- end;
- function TStringHelper.EndsWith(const AValue: string): Boolean;
- begin
- Result:=EndsWith(AValue,False);
- end;
- function TStringHelper.EndsWith(const AValue: string; IgnoreCase: Boolean): Boolean;
- Var
- L : integer;
- S : String;
- begin
- L:=system.Length(AVAlue);
- Result:=L>0;
- if Result then
- begin
- S:=system.Copy(Self,Length-L+1,L);
- Result:=system.Length(S)=L;
- if Result then
- if IgnoreCase then
- Result:=CompareText(S,AValue)=0
- else
- Result:=S=AValue;
- end;
- end;
- function TStringHelper.Equals(const AValue: string): Boolean;
- begin
- Result:=(Self=AValue);
- end;
- function TStringHelper.Format(const args: array of const): string;
- begin
- Result:=Format(Self,Args);
- end;
- function TStringHelper.GetHashCode: Integer;
- // Taken from contnrs, fphash
- var
- P,pmax : PChar;
- begin
- {$push}
- {$Q-}
- Result:=0;
- P:=Pchar(Self);
- pmax:=p+length;
- while (p<pmax) do
- begin
- Result:=LongWord(LongInt(Result shl 5) - LongInt(Result)) xor LongWord(P^);
- Inc(p);
- end;
- {$pop}
- end;
- function TStringHelper.IndexOf(AValue: Char): Integer;
- begin
- Result:=IndexOf(AValue,0,Length);
- end;
- function TStringHelper.IndexOf(const AValue: string): Integer;
- begin
- Result:=IndexOf(AValue,0,Length);
- end;
- function TStringHelper.IndexOf(AValue: Char; StartIndex: Integer): Integer;
- begin
- Result:=IndexOf(AValue,StartIndex,Length);
- end;
- function TStringHelper.IndexOf(const AValue: string; StartIndex: Integer
- ): Integer;
- begin
- Result:=IndexOf(AValue,StartIndex,Length);
- end;
- function TStringHelper.IndexOf(AValue: Char; StartIndex: Integer;
- ACount: Integer): Integer;
- Var
- S : String;
- begin
- S:=System.Copy(Self,StartIndex+1,ACount);
- Result:=Pos(AValue,S)-1;
- if Result<>-1 then
- Result:=Result+StartIndex;
- end;
- function TStringHelper.IndexOf(const AValue: string; StartIndex: Integer;
- ACount: Integer): Integer;
- Var
- S : String;
- begin
- S:=System.Copy(Self,StartIndex+1,ACount);
- Result:=Pos(AValue,S)-1;
- if Result<>-1 then
- Result:=Result+StartIndex;
- end;
- function TStringHelper.IndexOfUnQuoted(const AValue: string; StartQuote,
- EndQuote: Char; StartIndex: Integer = 0): Integer;
- Var
- LV : Integer;
- Function MatchAt(I : Integer) : Boolean ; Inline;
- Var
- J : integer;
- begin
- J:=1;
- Repeat
- Result:=(Self[I+J-1]=AValue[j]);
- Inc(J);
- Until (Not Result) or (J>LV);
- end;
- Var
- I,L: Integer;
- Q : Integer;
- begin
- Result:=-1;
- LV:=system.Length(AValue);
- L:=Length-LV+1;
- if L<0 then
- L:=0;
- I:=StartIndex+1;
- Q:=0;
- if StartQuote=EndQuote then
- begin
- While (Result=-1) and (I<=L) do
- begin
- if (Self[I]=StartQuote) then
- Q:=1-Q;
- if (Q=0) and MatchAt(i) then
- Result:=I-1;
- Inc(I);
- end;
- end
- else
- begin
- While (Result=-1) and (I<=L) do
- begin
- if Self[I]=StartQuote then
- Inc(Q)
- else if (Self[I]=EndQuote) and (Q>0) then
- Dec(Q);
- if (Q=0) and MatchAt(i) then
- Result:=I-1;
- Inc(I);
- end;
- end;
- end;
- function TStringHelper.IndexOfAny(const AnyOf: array of Char): Integer;
- begin
- Result:=IndexOfAny(AnyOf,0,Length);
- end;
- function TStringHelper.IndexOfAny(const AnyOf: array of Char;
- StartIndex: Integer): Integer;
- begin
- Result:=IndexOfAny(AnyOf,StartIndex,Length);
- end;
- function TStringHelper.IndexOfAny(const AnyOf: array of Char;
- StartIndex: Integer; ACount: Integer): Integer;
- Var
- i,L : Integer;
- begin
- I:=StartIndex+1;
- L:=I+ACount-1;
- If L>Length then
- L:=Length;
- Result:=-1;
- While (Result=-1) and (I<=L) do
- begin
- if HaveChar(Self[i],AnyOf) then
- Result:=I-1;
- Inc(I);
- end;
- end;
- function TStringHelper.IndexOfAny(const AnyOf: array of String): Integer;
- begin
- Result:=IndexOfAny(AnyOf,0,Length);
- end;
- function TStringHelper.IndexOfAny(const AnyOf: array of String;
- StartIndex: Integer): Integer;
- begin
- Result:=IndexOfAny(AnyOf,StartIndex,Length-StartIndex);
- end;
- function TStringHelper.IndexOfAny(const AnyOf: array of String;
- StartIndex: Integer; ACount: Integer): Integer;
- Var
- M : Integer;
- begin
- Result:=IndexOfAny(AnyOf,StartIndex,ACount,M);
- end;
- function TStringHelper.IndexOfAny(const AnyOf: array of String;
- StartIndex: Integer; ACount: Integer; out AMatch: Integer): Integer;
- Var
- L,I : Integer;
- begin
- Result:=-1;
- For I:=0 to System.Length(AnyOf)-1 do
- begin
- L:=IndexOf(AnyOf[i],StartIndex,ACount);
- If (L>=0) and ((Result=-1) or (L<Result)) then
- begin
- Result:=L;
- AMatch:=I;
- end;
- end;
- end;
- function TStringHelper.IndexOfAnyUnquoted(const AnyOf: array of Char;
- StartQuote, EndQuote: Char): Integer;
- begin
- Result:=IndexOfAnyUnquoted(AnyOf,StartQuote,EndQuote,0,Length);
- end;
- function TStringHelper.IndexOfAnyUnquoted(const AnyOf: array of Char;
- StartQuote, EndQuote: Char; StartIndex: Integer): Integer;
- begin
- Result:=IndexOfAnyUnquoted(AnyOf,StartQuote,EndQuote,StartIndex,Length);
- end;
- function TStringHelper.IndexOfAnyUnquoted(const AnyOf: array of Char;
- StartQuote, EndQuote: Char; StartIndex: Integer; ACount: Integer): Integer;
- Var
- I,L : Integer;
- Q : Integer;
- begin
- Result:=-1;
- L:=StartIndex+ACount-1;
- if L>Length then
- L:=Length;
- I:=StartIndex+1;
- Q:=0;
- if StartQuote=EndQuote then
- begin
- While (Result=-1) and (I<=L) do
- begin
- if (Self[I]=StartQuote) then
- Q:=1-Q;
- if (Q=0) and HaveChar(Self[i],AnyOf) then
- Result:=I-1;
- Inc(I);
- end;
- end
- else
- begin
- While (Result=-1) and (I<=L) do
- begin
- if Self[I]=StartQuote then
- Inc(Q)
- else if (Self[I]=EndQuote) and (Q>0) then
- Dec(Q);
- if (Q=0) and HaveChar(Self[i],AnyOf) then
- Result:=I-1;
- Inc(I);
- end;
- end;
- end;
- function TStringHelper.IndexOfAnyUnquoted(const AnyOf: array of string;
- StartQuote, EndQuote: Char; StartIndex: Integer; out Matched: Integer
- ): Integer;
- Var
- L,I : Integer;
- begin
- Result:=-1;
- For I:=0 to System.Length(AnyOf)-1 do
- begin
- L:=IndexOfUnquoted(AnyOf[i],StartQuote,EndQuote,StartIndex);
- If (L>=0) and ((Result=-1) or (L<Result)) then
- begin
- Result:=L;
- Matched:=I;
- end;
- end;
- end;
- function TStringHelper.Insert(StartIndex: Integer; const AValue: string
- ): string;
- begin
- system.Insert(AValue,Self,StartIndex+1);
- Result:=Self;
- end;
- function TStringHelper.IsDelimiter(const Delimiters: string; Index: Integer
- ): Boolean;
- begin
- Result:=sysutils.IsDelimiter(Delimiters,Self,Index+1);
- end;
- function TStringHelper.IsEmpty: Boolean;
- begin
- Result:=(Length=0)
- end;
- function TStringHelper.LastDelimiter(const Delims: string): Integer;
- begin
- Result:=sysutils.LastDelimiter(Delims,Self)-1;
- end;
- function TStringHelper.LastIndexOf(AValue: Char): Integer;
- begin
- Result:=LastIndexOf(AValue,Length-1,Length);
- end;
- function TStringHelper.LastIndexOf(const AValue: string): Integer;
- begin
- Result:=LastIndexOf(AValue,Length-1,Length);
- end;
- function TStringHelper.LastIndexOf(AValue: Char; AStartIndex: Integer): Integer;
- begin
- Result:=LastIndexOf(AValue,AStartIndex,Length);
- end;
- function TStringHelper.LastIndexOf(const AValue: string; AStartIndex: Integer
- ): Integer;
- begin
- Result:=LastIndexOf(AValue,AStartIndex,Length);
- end;
- function TStringHelper.LastIndexOf(AValue: Char; AStartIndex: Integer;
- ACount: Integer): Integer;
- Var
- Min : Integer;
- begin
- Result:=AStartIndex+1;
- Min:=Result-ACount+1;
- If Min<1 then
- Min:=1;
- While (Result>=Min) and (Self[Result]<>AValue) do
- Dec(Result);
- if Result<Min then
- Result:=-1
- else
- Result:=Result-1;
- end;
- function TStringHelper.LastIndexOf(const AValue: string; AStartIndex: Integer;
- ACount: Integer): Integer;
- begin
- Result:=LastIndexOf(AValue,AStartIndex,AStartIndex+1);
- end;
- function TStringHelper.LastIndexOfAny(const AnyOf: array of Char): Integer;
- begin
- Result:=LastIndexOfAny(AnyOf,Length-1,Length);
- end;
- function TStringHelper.LastIndexOfAny(const AnyOf: array of Char;
- AStartIndex: Integer): Integer;
- begin
- Result:=LastIndexOfAny(AnyOf,AStartIndex,Length);
- end;
- function TStringHelper.LastIndexOfAny(const AnyOf: array of Char;
- AStartIndex: Integer; ACount: Integer): Integer;
- Var
- Min : Integer;
- begin
- Result:=AStartIndex+1;
- Min:=Result-ACount+1;
- If Min<1 then
- Min:=1;
- While (Result>=Min) and Not HaveChar(Self[Result],AnyOf) do
- Dec(Result);
- if Result<Min then
- Result:=-1
- else
- Result:=Result-1;
- end;
- function TStringHelper.PadLeft(ATotalWidth: Integer): string;
- begin
- Result:=PadLeft(ATotalWidth,' ');
- end;
- function TStringHelper.PadLeft(ATotalWidth: Integer; PaddingChar: Char): string;
- Var
- L : Integer;
- begin
- Result:=Self;
- L:=ATotalWidth-Length;
- If L>0 then
- Result:=StringOfChar(PaddingChar,L)+Result;
- end;
- function TStringHelper.PadRight(ATotalWidth: Integer): string;
- begin
- Result:=PadRight(ATotalWidth,' ');
- end;
- function TStringHelper.PadRight(ATotalWidth: Integer; PaddingChar: Char
- ): string;
- Var
- L : Integer;
- begin
- Result:=Self;
- L:=ATotalWidth-Length;
- If L>0 then
- Result:=Result+StringOfChar(PaddingChar,L);
- end;
- function TStringHelper.QuotedString: string;
- begin
- Result:=QuotedStr(Self);
- end;
- function TStringHelper.QuotedString(const AQuoteChar: Char): string;
- begin
- Result:=AnsiQuotedStr(Self,AQuoteChar);
- end;
- function TStringHelper.Remove(StartIndex: Integer): string;
- begin
- Result:=Remove(StartIndex,Self.Length-StartIndex);
- end;
- function TStringHelper.Remove(StartIndex: Integer; ACount: Integer): string;
- begin
- Result:=Self;
- System.Delete(Result,StartIndex+1,ACount);
- end;
- function TStringHelper.Replace(OldChar: Char; NewChar: Char): string;
- begin
- Result:=Replace(OldChar,NewChar,[rfReplaceAll]);
- end;
- function TStringHelper.Replace(OldChar: Char; NewChar: Char;
- ReplaceFlags: TReplaceFlags): string;
- begin
- Result:=StringReplace(Self,OldChar,NewChar,ReplaceFlags);
- end;
- function TStringHelper.Replace(const OldValue: string; const NewValue: string
- ): string;
- begin
- Result:=Replace(OldValue,NewValue,[rfReplaceAll]);
- end;
- function TStringHelper.Replace(const OldValue: string; const NewValue: string;
- ReplaceFlags: TReplaceFlags): string;
- begin
- Result:=StringReplace(Self,OldValue,NewValue,ReplaceFlags);
- end;
- function TStringHelper.Split(const Separators: array of Char): TStringArray;
- begin
- Result:=SPlit(Separators,#0,#0,Length,TStringSplitOptions([]));
- end;
- function TStringHelper.Split(const Separators: array of Char; ACount: Integer
- ): TStringArray;
- begin
- Result:=SPlit(Separators,#0,#0,ACount,TStringSplitOptions([]));
- end;
- function TStringHelper.Split(const Separators: array of Char;
- Options: TStringSplitOptions): TStringArray;
- begin
- Result:=SPlit(Separators,Length,Options);
- end;
- function TStringHelper.Split(const Separators: array of Char; ACount: Integer;
- Options: TStringSplitOptions): TStringArray;
- begin
- Result:=SPlit(Separators,#0,#0,ACount,Options);
- end;
- function TStringHelper.Split(const Separators: array of string): TStringArray;
- begin
- Result:=Split(Separators,Length);
- end;
- function TStringHelper.Split(const Separators: array of string; ACount: Integer
- ): TStringArray;
- begin
- Result:=Split(Separators,ACount,TStringSplitOptions([]));
- end;
- function TStringHelper.Split(const Separators: array of string;
- Options: TStringSplitOptions): TStringArray;
- begin
- Result:=Split(Separators,Length,Options);
- end;
- function TStringHelper.Split(const Separators: array of string;
- ACount: Integer; Options: TStringSplitOptions): TStringArray;
- begin
- Result:=Split(Separators,#0,#0,ACount,Options);
- end;
- function TStringHelper.Split(const Separators: array of Char; AQuote: Char
- ): TStringArray;
- begin
- Result:=Split(Separators,AQuote,AQuote);
- end;
- function TStringHelper.Split(const Separators: array of Char; AQuoteStart,
- AQuoteEnd: Char): TStringArray;
- begin
- Result:=Split(Separators,AQuoteStart,AQuoteEnd,TStringSplitOptions([]));
- end;
- function TStringHelper.Split(const Separators: array of Char; AQuoteStart,
- AQuoteEnd: Char; Options: TStringSplitOptions): TStringArray;
- begin
- Result:=Split(Separators,AQuoteStart,AQuoteEnd,Length,Options);
- end;
- function TStringHelper.Split(const Separators: array of Char; AQuoteStart,
- AQuoteEnd: Char; ACount: Integer): TStringArray;
- begin
- Result:=Split(Separators,AQuoteStart,AQuoteEnd,ACount,TStringSplitOptions([]));
- end;
- function TStringHelper.Split(const Separators: array of Char; AQuoteStart,
- AQuoteEnd: Char; ACount: Integer; Options: TStringSplitOptions): TStringArray;
- Const
- BlockSize = 10;
- Function NextSep(StartIndex : integer) : Integer;
- begin
- if (AQuoteStart<>#0) then
- Result:=Self.IndexOfAnyUnQuoted(Separators,AQuoteStart,AQuoteEnd,StartIndex)
- else
- Result:=Self.IndexOfAny(Separators,StartIndex);
- end;
- Procedure MaybeGrow(Curlen : Integer);
- begin
- if System.Length(Result)<=CurLen then
- SetLength(Result,System.Length(Result)+BlockSize);
- end;
- Var
- Sep,LastSep,Len : integer;
- T : String;
- begin
- SetLength(Result,BlockSize);
- Len:=0;
- LastSep:=0;
- Sep:=NextSep(0);
- While (Sep<>-1) and ((ACount=0) or (Len<ACount)) do
- begin
- T:=SubString(LastSep,Sep-LastSep);
- // Writeln('Examining >',T,'< at pos,',LastSep,' till pos ',Sep);
- If (T<>'') or (not (ExcludeEmpty=Options)) then
- begin
- MaybeGrow(Len);
- Result[Len]:=T;
- Inc(Len);
- end;
- LastSep:=Sep+1;
- Sep:=NextSep(LastSep);
- end;
- if (LastSep<Length-1) and ((ACount=0) or (Len<ACount)) then
- begin
- T:=SubString(LastSep);
- // Writeln('Examining >',T,'< at pos,',LastSep,' till pos ',Sep);
- MaybeGrow(Len);
- Result[Len]:=T;
- Inc(Len);
- end;
- SetLength(Result,Len);
- end;
- function TStringHelper.Split(const Separators: array of string; AQuote: Char
- ): TStringArray;
- begin
- Result:=SPlit(Separators,AQuote,AQuote);
- end;
- function TStringHelper.Split(const Separators: array of string; AQuoteStart,
- AQuoteEnd: Char): TStringArray;
- begin
- Result:=SPlit(Separators,AQuoteStart,AQuoteEnd,Length,TStringSplitOptions([]));
- end;
- function TStringHelper.Split(const Separators: array of string; AQuoteStart,
- AQuoteEnd: Char; Options: TStringSplitOptions): TStringArray;
- begin
- Result:=SPlit(Separators,AQuoteStart,AQuoteEnd,Length,Options);
- end;
- function TStringHelper.Split(const Separators: array of string; AQuoteStart,
- AQuoteEnd: Char; ACount: Integer): TStringArray;
- begin
- Result:=SPlit(Separators,AQuoteStart,AQuoteEnd,ACount,TStringSplitOptions([]));
- end;
- function TStringHelper.Split(const Separators: array of string; AQuoteStart,
- AQuoteEnd: Char; ACount: Integer; Options: TStringSplitOptions): TStringArray;
- Const
- BlockSize = 10;
- Function NextSep(StartIndex : integer; out Match : Integer) : Integer;
- begin
- if (AQuoteStart<>#0) then
- Result:=Self.IndexOfAnyUnQuoted(Separators,AQuoteStart,AQuoteEnd,StartIndex,Match)
- else
- Result:=Self.IndexOfAny(Separators,StartIndex,Length,Match);
- if Result<>-1 then
- end;
- Procedure MaybeGrow(Curlen : Integer);
- begin
- if System.Length(Result)<=CurLen then
- SetLength(Result,System.Length(Result)+BlockSize);
- end;
- Var
- Sep,LastSep,Len,Match : integer;
- T : String;
- begin
- SetLength(Result,BlockSize);
- Len:=0;
- LastSep:=0;
- Sep:=NextSep(0,Match);
- While (Sep<>-1) and ((ACount=0) or (Len<ACount)) do
- begin
- T:=SubString(LastSep,Sep-LastSep);
- If (T<>'') or (not (ExcludeEmpty=Options)) then
- begin
- MaybeGrow(Len);
- Result[Len]:=T;
- Inc(Len);
- end;
- LastSep:=Sep+System.Length(Separators[Match]);
- Sep:=NextSep(LastSep,Match);
- end;
- if (LastSep<Length-1) and ((ACount=0) or (Len<ACount)) then
- begin
- T:=SubString(LastSep);
- // Writeln('Examining >',T,'< at pos,',LastSep,' till pos ',Sep);
- MaybeGrow(Len);
- Result[Len]:=T;
- Inc(Len);
- end;
- SetLength(Result,Len);
- end;
- function TStringHelper.StartsWith(const AValue: string): Boolean;
- begin
- Result:=StartsWith(AValue,False);
- end;
- function TStringHelper.StartsWith(const AValue: string; IgnoreCase: Boolean
- ): Boolean;
- Var
- L : Integer;
- begin
- L:=System.Length(AValue);
- Result:=L>0;
- if Result then
- if IgnoreCase then
- Result:=StrLiComp(PChar(AValue),PChar(Self),L)=0
- else
- Result:=StrLComp(PChar(AValue),PChar(Self),L)=0
- end;
- function TStringHelper.Substring(AStartIndex: Integer): string;
- begin
- Result:=Self.SubString(AStartIndex,Self.Length-AStartIndex);
- end;
- function TStringHelper.Substring(AStartIndex: Integer; ALen: Integer): string;
- begin
- Result:=system.Copy(Self,AStartIndex+1,ALen);
- end;
- function TStringHelper.ToBoolean: Boolean;
- begin
- Result:=StrToBool(Self);
- end;
- function TStringHelper.ToInteger: Integer;
- begin
- Result:=StrToInt(Self);
- end;
- function TStringHelper.ToInt64: Int64;
- begin
- Result:=StrToInt64(Self);
- end;
- function TStringHelper.ToSingle: Single;
- begin
- Result:=StrToFLoat(Self);
- end;
- function TStringHelper.ToDouble: Double;
- begin
- Result:=StrToFLoat(Self);
- end;
- function TStringHelper.ToExtended: Extended;
- begin
- Result:=StrToFLoat(Self);
- end;
- function TStringHelper.ToCharArray: TCharArray;
- begin
- Result:=ToCharArray(0,Self.Length);
- end;
- function TStringHelper.ToCharArray(AStartIndex: Integer; ALen: Integer
- ): TCharArray;
- Var
- I : Integer;
- begin
- SetLength(Result,ALen);
- For I:=0 to ALen-1 do
- Result[I]:=Self[AStartIndex+I+1];
- end;
- function TStringHelper.ToLower: string;
- begin
- Result:=LowerCase(Self);
- end;
- function TStringHelper.ToLowerInvariant: string;
- begin
- Result:=LowerCase(Self);
- end;
- function TStringHelper.ToUpper: string;
- begin
- Result:=UpperCase(Self);
- end;
- function TStringHelper.ToUpperInvariant: string;
- begin
- Result:=UpperCase(Self);
- end;
- function TStringHelper.Trim: string;
- begin
- Result:=SysUtils.Trim(Self);
- end;
- function TStringHelper.TrimLeft: string;
- begin
- Result:=SysUtils.TrimLeft(Self);
- end;
- function TStringHelper.TrimRight: string;
- begin
- Result:=SysUtils.TrimRight(Self);
- end;
- function TStringHelper.Trim(const ATrimChars: array of Char): string;
- begin
- Result:=Self.TrimLeft(ATrimChars).TrimRight(ATrimChars);
- end;
- function TStringHelper.TrimLeft(const ATrimChars: array of Char): string;
- Var
- I,Len : Integer;
- begin
- I:=1;
- Len:=Self.Length;
- While (I<=Len) and HaveChar(Self[i],ATrimChars) do Inc(I);
- if I=1 then
- Result:=Self
- else if I>Len then
- Result:=''
- else
- Result:=system.Copy(Self,I,Len-I+1);
- end;
- function TStringHelper.TrimRight(const ATrimChars: array of Char): string;
- Var
- I,Len : Integer;
- begin
- Len:=Self.Length;
- I:=Len;
- While (I>=1) and HaveChar(Self[i],ATrimChars) do Dec(I);
- if I<1 then
- Result:=''
- else if I=Len then
- Result:=Self
- else
- Result:=system.Copy(Self,1,I);
- end;
- function TStringHelper.TrimEnd(const ATrimChars: array of Char): string;
- begin
- Result:=TrimRight(ATrimChars);
- end;
- function TStringHelper.TrimStart(const ATrimChars: array of Char): string;
- begin
- Result:=TrimLeft(ATrimChars);
- end;
- { ---------------------------------------------------------------------
- TSingleHelper
- ---------------------------------------------------------------------}
- {$MACRO ON}
- {$IFDEF FPC_HAS_TYPE_SINGLE}
- {$define TFLOATHELPER:=TSingleHelper}
- {$define FLOATTYPE:=Single}
- {$i syshelpf.inc}
- {$ENDIF FPC_HAS_TYPE_SINGLE}
- { ---------------------------------------------------------------------
- TDoubleHelper
- ---------------------------------------------------------------------}
- {$IFDEF FPC_HAS_TYPE_DOUBLE}
- {$define TFLOATHELPER:=TDoubleHelper}
- {$define FLOATTYPE:=Double}
- {$i syshelpf.inc}
- {$ENDIF FPC_HAS_TYPE_DOUBLE}
- { ---------------------------------------------------------------------
- TExtendedHelper
- ---------------------------------------------------------------------}
- {$ifdef FPC_HAS_TYPE_EXTENDED}
- {$define TFLOATHELPER:=TExtendedHelper}
- {$define FLOATTYPE:=Extended}
- {$i syshelpf.inc}
- {$ENDIF FPC_HAS_TYPE_EXTENDED}
- { ---------------------------------------------------------------------
- TByteHelper
- ---------------------------------------------------------------------}
- {$define TORDINALHELPER:=TByteHelper}
- {$define TORDINALTYPE:=Byte}
- {$i syshelpo.inc}
- { ---------------------------------------------------------------------
- TShortintHelper
- ---------------------------------------------------------------------}
- {$define TORDINALHELPER:=TShortIntHelper}
- {$define TORDINALTYPE:=ShortInt}
- {$i syshelpo.inc}
- { ---------------------------------------------------------------------
- TSmallintHelper
- ---------------------------------------------------------------------}
- {$define TORDINALHELPER:=TSmallIntHelper}
- {$define TORDINALTYPE:=SmallInt}
- {$i syshelpo.inc}
- { ---------------------------------------------------------------------
- TWordHelper
- ---------------------------------------------------------------------}
- {$define TORDINALHELPER:=TWordHelper}
- {$define TORDINALTYPE:=Word}
- {$i syshelpo.inc}
- { ---------------------------------------------------------------------
- TCardinalHelper
- ---------------------------------------------------------------------}
- {$define TORDINALHELPER:=TCardinalHelper}
- {$define TORDINALTYPE:=Cardinal}
- {$i syshelpo.inc}
- { ---------------------------------------------------------------------
- TIntegerHelper
- ---------------------------------------------------------------------}
- {$define TORDINALHELPER:=TIntegerHelper}
- {$define TORDINALTYPE:=Integer}
- {$i syshelpo.inc}
- { ---------------------------------------------------------------------
- TInt64Helper
- ---------------------------------------------------------------------}
- {$define TORDINALHELPER:=TInt64Helper}
- {$define TORDINALTYPE:=Int64}
- {$i syshelpo.inc}
- { ---------------------------------------------------------------------
- TQWordHelper
- ---------------------------------------------------------------------}
- {$define TORDINALHELPER:=TQWordHelper}
- {$define TORDINALTYPE:=QWord}
- {$i syshelpo.inc}
- { ---------------------------------------------------------------------
- TNativeIntHelper
- ---------------------------------------------------------------------}
- {$define TORDINALHELPER:=TNativeIntHelper}
- {$define TORDINALTYPE:=NativeInt}
- {$i syshelpo.inc}
- { ---------------------------------------------------------------------
- TNativeUIntHelper
- ---------------------------------------------------------------------}
- {$define TORDINALHELPER:=TNativeUIntHelper}
- {$define TORDINALTYPE:=NativeUInt}
- {$i syshelpo.inc}
- { ---------------------------------------------------------------------
- TBooleanHelper
- ---------------------------------------------------------------------}
- {$define TBOOLHELPER:=TBooleanHelper}
- {$define TBOOLTYPE:=Boolean}
- {$i syshelpb.inc}
- { ---------------------------------------------------------------------
- TByteBoolHelper
- ---------------------------------------------------------------------}
- {$define TBOOLHELPER:=TByteBoolHelper}
- {$define TBOOLTYPE:=ByteBool}
- {$i syshelpb.inc}
- { ---------------------------------------------------------------------
- TWordBoolHelper
- ---------------------------------------------------------------------}
- {$define TBOOLHELPER:=TWordBoolHelper}
- {$define TBOOLTYPE:=WordBool}
- {$i syshelpb.inc}
- { ---------------------------------------------------------------------
- TLongBoolHelper
- ---------------------------------------------------------------------}
- {$define TBOOLHELPER:=TLongBoolHelper}
- {$define TBOOLTYPE:=LongBool}
- {$i syshelpb.inc}
|