syshelp.inc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  1. {%mainunit syshelpers.pp}
  2. { ---------------------------------------------------------------------
  3. TGUIDHelper
  4. ---------------------------------------------------------------------}
  5. Procedure NotImplemented(S : String);
  6. begin
  7. Raise Exception.Create('Not yet implemented : '+S);
  8. end;
  9. Class function TGUIDHelper.Create(const Data; BigEndian: Boolean): TGUID; overload; static;
  10. Const
  11. GUIDSize = SizeOf(TGUID);
  12. Var
  13. B : Array[1..GUIDSize] of Byte;
  14. begin
  15. Move(Data,B,GUIDSize);
  16. Result:=Create(B,0,BigEndian);
  17. end;
  18. class function TGUIDHelper.Create(const Data: array of Byte; AStartIndex: Cardinal; BigEndian: Boolean): TGUID; overload; static;
  19. Var
  20. A : Cardinal;
  21. B,C : Word;
  22. begin
  23. if ((System.Length(Data)-AStartIndex)<16) then
  24. raise EArgumentException.CreateFmt('The length of a GUID array must be at least %d',[]);
  25. Move(Data[AStartIndex],A,SizeOf(Cardinal));
  26. Move(Data[AStartIndex+4],B,SizeOf(Word));
  27. Move(Data[AStartIndex+6],C,SizeOf(Word));
  28. // Writeln('BigEndian : ',BigEndian,', CPU bigendian : ',(CPUendian=TEndian.Big));
  29. if BigEndian<>(CPUendian=TEndian.Big) then
  30. begin
  31. // Writeln('Swapping');
  32. A:=SwapEndian(A);
  33. B:=SwapEndian(B);
  34. C:=SwapEndian(C);
  35. end;
  36. 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]);
  37. end;
  38. Class Function TGUIDHelper.Create(const Data; DataEndian: TEndian = CPUEndian): TGUID; overload; static; inline;
  39. begin
  40. Result:=Create(Data,DataEndian=TEndian.Big)
  41. end;
  42. Class Function TGUIDHelper.Create(const B: TBytes; DataEndian: TEndian = CPUEndian): TGUID; overload; static; inline;
  43. begin
  44. Result:=Create(B,0,DataEndian);
  45. end;
  46. Class Function TGUIDHelper.Create(const B: TBytes; AStartIndex: Cardinal; DataEndian: TEndian = CPUEndian): TGUID; overload; static;
  47. begin
  48. if ((System.Length(B)-AStartIndex)<16) then
  49. raise EArgumentException.CreateFmt('The length of a GUID array must be at least %d',[]);
  50. Result:=Create(B,AStartIndex,DataEndian=TEndian.Big);
  51. end;
  52. Class Function TGUIDHelper.Create(const S: string): TGUID; overload; static;
  53. begin
  54. Result:=StringToGUID(S);
  55. end;
  56. Class Function TGUIDHelper.Create(A: Integer; B: SmallInt; C: SmallInt; const D: TBytes): TGUID; overload; static;
  57. begin
  58. if (System.Length(D)<>8) then
  59. raise EArgumentException.CreateFmt('The length of a GUID array must be %d',[]);
  60. Result:=Create(Cardinal(A),Word(B),Word(C),D[0],D[1],D[2],D[3],D[4],D[5],D[6],D[7]);
  61. end;
  62. Class Function TGUIDHelper.Create(A: Integer; B: SmallInt; C: SmallInt; D, E, F, G, H, I, J, K: Byte): TGUID; overload; static;
  63. begin
  64. Result:=Create(Cardinal(A),Word(B),Word(C),D,E,F,G,H,I,J,K);
  65. end;
  66. Class Function TGUIDHelper.Create(A: Cardinal; B: Word; C: Word; D, E, F, G, H, I, J, K: Byte): TGUID; overload; static;
  67. begin
  68. Result.D1 := Cardinal(A);
  69. Result.D2 := Word(B);
  70. Result.D3 := Word(C);
  71. Result.D4[0] := D;
  72. Result.D4[1] := E;
  73. Result.D4[2] := F;
  74. Result.D4[3] := G;
  75. Result.D4[4] := H;
  76. Result.D4[5] := I;
  77. Result.D4[6] := J;
  78. Result.D4[7] := K;
  79. end;
  80. Class Function TGUIDHelper.NewGuid: TGUID; static;
  81. begin
  82. CreateGUID(Result)
  83. end;
  84. Function TGUIDHelper.ToByteArray(DataEndian: TEndian = CPUEndian): TBytes;
  85. begin
  86. SetLength(Result, 16);
  87. if DataEndian<>CPUEndian then
  88. begin
  89. PCardinal(@Result[0])^ := SwapEndian(D1);
  90. PWord(@Result[4])^ := SwapEndian(D2);
  91. PWord(@Result[6])^ := SwapEndian(D3);
  92. Move(D4, Result[8], 8);
  93. end
  94. else
  95. Move(D1, Result[0], SizeOf(Self));
  96. end;
  97. Function TGUIDHelper.ToString(SkipBrackets : Boolean = False): string;
  98. begin
  99. Result:=GuidToString(Self);
  100. If SkipBrackets then
  101. Result:=Copy(Result,2,Length(Result)-2);
  102. end;
  103. { ---------------------------------------------------------------------
  104. TStringHelper
  105. ---------------------------------------------------------------------}
  106. Function HaveChar(AChar : Char; const AList: array of Char) : Boolean;
  107. Var
  108. I : SizeInt;
  109. begin
  110. I:=0;
  111. Result:=False;
  112. While (Not Result) and (I<Length(AList)) do
  113. begin
  114. Result:=(AList[i]=AChar);
  115. Inc(I);
  116. end;
  117. end;
  118. function TStringHelper.GetChar(AIndex: SizeInt): Char;
  119. begin
  120. Result:=Self[AIndex+1];
  121. end;
  122. function TStringHelper.GetLength: SizeInt;
  123. begin
  124. Result:=System.Length(Self);
  125. end;
  126. class function TStringHelper.Compare(const A: string; const B: string): Integer;
  127. begin
  128. Result:=Compare(A,0,B,0,System.Length(B),[]);
  129. end;
  130. class function TStringHelper.Compare(const A: string; const B: string;
  131. IgnoreCase: Boolean): Integer; //deprecated 'Use same with TCompareOptions';
  132. begin
  133. if IgnoreCase then
  134. Result:=Compare(A,B,[coIgnoreCase])
  135. else
  136. Result:=Compare(A,B,[]);
  137. end;
  138. class function TStringHelper.Compare(const A: string; const B: string;
  139. Options: TCompareOptions): Integer;
  140. begin
  141. Result:=Compare(A,0,B,0,System.Length(B),Options);
  142. end;
  143. class function TStringHelper.Compare(const A: string; IndexA: SizeInt;
  144. const B: string; IndexB: SizeInt; ALen: SizeInt): Integer;
  145. begin
  146. Result:=Compare(A,IndexA,B,IndexB,ALen,[]);
  147. end;
  148. class function TStringHelper.Compare(const A: string; IndexA: SizeInt;
  149. const B: string; IndexB: SizeInt; ALen: SizeInt; IgnoreCase: Boolean
  150. ): Integer; //deprecated 'Use same with TCompareOptions';
  151. begin
  152. if IgnoreCase then
  153. Result:=Compare(A,IndexA,B,IndexB,ALen,[coIgnoreCase])
  154. else
  155. Result:=Compare(A,IndexA,B,IndexB,ALen,[])
  156. end;
  157. class function TStringHelper.Compare(const A: string; IndexA: SizeInt;
  158. const B: string; IndexB: SizeInt; ALen: SizeInt; Options: TCompareOptions
  159. ): Integer;
  160. Var
  161. L : SizeInt;
  162. begin
  163. L:=ALen;
  164. If (L>system.Length(A)-IndexA) then
  165. L:=system.Length(A)-IndexA;
  166. If (L>system.Length(B)-IndexB) then
  167. L:=system.Length(B)-IndexB;
  168. if (coIgnoreCase in Options) then
  169. begin
  170. Result:=strlicomp(PAnsiChar(@A[IndexA+1]),PAnsiChar(@B[IndexB+1]),L)
  171. end
  172. else
  173. Result:=strlcomp(PAnsiChar(@A[IndexA+1]),PAnsiChar(@B[IndexB+1]),L);
  174. end;
  175. class function TStringHelper.CompareOrdinal(const A: string; const B: string
  176. ): Integer;
  177. Var
  178. L : SizeInt;
  179. begin
  180. L:=System.Length(B);
  181. if L>System.Length(A) then
  182. L:=System.Length(A);
  183. Result:=CompareOrdinal(A,0,B,0,L);
  184. end;
  185. class function TStringHelper.CompareOrdinal(const A: string; IndexA: SizeInt;
  186. const B: string; IndexB: SizeInt; ALen: SizeInt): Integer;
  187. begin
  188. Result:=StrLComp(PAnsiChar(@A[IndexA+1]), PAnsiChar(@B[IndexB+1]), ALen);
  189. end;
  190. class function TStringHelper.CompareText(const A: string; const B: string
  191. ): Integer;
  192. begin
  193. Result:=Sysutils.CompareText(A,B);
  194. end;
  195. class function TStringHelper.Copy(const Str: string): string;
  196. begin
  197. Result:=Str;
  198. UniqueString(Result);
  199. end;
  200. class function TStringHelper.Create(AChar: Char; ACount: SizeInt): string;
  201. begin
  202. Result:=StringOfChar(AChar,ACount);
  203. end;
  204. class function TStringHelper.Create(const AValue: array of Char): string;
  205. begin
  206. Result:=Create(AValue,0,System.Length(AValue));
  207. end;
  208. class function TStringHelper.Create(const AValue: array of Char;
  209. StartIndex: SizeInt; ALen: SizeInt): string;
  210. begin
  211. SetLength(Result,ALen);
  212. if ALen>0 then
  213. Move(AValue[StartIndex],Result[1],ALen);
  214. end;
  215. class function TStringHelper.EndsText(const ASubText, AText: string): Boolean;
  216. begin
  217. Result:=(ASubText<>'') and (sysutils.CompareText(System.Copy(AText,System.Length(AText)-System.Length(ASubText)+1,System.Length(ASubText)),ASubText)=0);
  218. end;
  219. class function TStringHelper.Equals(const a: string; const b: string): Boolean;
  220. begin
  221. Result:=A=B;
  222. end;
  223. class function TStringHelper.Format(const AFormat: string;
  224. const args: array of const): string;
  225. begin
  226. Result:=Sysutils.Format(AFormat,Args);
  227. end;
  228. class function TStringHelper.IsNullOrEmpty(const AValue: string): Boolean;
  229. begin
  230. Result:=system.Length(AValue)=0;
  231. end;
  232. class function TStringHelper.IsNullOrWhiteSpace(const AValue: string): Boolean;
  233. begin
  234. Result:=system.Length(SysUtils.Trim(AValue))=0;
  235. end;
  236. class function TStringHelper.Join(const Separator: string;
  237. const Values: array of const): string;
  238. Var
  239. SValues : Array of string;
  240. I,L : SizeInt;
  241. S : String;
  242. P : ^TVarRec;
  243. begin
  244. L:=System.Length(Values);
  245. SetLength(SValues,L);
  246. Dec(L);
  247. for I:=0 to L do
  248. begin
  249. S:='';
  250. P:=@Values[I];
  251. Case P^.VType of
  252. vtInteger : S:=IntToStr(P^.VInteger);
  253. vtBoolean : S:=BoolToStr(P^.VBoolean, True);
  254. vtChar : S:=P^.VChar;
  255. vtPChar : S:= string(P^.VPChar);
  256. {$ifndef FPUNONE}
  257. vtExtended : S:=FloatToStr(P^.VExtended^);
  258. {$endif}
  259. vtObject : S:=TObject(P^.VObject).Classname;
  260. vtClass : S:=P^.VClass.Classname;
  261. vtCurrency : S:=CurrToStr(P^.VCurrency^);
  262. vtVariant : S:=(P^.VVariant^);
  263. vtInt64 : S:=IntToStr(PInt64(P^.VInt64)^);
  264. vtQword : S:=IntToStr(PQWord(P^.VQword)^);
  265. vtWideChar : S:=WideString(P^.VWideChar);
  266. vtPWideChar : S:=WideString(P^.VPWideChar);
  267. vtUnicodeString : S:=UnicodeString(P^.VUnicodeString);
  268. vtAnsiString : S:=Ansistring(P^.VAnsiString);
  269. else
  270. S:=Format('Unknown type: %d',[P^.VType]);
  271. end;
  272. SValues[I]:=S;
  273. end;
  274. Result:=Join(Separator,SValues);
  275. end;
  276. class function TStringHelper.Join(const Separator: string;
  277. const Values: array of string): string;
  278. begin
  279. Result:=Join(Separator,Values,0,System.Length(Values));
  280. end;
  281. class function TStringHelper.Join(const Separator: string;
  282. const Values: array of string; StartIndex: SizeInt; ACount: SizeInt): string;
  283. Var
  284. I,L,VLen : SizeInt;
  285. begin
  286. VLen:=High(Values);
  287. If (ACount<0) or ((StartIndex>0) and (StartIndex>VLen)) then
  288. raise ERangeError.Create(SRangeError);
  289. If (ACount=0) or (VLen<0) then
  290. Result:=''
  291. else
  292. begin
  293. L:=StartIndex+ACount-1;
  294. if L>Vlen then
  295. L:=VLen;
  296. Result:=Values[StartIndex];
  297. For I:=StartIndex+1 to L do
  298. Result:=Result+Separator+Values[I];
  299. end;
  300. end;
  301. class function TStringHelper.LowerCase(const S: string): string;
  302. begin
  303. Result:=sysutils.Lowercase(S);
  304. end;
  305. class function TStringHelper.Parse(const AValue: Boolean): string;
  306. begin
  307. Result:=BoolToStr(AValue);
  308. end;
  309. class function TStringHelper.Parse(const AValue: Extended): string;
  310. begin
  311. Result:=FloatToStr(AValue);
  312. end;
  313. class function TStringHelper.Parse(const AValue: Int64): string;
  314. begin
  315. Result:=IntToStr(AValue);
  316. end;
  317. class function TStringHelper.Parse(const AValue: Integer): string;
  318. begin
  319. Result:=IntToStr(AValue);
  320. end;
  321. class function TStringHelper.ToBoolean(const S: string): Boolean;
  322. begin
  323. Result:=StrToBool(S);
  324. end;
  325. class function TStringHelper.ToDouble(const S: string): Double;
  326. begin
  327. Result:=StrToFloat(S);
  328. end;
  329. class function TStringHelper.ToExtended(const S: string): Extended;
  330. begin
  331. Result:=StrToFloat(S);
  332. end;
  333. class function TStringHelper.ToInt64(const S: string): Int64;
  334. begin
  335. Result:=StrToInt64(S);
  336. end;
  337. class function TStringHelper.ToInteger(const S: string): Integer;
  338. begin
  339. Result:=StrToInt(S);
  340. end;
  341. class function TStringHelper.ToSingle(const S: string): Single;
  342. begin
  343. Result:=StrToFloat(S);
  344. end;
  345. class function TStringHelper.UpperCase(const S: string): string;
  346. begin
  347. Result:=sysutils.Uppercase(S);
  348. end;
  349. function TStringHelper.CompareTo(const B: string): Integer;
  350. begin
  351. // Order is important
  352. Result:=sysUtils.StrComp(PChar(Self),PChar(B));
  353. end;
  354. procedure TStringHelper.CopyTo(SourceIndex: SizeInt; var destination: array of Char; DestinationIndex: SizeInt; ACount: SizeInt);
  355. Var
  356. P1,P2 : PChar;
  357. begin
  358. // Writeln('((',DestinationIndex,'+',ACount,')<',System.Length(Destination),') : ', ((DestinationIndex+ACount)<System.Length(Destination)));
  359. if ((DestinationIndex+ACount)<=System.Length(Destination)) then
  360. begin
  361. // Writeln('AHA');
  362. P1:=@Self[SourceIndex+1];
  363. P2:=@Destination[DestinationIndex];
  364. Move(P1^,P2^,ACount*SizeOf(Char));
  365. end;
  366. end;
  367. function TStringHelper.Contains(const AValue: string): Boolean;
  368. begin
  369. Result:=Pos(AValue,Self)>0;
  370. end;
  371. function TStringHelper.CountChar(const C: Char): SizeInt;
  372. Var
  373. S : Char;
  374. begin
  375. Result:=0;
  376. For S in Self do
  377. if (S=C) then
  378. Inc(Result);
  379. end;
  380. function TStringHelper.DeQuotedString: string;
  381. begin
  382. Result:=DeQuotedString('''');
  383. end;
  384. function TStringHelper.DeQuotedString(const AQuoteChar: Char): string;
  385. var
  386. L,I : SizeInt;
  387. Res : Array of Char;
  388. PS,PD : PChar;
  389. IsQuote : Boolean;
  390. begin
  391. L:=System.Length(Self);
  392. if (L<2) or Not ((Self[1]=AQuoteChar) and (Self[L]=AQuoteChar)) then
  393. Exit(Self);
  394. SetLength(Res,L);
  395. IsQuote:=False;
  396. PS:=@Self[2];
  397. PD:=@Res[0];
  398. For I:=2 to L-1 do
  399. begin
  400. if (PS^=AQuoteChar) then
  401. begin
  402. IsQuote:=Not IsQuote;
  403. if Not IsQuote then
  404. begin
  405. PD^:=PS^;
  406. Inc(PD);
  407. end;
  408. end
  409. else
  410. begin
  411. if IsQuote then
  412. IsQuote:=false;
  413. PD^:=PS^;
  414. Inc(PD);
  415. end;
  416. Inc(PS);
  417. end;
  418. SetString(Result,@Res[0],PD-@Res[0]);
  419. end;
  420. function TStringHelper.EndsWith(const AValue: string): Boolean;
  421. begin
  422. Result:=EndsWith(AValue,False);
  423. end;
  424. function TStringHelper.EndsWith(const AValue: string; IgnoreCase: Boolean): Boolean;
  425. Var
  426. L : SizeInt;
  427. S : String;
  428. begin
  429. L:=system.Length(AVAlue);
  430. Result:=L=0;
  431. if Not Result then
  432. begin
  433. S:=system.Copy(Self,Length-L+1,L);
  434. Result:=system.Length(S)=L;
  435. if Result then
  436. if IgnoreCase then
  437. Result:=CompareText(S,AValue)=0
  438. else
  439. Result:=S=AValue;
  440. end;
  441. end;
  442. function TStringHelper.Equals(const AValue: string): Boolean;
  443. begin
  444. Result:=(Self=AValue);
  445. end;
  446. function TStringHelper.Format(const args: array of const): string;
  447. begin
  448. Result:=Format(Self,Args);
  449. end;
  450. function TStringHelper.GetHashCode: Integer;
  451. // Taken from contnrs, fphash
  452. var
  453. P,pmax : PChar;
  454. begin
  455. {$push}
  456. {$Q-}
  457. Result:=0;
  458. P:=Pchar(Self);
  459. pmax:=p+length;
  460. while (p<pmax) do
  461. begin
  462. Result:=LongWord(LongInt(Result shl 5) - LongInt(Result)) xor LongWord(P^);
  463. Inc(p);
  464. end;
  465. {$pop}
  466. end;
  467. function TStringHelper.IndexOf(AValue: Char): SizeInt;
  468. begin
  469. Result:=IndexOf(AValue,0,Length);
  470. end;
  471. function TStringHelper.IndexOf(const AValue: string): SizeInt;
  472. begin
  473. Result:=IndexOf(AValue,0,Length);
  474. end;
  475. function TStringHelper.IndexOf(AValue: Char; StartIndex: SizeInt): SizeInt;
  476. begin
  477. Result:=IndexOf(AValue,StartIndex,Length);
  478. end;
  479. function TStringHelper.IndexOf(const AValue: string; StartIndex: SizeInt
  480. ): SizeInt;
  481. begin
  482. Result:=IndexOf(AValue,StartIndex,Length);
  483. end;
  484. function TStringHelper.IndexOf(AValue: Char; StartIndex: SizeInt;
  485. ACount: SizeInt): SizeInt;
  486. Var
  487. S : String;
  488. begin
  489. S:=System.Copy(Self,StartIndex+1,ACount);
  490. Result:=Pos(AValue,S)-1;
  491. if Result<>-1 then
  492. Result:=Result+StartIndex;
  493. end;
  494. function TStringHelper.IndexOf(const AValue: string; StartIndex: SizeInt;
  495. ACount: SizeInt): SizeInt;
  496. Var
  497. S : String;
  498. begin
  499. S:=System.Copy(Self,StartIndex+1,ACount);
  500. Result:=Pos(AValue,S)-1;
  501. if Result<>-1 then
  502. Result:=Result+StartIndex;
  503. end;
  504. function TStringHelper.IndexOfUnQuoted(const AValue: string; StartQuote,
  505. EndQuote: Char; StartIndex: SizeInt = 0): SizeInt;
  506. Var
  507. LV : SizeInt;
  508. Function MatchAt(I : SizeInt) : Boolean ; Inline;
  509. Var
  510. J : SizeInt;
  511. begin
  512. J:=1;
  513. Repeat
  514. Result:=(Self[I+J-1]=AValue[j]);
  515. Inc(J);
  516. Until (Not Result) or (J>LV);
  517. end;
  518. Var
  519. I,L,Q: SizeInt;
  520. begin
  521. Result:=-1;
  522. LV:=system.Length(AValue);
  523. L:=Length-LV+1;
  524. if L<0 then
  525. L:=0;
  526. I:=StartIndex+1;
  527. Q:=0;
  528. if StartQuote=EndQuote then
  529. begin
  530. While (Result=-1) and (I<=L) do
  531. begin
  532. if (Self[I]=StartQuote) then
  533. Q:=1-Q;
  534. if (Q=0) and MatchAt(i) then
  535. Result:=I-1;
  536. Inc(I);
  537. end;
  538. end
  539. else
  540. begin
  541. While (Result=-1) and (I<=L) do
  542. begin
  543. if Self[I]=StartQuote then
  544. Inc(Q)
  545. else if (Self[I]=EndQuote) and (Q>0) then
  546. Dec(Q);
  547. if (Q=0) and MatchAt(i) then
  548. Result:=I-1;
  549. Inc(I);
  550. end;
  551. end;
  552. end;
  553. function TStringHelper.IndexOfAny(const AnyOf: array of Char): SizeInt;
  554. begin
  555. Result:=IndexOfAny(AnyOf,0,Length);
  556. end;
  557. function TStringHelper.IndexOfAny(const AnyOf: array of Char;
  558. StartIndex: SizeInt): SizeInt;
  559. begin
  560. Result:=IndexOfAny(AnyOf,StartIndex,Length);
  561. end;
  562. function TStringHelper.IndexOfAny(const AnyOf: array of Char;
  563. StartIndex: SizeInt; ACount: SizeInt): SizeInt;
  564. Var
  565. i,L : SizeInt;
  566. begin
  567. I:=StartIndex+1;
  568. L:=I+ACount-1;
  569. If L>Length then
  570. L:=Length;
  571. Result:=-1;
  572. While (Result=-1) and (I<=L) do
  573. begin
  574. if HaveChar(Self[i],AnyOf) then
  575. Result:=I-1;
  576. Inc(I);
  577. end;
  578. end;
  579. function TStringHelper.IndexOfAny(const AnyOf: array of String): SizeInt;
  580. begin
  581. Result:=IndexOfAny(AnyOf,0,Length);
  582. end;
  583. function TStringHelper.IndexOfAny(const AnyOf: array of String;
  584. StartIndex: SizeInt): SizeInt;
  585. begin
  586. Result:=IndexOfAny(AnyOf,StartIndex,Length-StartIndex);
  587. end;
  588. function TStringHelper.IndexOfAny(const AnyOf: array of String;
  589. StartIndex: SizeInt; ACount: SizeInt): SizeInt;
  590. Var
  591. M : SizeInt;
  592. begin
  593. Result:=IndexOfAny(AnyOf,StartIndex,ACount,M);
  594. end;
  595. function TStringHelper.IndexOfAny(const AnyOf: array of String;
  596. StartIndex: SizeInt; ACount: SizeInt; out AMatch: SizeInt): SizeInt;
  597. Var
  598. L,I : SizeInt;
  599. begin
  600. Result:=-1;
  601. For I:=0 to System.Length(AnyOf)-1 do
  602. begin
  603. L:=IndexOf(AnyOf[i],StartIndex,ACount);
  604. If (L>=0) and ((Result=-1) or (L<Result)) then
  605. begin
  606. Result:=L;
  607. AMatch:=I;
  608. end;
  609. end;
  610. end;
  611. function TStringHelper.IndexOfAnyUnquoted(const AnyOf: array of Char;
  612. StartQuote, EndQuote: Char): SizeInt;
  613. begin
  614. Result:=IndexOfAnyUnquoted(AnyOf,StartQuote,EndQuote,0,Length);
  615. end;
  616. function TStringHelper.IndexOfAnyUnquoted(const AnyOf: array of Char;
  617. StartQuote, EndQuote: Char; StartIndex: SizeInt): SizeInt;
  618. begin
  619. Result:=IndexOfAnyUnquoted(AnyOf,StartQuote,EndQuote,StartIndex,Length);
  620. end;
  621. function TStringHelper.IndexOfAnyUnquoted(const AnyOf: array of Char;
  622. StartQuote, EndQuote: Char; StartIndex: SizeInt; ACount: SizeInt): SizeInt;
  623. Var
  624. I,L : SizeInt;
  625. Q : SizeInt;
  626. begin
  627. Result:=-1;
  628. L:=StartIndex+ACount-1;
  629. if L>Length then
  630. L:=Length;
  631. I:=StartIndex+1;
  632. Q:=0;
  633. if StartQuote=EndQuote then
  634. begin
  635. While (Result=-1) and (I<=L) do
  636. begin
  637. if (Self[I]=StartQuote) then
  638. Q:=1-Q;
  639. if (Q=0) and HaveChar(Self[i],AnyOf) then
  640. Result:=I-1;
  641. Inc(I);
  642. end;
  643. end
  644. else
  645. begin
  646. While (Result=-1) and (I<=L) do
  647. begin
  648. if Self[I]=StartQuote then
  649. Inc(Q)
  650. else if (Self[I]=EndQuote) and (Q>0) then
  651. Dec(Q);
  652. if (Q=0) and HaveChar(Self[i],AnyOf) then
  653. Result:=I-1;
  654. Inc(I);
  655. end;
  656. end;
  657. end;
  658. function TStringHelper.IndexOfAnyUnquoted(const AnyOf: array of string;
  659. StartQuote, EndQuote: Char; StartIndex: SizeInt; out Matched: SizeInt
  660. ): SizeInt;
  661. Var
  662. L,I : SizeInt;
  663. begin
  664. Result:=-1;
  665. For I:=0 to System.Length(AnyOf)-1 do
  666. begin
  667. L:=IndexOfUnquoted(AnyOf[i],StartQuote,EndQuote,StartIndex);
  668. If (L>=0) and ((Result=-1) or (L<Result)) then
  669. begin
  670. Result:=L;
  671. Matched:=I;
  672. end;
  673. end;
  674. end;
  675. function TStringHelper.Insert(StartIndex: SizeInt; const AValue: string
  676. ): string;
  677. begin
  678. system.Insert(AValue,Self,StartIndex+1);
  679. Result:=Self;
  680. end;
  681. function TStringHelper.IsDelimiter(const Delimiters: string; Index: SizeInt
  682. ): Boolean;
  683. begin
  684. Result:=sysutils.IsDelimiter(Delimiters,Self,Index+1);
  685. end;
  686. function TStringHelper.IsEmpty: Boolean;
  687. begin
  688. Result:=(Length=0)
  689. end;
  690. function TStringHelper.LastDelimiter(const Delims: string): SizeInt;
  691. begin
  692. Result:=sysutils.LastDelimiter(Delims,Self)-1;
  693. end;
  694. function TStringHelper.LastIndexOf(AValue: Char): SizeInt;
  695. begin
  696. Result:=LastIndexOf(AValue,Length-1,Length);
  697. end;
  698. function TStringHelper.LastIndexOf(const AValue: string): SizeInt;
  699. begin
  700. Result:=LastIndexOf(AValue,Length-1,Length);
  701. end;
  702. function TStringHelper.LastIndexOf(AValue: Char; AStartIndex: SizeInt): SizeInt;
  703. begin
  704. Result:=LastIndexOf(AValue,AStartIndex,Length);
  705. end;
  706. function TStringHelper.LastIndexOf(const AValue: string; AStartIndex: SizeInt
  707. ): SizeInt;
  708. begin
  709. Result:=LastIndexOf(AValue,AStartIndex,Length);
  710. end;
  711. function TStringHelper.LastIndexOf(AValue: Char; AStartIndex: SizeInt;
  712. ACount: SizeInt): SizeInt;
  713. Var
  714. Min : SizeInt;
  715. begin
  716. Result:=AStartIndex+1;
  717. Min:=Result-ACount+1;
  718. If Min<1 then
  719. Min:=1;
  720. While (Result>=Min) and (Self[Result]<>AValue) do
  721. Dec(Result);
  722. if Result<Min then
  723. Result:=-1
  724. else
  725. Result:=Result-1;
  726. end;
  727. function TStringHelper.LastIndexOf(const AValue: string; AStartIndex: SizeInt; ACount: SizeInt): SizeInt;
  728. var
  729. I,L,LS,M : SizeInt;
  730. S : String;
  731. P : PChar;
  732. begin
  733. Result:=-1;
  734. LS:=system.Length(Self);
  735. L:=system.Length(AValue);
  736. if (L=0) or (L>LS) then
  737. Exit;
  738. P:=PChar(AValue);
  739. S:=Self;
  740. I:=AStartIndex+1; // 1 based
  741. if (I>LS) then
  742. I:=LS;
  743. I:=I-L+1;
  744. M:=AStartIndex-ACount+1; // 1 based
  745. if M<1 then
  746. M:=1;
  747. while (Result=-1) and (I>=M) do
  748. begin
  749. if (0=StrLComp(PChar(@S[I]),P,L)) then
  750. Result:=I-1;
  751. Dec(I);
  752. end;
  753. end;
  754. function TStringHelper.LastIndexOfAny(const AnyOf: array of Char): SizeInt;
  755. begin
  756. Result:=LastIndexOfAny(AnyOf,Length-1,Length);
  757. end;
  758. function TStringHelper.LastIndexOfAny(const AnyOf: array of Char;
  759. AStartIndex: SizeInt): SizeInt;
  760. begin
  761. Result:=LastIndexOfAny(AnyOf,AStartIndex,Length);
  762. end;
  763. function TStringHelper.LastIndexOfAny(const AnyOf: array of Char;
  764. AStartIndex: SizeInt; ACount: SizeInt): SizeInt;
  765. Var
  766. Min : SizeInt;
  767. begin
  768. Result:=AStartIndex+1;
  769. Min:=Result-ACount+1;
  770. If Min<1 then
  771. Min:=1;
  772. While (Result>=Min) and Not HaveChar(Self[Result],AnyOf) do
  773. Dec(Result);
  774. if Result<Min then
  775. Result:=-1
  776. else
  777. Result:=Result-1;
  778. end;
  779. function TStringHelper.PadLeft(ATotalWidth: SizeInt): string;
  780. begin
  781. Result:=PadLeft(ATotalWidth,' ');
  782. end;
  783. function TStringHelper.PadLeft(ATotalWidth: SizeInt; PaddingChar: Char): string;
  784. Var
  785. L : SizeInt;
  786. begin
  787. Result:=Self;
  788. L:=ATotalWidth-Length;
  789. If L>0 then
  790. Result:=StringOfChar(PaddingChar,L)+Result;
  791. end;
  792. function TStringHelper.PadRight(ATotalWidth: SizeInt): string;
  793. begin
  794. Result:=PadRight(ATotalWidth,' ');
  795. end;
  796. function TStringHelper.PadRight(ATotalWidth: SizeInt; PaddingChar: Char
  797. ): string;
  798. Var
  799. L : SizeInt;
  800. begin
  801. Result:=Self;
  802. L:=ATotalWidth-Length;
  803. If L>0 then
  804. Result:=Result+StringOfChar(PaddingChar,L);
  805. end;
  806. function TStringHelper.QuotedString: string;
  807. begin
  808. Result:=QuotedStr(Self);
  809. end;
  810. function TStringHelper.QuotedString(const AQuoteChar: Char): string;
  811. begin
  812. Result:=AnsiQuotedStr(Self,AQuoteChar);
  813. end;
  814. function TStringHelper.Remove(StartIndex: SizeInt): string;
  815. begin
  816. Result:=Remove(StartIndex,Self.Length-StartIndex);
  817. end;
  818. function TStringHelper.Remove(StartIndex: SizeInt; ACount: SizeInt): string;
  819. begin
  820. Result:=Self;
  821. System.Delete(Result,StartIndex+1,ACount);
  822. end;
  823. function TStringHelper.Replace(OldChar: Char; NewChar: Char): string;
  824. begin
  825. Result:=Replace(OldChar,NewChar,[rfReplaceAll]);
  826. end;
  827. function TStringHelper.Replace(OldChar: Char; NewChar: Char;
  828. ReplaceFlags: TReplaceFlags): string;
  829. begin
  830. Result:=StringReplace(Self,OldChar,NewChar,ReplaceFlags);
  831. end;
  832. function TStringHelper.Replace(const OldValue: string; const NewValue: string
  833. ): string;
  834. begin
  835. Result:=Replace(OldValue,NewValue,[rfReplaceAll]);
  836. end;
  837. function TStringHelper.Replace(const OldValue: string; const NewValue: string;
  838. ReplaceFlags: TReplaceFlags): string;
  839. begin
  840. Result:=StringReplace(Self,OldValue,NewValue,ReplaceFlags);
  841. end;
  842. function TStringHelper.Split(const Separators: array of Char): TStringArray;
  843. begin
  844. Result:=SPlit(Separators,#0,#0,Length,TStringSplitOptions.None);
  845. end;
  846. function TStringHelper.Split(const Separators: array of Char; ACount: SizeInt
  847. ): TStringArray;
  848. begin
  849. Result:=SPlit(Separators,#0,#0,ACount,TStringSplitOptions.None);
  850. end;
  851. function TStringHelper.Split(const Separators: array of Char;
  852. Options: TStringSplitOptions): TStringArray;
  853. begin
  854. Result:=SPlit(Separators,Length,Options);
  855. end;
  856. function TStringHelper.Split(const Separators: array of Char; ACount: SizeInt;
  857. Options: TStringSplitOptions): TStringArray;
  858. begin
  859. Result:=SPlit(Separators,#0,#0,ACount,Options);
  860. end;
  861. function TStringHelper.Split(const Separators: array of string): TStringArray;
  862. begin
  863. Result:=Split(Separators,Length);
  864. end;
  865. function TStringHelper.Split(const Separators: array of string; ACount: SizeInt
  866. ): TStringArray;
  867. begin
  868. Result:=Split(Separators,ACount,TStringSplitOptions.None);
  869. end;
  870. function TStringHelper.Split(const Separators: array of string;
  871. Options: TStringSplitOptions): TStringArray;
  872. begin
  873. Result:=Split(Separators,Length,Options);
  874. end;
  875. function TStringHelper.Split(const Separators: array of string;
  876. ACount: SizeInt; Options: TStringSplitOptions): TStringArray;
  877. begin
  878. Result:=Split(Separators,#0,#0,ACount,Options);
  879. end;
  880. function TStringHelper.Split(const Separators: array of Char; AQuote: Char
  881. ): TStringArray;
  882. begin
  883. Result:=Split(Separators,AQuote,AQuote);
  884. end;
  885. function TStringHelper.Split(const Separators: array of Char; AQuoteStart,
  886. AQuoteEnd: Char): TStringArray;
  887. begin
  888. Result:=Split(Separators,AQuoteStart,AQuoteEnd,TStringSplitOptions.None);
  889. end;
  890. function TStringHelper.Split(const Separators: array of Char; AQuoteStart,
  891. AQuoteEnd: Char; Options: TStringSplitOptions): TStringArray;
  892. begin
  893. Result:=Split(Separators,AQuoteStart,AQuoteEnd,Length,Options);
  894. end;
  895. function TStringHelper.Split(const Separators: array of Char; AQuoteStart,
  896. AQuoteEnd: Char; ACount: SizeInt): TStringArray;
  897. begin
  898. Result:=Split(Separators,AQuoteStart,AQuoteEnd,ACount,TStringSplitOptions.None);
  899. end;
  900. function TStringHelper.Split(const Separators: array of Char; AQuoteStart,
  901. AQuoteEnd: Char; ACount: SizeInt; Options: TStringSplitOptions): TStringArray;
  902. Const
  903. BlockSize = 10;
  904. Function NextSep(StartIndex : SizeInt) : SizeInt;
  905. begin
  906. if (AQuoteStart<>#0) then
  907. Result:=Self.IndexOfAnyUnQuoted(Separators,AQuoteStart,AQuoteEnd,StartIndex)
  908. else
  909. Result:=Self.IndexOfAny(Separators,StartIndex);
  910. end;
  911. Procedure MaybeGrow(Curlen : SizeInt);
  912. begin
  913. if System.Length(Result)<=CurLen then
  914. SetLength(Result,System.Length(Result)+BlockSize);
  915. end;
  916. Var
  917. Sep,LastSep,Len : SizeInt;
  918. T : String;
  919. begin
  920. SetLength(Result,BlockSize);
  921. Len:=0;
  922. LastSep:=0;
  923. Sep:=NextSep(0);
  924. While (Sep<>-1) and ((ACount=0) or (Len<ACount)) do
  925. begin
  926. T:=SubString(LastSep,Sep-LastSep);
  927. // Writeln('Examining >',T,'< at pos,',LastSep,' till pos ',Sep);
  928. If (T<>'') or (not (TStringSplitOptions.ExcludeEmpty=Options)) then
  929. begin
  930. MaybeGrow(Len);
  931. Result[Len]:=T;
  932. Inc(Len);
  933. end;
  934. LastSep:=Sep+1;
  935. Sep:=NextSep(LastSep);
  936. end;
  937. if (LastSep<Length) and ((ACount=0) or (Len<ACount)) then
  938. begin
  939. T:=SubString(LastSep);
  940. // Writeln('Examining >',T,'< at pos,',LastSep,' till pos ',Sep);
  941. MaybeGrow(Len);
  942. Result[Len]:=T;
  943. Inc(Len);
  944. end;
  945. SetLength(Result,Len);
  946. end;
  947. function TStringHelper.Split(const Separators: array of string; AQuote: Char
  948. ): TStringArray;
  949. begin
  950. Result:=SPlit(Separators,AQuote,AQuote);
  951. end;
  952. function TStringHelper.Split(const Separators: array of string; AQuoteStart,
  953. AQuoteEnd: Char): TStringArray;
  954. begin
  955. Result:=SPlit(Separators,AQuoteStart,AQuoteEnd,Length,TStringSplitOptions.None);
  956. end;
  957. function TStringHelper.Split(const Separators: array of string; AQuoteStart,
  958. AQuoteEnd: Char; Options: TStringSplitOptions): TStringArray;
  959. begin
  960. Result:=SPlit(Separators,AQuoteStart,AQuoteEnd,Length,Options);
  961. end;
  962. function TStringHelper.Split(const Separators: array of string; AQuoteStart,
  963. AQuoteEnd: Char; ACount: SizeInt): TStringArray;
  964. begin
  965. Result:=SPlit(Separators,AQuoteStart,AQuoteEnd,ACount,TStringSplitOptions.None);
  966. end;
  967. function TStringHelper.Split(const Separators: array of string; AQuoteStart,
  968. AQuoteEnd: Char; ACount: SizeInt; Options: TStringSplitOptions): TStringArray;
  969. Const
  970. BlockSize = 10;
  971. Function NextSep(StartIndex : SizeInt; out Match : SizeInt) : SizeInt;
  972. begin
  973. if (AQuoteStart<>#0) then
  974. Result:=Self.IndexOfAnyUnQuoted(Separators,AQuoteStart,AQuoteEnd,StartIndex,Match)
  975. else
  976. Result:=Self.IndexOfAny(Separators,StartIndex,Length,Match);
  977. if Result<>-1 then
  978. end;
  979. Procedure MaybeGrow(Curlen : SizeInt);
  980. begin
  981. if System.Length(Result)<=CurLen then
  982. SetLength(Result,System.Length(Result)+BlockSize);
  983. end;
  984. Var
  985. Sep,LastSep,Len,Match : SizeInt;
  986. T : String;
  987. begin
  988. SetLength(Result,BlockSize);
  989. Len:=0;
  990. LastSep:=0;
  991. Sep:=NextSep(0,Match);
  992. While (Sep<>-1) and ((ACount=0) or (Len<ACount)) do
  993. begin
  994. T:=SubString(LastSep,Sep-LastSep);
  995. If (T<>'') or (not (TStringSplitOptions.ExcludeEmpty=Options)) then
  996. begin
  997. MaybeGrow(Len);
  998. Result[Len]:=T;
  999. Inc(Len);
  1000. end;
  1001. LastSep:=Sep+System.Length(Separators[Match]);
  1002. Sep:=NextSep(LastSep,Match);
  1003. end;
  1004. if (LastSep<Length) and ((ACount=0) or (Len<ACount)) then
  1005. begin
  1006. T:=SubString(LastSep);
  1007. // Writeln('Examining >',T,'< at pos,',LastSep,' till pos ',Sep);
  1008. MaybeGrow(Len);
  1009. Result[Len]:=T;
  1010. Inc(Len);
  1011. end;
  1012. SetLength(Result,Len);
  1013. end;
  1014. function TStringHelper.StartsWith(const AValue: string): Boolean;
  1015. begin
  1016. Result:=StartsWith(AValue,False);
  1017. end;
  1018. function TStringHelper.StartsWith(const AValue: string; IgnoreCase: Boolean
  1019. ): Boolean;
  1020. Var
  1021. L : SizeInt;
  1022. S : String;
  1023. begin
  1024. L:=System.Length(AValue);
  1025. Result:=L<=0;
  1026. if not Result then
  1027. begin
  1028. S:=System.Copy(Self,1,L);
  1029. Result:=(System.Length(S)=L);
  1030. if Result then
  1031. if IgnoreCase then
  1032. Result:=SameText(S,aValue)
  1033. else
  1034. Result:=SameStr(S,AValue);
  1035. end;
  1036. end;
  1037. function TStringHelper.Substring(AStartIndex: SizeInt): string;
  1038. begin
  1039. Result:=Self.SubString(AStartIndex,Self.Length-AStartIndex);
  1040. end;
  1041. function TStringHelper.Substring(AStartIndex: SizeInt; ALen: SizeInt): string;
  1042. begin
  1043. Result:=system.Copy(Self,AStartIndex+1,ALen);
  1044. end;
  1045. function TStringHelper.ToBoolean: Boolean;
  1046. begin
  1047. Result:=StrToBool(Self);
  1048. end;
  1049. function TStringHelper.ToInteger: Integer;
  1050. begin
  1051. Result:=StrToInt(Self);
  1052. end;
  1053. function TStringHelper.ToInt64: Int64;
  1054. begin
  1055. Result:=StrToInt64(Self);
  1056. end;
  1057. function TStringHelper.ToSingle: Single;
  1058. begin
  1059. Result:=StrToFLoat(Self);
  1060. end;
  1061. function TStringHelper.ToDouble: Double;
  1062. begin
  1063. Result:=StrToFLoat(Self);
  1064. end;
  1065. function TStringHelper.ToExtended: Extended;
  1066. begin
  1067. Result:=StrToFLoat(Self);
  1068. end;
  1069. function TStringHelper.ToCharArray: TCharArray;
  1070. begin
  1071. Result:=ToCharArray(0,Self.Length);
  1072. end;
  1073. function TStringHelper.ToCharArray(AStartIndex: SizeInt; ALen: SizeInt
  1074. ): TCharArray;
  1075. Var
  1076. I : SizeInt;
  1077. begin
  1078. SetLength(Result,ALen);
  1079. For I:=0 to ALen-1 do
  1080. Result[I]:=Self[AStartIndex+I+1];
  1081. end;
  1082. function TStringHelper.ToLower: string;
  1083. begin
  1084. Result:=LowerCase(Self);
  1085. end;
  1086. function TStringHelper.ToLowerInvariant: string;
  1087. begin
  1088. Result:=LowerCase(Self);
  1089. end;
  1090. function TStringHelper.ToUpper: string;
  1091. begin
  1092. Result:=UpperCase(Self);
  1093. end;
  1094. function TStringHelper.ToUpperInvariant: string;
  1095. begin
  1096. Result:=UpperCase(Self);
  1097. end;
  1098. function TStringHelper.Trim: string;
  1099. begin
  1100. Result:=SysUtils.Trim(Self);
  1101. end;
  1102. function TStringHelper.TrimLeft: string;
  1103. begin
  1104. Result:=SysUtils.TrimLeft(Self);
  1105. end;
  1106. function TStringHelper.TrimRight: string;
  1107. begin
  1108. Result:=SysUtils.TrimRight(Self);
  1109. end;
  1110. function TStringHelper.Trim(const ATrimChars: array of Char): string;
  1111. begin
  1112. Result:=Self.TrimLeft(ATrimChars).TrimRight(ATrimChars);
  1113. end;
  1114. function TStringHelper.TrimLeft(const ATrimChars: array of Char): string;
  1115. Var
  1116. I,Len : SizeInt;
  1117. begin
  1118. I:=1;
  1119. Len:=Self.Length;
  1120. While (I<=Len) and HaveChar(Self[i],ATrimChars) do Inc(I);
  1121. if I=1 then
  1122. Result:=Self
  1123. else if I>Len then
  1124. Result:=''
  1125. else
  1126. Result:=system.Copy(Self,I,Len-I+1);
  1127. end;
  1128. function TStringHelper.TrimRight(const ATrimChars: array of Char): string;
  1129. Var
  1130. I,Len : SizeInt;
  1131. begin
  1132. Len:=Self.Length;
  1133. I:=Len;
  1134. While (I>=1) and HaveChar(Self[i],ATrimChars) do Dec(I);
  1135. if I<1 then
  1136. Result:=''
  1137. else if I=Len then
  1138. Result:=Self
  1139. else
  1140. Result:=system.Copy(Self,1,I);
  1141. end;
  1142. function TStringHelper.TrimEnd(const ATrimChars: array of Char): string;
  1143. begin
  1144. Result:=TrimRight(ATrimChars);
  1145. end;
  1146. function TStringHelper.TrimStart(const ATrimChars: array of Char): string;
  1147. begin
  1148. Result:=TrimLeft(ATrimChars);
  1149. end;
  1150. { ---------------------------------------------------------------------
  1151. TSingleHelper
  1152. ---------------------------------------------------------------------}
  1153. {$MACRO ON}
  1154. {$IFDEF FPC_HAS_TYPE_SINGLE}
  1155. {$define TFLOATHELPER:=TSingleHelper}
  1156. {$define FLOATTYPE:=Single}
  1157. {$define TFloatRec:=TSingleRec}
  1158. {$i syshelpf.inc}
  1159. {$UNDEF TFloatRec}
  1160. {$ENDIF FPC_HAS_TYPE_SINGLE}
  1161. { ---------------------------------------------------------------------
  1162. TDoubleHelper
  1163. ---------------------------------------------------------------------}
  1164. {$IFDEF FPC_HAS_TYPE_DOUBLE}
  1165. {$define TFLOATHELPER:=TDoubleHelper}
  1166. {$define FLOATTYPE:=Double}
  1167. {$define TFloatRec:=TDoubleRec}
  1168. {$i syshelpf.inc}
  1169. {$UNDEF TFloatRec}
  1170. {$ENDIF FPC_HAS_TYPE_DOUBLE}
  1171. { ---------------------------------------------------------------------
  1172. TExtendedHelper
  1173. ---------------------------------------------------------------------}
  1174. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1175. {$define TFLOATHELPER:=TExtendedHelper}
  1176. {$define FLOATTYPE:=Extended}
  1177. {$define TFloatRec:=TExtended80Rec}
  1178. {$i syshelpf.inc}
  1179. {$UNDEF TFloatRec}
  1180. {$ENDIF FPC_HAS_TYPE_EXTENDED}
  1181. { ---------------------------------------------------------------------
  1182. TByteHelper
  1183. ---------------------------------------------------------------------}
  1184. {$define TORDINALHELPER:=TByteHelper}
  1185. {$define TORDINALTYPE:=Byte}
  1186. {$i syshelpo.inc}
  1187. { ---------------------------------------------------------------------
  1188. TShortintHelper
  1189. ---------------------------------------------------------------------}
  1190. {$define TORDINALHELPER:=TShortIntHelper}
  1191. {$define TORDINALTYPE:=ShortInt}
  1192. {$i syshelpo.inc}
  1193. { ---------------------------------------------------------------------
  1194. TSmallintHelper
  1195. ---------------------------------------------------------------------}
  1196. {$define TORDINALHELPER:=TSmallIntHelper}
  1197. {$define TORDINALTYPE:=SmallInt}
  1198. {$i syshelpo.inc}
  1199. { ---------------------------------------------------------------------
  1200. TWordHelper
  1201. ---------------------------------------------------------------------}
  1202. {$define TORDINALHELPER:=TWordHelper}
  1203. {$define TORDINALTYPE:=Word}
  1204. {$i syshelpo.inc}
  1205. { ---------------------------------------------------------------------
  1206. TCardinalHelper
  1207. ---------------------------------------------------------------------}
  1208. {$define TORDINALHELPER:=TCardinalHelper}
  1209. {$define TORDINALTYPE:=Cardinal}
  1210. {$i syshelpo.inc}
  1211. { ---------------------------------------------------------------------
  1212. TIntegerHelper
  1213. ---------------------------------------------------------------------}
  1214. {$define TORDINALHELPER:=TIntegerHelper}
  1215. {$define TORDINALTYPE:=Integer}
  1216. {$i syshelpo.inc}
  1217. { ---------------------------------------------------------------------
  1218. TInt64Helper
  1219. ---------------------------------------------------------------------}
  1220. {$define TORDINALHELPER:=TInt64Helper}
  1221. {$define TORDINALTYPE:=Int64}
  1222. {$i syshelpo.inc}
  1223. { ---------------------------------------------------------------------
  1224. TQWordHelper
  1225. ---------------------------------------------------------------------}
  1226. {$define TORDINALHELPER:=TQWordHelper}
  1227. {$define TORDINALTYPE:=QWord}
  1228. {$i syshelpo.inc}
  1229. { ---------------------------------------------------------------------
  1230. TNativeIntHelper
  1231. ---------------------------------------------------------------------}
  1232. {$define TORDINALHELPER:=TNativeIntHelper}
  1233. {$define TORDINALTYPE:=NativeInt}
  1234. {$i syshelpo.inc}
  1235. { ---------------------------------------------------------------------
  1236. TNativeUIntHelper
  1237. ---------------------------------------------------------------------}
  1238. {$define TORDINALHELPER:=TNativeUIntHelper}
  1239. {$define TORDINALTYPE:=NativeUInt}
  1240. {$i syshelpo.inc}
  1241. { ---------------------------------------------------------------------
  1242. TBooleanHelper
  1243. ---------------------------------------------------------------------}
  1244. {$define TBOOLHELPER:=TBooleanHelper}
  1245. {$define TBOOLTYPE:=Boolean}
  1246. {$i syshelpb.inc}
  1247. { ---------------------------------------------------------------------
  1248. TByteBoolHelper
  1249. ---------------------------------------------------------------------}
  1250. {$define TBOOLHELPER:=TByteBoolHelper}
  1251. {$define TBOOLTYPE:=ByteBool}
  1252. {$i syshelpb.inc}
  1253. { ---------------------------------------------------------------------
  1254. TWordBoolHelper
  1255. ---------------------------------------------------------------------}
  1256. {$define TBOOLHELPER:=TWordBoolHelper}
  1257. {$define TBOOLTYPE:=WordBool}
  1258. {$i syshelpb.inc}
  1259. { ---------------------------------------------------------------------
  1260. TLongBoolHelper
  1261. ---------------------------------------------------------------------}
  1262. {$define TBOOLHELPER:=TLongBoolHelper}
  1263. {$define TBOOLTYPE:=LongBool}
  1264. {$i syshelpb.inc}