syshelp.inc 43 KB

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