syshelp.inc 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685
  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. {$i syshelpo.inc}
  1200. { ---------------------------------------------------------------------
  1201. TShortintHelper
  1202. ---------------------------------------------------------------------}
  1203. {$define TORDINALHELPER:=TShortIntHelper}
  1204. {$define TORDINALTYPE:=ShortInt}
  1205. {$define TORDINALBITINDEX:=TShortIntBitIndex}
  1206. {$i syshelpo.inc}
  1207. { ---------------------------------------------------------------------
  1208. TSmallintHelper
  1209. ---------------------------------------------------------------------}
  1210. {$define TORDINALHELPER:=TSmallIntHelper}
  1211. {$define TORDINALTYPE:=SmallInt}
  1212. {$define TORDINALBITINDEX:=TSmallIntBitIndex}
  1213. {$i syshelpo.inc}
  1214. { ---------------------------------------------------------------------
  1215. TWordHelper
  1216. ---------------------------------------------------------------------}
  1217. {$define TORDINALHELPER:=TWordHelper}
  1218. {$define TORDINALTYPE:=Word}
  1219. {$define TORDINALBITINDEX:=TWordBitIndex}
  1220. {$i syshelpo.inc}
  1221. { ---------------------------------------------------------------------
  1222. TCardinalHelper
  1223. ---------------------------------------------------------------------}
  1224. {$define TORDINALHELPER:=TCardinalHelper}
  1225. {$define TORDINALTYPE:=Cardinal}
  1226. {$define TORDINALBITINDEX:=TCardinalBitIndex}
  1227. {$i syshelpo.inc}
  1228. { ---------------------------------------------------------------------
  1229. TIntegerHelper
  1230. ---------------------------------------------------------------------}
  1231. {$define TORDINALHELPER:=TIntegerHelper}
  1232. {$define TORDINALTYPE:=Integer}
  1233. {$define TORDINALBITINDEX:=TIntegerBitIndex}
  1234. {$i syshelpo.inc}
  1235. { ---------------------------------------------------------------------
  1236. TInt64Helper
  1237. ---------------------------------------------------------------------}
  1238. {$define TORDINALHELPER:=TInt64Helper}
  1239. {$define TORDINALTYPE:=Int64}
  1240. {$define TORDINALBITINDEX:=TInt64BitIndex}
  1241. {$i syshelpo.inc}
  1242. { ---------------------------------------------------------------------
  1243. TQWordHelper
  1244. ---------------------------------------------------------------------}
  1245. {$define TORDINALHELPER:=TQWordHelper}
  1246. {$define TORDINALTYPE:=QWord}
  1247. {$define TORDINALBITINDEX:=TQwordBitIndex}
  1248. {$i syshelpo.inc}
  1249. { ---------------------------------------------------------------------
  1250. TNativeIntHelper
  1251. ---------------------------------------------------------------------}
  1252. {$define TORDINALHELPER:=TNativeIntHelper}
  1253. {$define TORDINALTYPE:=NativeInt}
  1254. {$define TORDINALBITINDEX:=TNativeIntBitIndex}
  1255. {$i syshelpo.inc}
  1256. { ---------------------------------------------------------------------
  1257. TNativeUIntHelper
  1258. ---------------------------------------------------------------------}
  1259. {$define TORDINALHELPER:=TNativeUIntHelper}
  1260. {$define TORDINALTYPE:=NativeUInt}
  1261. {$define TORDINALBITINDEX:=TNativeUIntBitIndex}
  1262. {$i syshelpo.inc}
  1263. { ---------------------------------------------------------------------
  1264. TBooleanHelper
  1265. ---------------------------------------------------------------------}
  1266. {$define TBOOLHELPER:=TBooleanHelper}
  1267. {$define TBOOLTYPE:=Boolean}
  1268. {$i syshelpb.inc}
  1269. { ---------------------------------------------------------------------
  1270. TByteBoolHelper
  1271. ---------------------------------------------------------------------}
  1272. {$define TBOOLHELPER:=TByteBoolHelper}
  1273. {$define TBOOLTYPE:=ByteBool}
  1274. {$i syshelpb.inc}
  1275. { ---------------------------------------------------------------------
  1276. TWordBoolHelper
  1277. ---------------------------------------------------------------------}
  1278. {$define TBOOLHELPER:=TWordBoolHelper}
  1279. {$define TBOOLTYPE:=WordBool}
  1280. {$i syshelpb.inc}
  1281. { ---------------------------------------------------------------------
  1282. TLongBoolHelper
  1283. ---------------------------------------------------------------------}
  1284. {$define TBOOLHELPER:=TLongBoolHelper}
  1285. {$define TBOOLTYPE:=LongBool}
  1286. {$i syshelpb.inc}