syshelp.inc 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  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+2; // 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+1,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+1,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+1);
  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+1,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+1,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. If (T<>'') or (not (TStringSplitOptions.ExcludeEmpty=Options)) then
  942. begin
  943. MaybeGrow(Len);
  944. Result[Len]:=T;
  945. Inc(Len);
  946. end;
  947. end;
  948. if (TStringSplitOptions.ExcludeLastEmpty=Options) then
  949. if (Len > 0) and (Result[Len-1] = '') then
  950. dec(Len);
  951. SetLength(Result,Len);
  952. end;
  953. function TStringHelper.Split(const Separators: array of string; AQuote: Char
  954. ): TStringArray;
  955. begin
  956. Result:=SPlit(Separators,AQuote,AQuote);
  957. end;
  958. function TStringHelper.Split(const Separators: array of string; AQuoteStart,
  959. AQuoteEnd: Char): TStringArray;
  960. begin
  961. Result:=SPlit(Separators,AQuoteStart,AQuoteEnd,Length+1,TStringSplitOptions.None);
  962. end;
  963. function TStringHelper.Split(const Separators: array of string; AQuoteStart,
  964. AQuoteEnd: Char; Options: TStringSplitOptions): TStringArray;
  965. begin
  966. Result:=SPlit(Separators,AQuoteStart,AQuoteEnd,Length+1,Options);
  967. end;
  968. function TStringHelper.Split(const Separators: array of string; AQuoteStart,
  969. AQuoteEnd: Char; ACount: SizeInt): TStringArray;
  970. begin
  971. Result:=SPlit(Separators,AQuoteStart,AQuoteEnd,ACount,TStringSplitOptions.None);
  972. end;
  973. function TStringHelper.Split(const Separators: array of string; AQuoteStart,
  974. AQuoteEnd: Char; ACount: SizeInt; Options: TStringSplitOptions): TStringArray;
  975. Const
  976. BlockSize = 10;
  977. Function NextSep(StartIndex : SizeInt; out Match : SizeInt) : SizeInt;
  978. begin
  979. if (AQuoteStart<>#0) then
  980. Result:=Self.IndexOfAnyUnQuoted(Separators,AQuoteStart,AQuoteEnd,StartIndex,Match)
  981. else
  982. Result:=Self.IndexOfAny(Separators,StartIndex,Length,Match);
  983. if Result<>-1 then
  984. end;
  985. Procedure MaybeGrow(Curlen : SizeInt);
  986. begin
  987. if System.Length(Result)<=CurLen then
  988. SetLength(Result,System.Length(Result)+BlockSize);
  989. end;
  990. Var
  991. Sep,LastSep,Len,Match : SizeInt;
  992. T : String;
  993. begin
  994. SetLength(Result,BlockSize);
  995. Len:=0;
  996. LastSep:=0;
  997. Sep:=NextSep(0,Match);
  998. While (Sep<>-1) and ((ACount=0) or (Len<ACount)) do
  999. begin
  1000. T:=SubString(LastSep,Sep-LastSep);
  1001. If (T<>'') or (not (TStringSplitOptions.ExcludeEmpty=Options)) then
  1002. begin
  1003. MaybeGrow(Len);
  1004. Result[Len]:=T;
  1005. Inc(Len);
  1006. end;
  1007. LastSep:=Sep+System.Length(Separators[Match]);
  1008. Sep:=NextSep(LastSep,Match);
  1009. end;
  1010. if (LastSep<=Length) and ((ACount=0) or (Len<ACount)) then
  1011. begin
  1012. T:=SubString(LastSep);
  1013. // Writeln('Examining >',T,'< at pos,',LastSep,' till pos ',Sep);
  1014. If (T<>'') or (not (TStringSplitOptions.ExcludeEmpty=Options)) then
  1015. begin
  1016. MaybeGrow(Len);
  1017. Result[Len]:=T;
  1018. Inc(Len);
  1019. end;
  1020. end;
  1021. If (TStringSplitOptions.ExcludeLastEmpty=Options) then
  1022. if (Len > 0) and (Result[Len-1] = '') then
  1023. dec(Len);
  1024. SetLength(Result,Len);
  1025. end;
  1026. function TStringHelper.StartsWith(const AValue: string): Boolean;
  1027. begin
  1028. Result:=StartsWith(AValue,False);
  1029. end;
  1030. function TStringHelper.StartsWith(const AValue: string; IgnoreCase: Boolean
  1031. ): Boolean;
  1032. Var
  1033. L : SizeInt;
  1034. S : String;
  1035. begin
  1036. L:=System.Length(AValue);
  1037. Result:=L<=0;
  1038. if not Result then
  1039. begin
  1040. S:=System.Copy(Self,1,L);
  1041. Result:=(System.Length(S)=L);
  1042. if Result then
  1043. if IgnoreCase then
  1044. Result:=SameText(S,aValue)
  1045. else
  1046. Result:=SameStr(S,AValue);
  1047. end;
  1048. end;
  1049. function TStringHelper.Substring(AStartIndex: SizeInt): string;
  1050. begin
  1051. Result:=Self.SubString(AStartIndex,Self.Length-AStartIndex);
  1052. end;
  1053. function TStringHelper.Substring(AStartIndex: SizeInt; ALen: SizeInt): string;
  1054. begin
  1055. Result:=system.Copy(Self,AStartIndex+1,ALen);
  1056. end;
  1057. function TStringHelper.ToBoolean: Boolean;
  1058. begin
  1059. Result:=StrToBool(Self);
  1060. end;
  1061. function TStringHelper.ToInteger: Integer;
  1062. begin
  1063. Result:=StrToInt(Self);
  1064. end;
  1065. function TStringHelper.ToInt64: Int64;
  1066. begin
  1067. Result:=StrToInt64(Self);
  1068. end;
  1069. function TStringHelper.ToSingle: Single;
  1070. begin
  1071. Result:=StrToFLoat(Self);
  1072. end;
  1073. function TStringHelper.ToDouble: Double;
  1074. begin
  1075. Result:=StrToFLoat(Self);
  1076. end;
  1077. function TStringHelper.ToExtended: Extended;
  1078. begin
  1079. Result:=StrToFLoat(Self);
  1080. end;
  1081. function TStringHelper.ToCharArray: TCharArray;
  1082. begin
  1083. Result:=ToCharArray(0,Self.Length);
  1084. end;
  1085. function TStringHelper.ToCharArray(AStartIndex: SizeInt; ALen: SizeInt
  1086. ): TCharArray;
  1087. Var
  1088. I : SizeInt;
  1089. begin
  1090. SetLength(Result,ALen);
  1091. For I:=0 to ALen-1 do
  1092. Result[I]:=Self[AStartIndex+I+1];
  1093. end;
  1094. function TStringHelper.ToLower: string;
  1095. begin
  1096. Result:=LowerCase(Self);
  1097. end;
  1098. function TStringHelper.ToLowerInvariant: string;
  1099. begin
  1100. Result:=LowerCase(Self);
  1101. end;
  1102. function TStringHelper.ToUpper: string;
  1103. begin
  1104. Result:=UpperCase(Self);
  1105. end;
  1106. function TStringHelper.ToUpperInvariant: string;
  1107. begin
  1108. Result:=UpperCase(Self);
  1109. end;
  1110. function TStringHelper.Trim: string;
  1111. begin
  1112. Result:=SysUtils.Trim(Self);
  1113. end;
  1114. function TStringHelper.TrimLeft: string;
  1115. begin
  1116. Result:=SysUtils.TrimLeft(Self);
  1117. end;
  1118. function TStringHelper.TrimRight: string;
  1119. begin
  1120. Result:=SysUtils.TrimRight(Self);
  1121. end;
  1122. function TStringHelper.Trim(const ATrimChars: array of Char): string;
  1123. begin
  1124. Result:=Self.TrimLeft(ATrimChars).TrimRight(ATrimChars);
  1125. end;
  1126. function TStringHelper.TrimLeft(const ATrimChars: array of Char): string;
  1127. Var
  1128. I,Len : SizeInt;
  1129. begin
  1130. I:=1;
  1131. Len:=Self.Length;
  1132. While (I<=Len) and HaveChar(Self[i],ATrimChars) do Inc(I);
  1133. if I=1 then
  1134. Result:=Self
  1135. else if I>Len then
  1136. Result:=''
  1137. else
  1138. Result:=system.Copy(Self,I,Len-I+1);
  1139. end;
  1140. function TStringHelper.TrimRight(const ATrimChars: array of Char): string;
  1141. Var
  1142. I,Len : SizeInt;
  1143. begin
  1144. Len:=Self.Length;
  1145. I:=Len;
  1146. While (I>=1) and HaveChar(Self[i],ATrimChars) do Dec(I);
  1147. if I<1 then
  1148. Result:=''
  1149. else if I=Len then
  1150. Result:=Self
  1151. else
  1152. Result:=system.Copy(Self,1,I);
  1153. end;
  1154. function TStringHelper.TrimEnd(const ATrimChars: array of Char): string;
  1155. begin
  1156. Result:=TrimRight(ATrimChars);
  1157. end;
  1158. function TStringHelper.TrimStart(const ATrimChars: array of Char): string;
  1159. begin
  1160. Result:=TrimLeft(ATrimChars);
  1161. end;
  1162. { ---------------------------------------------------------------------
  1163. TSingleHelper
  1164. ---------------------------------------------------------------------}
  1165. {$MACRO ON}
  1166. {$IFDEF FPC_HAS_TYPE_SINGLE}
  1167. {$define TFLOATHELPER:=TSingleHelper}
  1168. {$define FLOATTYPE:=Single}
  1169. {$define TFloatRec:=TSingleRec}
  1170. {$i syshelpf.inc}
  1171. {$UNDEF TFloatRec}
  1172. {$ENDIF FPC_HAS_TYPE_SINGLE}
  1173. { ---------------------------------------------------------------------
  1174. TDoubleHelper
  1175. ---------------------------------------------------------------------}
  1176. {$IFDEF FPC_HAS_TYPE_DOUBLE}
  1177. {$define TFLOATHELPER:=TDoubleHelper}
  1178. {$define FLOATTYPE:=Double}
  1179. {$define TFloatRec:=TDoubleRec}
  1180. {$i syshelpf.inc}
  1181. {$UNDEF TFloatRec}
  1182. {$ENDIF FPC_HAS_TYPE_DOUBLE}
  1183. { ---------------------------------------------------------------------
  1184. TExtendedHelper
  1185. ---------------------------------------------------------------------}
  1186. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1187. {$define TFLOATHELPER:=TExtendedHelper}
  1188. {$define FLOATTYPE:=Extended}
  1189. {$define TFloatRec:=TExtended80Rec}
  1190. {$i syshelpf.inc}
  1191. {$UNDEF TFloatRec}
  1192. {$ENDIF FPC_HAS_TYPE_EXTENDED}
  1193. { ---------------------------------------------------------------------
  1194. TByteHelper
  1195. ---------------------------------------------------------------------}
  1196. {$define TORDINALHELPER:=TByteHelper}
  1197. {$define TORDINALTYPE:=Byte}
  1198. {$define TORDINALBITINDEX:=TByteBitIndex}
  1199. {$define TORDINALNIBBLEINDEX:=TByteNibbleIndex}
  1200. {$define TORDINALOVERLAY:=TByteOverlay}
  1201. {$define TORDINALTYPESIZE1}
  1202. {$i syshelpo.inc}
  1203. {$undef TORDINALTYPESIZE1}
  1204. { ---------------------------------------------------------------------
  1205. TShortintHelper
  1206. ---------------------------------------------------------------------}
  1207. {$define TORDINALHELPER:=TShortIntHelper}
  1208. {$define TORDINALTYPE:=ShortInt}
  1209. {$define TORDINALBITINDEX:=TShortIntBitIndex}
  1210. {$define TORDINALNIBBLEINDEX:=TShortIntNibbleIndex}
  1211. {$define TORDINALOVERLAY:=TShortIntOverlay}
  1212. {$define TORDINALTYPESIZE1}
  1213. {$i syshelpo.inc}
  1214. {$undef TORDINALTYPESIZE1}
  1215. { ---------------------------------------------------------------------
  1216. TSmallintHelper
  1217. ---------------------------------------------------------------------}
  1218. {$define TORDINALHELPER:=TSmallIntHelper}
  1219. {$define TORDINALTYPE:=SmallInt}
  1220. {$define TORDINALBITINDEX:=TSmallIntBitIndex}
  1221. {$define TORDINALNIBBLEINDEX:=TSmallIntNibbleIndex}
  1222. {$define TORDINALBYTEINDEX:=TWordByteIndex}
  1223. {$define TORDINALOVERLAY:=TWordOverlay}
  1224. {$define TORDINALTYPESIZE2}
  1225. {$i syshelpo.inc}
  1226. {$undef TORDINALTYPESIZE2}
  1227. { ---------------------------------------------------------------------
  1228. TWordHelper
  1229. ---------------------------------------------------------------------}
  1230. {$define TORDINALHELPER:=TWordHelper}
  1231. {$define TORDINALTYPE:=Word}
  1232. {$define TORDINALBITINDEX:=TWordBitIndex}
  1233. {$define TORDINALNIBBLEINDEX:=TWordNibbleIndex}
  1234. {$define TORDINALBYTEINDEX:=TWordByteIndex}
  1235. {$define TORDINALOVERLAY:=TWordOverlay}
  1236. {$define TORDINALTYPESIZE2}
  1237. {$i syshelpo.inc}
  1238. {$undef TORDINALTYPESIZE2}
  1239. { ---------------------------------------------------------------------
  1240. TCardinalHelper
  1241. ---------------------------------------------------------------------}
  1242. {$define TORDINALHELPER:=TCardinalHelper}
  1243. {$define TORDINALTYPE:=Cardinal}
  1244. {$define TORDINALBITINDEX:=TCardinalBitIndex}
  1245. {$define TORDINALNIBBLEINDEX:=TCardinalNibbleIndex}
  1246. {$define TORDINALBYTEINDEX:=TCardinalByteIndex}
  1247. {$define TORDINALWORDINDEX:=TCardinalWordIndex}
  1248. {$define TORDINALOVERLAY:=TDwordOverlay}
  1249. {$define TORDINALTYPESIZE4}
  1250. {$i syshelpo.inc}
  1251. {$undef TORDINALTYPESIZE4}
  1252. { ---------------------------------------------------------------------
  1253. TIntegerHelper
  1254. ---------------------------------------------------------------------}
  1255. {$define TORDINALHELPER:=TIntegerHelper}
  1256. {$define TORDINALTYPE:=Integer}
  1257. {$define TORDINALBITINDEX:=TIntegerBitIndex}
  1258. {$define TORDINALNIBBLEINDEX:=TIntegerNibbleIndex}
  1259. {$define TORDINALBYTEINDEX:=TIntegerByteIndex}
  1260. {$define TORDINALWORDINDEX:=TIntegerWordIndex}
  1261. {$if sizeof(Integer)=2}
  1262. {$define TORDINALOVERLAY:=TWordOverlay}
  1263. {$define TORDINALTYPESIZE2}
  1264. {$elseif sizeof(Integer)=4}
  1265. {$define TORDINALOVERLAY:=TDwordOverlay}
  1266. {$define TORDINALTYPESIZE4}
  1267. {$else}
  1268. {$fatal Unsupported Integer type size}
  1269. {$endif}
  1270. {$i syshelpo.inc}
  1271. {$undef TORDINALTYPESIZE2}
  1272. {$undef TORDINALTYPESIZE4}
  1273. { ---------------------------------------------------------------------
  1274. TLongIntHelper
  1275. ---------------------------------------------------------------------}
  1276. {$define TORDINALHELPER:=TLongIntHelper}
  1277. {$define TORDINALTYPE:=LongInt}
  1278. {$define TORDINALBITINDEX:=TLongIntBitIndex}
  1279. {$define TORDINALNIBBLEINDEX:=TLongIntNibbleIndex}
  1280. {$define TORDINALBYTEINDEX:=TLongIntByteIndex}
  1281. {$define TORDINALWORDINDEX:=TLongIntWordIndex}
  1282. {$define TORDINALOVERLAY:=TDwordOverlay}
  1283. {$define TORDINALTYPESIZE4}
  1284. {$i syshelpo.inc}
  1285. {$undef TORDINALTYPESIZE4}
  1286. { ---------------------------------------------------------------------
  1287. TInt64Helper
  1288. ---------------------------------------------------------------------}
  1289. {$define TORDINALHELPER:=TInt64Helper}
  1290. {$define TORDINALTYPE:=Int64}
  1291. {$define TORDINALBITINDEX:=TInt64BitIndex}
  1292. {$define TORDINALNIBBLEINDEX:=TInt64NibbleIndex}
  1293. {$define TORDINALBYTEINDEX:=TInt64ByteIndex}
  1294. {$define TORDINALWORDINDEX:=TInt64WordIndex}
  1295. {$define TORDINALDWORDINDEX:=TInt64DWordIndex}
  1296. {$define TORDINALOVERLAY:=TQwordOverlay}
  1297. {$define TORDINALTYPESIZE8}
  1298. {$i syshelpo.inc}
  1299. {$undef TORDINALTYPESIZE8}
  1300. { ---------------------------------------------------------------------
  1301. TQWordHelper
  1302. ---------------------------------------------------------------------}
  1303. {$define TORDINALHELPER:=TQWordHelper}
  1304. {$define TORDINALTYPE:=QWord}
  1305. {$define TORDINALBITINDEX:=TQwordBitIndex}
  1306. {$define TORDINALNIBBLEINDEX:=TQwordNibbleIndex}
  1307. {$define TORDINALBYTEINDEX:=TQwordByteIndex}
  1308. {$define TORDINALWORDINDEX:=TQWordWordIndex}
  1309. {$define TORDINALDWORDINDEX:=TQWordDWordIndex}
  1310. {$define TORDINALOVERLAY:=TQwordOverlay}
  1311. {$define TORDINALTYPESIZE8}
  1312. {$i syshelpo.inc}
  1313. {$undef TORDINALTYPESIZE8}
  1314. { ---------------------------------------------------------------------
  1315. TNativeIntHelper
  1316. ---------------------------------------------------------------------}
  1317. {$define TORDINALHELPER:=TNativeIntHelper}
  1318. {$define TORDINALTYPE:=NativeInt}
  1319. {$define TORDINALBITINDEX:=TNativeIntBitIndex}
  1320. {$if sizeof(NativeInt)=2}
  1321. {$define TORDINALNIBBLEINDEX:=TSmallIntNibbleIndex}
  1322. {$define TORDINALBYTEINDEX:=TSmallIntByteIndex}
  1323. {$define TORDINALOVERLAY:=TSmallIntOverlay}
  1324. {$define TORDINALTYPESIZE2}
  1325. {$elseif sizeof(NativeInt)=4}
  1326. {$define TORDINALNIBBLEINDEX:=TLongIntNibbleIndex}
  1327. {$define TORDINALBYTEINDEX:=TLongIntByteIndex}
  1328. {$define TORDINALWORDINDEX:=TLongIntWordIndex}
  1329. {$define TORDINALOVERLAY:=TLongIntOverlay}
  1330. {$define TORDINALTYPESIZE4}
  1331. {$elseif sizeof(NativeInt)=8}
  1332. {$define TORDINALNIBBLEINDEX:=TInt64NibbleIndex}
  1333. {$define TORDINALBYTEINDEX:=TInt64ByteIndex}
  1334. {$define TORDINALWORDINDEX:=TInt64WordIndex}
  1335. {$define TORDINALDWORDINDEX:=TInt64DWordIndex}
  1336. {$define TORDINALOVERLAY:=TInt64Overlay}
  1337. {$define TORDINALTYPESIZE8}
  1338. {$else}
  1339. {$fatal Unsupported NativeInt type size}
  1340. {$endif}
  1341. {$i syshelpo.inc}
  1342. {$undef TORDINALTYPESIZE2}
  1343. {$undef TORDINALTYPESIZE4}
  1344. {$undef TORDINALTYPESIZE8}
  1345. { ---------------------------------------------------------------------
  1346. TNativeUIntHelper
  1347. ---------------------------------------------------------------------}
  1348. {$define TORDINALHELPER:=TNativeUIntHelper}
  1349. {$define TORDINALTYPE:=NativeUInt}
  1350. {$define TORDINALBITINDEX:=TNativeUIntBitIndex}
  1351. {$if sizeof(NativeUInt)=2}
  1352. {$define TORDINALNIBBLEINDEX:=TWordNibbleIndex}
  1353. {$define TORDINALBYTEINDEX:=TWordByteIndex}
  1354. {$define TORDINALOVERLAY:=TWordOverlay}
  1355. {$define TORDINALTYPESIZE2}
  1356. {$elseif sizeof(NativeUInt)=4}
  1357. {$define TORDINALNIBBLEINDEX:=TDwordNibbleIndex}
  1358. {$define TORDINALBYTEINDEX:=TDwordByteIndex}
  1359. {$define TORDINALWORDINDEX:=TDwordWordIndex}
  1360. {$define TORDINALOVERLAY:=TDwordOverlay}
  1361. {$define TORDINALTYPESIZE4}
  1362. {$elseif sizeof(NativeUInt)=8}
  1363. {$define TORDINALNIBBLEINDEX:=TQwordNibbleIndex}
  1364. {$define TORDINALBYTEINDEX:=TQwordByteIndex}
  1365. {$define TORDINALWORDINDEX:=TQwordWordIndex}
  1366. {$define TORDINALDWORDINDEX:=TQwordDWordIndex}
  1367. {$define TORDINALOVERLAY:=TQwordOverlay}
  1368. {$define TORDINALTYPESIZE8}
  1369. {$else}
  1370. {$fatal Unsupported NativeUInt type size}
  1371. {$endif}
  1372. {$i syshelpo.inc}
  1373. {$undef TORDINALTYPESIZE2}
  1374. {$undef TORDINALTYPESIZE4}
  1375. {$undef TORDINALTYPESIZE8}
  1376. { ---------------------------------------------------------------------
  1377. TBooleanHelper
  1378. ---------------------------------------------------------------------}
  1379. {$define TBOOLHELPER:=TBooleanHelper}
  1380. {$define TBOOLTYPE:=Boolean}
  1381. {$i syshelpb.inc}
  1382. { ---------------------------------------------------------------------
  1383. TBoolean8Helper
  1384. ---------------------------------------------------------------------}
  1385. {$define TBOOLHELPER:=TBoolean8Helper}
  1386. {$define TBOOLTYPE:=Boolean8}
  1387. {$i syshelpb.inc}
  1388. { ---------------------------------------------------------------------
  1389. TBoolean16Helper
  1390. ---------------------------------------------------------------------}
  1391. {$define TBOOLHELPER:=TBoolean16Helper}
  1392. {$define TBOOLTYPE:=Boolean16}
  1393. {$i syshelpb.inc}
  1394. { ---------------------------------------------------------------------
  1395. TBoolean32Helper
  1396. ---------------------------------------------------------------------}
  1397. {$define TBOOLHELPER:=TBoolean32Helper}
  1398. {$define TBOOLTYPE:=Boolean32}
  1399. {$i syshelpb.inc}
  1400. { ---------------------------------------------------------------------
  1401. TBoolean64Helper
  1402. ---------------------------------------------------------------------}
  1403. {$define TBOOLHELPER:=TBoolean64Helper}
  1404. {$define TBOOLTYPE:=Boolean64}
  1405. {$i syshelpb.inc}
  1406. { ---------------------------------------------------------------------
  1407. TByteBoolHelper
  1408. ---------------------------------------------------------------------}
  1409. {$define TBOOLHELPER:=TByteBoolHelper}
  1410. {$define TBOOLTYPE:=ByteBool}
  1411. {$i syshelpb.inc}
  1412. { ---------------------------------------------------------------------
  1413. TWordBoolHelper
  1414. ---------------------------------------------------------------------}
  1415. {$define TBOOLHELPER:=TWordBoolHelper}
  1416. {$define TBOOLTYPE:=WordBool}
  1417. {$i syshelpb.inc}
  1418. { ---------------------------------------------------------------------
  1419. TLongBoolHelper
  1420. ---------------------------------------------------------------------}
  1421. {$define TBOOLHELPER:=TLongBoolHelper}
  1422. {$define TBOOLTYPE:=LongBool}
  1423. {$i syshelpb.inc}
  1424. { ---------------------------------------------------------------------
  1425. TQWordBoolHelper
  1426. ---------------------------------------------------------------------}
  1427. {$define TBOOLHELPER:=TQWordBoolHelper}
  1428. {$define TBOOLTYPE:=QWordBool}
  1429. {$i syshelpb.inc}