syshelp.inc 44 KB

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