syshelp.inc 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  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. const
  234. LWhiteSpace = [#0..' '];
  235. var
  236. I: SizeInt;
  237. begin
  238. for I:=1 to System.Length(AValue) do
  239. if not (AValue[I] in LWhiteSpace) then
  240. exit(False);
  241. Result:=True;
  242. end;
  243. class function TStringHelper.Join(const Separator: string;
  244. const Values: array of const): string;
  245. Var
  246. SValues : Array of string;
  247. I,L : SizeInt;
  248. S : String;
  249. P : ^TVarRec;
  250. begin
  251. L:=System.Length(Values);
  252. SetLength(SValues,L);
  253. Dec(L);
  254. for I:=0 to L do
  255. begin
  256. S:='';
  257. P:=@Values[I];
  258. Case P^.VType of
  259. vtInteger : S:=IntToStr(P^.VInteger);
  260. vtBoolean : S:=BoolToStr(P^.VBoolean, True);
  261. vtChar : S:=P^.VChar;
  262. vtPChar : S:= string(P^.VPChar);
  263. {$ifndef FPUNONE}
  264. vtExtended : S:=FloatToStr(P^.VExtended^);
  265. {$endif}
  266. vtObject : S:=TObject(P^.VObject).Classname;
  267. vtClass : S:=P^.VClass.Classname;
  268. vtCurrency : S:=CurrToStr(P^.VCurrency^);
  269. vtVariant : S:=(P^.VVariant^);
  270. vtInt64 : S:=IntToStr(PInt64(P^.VInt64)^);
  271. vtQword : S:=IntToStr(PQWord(P^.VQword)^);
  272. vtWideChar : S:=WideString(P^.VWideChar);
  273. vtPWideChar : S:=WideString(P^.VPWideChar);
  274. vtUnicodeString : S:=UnicodeString(P^.VUnicodeString);
  275. vtAnsiString : S:=Ansistring(P^.VAnsiString);
  276. else
  277. S:=Format('Unknown type: %d',[P^.VType]);
  278. end;
  279. SValues[I]:=S;
  280. end;
  281. Result:=Join(Separator,SValues);
  282. end;
  283. class function TStringHelper.Join(const Separator: string;
  284. const Values: array of string): string;
  285. begin
  286. Result:=Join(Separator,Values,0,System.Length(Values));
  287. end;
  288. class function TStringHelper.Join(const Separator: string;
  289. const Values: array of string; StartIndex: SizeInt; ACount: SizeInt): string;
  290. Var
  291. I,L,VLen : SizeInt;
  292. begin
  293. VLen:=High(Values);
  294. If (ACount<0) or ((StartIndex>0) and (StartIndex>VLen)) then
  295. raise ERangeError.Create(SRangeError);
  296. If (ACount=0) or (VLen<0) then
  297. Result:=''
  298. else
  299. begin
  300. L:=StartIndex+ACount-1;
  301. if L>Vlen then
  302. L:=VLen;
  303. Result:=Values[StartIndex];
  304. For I:=StartIndex+1 to L do
  305. Result:=Result+Separator+Values[I];
  306. end;
  307. end;
  308. class function TStringHelper.LowerCase(const S: string): string;
  309. begin
  310. Result:=sysutils.Lowercase(S);
  311. end;
  312. class function TStringHelper.Parse(const AValue: Boolean): string;
  313. begin
  314. Result:=BoolToStr(AValue);
  315. end;
  316. class function TStringHelper.Parse(const AValue: Extended): string;
  317. begin
  318. Result:=FloatToStr(AValue);
  319. end;
  320. class function TStringHelper.Parse(const AValue: Int64): string;
  321. begin
  322. Result:=IntToStr(AValue);
  323. end;
  324. class function TStringHelper.Parse(const AValue: Integer): string;
  325. begin
  326. Result:=IntToStr(AValue);
  327. end;
  328. class function TStringHelper.ToBoolean(const S: string): Boolean;
  329. begin
  330. Result:=StrToBool(S);
  331. end;
  332. class function TStringHelper.ToDouble(const S: string): Double;
  333. begin
  334. Result:=StrToFloat(S);
  335. end;
  336. class function TStringHelper.ToExtended(const S: string): Extended;
  337. begin
  338. Result:=StrToFloat(S);
  339. end;
  340. class function TStringHelper.ToInt64(const S: string): Int64;
  341. begin
  342. Result:=StrToInt64(S);
  343. end;
  344. class function TStringHelper.ToInteger(const S: string): Integer;
  345. begin
  346. Result:=StrToInt(S);
  347. end;
  348. class function TStringHelper.ToSingle(const S: string): Single;
  349. begin
  350. Result:=StrToFloat(S);
  351. end;
  352. class function TStringHelper.UpperCase(const S: string): string;
  353. begin
  354. Result:=sysutils.Uppercase(S);
  355. end;
  356. function TStringHelper.CompareTo(const B: string): Integer;
  357. begin
  358. // Order is important
  359. Result:=sysUtils.StrComp(PChar(Self),PChar(B));
  360. end;
  361. procedure TStringHelper.CopyTo(SourceIndex: SizeInt; var destination: array of Char; DestinationIndex: SizeInt; ACount: SizeInt);
  362. Var
  363. P1,P2 : PChar;
  364. begin
  365. // Writeln('((',DestinationIndex,'+',ACount,')<',System.Length(Destination),') : ', ((DestinationIndex+ACount)<System.Length(Destination)));
  366. if ((DestinationIndex+ACount)<=System.Length(Destination)) then
  367. begin
  368. // Writeln('AHA');
  369. P1:=@Self[SourceIndex+1];
  370. P2:=@Destination[DestinationIndex];
  371. Move(P1^,P2^,ACount*SizeOf(Char));
  372. end;
  373. end;
  374. function TStringHelper.Contains(const AValue: string): Boolean;
  375. begin
  376. Result:=Pos(AValue,Self)>0;
  377. end;
  378. function TStringHelper.CountChar(const C: Char): SizeInt;
  379. Var
  380. S : Char;
  381. begin
  382. Result:=0;
  383. For S in Self do
  384. if (S=C) then
  385. Inc(Result);
  386. end;
  387. function TStringHelper.DeQuotedString: string;
  388. begin
  389. Result:=DeQuotedString('''');
  390. end;
  391. function TStringHelper.DeQuotedString(const AQuoteChar: Char): string;
  392. var
  393. L,I : SizeInt;
  394. Res : Array of Char;
  395. PS,PD : PChar;
  396. IsQuote : Boolean;
  397. begin
  398. L:=System.Length(Self);
  399. if (L<2) or Not ((Self[1]=AQuoteChar) and (Self[L]=AQuoteChar)) then
  400. Exit(Self);
  401. SetLength(Res,L);
  402. IsQuote:=False;
  403. PS:=@Self[2];
  404. PD:=@Res[0];
  405. For I:=2 to L-1 do
  406. begin
  407. if (PS^=AQuoteChar) then
  408. begin
  409. IsQuote:=Not IsQuote;
  410. if Not IsQuote then
  411. begin
  412. PD^:=PS^;
  413. Inc(PD);
  414. end;
  415. end
  416. else
  417. begin
  418. if IsQuote then
  419. IsQuote:=false;
  420. PD^:=PS^;
  421. Inc(PD);
  422. end;
  423. Inc(PS);
  424. end;
  425. SetString(Result,@Res[0],PD-@Res[0]);
  426. end;
  427. function TStringHelper.EndsWith(const AValue: string): Boolean;
  428. begin
  429. Result:=EndsWith(AValue,False);
  430. end;
  431. function TStringHelper.EndsWith(const AValue: string; IgnoreCase: Boolean): Boolean;
  432. Var
  433. L : SizeInt;
  434. S : String;
  435. begin
  436. L:=system.Length(AVAlue);
  437. Result:=L=0;
  438. if Not Result then
  439. begin
  440. S:=system.Copy(Self,Length-L+1,L);
  441. Result:=system.Length(S)=L;
  442. if Result then
  443. if IgnoreCase then
  444. Result:=CompareText(S,AValue)=0
  445. else
  446. Result:=S=AValue;
  447. end;
  448. end;
  449. function TStringHelper.Equals(const AValue: string): Boolean;
  450. begin
  451. Result:=(Self=AValue);
  452. end;
  453. function TStringHelper.Format(const args: array of const): string;
  454. begin
  455. Result:=Format(Self,Args);
  456. end;
  457. function TStringHelper.GetHashCode: Integer;
  458. // Taken from contnrs, fphash
  459. var
  460. P,pmax : PChar;
  461. begin
  462. {$push}
  463. {$Q-}
  464. Result:=0;
  465. P:=Pchar(Self);
  466. pmax:=p+length;
  467. while (p<pmax) do
  468. begin
  469. Result:=LongWord(LongInt(Result shl 5) - LongInt(Result)) xor LongWord(P^);
  470. Inc(p);
  471. end;
  472. {$pop}
  473. end;
  474. function TStringHelper.IndexOf(AValue: Char): SizeInt;
  475. begin
  476. Result:=IndexOf(AValue,0,Length);
  477. end;
  478. function TStringHelper.IndexOf(const AValue: string): SizeInt;
  479. begin
  480. Result:=IndexOf(AValue,0,Length);
  481. end;
  482. function TStringHelper.IndexOf(AValue: Char; StartIndex: SizeInt): SizeInt;
  483. begin
  484. Result:=IndexOf(AValue,StartIndex,Length);
  485. end;
  486. function TStringHelper.IndexOf(const AValue: string; StartIndex: SizeInt
  487. ): SizeInt;
  488. begin
  489. Result:=IndexOf(AValue,StartIndex,Length);
  490. end;
  491. function TStringHelper.IndexOf(AValue: Char; StartIndex: SizeInt;
  492. ACount: SizeInt): SizeInt;
  493. Var
  494. S : String;
  495. begin
  496. S:=System.Copy(Self,StartIndex+1,ACount);
  497. Result:=Pos(AValue,S)-1;
  498. if Result<>-1 then
  499. Result:=Result+StartIndex;
  500. end;
  501. function TStringHelper.IndexOf(const AValue: string; StartIndex: SizeInt;
  502. ACount: SizeInt): SizeInt;
  503. Var
  504. S : String;
  505. begin
  506. S:=System.Copy(Self,StartIndex+1,ACount);
  507. Result:=Pos(AValue,S)-1;
  508. if Result<>-1 then
  509. Result:=Result+StartIndex;
  510. end;
  511. function TStringHelper.IndexOfUnQuoted(const AValue: string; StartQuote,
  512. EndQuote: Char; StartIndex: SizeInt = 0): SizeInt;
  513. Var
  514. LV : SizeInt;
  515. Function MatchAt(I : SizeInt) : Boolean ; Inline;
  516. Var
  517. J : SizeInt;
  518. begin
  519. J:=1;
  520. Repeat
  521. Result:=(Self[I+J-1]=AValue[j]);
  522. Inc(J);
  523. Until (Not Result) or (J>LV);
  524. end;
  525. Var
  526. I,L,Q: SizeInt;
  527. begin
  528. Result:=-1;
  529. LV:=system.Length(AValue);
  530. L:=Length-LV+1;
  531. if L<0 then
  532. L:=0;
  533. I:=StartIndex+1;
  534. Q:=0;
  535. if StartQuote=EndQuote then
  536. begin
  537. While (Result=-1) and (I<=L) do
  538. begin
  539. if (Self[I]=StartQuote) then
  540. Q:=1-Q;
  541. if (Q=0) and MatchAt(i) then
  542. Result:=I-1;
  543. Inc(I);
  544. end;
  545. end
  546. else
  547. begin
  548. While (Result=-1) and (I<=L) do
  549. begin
  550. if Self[I]=StartQuote then
  551. Inc(Q)
  552. else if (Self[I]=EndQuote) and (Q>0) then
  553. Dec(Q);
  554. if (Q=0) and MatchAt(i) then
  555. Result:=I-1;
  556. Inc(I);
  557. end;
  558. end;
  559. end;
  560. function TStringHelper.IndexOfAny(const AnyOf: array of Char): SizeInt;
  561. begin
  562. Result:=IndexOfAny(AnyOf,0,Length);
  563. end;
  564. function TStringHelper.IndexOfAny(const AnyOf: array of Char;
  565. StartIndex: SizeInt): SizeInt;
  566. begin
  567. Result:=IndexOfAny(AnyOf,StartIndex,Length);
  568. end;
  569. function TStringHelper.IndexOfAny(const AnyOf: array of Char;
  570. StartIndex: SizeInt; ACount: SizeInt): SizeInt;
  571. Var
  572. i,L : SizeInt;
  573. begin
  574. I:=StartIndex+1;
  575. L:=I+ACount-1;
  576. If L>Length then
  577. L:=Length;
  578. Result:=-1;
  579. While (Result=-1) and (I<=L) do
  580. begin
  581. if HaveChar(Self[i],AnyOf) then
  582. Result:=I-1;
  583. Inc(I);
  584. end;
  585. end;
  586. function TStringHelper.IndexOfAny(const AnyOf: array of String): SizeInt;
  587. begin
  588. Result:=IndexOfAny(AnyOf,0,Length);
  589. end;
  590. function TStringHelper.IndexOfAny(const AnyOf: array of String;
  591. StartIndex: SizeInt): SizeInt;
  592. begin
  593. Result:=IndexOfAny(AnyOf,StartIndex,Length-StartIndex);
  594. end;
  595. function TStringHelper.IndexOfAny(const AnyOf: array of String;
  596. StartIndex: SizeInt; ACount: SizeInt): SizeInt;
  597. Var
  598. M : SizeInt;
  599. begin
  600. Result:=IndexOfAny(AnyOf,StartIndex,ACount,M);
  601. end;
  602. function TStringHelper.IndexOfAny(const AnyOf: array of String;
  603. StartIndex: SizeInt; ACount: SizeInt; out AMatch: SizeInt): SizeInt;
  604. Var
  605. L,I : SizeInt;
  606. begin
  607. Result:=-1;
  608. For I:=0 to System.Length(AnyOf)-1 do
  609. begin
  610. L:=IndexOf(AnyOf[i],StartIndex,ACount);
  611. If (L>=0) and ((Result=-1) or (L<Result)) then
  612. begin
  613. Result:=L;
  614. AMatch:=I;
  615. end;
  616. end;
  617. end;
  618. function TStringHelper.IndexOfAnyUnquoted(const AnyOf: array of Char;
  619. StartQuote, EndQuote: Char): SizeInt;
  620. begin
  621. Result:=IndexOfAnyUnquoted(AnyOf,StartQuote,EndQuote,0,Length);
  622. end;
  623. function TStringHelper.IndexOfAnyUnquoted(const AnyOf: array of Char;
  624. StartQuote, EndQuote: Char; StartIndex: SizeInt): SizeInt;
  625. begin
  626. Result:=IndexOfAnyUnquoted(AnyOf,StartQuote,EndQuote,StartIndex,Length);
  627. end;
  628. function TStringHelper.IndexOfAnyUnquoted(const AnyOf: array of Char;
  629. StartQuote, EndQuote: Char; StartIndex: SizeInt; ACount: SizeInt): SizeInt;
  630. Var
  631. I,L : SizeInt;
  632. Q : SizeInt;
  633. begin
  634. Result:=-1;
  635. L:=StartIndex+ACount-1;
  636. if L>Length then
  637. L:=Length;
  638. I:=StartIndex+1;
  639. Q:=0;
  640. if StartQuote=EndQuote then
  641. begin
  642. While (Result=-1) and (I<=L) do
  643. begin
  644. if (Self[I]=StartQuote) then
  645. Q:=1-Q;
  646. if (Q=0) and HaveChar(Self[i],AnyOf) then
  647. Result:=I-1;
  648. Inc(I);
  649. end;
  650. end
  651. else
  652. begin
  653. While (Result=-1) and (I<=L) do
  654. begin
  655. if Self[I]=StartQuote then
  656. Inc(Q)
  657. else if (Self[I]=EndQuote) and (Q>0) then
  658. Dec(Q);
  659. if (Q=0) and HaveChar(Self[i],AnyOf) then
  660. Result:=I-1;
  661. Inc(I);
  662. end;
  663. end;
  664. end;
  665. function TStringHelper.IndexOfAnyUnquoted(const AnyOf: array of string;
  666. StartQuote, EndQuote: Char; StartIndex: SizeInt; out Matched: SizeInt
  667. ): SizeInt;
  668. Var
  669. L,I : SizeInt;
  670. begin
  671. Result:=-1;
  672. For I:=0 to System.Length(AnyOf)-1 do
  673. begin
  674. L:=IndexOfUnquoted(AnyOf[i],StartQuote,EndQuote,StartIndex);
  675. If (L>=0) and ((Result=-1) or (L<Result)) then
  676. begin
  677. Result:=L;
  678. Matched:=I;
  679. end;
  680. end;
  681. end;
  682. function TStringHelper.Insert(StartIndex: SizeInt; const AValue: string
  683. ): string;
  684. begin
  685. system.Insert(AValue,Self,StartIndex+1);
  686. Result:=Self;
  687. end;
  688. function TStringHelper.IsDelimiter(const Delimiters: string; Index: SizeInt
  689. ): Boolean;
  690. begin
  691. Result:=sysutils.IsDelimiter(Delimiters,Self,Index+1);
  692. end;
  693. function TStringHelper.IsEmpty: Boolean;
  694. begin
  695. Result:=(Length=0)
  696. end;
  697. function TStringHelper.LastDelimiter(const Delims: string): SizeInt;
  698. begin
  699. Result:=sysutils.LastDelimiter(Delims,Self)-1;
  700. end;
  701. function TStringHelper.LastIndexOf(AValue: Char): SizeInt;
  702. begin
  703. Result:=LastIndexOf(AValue,Length-1,Length);
  704. end;
  705. function TStringHelper.LastIndexOf(const AValue: string): SizeInt;
  706. begin
  707. Result:=LastIndexOf(AValue,Length-1,Length);
  708. end;
  709. function TStringHelper.LastIndexOf(AValue: Char; AStartIndex: SizeInt): SizeInt;
  710. begin
  711. Result:=LastIndexOf(AValue,AStartIndex,Length);
  712. end;
  713. function TStringHelper.LastIndexOf(const AValue: string; AStartIndex: SizeInt
  714. ): SizeInt;
  715. begin
  716. Result:=LastIndexOf(AValue,AStartIndex,Length);
  717. end;
  718. function TStringHelper.LastIndexOf(AValue: Char; AStartIndex: SizeInt;
  719. ACount: SizeInt): SizeInt;
  720. Var
  721. Min : SizeInt;
  722. begin
  723. Result:=AStartIndex+1;
  724. Min:=Result-ACount+1;
  725. If Min<1 then
  726. Min:=1;
  727. While (Result>=Min) and (Self[Result]<>AValue) do
  728. Dec(Result);
  729. if Result<Min then
  730. Result:=-1
  731. else
  732. Result:=Result-1;
  733. end;
  734. function TStringHelper.LastIndexOf(const AValue: string; AStartIndex: SizeInt; ACount: SizeInt): SizeInt;
  735. var
  736. I,L,LS,M : SizeInt;
  737. S : String;
  738. P : PChar;
  739. begin
  740. Result:=-1;
  741. LS:=system.Length(Self);
  742. L:=system.Length(AValue);
  743. if (L=0) or (L>LS) then
  744. Exit;
  745. P:=PChar(AValue);
  746. S:=Self;
  747. I:=AStartIndex+1; // 1 based
  748. if (I>LS) then
  749. I:=LS;
  750. I:=I-L+1;
  751. M:=AStartIndex-ACount+2; // 1 based
  752. if M<1 then
  753. M:=1;
  754. while (Result=-1) and (I>=M) do
  755. begin
  756. if (0=StrLComp(PChar(@S[I]),P,L)) then
  757. Result:=I-1;
  758. Dec(I);
  759. end;
  760. end;
  761. function TStringHelper.LastIndexOfAny(const AnyOf: array of Char): SizeInt;
  762. begin
  763. Result:=LastIndexOfAny(AnyOf,Length-1,Length);
  764. end;
  765. function TStringHelper.LastIndexOfAny(const AnyOf: array of Char;
  766. AStartIndex: SizeInt): SizeInt;
  767. begin
  768. Result:=LastIndexOfAny(AnyOf,AStartIndex,Length);
  769. end;
  770. function TStringHelper.LastIndexOfAny(const AnyOf: array of Char;
  771. AStartIndex: SizeInt; ACount: SizeInt): SizeInt;
  772. Var
  773. Min : SizeInt;
  774. begin
  775. Result:=AStartIndex+1;
  776. Min:=Result-ACount+1;
  777. If Min<1 then
  778. Min:=1;
  779. While (Result>=Min) and Not HaveChar(Self[Result],AnyOf) do
  780. Dec(Result);
  781. if Result<Min then
  782. Result:=-1
  783. else
  784. Result:=Result-1;
  785. end;
  786. function TStringHelper.PadLeft(ATotalWidth: SizeInt): string;
  787. begin
  788. Result:=PadLeft(ATotalWidth,' ');
  789. end;
  790. function TStringHelper.PadLeft(ATotalWidth: SizeInt; PaddingChar: Char): string;
  791. Var
  792. L : SizeInt;
  793. begin
  794. Result:=Self;
  795. L:=ATotalWidth-Length;
  796. If L>0 then
  797. Result:=StringOfChar(PaddingChar,L)+Result;
  798. end;
  799. function TStringHelper.PadRight(ATotalWidth: SizeInt): string;
  800. begin
  801. Result:=PadRight(ATotalWidth,' ');
  802. end;
  803. function TStringHelper.PadRight(ATotalWidth: SizeInt; PaddingChar: Char
  804. ): string;
  805. Var
  806. L : SizeInt;
  807. begin
  808. Result:=Self;
  809. L:=ATotalWidth-Length;
  810. If L>0 then
  811. Result:=Result+StringOfChar(PaddingChar,L);
  812. end;
  813. function TStringHelper.QuotedString: string;
  814. begin
  815. Result:=QuotedStr(Self);
  816. end;
  817. function TStringHelper.QuotedString(const AQuoteChar: Char): string;
  818. begin
  819. Result:=AnsiQuotedStr(Self,AQuoteChar);
  820. end;
  821. function TStringHelper.Remove(StartIndex: SizeInt): string;
  822. begin
  823. Result:=Remove(StartIndex,Self.Length-StartIndex);
  824. end;
  825. function TStringHelper.Remove(StartIndex: SizeInt; ACount: SizeInt): string;
  826. begin
  827. Result:=Self;
  828. System.Delete(Result,StartIndex+1,ACount);
  829. end;
  830. function TStringHelper.Replace(OldChar: Char; NewChar: Char): string;
  831. begin
  832. Result:=Replace(OldChar,NewChar,[rfReplaceAll]);
  833. end;
  834. function TStringHelper.Replace(OldChar: Char; NewChar: Char;
  835. ReplaceFlags: TReplaceFlags): string;
  836. begin
  837. Result:=StringReplace(Self,OldChar,NewChar,ReplaceFlags);
  838. end;
  839. function TStringHelper.Replace(const OldValue: string; const NewValue: string
  840. ): string;
  841. begin
  842. Result:=Replace(OldValue,NewValue,[rfReplaceAll]);
  843. end;
  844. function TStringHelper.Replace(const OldValue: string; const NewValue: string;
  845. ReplaceFlags: TReplaceFlags): string;
  846. begin
  847. Result:=StringReplace(Self,OldValue,NewValue,ReplaceFlags);
  848. end;
  849. function TStringHelper.Split(const Separators: array of Char): TStringArray;
  850. begin
  851. Result:=Split(Separators,#0,#0,Length+1,TStringSplitOptions.None);
  852. end;
  853. function TStringHelper.Split(const Separators: array of Char; ACount: SizeInt
  854. ): TStringArray;
  855. begin
  856. Result:=Split(Separators,#0,#0,ACount,TStringSplitOptions.None);
  857. end;
  858. function TStringHelper.Split(const Separators: array of Char;
  859. Options: TStringSplitOptions): TStringArray;
  860. begin
  861. Result:=Split(Separators,Length+1,Options);
  862. end;
  863. function TStringHelper.Split(const Separators: array of Char; ACount: SizeInt;
  864. Options: TStringSplitOptions): TStringArray;
  865. begin
  866. Result:=Split(Separators,#0,#0,ACount,Options);
  867. end;
  868. function TStringHelper.Split(const Separators: array of string): TStringArray;
  869. begin
  870. Result:=Split(Separators,Length+1);
  871. end;
  872. function TStringHelper.Split(const Separators: array of string; ACount: SizeInt
  873. ): TStringArray;
  874. begin
  875. Result:=Split(Separators,ACount,TStringSplitOptions.None);
  876. end;
  877. function TStringHelper.Split(const Separators: array of string;
  878. Options: TStringSplitOptions): TStringArray;
  879. begin
  880. Result:=Split(Separators,Length+1,Options);
  881. end;
  882. function TStringHelper.Split(const Separators: array of string;
  883. ACount: SizeInt; Options: TStringSplitOptions): TStringArray;
  884. begin
  885. Result:=Split(Separators,#0,#0,ACount,Options);
  886. end;
  887. function TStringHelper.Split(const Separators: array of Char; AQuote: Char
  888. ): TStringArray;
  889. begin
  890. Result:=Split(Separators,AQuote,AQuote);
  891. end;
  892. function TStringHelper.Split(const Separators: array of Char; AQuoteStart,
  893. AQuoteEnd: Char): TStringArray;
  894. begin
  895. Result:=Split(Separators,AQuoteStart,AQuoteEnd,TStringSplitOptions.None);
  896. end;
  897. function TStringHelper.Split(const Separators: array of Char; AQuoteStart,
  898. AQuoteEnd: Char; Options: TStringSplitOptions): TStringArray;
  899. begin
  900. Result:=Split(Separators,AQuoteStart,AQuoteEnd,Length+1,Options);
  901. end;
  902. function TStringHelper.Split(const Separators: array of Char; AQuoteStart,
  903. AQuoteEnd: Char; ACount: SizeInt): TStringArray;
  904. begin
  905. Result:=Split(Separators,AQuoteStart,AQuoteEnd,ACount,TStringSplitOptions.None);
  906. end;
  907. function TStringHelper.Split(const Separators: array of Char; AQuoteStart,
  908. AQuoteEnd: Char; ACount: SizeInt; Options: TStringSplitOptions): TStringArray;
  909. Const
  910. BlockSize = 10;
  911. Function NextSep(StartIndex : SizeInt) : SizeInt;
  912. begin
  913. if (AQuoteStart<>#0) then
  914. Result:=Self.IndexOfAnyUnQuoted(Separators,AQuoteStart,AQuoteEnd,StartIndex)
  915. else
  916. Result:=Self.IndexOfAny(Separators,StartIndex);
  917. end;
  918. Procedure MaybeGrow(Curlen : SizeInt);
  919. begin
  920. if System.Length(Result)<=CurLen then
  921. SetLength(Result,System.Length(Result)+BlockSize);
  922. end;
  923. Var
  924. Sep,LastSep,Len : SizeInt;
  925. T : String;
  926. begin
  927. SetLength(Result,BlockSize);
  928. Len:=0;
  929. LastSep:=0;
  930. Sep:=NextSep(0);
  931. While (Sep<>-1) and ((ACount=0) or (Len<ACount)) do
  932. begin
  933. T:=SubString(LastSep,Sep-LastSep);
  934. // Writeln('Examining >',T,'< at pos ',LastSep,', till pos ',Sep);
  935. If (T<>'') or (not (TStringSplitOptions.ExcludeEmpty=Options)) then
  936. begin
  937. MaybeGrow(Len);
  938. Result[Len]:=T;
  939. Inc(Len);
  940. end;
  941. LastSep:=Sep+1;
  942. Sep:=NextSep(LastSep);
  943. end;
  944. if (LastSep<=Length) and ((ACount=0) or (Len<ACount)) then
  945. begin
  946. T:=SubString(LastSep);
  947. // Writeln('Examining >',T,'< at pos,',LastSep,' till pos ',Sep);
  948. If (T<>'') or (not (TStringSplitOptions.ExcludeEmpty=Options)) then
  949. begin
  950. MaybeGrow(Len);
  951. Result[Len]:=T;
  952. Inc(Len);
  953. end;
  954. end;
  955. if (TStringSplitOptions.ExcludeLastEmpty=Options) then
  956. if (Len > 0) and (Result[Len-1] = '') then
  957. dec(Len);
  958. SetLength(Result,Len);
  959. end;
  960. function TStringHelper.Split(const Separators: array of string; AQuote: Char
  961. ): TStringArray;
  962. begin
  963. Result:=SPlit(Separators,AQuote,AQuote);
  964. end;
  965. function TStringHelper.Split(const Separators: array of string; AQuoteStart,
  966. AQuoteEnd: Char): TStringArray;
  967. begin
  968. Result:=SPlit(Separators,AQuoteStart,AQuoteEnd,Length+1,TStringSplitOptions.None);
  969. end;
  970. function TStringHelper.Split(const Separators: array of string; AQuoteStart,
  971. AQuoteEnd: Char; Options: TStringSplitOptions): TStringArray;
  972. begin
  973. Result:=SPlit(Separators,AQuoteStart,AQuoteEnd,Length+1,Options);
  974. end;
  975. function TStringHelper.Split(const Separators: array of string; AQuoteStart,
  976. AQuoteEnd: Char; ACount: SizeInt): TStringArray;
  977. begin
  978. Result:=SPlit(Separators,AQuoteStart,AQuoteEnd,ACount,TStringSplitOptions.None);
  979. end;
  980. function TStringHelper.Split(const Separators: array of string; AQuoteStart,
  981. AQuoteEnd: Char; ACount: SizeInt; Options: TStringSplitOptions): TStringArray;
  982. Const
  983. BlockSize = 10;
  984. Function NextSep(StartIndex : SizeInt; out Match : SizeInt) : SizeInt;
  985. begin
  986. if (AQuoteStart<>#0) then
  987. Result:=Self.IndexOfAnyUnQuoted(Separators,AQuoteStart,AQuoteEnd,StartIndex,Match)
  988. else
  989. Result:=Self.IndexOfAny(Separators,StartIndex,Length,Match);
  990. if Result<>-1 then
  991. end;
  992. Procedure MaybeGrow(Curlen : SizeInt);
  993. begin
  994. if System.Length(Result)<=CurLen then
  995. SetLength(Result,System.Length(Result)+BlockSize);
  996. end;
  997. Var
  998. Sep,LastSep,Len,Match : SizeInt;
  999. T : String;
  1000. begin
  1001. SetLength(Result,BlockSize);
  1002. Len:=0;
  1003. LastSep:=0;
  1004. Sep:=NextSep(0,Match);
  1005. While (Sep<>-1) and ((ACount=0) or (Len<ACount)) do
  1006. begin
  1007. T:=SubString(LastSep,Sep-LastSep);
  1008. If (T<>'') or (not (TStringSplitOptions.ExcludeEmpty=Options)) then
  1009. begin
  1010. MaybeGrow(Len);
  1011. Result[Len]:=T;
  1012. Inc(Len);
  1013. end;
  1014. LastSep:=Sep+System.Length(Separators[Match]);
  1015. Sep:=NextSep(LastSep,Match);
  1016. end;
  1017. if (LastSep<=Length) and ((ACount=0) or (Len<ACount)) then
  1018. begin
  1019. T:=SubString(LastSep);
  1020. // Writeln('Examining >',T,'< at pos,',LastSep,' till pos ',Sep);
  1021. If (T<>'') or (not (TStringSplitOptions.ExcludeEmpty=Options)) then
  1022. begin
  1023. MaybeGrow(Len);
  1024. Result[Len]:=T;
  1025. Inc(Len);
  1026. end;
  1027. end;
  1028. If (TStringSplitOptions.ExcludeLastEmpty=Options) then
  1029. if (Len > 0) and (Result[Len-1] = '') then
  1030. dec(Len);
  1031. SetLength(Result,Len);
  1032. end;
  1033. function TStringHelper.StartsWith(const AValue: string): Boolean;
  1034. begin
  1035. Result:=StartsWith(AValue,False);
  1036. end;
  1037. function TStringHelper.StartsWith(const AValue: string; IgnoreCase: Boolean
  1038. ): Boolean;
  1039. Var
  1040. L : SizeInt;
  1041. S : String;
  1042. begin
  1043. L:=System.Length(AValue);
  1044. Result:=L<=0;
  1045. if not Result then
  1046. begin
  1047. S:=System.Copy(Self,1,L);
  1048. Result:=(System.Length(S)=L);
  1049. if Result then
  1050. if IgnoreCase then
  1051. Result:=SameText(S,aValue)
  1052. else
  1053. Result:=SameStr(S,AValue);
  1054. end;
  1055. end;
  1056. function TStringHelper.Substring(AStartIndex: SizeInt): string;
  1057. begin
  1058. Result:=Self.SubString(AStartIndex,Self.Length-AStartIndex);
  1059. end;
  1060. function TStringHelper.Substring(AStartIndex: SizeInt; ALen: SizeInt): string;
  1061. begin
  1062. Result:=system.Copy(Self,AStartIndex+1,ALen);
  1063. end;
  1064. function TStringHelper.ToBoolean: Boolean;
  1065. begin
  1066. Result:=StrToBool(Self);
  1067. end;
  1068. function TStringHelper.ToInteger: Integer;
  1069. begin
  1070. Result:=StrToInt(Self);
  1071. end;
  1072. function TStringHelper.ToInt64: Int64;
  1073. begin
  1074. Result:=StrToInt64(Self);
  1075. end;
  1076. function TStringHelper.ToSingle: Single;
  1077. begin
  1078. Result:=StrToFLoat(Self);
  1079. end;
  1080. function TStringHelper.ToDouble: Double;
  1081. begin
  1082. Result:=StrToFLoat(Self);
  1083. end;
  1084. function TStringHelper.ToExtended: Extended;
  1085. begin
  1086. Result:=StrToFLoat(Self);
  1087. end;
  1088. function TStringHelper.ToCharArray: TCharArray;
  1089. begin
  1090. Result:=ToCharArray(0,Self.Length);
  1091. end;
  1092. function TStringHelper.ToCharArray(AStartIndex: SizeInt; ALen: SizeInt
  1093. ): TCharArray;
  1094. Var
  1095. I : SizeInt;
  1096. begin
  1097. SetLength(Result,ALen);
  1098. For I:=0 to ALen-1 do
  1099. Result[I]:=Self[AStartIndex+I+1];
  1100. end;
  1101. function TStringHelper.ToLower: string;
  1102. begin
  1103. Result:=LowerCase(Self);
  1104. end;
  1105. function TStringHelper.ToLowerInvariant: string;
  1106. begin
  1107. Result:=LowerCase(Self);
  1108. end;
  1109. function TStringHelper.ToUpper: string;
  1110. begin
  1111. Result:=UpperCase(Self);
  1112. end;
  1113. function TStringHelper.ToUpperInvariant: string;
  1114. begin
  1115. Result:=UpperCase(Self);
  1116. end;
  1117. function TStringHelper.Trim: string;
  1118. begin
  1119. Result:=SysUtils.Trim(Self);
  1120. end;
  1121. function TStringHelper.TrimLeft: string;
  1122. begin
  1123. Result:=SysUtils.TrimLeft(Self);
  1124. end;
  1125. function TStringHelper.TrimRight: string;
  1126. begin
  1127. Result:=SysUtils.TrimRight(Self);
  1128. end;
  1129. function TStringHelper.Trim(const ATrimChars: array of Char): string;
  1130. begin
  1131. Result:=Self.TrimLeft(ATrimChars).TrimRight(ATrimChars);
  1132. end;
  1133. function TStringHelper.TrimLeft(const ATrimChars: array of Char): string;
  1134. Var
  1135. I,Len : SizeInt;
  1136. begin
  1137. I:=1;
  1138. Len:=Self.Length;
  1139. While (I<=Len) and HaveChar(Self[i],ATrimChars) do Inc(I);
  1140. if I=1 then
  1141. Result:=Self
  1142. else if I>Len then
  1143. Result:=''
  1144. else
  1145. Result:=system.Copy(Self,I,Len-I+1);
  1146. end;
  1147. function TStringHelper.TrimRight(const ATrimChars: array of Char): string;
  1148. Var
  1149. I,Len : SizeInt;
  1150. begin
  1151. Len:=Self.Length;
  1152. I:=Len;
  1153. While (I>=1) and HaveChar(Self[i],ATrimChars) do Dec(I);
  1154. if I<1 then
  1155. Result:=''
  1156. else if I=Len then
  1157. Result:=Self
  1158. else
  1159. Result:=system.Copy(Self,1,I);
  1160. end;
  1161. function TStringHelper.TrimEnd(const ATrimChars: array of Char): string;
  1162. begin
  1163. Result:=TrimRight(ATrimChars);
  1164. end;
  1165. function TStringHelper.TrimStart(const ATrimChars: array of Char): string;
  1166. begin
  1167. Result:=TrimLeft(ATrimChars);
  1168. end;
  1169. { ---------------------------------------------------------------------
  1170. TSingleHelper
  1171. ---------------------------------------------------------------------}
  1172. {$MACRO ON}
  1173. {$IFDEF FPC_HAS_TYPE_SINGLE}
  1174. {$define TFLOATHELPER:=TSingleHelper}
  1175. {$define FLOATTYPE:=Single}
  1176. {$define TFloatRec:=TSingleRec}
  1177. {$i syshelpf.inc}
  1178. {$UNDEF TFloatRec}
  1179. {$ENDIF FPC_HAS_TYPE_SINGLE}
  1180. { ---------------------------------------------------------------------
  1181. TDoubleHelper
  1182. ---------------------------------------------------------------------}
  1183. {$IFDEF FPC_HAS_TYPE_DOUBLE}
  1184. {$define TFLOATHELPER:=TDoubleHelper}
  1185. {$define FLOATTYPE:=Double}
  1186. {$define TFloatRec:=TDoubleRec}
  1187. {$i syshelpf.inc}
  1188. {$UNDEF TFloatRec}
  1189. {$ENDIF FPC_HAS_TYPE_DOUBLE}
  1190. { ---------------------------------------------------------------------
  1191. TExtendedHelper
  1192. ---------------------------------------------------------------------}
  1193. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1194. {$define TFLOATHELPER:=TExtendedHelper}
  1195. {$define FLOATTYPE:=Extended}
  1196. {$define TFloatRec:=TExtended80Rec}
  1197. {$i syshelpf.inc}
  1198. {$UNDEF TFloatRec}
  1199. {$ENDIF FPC_HAS_TYPE_EXTENDED}
  1200. { ---------------------------------------------------------------------
  1201. TByteHelper
  1202. ---------------------------------------------------------------------}
  1203. {$define TORDINALHELPER:=TByteHelper}
  1204. {$define TORDINALTYPE:=Byte}
  1205. {$define TORDINALBITINDEX:=TByteBitIndex}
  1206. {$define TORDINALNIBBLEINDEX:=TByteNibbleIndex}
  1207. {$define TORDINALOVERLAY:=TByteOverlay}
  1208. {$define TORDINALTYPESIZE1}
  1209. {$i syshelpo.inc}
  1210. {$undef TORDINALTYPESIZE1}
  1211. { ---------------------------------------------------------------------
  1212. TShortintHelper
  1213. ---------------------------------------------------------------------}
  1214. {$define TORDINALHELPER:=TShortIntHelper}
  1215. {$define TORDINALTYPE:=ShortInt}
  1216. {$define TORDINALBITINDEX:=TShortIntBitIndex}
  1217. {$define TORDINALNIBBLEINDEX:=TShortIntNibbleIndex}
  1218. {$define TORDINALOVERLAY:=TShortIntOverlay}
  1219. {$define TORDINALTYPESIZE1}
  1220. {$i syshelpo.inc}
  1221. {$undef TORDINALTYPESIZE1}
  1222. { ---------------------------------------------------------------------
  1223. TSmallintHelper
  1224. ---------------------------------------------------------------------}
  1225. {$define TORDINALHELPER:=TSmallIntHelper}
  1226. {$define TORDINALTYPE:=SmallInt}
  1227. {$define TORDINALBITINDEX:=TSmallIntBitIndex}
  1228. {$define TORDINALNIBBLEINDEX:=TSmallIntNibbleIndex}
  1229. {$define TORDINALBYTEINDEX:=TWordByteIndex}
  1230. {$define TORDINALOVERLAY:=TWordOverlay}
  1231. {$define TORDINALTYPESIZE2}
  1232. {$i syshelpo.inc}
  1233. {$undef TORDINALTYPESIZE2}
  1234. { ---------------------------------------------------------------------
  1235. TWordHelper
  1236. ---------------------------------------------------------------------}
  1237. {$define TORDINALHELPER:=TWordHelper}
  1238. {$define TORDINALTYPE:=Word}
  1239. {$define TORDINALBITINDEX:=TWordBitIndex}
  1240. {$define TORDINALNIBBLEINDEX:=TWordNibbleIndex}
  1241. {$define TORDINALBYTEINDEX:=TWordByteIndex}
  1242. {$define TORDINALOVERLAY:=TWordOverlay}
  1243. {$define TORDINALTYPESIZE2}
  1244. {$i syshelpo.inc}
  1245. {$undef TORDINALTYPESIZE2}
  1246. { ---------------------------------------------------------------------
  1247. TCardinalHelper
  1248. ---------------------------------------------------------------------}
  1249. {$define TORDINALHELPER:=TCardinalHelper}
  1250. {$define TORDINALTYPE:=Cardinal}
  1251. {$define TORDINALBITINDEX:=TCardinalBitIndex}
  1252. {$define TORDINALNIBBLEINDEX:=TCardinalNibbleIndex}
  1253. {$define TORDINALBYTEINDEX:=TCardinalByteIndex}
  1254. {$define TORDINALWORDINDEX:=TCardinalWordIndex}
  1255. {$define TORDINALOVERLAY:=TDwordOverlay}
  1256. {$define TORDINALTYPESIZE4}
  1257. {$i syshelpo.inc}
  1258. {$undef TORDINALTYPESIZE4}
  1259. { ---------------------------------------------------------------------
  1260. TIntegerHelper
  1261. ---------------------------------------------------------------------}
  1262. {$define TORDINALHELPER:=TIntegerHelper}
  1263. {$define TORDINALTYPE:=Integer}
  1264. {$define TORDINALBITINDEX:=TIntegerBitIndex}
  1265. {$define TORDINALNIBBLEINDEX:=TIntegerNibbleIndex}
  1266. {$define TORDINALBYTEINDEX:=TIntegerByteIndex}
  1267. {$define TORDINALWORDINDEX:=TIntegerWordIndex}
  1268. {$if sizeof(Integer)=2}
  1269. {$define TORDINALOVERLAY:=TWordOverlay}
  1270. {$define TORDINALTYPESIZE2}
  1271. {$elseif sizeof(Integer)=4}
  1272. {$define TORDINALOVERLAY:=TDwordOverlay}
  1273. {$define TORDINALTYPESIZE4}
  1274. {$else}
  1275. {$fatal Unsupported Integer type size}
  1276. {$endif}
  1277. {$i syshelpo.inc}
  1278. {$undef TORDINALTYPESIZE2}
  1279. {$undef TORDINALTYPESIZE4}
  1280. { ---------------------------------------------------------------------
  1281. TLongIntHelper
  1282. ---------------------------------------------------------------------}
  1283. {$define TORDINALHELPER:=TLongIntHelper}
  1284. {$define TORDINALTYPE:=LongInt}
  1285. {$define TORDINALBITINDEX:=TLongIntBitIndex}
  1286. {$define TORDINALNIBBLEINDEX:=TLongIntNibbleIndex}
  1287. {$define TORDINALBYTEINDEX:=TLongIntByteIndex}
  1288. {$define TORDINALWORDINDEX:=TLongIntWordIndex}
  1289. {$define TORDINALOVERLAY:=TDwordOverlay}
  1290. {$define TORDINALTYPESIZE4}
  1291. {$i syshelpo.inc}
  1292. {$undef TORDINALTYPESIZE4}
  1293. { ---------------------------------------------------------------------
  1294. TInt64Helper
  1295. ---------------------------------------------------------------------}
  1296. {$define TORDINALHELPER:=TInt64Helper}
  1297. {$define TORDINALTYPE:=Int64}
  1298. {$define TORDINALBITINDEX:=TInt64BitIndex}
  1299. {$define TORDINALNIBBLEINDEX:=TInt64NibbleIndex}
  1300. {$define TORDINALBYTEINDEX:=TInt64ByteIndex}
  1301. {$define TORDINALWORDINDEX:=TInt64WordIndex}
  1302. {$define TORDINALDWORDINDEX:=TInt64DWordIndex}
  1303. {$define TORDINALOVERLAY:=TQwordOverlay}
  1304. {$define TORDINALTYPESIZE8}
  1305. {$i syshelpo.inc}
  1306. {$undef TORDINALTYPESIZE8}
  1307. { ---------------------------------------------------------------------
  1308. TQWordHelper
  1309. ---------------------------------------------------------------------}
  1310. {$define TORDINALHELPER:=TQWordHelper}
  1311. {$define TORDINALTYPE:=QWord}
  1312. {$define TORDINALBITINDEX:=TQwordBitIndex}
  1313. {$define TORDINALNIBBLEINDEX:=TQwordNibbleIndex}
  1314. {$define TORDINALBYTEINDEX:=TQwordByteIndex}
  1315. {$define TORDINALWORDINDEX:=TQWordWordIndex}
  1316. {$define TORDINALDWORDINDEX:=TQWordDWordIndex}
  1317. {$define TORDINALOVERLAY:=TQwordOverlay}
  1318. {$define TORDINALTYPESIZE8}
  1319. {$i syshelpo.inc}
  1320. {$undef TORDINALTYPESIZE8}
  1321. { ---------------------------------------------------------------------
  1322. TNativeIntHelper
  1323. ---------------------------------------------------------------------}
  1324. {$define TORDINALHELPER:=TNativeIntHelper}
  1325. {$define TORDINALTYPE:=NativeInt}
  1326. {$define TORDINALBITINDEX:=TNativeIntBitIndex}
  1327. {$if sizeof(NativeInt)=2}
  1328. {$define TORDINALNIBBLEINDEX:=TSmallIntNibbleIndex}
  1329. {$define TORDINALBYTEINDEX:=TSmallIntByteIndex}
  1330. {$define TORDINALOVERLAY:=TSmallIntOverlay}
  1331. {$define TORDINALTYPESIZE2}
  1332. {$elseif sizeof(NativeInt)=4}
  1333. {$define TORDINALNIBBLEINDEX:=TLongIntNibbleIndex}
  1334. {$define TORDINALBYTEINDEX:=TLongIntByteIndex}
  1335. {$define TORDINALWORDINDEX:=TLongIntWordIndex}
  1336. {$define TORDINALOVERLAY:=TLongIntOverlay}
  1337. {$define TORDINALTYPESIZE4}
  1338. {$elseif sizeof(NativeInt)=8}
  1339. {$define TORDINALNIBBLEINDEX:=TInt64NibbleIndex}
  1340. {$define TORDINALBYTEINDEX:=TInt64ByteIndex}
  1341. {$define TORDINALWORDINDEX:=TInt64WordIndex}
  1342. {$define TORDINALDWORDINDEX:=TInt64DWordIndex}
  1343. {$define TORDINALOVERLAY:=TInt64Overlay}
  1344. {$define TORDINALTYPESIZE8}
  1345. {$else}
  1346. {$fatal Unsupported NativeInt type size}
  1347. {$endif}
  1348. {$i syshelpo.inc}
  1349. {$undef TORDINALTYPESIZE2}
  1350. {$undef TORDINALTYPESIZE4}
  1351. {$undef TORDINALTYPESIZE8}
  1352. { ---------------------------------------------------------------------
  1353. TNativeUIntHelper
  1354. ---------------------------------------------------------------------}
  1355. {$define TORDINALHELPER:=TNativeUIntHelper}
  1356. {$define TORDINALTYPE:=NativeUInt}
  1357. {$define TORDINALBITINDEX:=TNativeUIntBitIndex}
  1358. {$if sizeof(NativeUInt)=2}
  1359. {$define TORDINALNIBBLEINDEX:=TWordNibbleIndex}
  1360. {$define TORDINALBYTEINDEX:=TWordByteIndex}
  1361. {$define TORDINALOVERLAY:=TWordOverlay}
  1362. {$define TORDINALTYPESIZE2}
  1363. {$elseif sizeof(NativeUInt)=4}
  1364. {$define TORDINALNIBBLEINDEX:=TDwordNibbleIndex}
  1365. {$define TORDINALBYTEINDEX:=TDwordByteIndex}
  1366. {$define TORDINALWORDINDEX:=TDwordWordIndex}
  1367. {$define TORDINALOVERLAY:=TDwordOverlay}
  1368. {$define TORDINALTYPESIZE4}
  1369. {$elseif sizeof(NativeUInt)=8}
  1370. {$define TORDINALNIBBLEINDEX:=TQwordNibbleIndex}
  1371. {$define TORDINALBYTEINDEX:=TQwordByteIndex}
  1372. {$define TORDINALWORDINDEX:=TQwordWordIndex}
  1373. {$define TORDINALDWORDINDEX:=TQwordDWordIndex}
  1374. {$define TORDINALOVERLAY:=TQwordOverlay}
  1375. {$define TORDINALTYPESIZE8}
  1376. {$else}
  1377. {$fatal Unsupported NativeUInt type size}
  1378. {$endif}
  1379. {$i syshelpo.inc}
  1380. {$undef TORDINALTYPESIZE2}
  1381. {$undef TORDINALTYPESIZE4}
  1382. {$undef TORDINALTYPESIZE8}
  1383. { ---------------------------------------------------------------------
  1384. TBooleanHelper
  1385. ---------------------------------------------------------------------}
  1386. {$define TBOOLHELPER:=TBooleanHelper}
  1387. {$define TBOOLTYPE:=Boolean}
  1388. {$i syshelpb.inc}
  1389. { ---------------------------------------------------------------------
  1390. TBoolean8Helper
  1391. ---------------------------------------------------------------------}
  1392. {$define TBOOLHELPER:=TBoolean8Helper}
  1393. {$define TBOOLTYPE:=Boolean8}
  1394. {$i syshelpb.inc}
  1395. { ---------------------------------------------------------------------
  1396. TBoolean16Helper
  1397. ---------------------------------------------------------------------}
  1398. {$define TBOOLHELPER:=TBoolean16Helper}
  1399. {$define TBOOLTYPE:=Boolean16}
  1400. {$i syshelpb.inc}
  1401. { ---------------------------------------------------------------------
  1402. TBoolean32Helper
  1403. ---------------------------------------------------------------------}
  1404. {$define TBOOLHELPER:=TBoolean32Helper}
  1405. {$define TBOOLTYPE:=Boolean32}
  1406. {$i syshelpb.inc}
  1407. { ---------------------------------------------------------------------
  1408. TBoolean64Helper
  1409. ---------------------------------------------------------------------}
  1410. {$define TBOOLHELPER:=TBoolean64Helper}
  1411. {$define TBOOLTYPE:=Boolean64}
  1412. {$i syshelpb.inc}
  1413. { ---------------------------------------------------------------------
  1414. TByteBoolHelper
  1415. ---------------------------------------------------------------------}
  1416. {$define TBOOLHELPER:=TByteBoolHelper}
  1417. {$define TBOOLTYPE:=ByteBool}
  1418. {$i syshelpb.inc}
  1419. { ---------------------------------------------------------------------
  1420. TWordBoolHelper
  1421. ---------------------------------------------------------------------}
  1422. {$define TBOOLHELPER:=TWordBoolHelper}
  1423. {$define TBOOLTYPE:=WordBool}
  1424. {$i syshelpb.inc}
  1425. { ---------------------------------------------------------------------
  1426. TLongBoolHelper
  1427. ---------------------------------------------------------------------}
  1428. {$define TBOOLHELPER:=TLongBoolHelper}
  1429. {$define TBOOLTYPE:=LongBool}
  1430. {$i syshelpb.inc}
  1431. { ---------------------------------------------------------------------
  1432. TQWordBoolHelper
  1433. ---------------------------------------------------------------------}
  1434. {$define TBOOLHELPER:=TQWordBoolHelper}
  1435. {$define TBOOLTYPE:=QWordBool}
  1436. {$i syshelpb.inc}