syshelps.inc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  1. {%MainUnit sysutils.pp}
  2. { ---------------------------------------------------------------------
  3. TStringHelper
  4. ---------------------------------------------------------------------}
  5. {$IFNDEF IS_SHORTSTRINGHELPER}
  6. {$IFNDEF IS_UNICODESTRINGHELPER}
  7. // Doubles with (wide/ansi)string...
  8. Function HaveChar(AChar : TStringChar; const AList: array of TStringChar) : Boolean;
  9. Var
  10. I : SizeInt;
  11. begin
  12. I:=0;
  13. Result:=False;
  14. While (Not Result) and (I<Length(AList)) do
  15. begin
  16. Result:=(AList[i]=AChar);
  17. Inc(I);
  18. end;
  19. end;
  20. {$ENDIF}
  21. {$ENDIF}
  22. function TStringHelper.GetChar(AIndex: SizeInt): TStringChar;
  23. begin
  24. Result:=Self[AIndex+1];
  25. end;
  26. function TStringHelper.GetLength: SizeInt;
  27. begin
  28. Result:=System.Length(Self);
  29. end;
  30. class function TStringHelper.Compare(const A: TStringType; const B: TStringType): Integer;
  31. begin
  32. Result:=Compare(A,0,B,0,System.Length(B),[]);
  33. end;
  34. class function TStringHelper.Compare(const A: TStringType; const B: TStringType;
  35. IgnoreCase: Boolean): Integer; //deprecated 'Use same with TCompareOptions';
  36. begin
  37. if IgnoreCase then
  38. Result:=Compare(A,B,[coIgnoreCase])
  39. else
  40. Result:=Compare(A,B,[]);
  41. end;
  42. class function TStringHelper.Compare(const A: TStringType; const B: TStringType;
  43. Options: TCompareOptions): Integer;
  44. begin
  45. Result:=Compare(A,0,B,0,System.Length(B),Options);
  46. end;
  47. class function TStringHelper.Compare(const A: TStringType; IndexA: SizeInt;
  48. const B: TStringType; IndexB: SizeInt; ALen: SizeInt): Integer;
  49. begin
  50. Result:=Compare(A,IndexA,B,IndexB,ALen,[]);
  51. end;
  52. class function TStringHelper.Compare(const A: TStringType; IndexA: SizeInt;
  53. const B: TStringType; IndexB: SizeInt; ALen: SizeInt; IgnoreCase: Boolean
  54. ): Integer; //deprecated 'Use same with TCompareOptions';
  55. begin
  56. if IgnoreCase then
  57. Result:=Compare(A,IndexA,B,IndexB,ALen,[coIgnoreCase])
  58. else
  59. Result:=Compare(A,IndexA,B,IndexB,ALen,[])
  60. end;
  61. class function TStringHelper.Compare(const A: TStringType; IndexA: SizeInt;
  62. const B: TStringType; IndexB: SizeInt; ALen: SizeInt; Options: TCompareOptions
  63. ): Integer;
  64. Var
  65. L : SizeInt;
  66. begin
  67. L:=ALen;
  68. If (L>system.Length(A)-IndexA) then
  69. L:=system.Length(A)-IndexA;
  70. If (L>system.Length(B)-IndexB) then
  71. L:=system.Length(B)-IndexB;
  72. if (coIgnoreCase in Options) then
  73. begin
  74. Result:=strlicomp(PTStringChar(@A[IndexA+1]),PTStringChar(@B[IndexB+1]),L)
  75. end
  76. else
  77. Result:=strlcomp(PTStringChar(@A[IndexA+1]),PTStringChar(@B[IndexB+1]),L);
  78. end;
  79. class function TStringHelper.CompareOrdinal(const A: TStringType; const B: TStringType
  80. ): Integer;
  81. Var
  82. L : SizeInt;
  83. begin
  84. L:=System.Length(B);
  85. if L>System.Length(A) then
  86. L:=System.Length(A);
  87. Result:=CompareOrdinal(A,0,B,0,L);
  88. end;
  89. class function TStringHelper.CompareOrdinal(const A: TStringType; IndexA: SizeInt;
  90. const B: TStringType; IndexB: SizeInt; ALen: SizeInt): Integer;
  91. begin
  92. Result:=StrLComp(PTStringChar(@A[IndexA+1]), PTStringChar(@B[IndexB+1]), ALen);
  93. end;
  94. class function TStringHelper.CompareText(const A: TStringType; const B: TStringType
  95. ): Integer;
  96. begin
  97. Result:=Sysutils.CompareText(A,B);
  98. end;
  99. class function TStringHelper.Copy(const Str: TStringType): TStringType;
  100. begin
  101. Result:=Str;
  102. {$IFNDEF IS_SHORTSTRINGHELPER}
  103. UniqueString(Result);
  104. {$ENDIF}
  105. end;
  106. class function TStringHelper.Create(AChar: TStringChar; ACount: SizeInt): TStringType;
  107. begin
  108. Result:=StringOfChar(AChar,ACount);
  109. end;
  110. class function TStringHelper.Create(const AValue: array of TStringChar): TStringType;
  111. begin
  112. Result:=Create(AValue,0,System.Length(AValue));
  113. end;
  114. class function TStringHelper.Create(const AValue: array of TStringChar;
  115. StartIndex: SizeInt; ALen: SizeInt): TStringType;
  116. begin
  117. SetLength(Result,ALen);
  118. if ALen>0 then
  119. Move(AValue[StartIndex],Result[1],ALen);
  120. end;
  121. class function TStringHelper.EndsText(const ASubText, AText: TStringType): Boolean;
  122. begin
  123. Result:=(ASubText<>'') and (sysutils.CompareText(System.Copy(AText,System.Length(AText)-System.Length(ASubText)+1,System.Length(ASubText)),ASubText)=0);
  124. end;
  125. class function TStringHelper.Equals(const a: TStringType; const b: TStringType): Boolean;
  126. begin
  127. Result:=A=B;
  128. end;
  129. class function TStringHelper.Format(const AFormat: TStringType;
  130. const args: array of const): TStringType;
  131. begin
  132. Result:=Sysutils.Format(AFormat,Args);
  133. end;
  134. class function TStringHelper.IsNullOrEmpty(const AValue: TStringType): Boolean;
  135. begin
  136. Result:=system.Length(AValue)=0;
  137. end;
  138. class function TStringHelper.IsNullOrWhiteSpace(const AValue: TStringType): Boolean;
  139. const
  140. LWhiteSpace = [#0..' '];
  141. var
  142. I: SizeInt;
  143. begin
  144. for I:=1 to System.Length(AValue) do
  145. if not (AValue[I] in LWhiteSpace) then
  146. exit(False);
  147. Result:=True;
  148. end;
  149. class function TStringHelper.Join(const Separator: TStringType;
  150. const Values: array of const): TStringType;
  151. Var
  152. SValues : Array of TStringType;
  153. I,L : SizeInt;
  154. S : TStringType;
  155. P : ^TVarRec;
  156. begin
  157. L:=System.Length(Values);
  158. SetLength(SValues,L);
  159. Dec(L);
  160. for I:=0 to L do
  161. begin
  162. S:='';
  163. P:=@Values[I];
  164. Case P^.VType of
  165. vtInteger : S:=IntToStr(P^.VInteger);
  166. vtBoolean : S:=BoolToStr(P^.VBoolean, True);
  167. vtChar : S:=P^.VChar;
  168. vtPChar : S:= TStringType(P^.VPChar);
  169. {$ifndef FPUNONE}
  170. vtExtended : S:=FloatToStr(P^.VExtended^);
  171. {$endif}
  172. vtObject : S:=TObject(P^.VObject).Classname;
  173. vtClass : S:=P^.VClass.Classname;
  174. vtCurrency : S:=CurrToStr(P^.VCurrency^);
  175. vtVariant : S:=(P^.VVariant^);
  176. vtInt64 : S:=IntToStr(PInt64(P^.VInt64)^);
  177. vtQword : S:=IntToStr(PQWord(P^.VQword)^);
  178. vtWideChar : S:=WideString(P^.VWideChar);
  179. vtPWideChar : S:=WideString(P^.VPWideChar);
  180. vtUnicodeString : S:=UnicodeString(P^.VUnicodeString);
  181. vtAnsiString : S:=Ansistring(P^.VAnsiString);
  182. else
  183. S:=Format('Unknown type: %d',[P^.VType]);
  184. end;
  185. SValues[I]:=S;
  186. end;
  187. Result:=Join(Separator,SValues);
  188. end;
  189. class function TStringHelper.Join(const Separator: TStringType;
  190. const Values: array of TStringType): TStringType;
  191. begin
  192. Result:=Join(Separator,Values,0,System.Length(Values));
  193. end;
  194. class function TStringHelper.Join(const Separator: TStringType;
  195. const Values: array of TStringType; StartIndex: SizeInt; ACount: SizeInt): TStringType;
  196. Var
  197. VLen,I,CountLim,NR,NSep,N : SizeInt;
  198. Rp: PTStringChar;
  199. begin
  200. VLen:=System.Length(Values);
  201. If (ACount=0) then
  202. Exit('');
  203. CountLim:=VLen-StartIndex;
  204. if ACount>CountLim then
  205. ACount:=CountLim;
  206. If (ACount<0) or (StartIndex>VLen) or (StartIndex<0) then
  207. raise ERangeError.Create(SRangeError);
  208. if ACount=1 then
  209. exit(Values[StartIndex]);
  210. NSep:=System.Length(Separator);
  211. NR:=(ACount-1)*NSep;
  212. for I:=StartIndex to StartIndex+ACount-1 do
  213. NR:=NR+System.Length(Values[I]);
  214. SetLength(Result,NR);
  215. Rp:=@Result[1];
  216. for I:=StartIndex to StartIndex+ACount-1 do
  217. begin
  218. if I>StartIndex then
  219. begin
  220. Move(separator[1],Rp^,NSep*sizeof(TStringChar));
  221. Rp:=Rp+NSep;
  222. end;
  223. N:=System.Length(Values[I]);
  224. Move(Values[I][1],Rp^,N*sizeof(TStringChar));
  225. Rp:=Rp+N;
  226. end;
  227. end;
  228. class function TStringHelper.LowerCase(const S: TStringType): TStringType;
  229. begin
  230. Result:=sysutils.Lowercase(S);
  231. end;
  232. class function TStringHelper.Parse(const AValue: Boolean): TStringType;
  233. begin
  234. Result:=BoolToStr(AValue);
  235. end;
  236. class function TStringHelper.Parse(const AValue: Extended): TStringType;
  237. begin
  238. Result:=FloatToStr(AValue);
  239. end;
  240. class function TStringHelper.Parse(const AValue: Int64): TStringType;
  241. begin
  242. Result:=IntToStr(AValue);
  243. end;
  244. class function TStringHelper.Parse(const AValue: Integer): TStringType;
  245. begin
  246. Result:=IntToStr(AValue);
  247. end;
  248. class function TStringHelper.ToBoolean(const S: TStringType): Boolean;
  249. begin
  250. Result:=StrToBool(S);
  251. end;
  252. class function TStringHelper.ToDouble(const S: TStringType): Double;
  253. begin
  254. Result:=StrToFloat(S);
  255. end;
  256. class function TStringHelper.ToExtended(const S: TStringType): Extended;
  257. begin
  258. Result:=StrToFloat(S);
  259. end;
  260. class function TStringHelper.ToInt64(const S: TStringType): Int64;
  261. begin
  262. Result:=StrToInt64(S);
  263. end;
  264. class function TStringHelper.ToInteger(const S: TStringType): Integer;
  265. begin
  266. Result:=StrToInt(S);
  267. end;
  268. class function TStringHelper.ToSingle(const S: TStringType): Single;
  269. begin
  270. Result:=StrToFloat(S);
  271. end;
  272. class function TStringHelper.UpperCase(const S: TStringType): TStringType;
  273. begin
  274. Result:=sysutils.Uppercase(S);
  275. end;
  276. function TStringHelper.CompareTo(const B: TStringType): Integer;
  277. begin
  278. // Order is important
  279. {$IFDEF IS_SHORTSTRINGHELPER}
  280. Result:=sysUtils.StrComp(PTStringChar(@Self[1]),PTStringChar(@B[1]));
  281. {$ELSE}
  282. Result:=sysUtils.StrComp(PTStringChar(Self),PTStringChar(B));
  283. {$ENDIF}
  284. end;
  285. procedure TStringHelper.CopyTo(SourceIndex: SizeInt; var destination: array of TStringChar; DestinationIndex: SizeInt; ACount: SizeInt);
  286. Var
  287. P1,P2 : PTStringChar;
  288. begin
  289. // Writeln('((',DestinationIndex,'+',ACount,')<',System.Length(Destination),') : ', ((DestinationIndex+ACount)<System.Length(Destination)));
  290. if ((DestinationIndex+ACount)<=System.Length(Destination)) then
  291. begin
  292. // Writeln('AHA');
  293. P1:=@Self[SourceIndex+1];
  294. P2:=@Destination[DestinationIndex];
  295. Move(P1^,P2^,ACount*SizeOf(TStringChar));
  296. end;
  297. end;
  298. function TStringHelper.Contains(const AValue: TStringType; IgnoreCase: Boolean): Boolean;
  299. begin
  300. if IgnoreCase then
  301. Result:=Pos(LowerCase(AValue),LowerCase(Self))>0
  302. else
  303. Result:=Pos(AValue,Self)>0;
  304. end;
  305. function TStringHelper.CountChar(const C: TStringChar): SizeInt;
  306. Var
  307. S : TStringChar;
  308. begin
  309. Result:=0;
  310. For S in Self do
  311. if (S=C) then
  312. Inc(Result);
  313. end;
  314. function TStringHelper.DeQuotedString: TStringType;
  315. begin
  316. Result:=DeQuotedString('''');
  317. end;
  318. function TStringHelper.DeQuotedString(const AQuoteChar: TStringChar): TStringType;
  319. var
  320. L,I : SizeInt;
  321. Res : Array of TStringChar;
  322. PS,PD : PTStringChar;
  323. IsQuote : Boolean;
  324. begin
  325. L:=System.Length(Self);
  326. if (L<2) or Not ((Self[1]=AQuoteChar) and (Self[L]=AQuoteChar)) then
  327. Exit(Self);
  328. SetLength(Res,L);
  329. IsQuote:=False;
  330. PS:=@Self[2];
  331. PD:=@Res[0];
  332. For I:=2 to L-1 do
  333. begin
  334. if (PS^=AQuoteChar) then
  335. begin
  336. IsQuote:=Not IsQuote;
  337. if Not IsQuote then
  338. begin
  339. PD^:=PS^;
  340. Inc(PD);
  341. end;
  342. end
  343. else
  344. begin
  345. if IsQuote then
  346. IsQuote:=false;
  347. PD^:=PS^;
  348. Inc(PD);
  349. end;
  350. Inc(PS);
  351. end;
  352. SetString(Result,@Res[0],PD-@Res[0]);
  353. end;
  354. function TStringHelper.EndsWith(const AValue: TStringType): Boolean;
  355. begin
  356. Result:=EndsWith(AValue,False);
  357. end;
  358. function TStringHelper.EndsWith(const AValue: TStringType; IgnoreCase: Boolean): Boolean;
  359. Var
  360. L,NS : SizeInt;
  361. begin
  362. L:=system.Length(AVAlue);
  363. NS:=System.Length(Self);
  364. Result:=L<=NS;
  365. if Result then
  366. if IgnoreCase then
  367. Result:=SameText(System.Copy(Self,NS-L+1,L),AValue)
  368. else
  369. {$IFDEF IS_SHORTSTRINGHELPER}
  370. Result:=CompareChar(PTStringChar(@Self[1])[NS-L],PTStringChar(@AValue[1])^,L)=0;
  371. {$ELSE}
  372. Result:=CompareChar(PTStringChar(Pointer(Self))[NS-L],PTStringChar(Pointer(AValue))^,L)=0;
  373. {$ENDIF}
  374. end;
  375. function TStringHelper.Equals(const AValue: TStringType; IgnoreCase: Boolean = False): Boolean;
  376. begin
  377. if IgnoreCase then
  378. Result:=SameText(Self,aValue)
  379. else
  380. Result:=(Self=AValue);
  381. end;
  382. function TStringHelper.Format(const args: array of const): TStringType;
  383. begin
  384. Result:=Format(Self,Args);
  385. end;
  386. function TStringHelper.GetHashCode: Integer;
  387. // Taken from contnrs, fphash
  388. var
  389. P,pmax : PTStringChar;
  390. begin
  391. {$push}
  392. {$Q-}
  393. Result:=0;
  394. {$IFDEF IS_SHORTSTRINGHELPER}
  395. P:=PTStringChar(@Self[1]);
  396. {$ELSE}
  397. P:=PTStringChar(Self);
  398. {$ENDIF}
  399. pmax:=p+length;
  400. while (p<pmax) do
  401. begin
  402. Result:=LongWord(LongInt(Result shl 5) - LongInt(Result)) xor LongWord(P^);
  403. Inc(p);
  404. end;
  405. {$pop}
  406. end;
  407. function TStringHelper.IndexOf(AValue: TStringChar): SizeInt;
  408. begin
  409. Result:=IndexOf(AValue,0,Length);
  410. end;
  411. function TStringHelper.IndexOf(const AValue: TStringType): SizeInt;
  412. begin
  413. Result:=IndexOf(AValue,0,Length);
  414. end;
  415. function TStringHelper.IndexOf(AValue: TStringChar; StartIndex: SizeInt): SizeInt;
  416. begin
  417. Result:=IndexOf(AValue,StartIndex,Length);
  418. end;
  419. function TStringHelper.IndexOf(const AValue: TStringType; StartIndex: SizeInt
  420. ): SizeInt;
  421. begin
  422. Result:=IndexOf(AValue,StartIndex,Length);
  423. end;
  424. function TStringHelper.IndexOf(AValue: TStringChar; StartIndex: SizeInt;
  425. ACount: SizeInt): SizeInt;
  426. Var
  427. CountLim : SizeInt;
  428. begin
  429. if StartIndex<0 then
  430. StartIndex:=0;
  431. CountLim:=System.Length(Self)-StartIndex;
  432. if ACount>CountLim then
  433. ACount:=CountLim;
  434. if ACount<=0 then
  435. Exit(-1);
  436. // pointer casts are to access self as 0 based index!
  437. {$IFDEF IS_SHORTSTRINGHELPER}
  438. Result:=IndexChar(PTStringChar(@self[1])[StartIndex],ACount,AValue);
  439. {$ELSE}
  440. Result:=IndexChar(PTStringChar(Pointer(self))[StartIndex],ACount,AValue);
  441. {$ENDIF}
  442. if Result>=0 then
  443. Result:=Result+StartIndex;
  444. end;
  445. function TStringHelper.IndexOf(const AValue: TStringType; StartIndex: SizeInt;
  446. ACount: SizeInt): SizeInt;
  447. Var
  448. CountLim,NV,Ofs : SizeInt;
  449. SP,SE : PTStringChar;
  450. begin
  451. if StartIndex<0 then
  452. StartIndex:=0;
  453. CountLim:=System.Length(Self)-StartIndex;
  454. if ACount>CountLim then
  455. ACount:=CountLim;
  456. NV:=System.Length(AValue);
  457. if (NV>0) and (ACount>=NV) then
  458. begin
  459. {$IFDEF IS_SHORTSTRINGHELPER}
  460. SP:=PTStringChar(@Self[1])+StartIndex;
  461. {$ELSE}
  462. SP:=PTStringChar(Pointer(Self))+StartIndex;
  463. {$ENDIF}
  464. SE:=SP+ACount-NV+1;
  465. repeat
  466. {$IFDEF IS_SHORTSTRINGHELPER}
  467. Ofs:=IndexChar(SP^,SE-SP,PTStringChar(@AValue[1])[0]);
  468. {$ELSE}
  469. Ofs:=IndexChar(SP^,SE-SP,PTStringChar(Pointer(AValue))[0]);
  470. {$ENDIF}
  471. if Ofs<0 then
  472. Break;
  473. SP:=SP+Ofs+1;
  474. {$IFDEF IS_SHORTSTRINGHELPER}
  475. if CompareChar(SP^,PTStringChar(@AValue[1])[1],NV-1)=0 then
  476. Exit(SP-PTStringChar(@Self[1])-1);
  477. {$ELSE}
  478. if CompareChar(SP^,PTStringChar(Pointer(AValue))[1],NV-1)=0 then
  479. Exit(SP-PTStringChar(Pointer(Self))-1);
  480. {$ENDIF}
  481. until false;
  482. end;
  483. Result:=-1;
  484. end;
  485. function TStringHelper.IndexOfUnQuoted(const AValue: TStringType; StartQuote,
  486. EndQuote: TStringChar; StartIndex: SizeInt = 0): SizeInt;
  487. Var
  488. LV : SizeInt;
  489. Function MatchAt(I : SizeInt) : Boolean ; Inline;
  490. Var
  491. J : SizeInt;
  492. begin
  493. J:=1;
  494. Repeat
  495. Result:=(Self[I+J-1]=AValue[j]);
  496. Inc(J);
  497. Until (Not Result) or (J>LV);
  498. end;
  499. Var
  500. I,L,Q: SizeInt;
  501. begin
  502. Result:=-1;
  503. LV:=system.Length(AValue);
  504. L:=Length-LV+1;
  505. if L<0 then
  506. L:=0;
  507. I:=StartIndex+1;
  508. Q:=0;
  509. if StartQuote=EndQuote then
  510. begin
  511. While (Result=-1) and (I<=L) do
  512. begin
  513. if (Self[I]=StartQuote) then
  514. Q:=1-Q;
  515. if (Q=0) and MatchAt(i) then
  516. Result:=I-1;
  517. Inc(I);
  518. end;
  519. end
  520. else
  521. begin
  522. While (Result=-1) and (I<=L) do
  523. begin
  524. if Self[I]=StartQuote then
  525. Inc(Q)
  526. else if (Self[I]=EndQuote) and (Q>0) then
  527. Dec(Q);
  528. if (Q=0) and MatchAt(i) then
  529. Result:=I-1;
  530. Inc(I);
  531. end;
  532. end;
  533. end;
  534. function TStringHelper.IndexOfAny(const AnyOf: array of TStringChar): SizeInt;
  535. begin
  536. Result:=IndexOfAny(AnyOf,0,Length);
  537. end;
  538. function TStringHelper.IndexOfAny(const AnyOf: array of TStringChar;
  539. StartIndex: SizeInt): SizeInt;
  540. begin
  541. Result:=IndexOfAny(AnyOf,StartIndex,Length);
  542. end;
  543. function TStringHelper.IndexOfAny(const AnyOf: array of TStringChar;
  544. StartIndex: SizeInt; ACount: SizeInt): SizeInt;
  545. Var
  546. i,L : SizeInt;
  547. begin
  548. I:=StartIndex+1;
  549. L:=I+ACount-1;
  550. If L>Length then
  551. L:=Length;
  552. Result:=-1;
  553. While (Result=-1) and (I<=L) do
  554. begin
  555. if HaveChar(Self[i],AnyOf) then
  556. Result:=I-1;
  557. Inc(I);
  558. end;
  559. end;
  560. function TStringHelper.IndexOfAny(const AnyOf: array of TStringType): SizeInt;
  561. begin
  562. Result:=IndexOfAny(AnyOf,0,Length);
  563. end;
  564. function TStringHelper.IndexOfAny(const AnyOf: array of TStringType;
  565. StartIndex: SizeInt): SizeInt;
  566. begin
  567. Result:=IndexOfAny(AnyOf,StartIndex,Length-StartIndex);
  568. end;
  569. function TStringHelper.IndexOfAny(const AnyOf: array of TStringType;
  570. StartIndex: SizeInt; ACount: SizeInt): SizeInt;
  571. Var
  572. M : SizeInt;
  573. begin
  574. Result:=IndexOfAny(AnyOf,StartIndex,ACount,M);
  575. end;
  576. function TStringHelper.IndexOfAny(const AnyOf: array of TStringType;
  577. StartIndex: SizeInt; ACount: SizeInt; out AMatch: SizeInt): SizeInt;
  578. Var
  579. L,I : SizeInt;
  580. begin
  581. Result:=-1;
  582. For I:=0 to System.Length(AnyOf)-1 do
  583. begin
  584. L:=IndexOf(AnyOf[i],StartIndex,ACount);
  585. If (L>=0) and ((Result=-1) or (L<Result)) then
  586. begin
  587. Result:=L;
  588. AMatch:=I;
  589. end;
  590. end;
  591. end;
  592. function TStringHelper.IndexOfAnyUnquoted(const AnyOf: array of TStringChar;
  593. StartQuote, EndQuote: TStringChar): SizeInt;
  594. begin
  595. Result:=IndexOfAnyUnquoted(AnyOf,StartQuote,EndQuote,0,Length);
  596. end;
  597. function TStringHelper.IndexOfAnyUnquoted(const AnyOf: array of TStringChar;
  598. StartQuote, EndQuote: TStringChar; StartIndex: SizeInt): SizeInt;
  599. begin
  600. Result:=IndexOfAnyUnquoted(AnyOf,StartQuote,EndQuote,StartIndex,Length);
  601. end;
  602. function TStringHelper.IndexOfAnyUnquoted(const AnyOf: array of TStringChar;
  603. StartQuote, EndQuote: TStringChar; StartIndex: SizeInt; ACount: SizeInt): SizeInt;
  604. Var
  605. I,L : SizeInt;
  606. Q : SizeInt;
  607. begin
  608. Result:=-1;
  609. L:=StartIndex+ACount-1;
  610. if L>Length then
  611. L:=Length;
  612. I:=StartIndex+1;
  613. Q:=0;
  614. if StartQuote=EndQuote then
  615. begin
  616. While (Result=-1) and (I<=L) do
  617. begin
  618. if (Self[I]=StartQuote) then
  619. Q:=1-Q;
  620. if (Q=0) and HaveChar(Self[i],AnyOf) then
  621. Result:=I-1;
  622. Inc(I);
  623. end;
  624. end
  625. else
  626. begin
  627. While (Result=-1) and (I<=L) do
  628. begin
  629. if Self[I]=StartQuote then
  630. Inc(Q)
  631. else if (Self[I]=EndQuote) and (Q>0) then
  632. Dec(Q);
  633. if (Q=0) and HaveChar(Self[i],AnyOf) then
  634. Result:=I-1;
  635. Inc(I);
  636. end;
  637. end;
  638. end;
  639. function TStringHelper.IndexOfAnyUnquoted(const AnyOf: array of TStringType;
  640. StartQuote, EndQuote: TStringChar; StartIndex: SizeInt; out Matched: SizeInt
  641. ): SizeInt;
  642. Var
  643. L,I : SizeInt;
  644. begin
  645. Result:=-1;
  646. For I:=0 to System.Length(AnyOf)-1 do
  647. begin
  648. L:=IndexOfUnquoted(AnyOf[i],StartQuote,EndQuote,StartIndex);
  649. If (L>=0) and ((Result=-1) or (L<Result)) then
  650. begin
  651. Result:=L;
  652. Matched:=I;
  653. end;
  654. end;
  655. end;
  656. function TStringHelper.Insert(StartIndex: SizeInt; const AValue: TStringType
  657. ): TStringType;
  658. begin
  659. system.Insert(AValue,Self,StartIndex+1);
  660. Result:=Self;
  661. end;
  662. function TStringHelper.IsDelimiter(const Delimiters: TStringType; Index: SizeInt
  663. ): Boolean;
  664. begin
  665. Result:=sysutils.IsDelimiter(Delimiters,Self,Index+1);
  666. end;
  667. function TStringHelper.IsEmpty: Boolean;
  668. begin
  669. Result:=(Length=0)
  670. end;
  671. function TStringHelper.LastDelimiter(const Delims: TStringType): SizeInt;
  672. begin
  673. Result:=sysutils.LastDelimiter(Delims,Self)-1;
  674. end;
  675. function TStringHelper.LastIndexOf(AValue: TStringChar): SizeInt;
  676. begin
  677. Result:=LastIndexOf(AValue,Length-1,Length);
  678. end;
  679. function TStringHelper.LastIndexOf(const AValue: TStringType): SizeInt;
  680. begin
  681. Result:=LastIndexOf(AValue,Length-1,Length);
  682. end;
  683. function TStringHelper.LastIndexOf(AValue: TStringChar; AStartIndex: SizeInt): SizeInt;
  684. begin
  685. Result:=LastIndexOf(AValue,AStartIndex,Length);
  686. end;
  687. function TStringHelper.LastIndexOf(const AValue: TStringType; AStartIndex: SizeInt
  688. ): SizeInt;
  689. begin
  690. Result:=LastIndexOf(AValue,AStartIndex,Length);
  691. end;
  692. function TStringHelper.LastIndexOf(AValue: TStringChar; AStartIndex: SizeInt;
  693. ACount: SizeInt): SizeInt;
  694. Var
  695. Min : SizeInt;
  696. begin
  697. Result:=AStartIndex+1;
  698. Min:=Result-ACount+1;
  699. If Min<1 then
  700. Min:=1;
  701. While (Result>=Min) and (Self[Result]<>AValue) do
  702. Dec(Result);
  703. if Result<Min then
  704. Result:=-1
  705. else
  706. Result:=Result-1;
  707. end;
  708. function TStringHelper.LastIndexOf(const AValue: TStringType; AStartIndex: SizeInt; ACount: SizeInt): SizeInt;
  709. var
  710. I,L,LS,M : SizeInt;
  711. S : TStringType;
  712. P : PTStringChar;
  713. begin
  714. Result:=-1;
  715. LS:=system.Length(Self);
  716. L:=system.Length(AValue);
  717. if (L=0) or (L>LS) then
  718. Exit;
  719. {$IFDEF IS_SHORTSTRINGHELPER}
  720. P:=PTStringChar(@AValue[1]);
  721. {$ELSE}
  722. P:=PTStringChar(AValue);
  723. {$ENDIF}
  724. S:=Self;
  725. I:=AStartIndex+1; // 1 based
  726. if (I>LS) then
  727. I:=LS;
  728. I:=I-L+1;
  729. M:=AStartIndex-ACount+2; // 1 based
  730. if M<1 then
  731. M:=1;
  732. while (Result=-1) and (I>=M) do
  733. begin
  734. if (0=StrLComp(PTStringChar(@S[I]),P,L)) then
  735. Result:=I-1;
  736. Dec(I);
  737. end;
  738. end;
  739. function TStringHelper.LastIndexOfAny(const AnyOf: array of TStringChar): SizeInt;
  740. begin
  741. Result:=LastIndexOfAny(AnyOf,Length-1,Length);
  742. end;
  743. function TStringHelper.LastIndexOfAny(const AnyOf: array of TStringChar;
  744. AStartIndex: SizeInt): SizeInt;
  745. begin
  746. Result:=LastIndexOfAny(AnyOf,AStartIndex,Length);
  747. end;
  748. function TStringHelper.LastIndexOfAny(const AnyOf: array of TStringChar;
  749. AStartIndex: SizeInt; ACount: SizeInt): SizeInt;
  750. Var
  751. Min : SizeInt;
  752. begin
  753. Result:=AStartIndex+1;
  754. Min:=Result-ACount+1;
  755. If Min<1 then
  756. Min:=1;
  757. While (Result>=Min) and Not HaveChar(Self[Result],AnyOf) do
  758. Dec(Result);
  759. if Result<Min then
  760. Result:=-1
  761. else
  762. Result:=Result-1;
  763. end;
  764. function TStringHelper.PadLeft(ATotalWidth: SizeInt): TStringType;
  765. begin
  766. Result:=PadLeft(ATotalWidth,' ');
  767. end;
  768. function TStringHelper.PadLeft(ATotalWidth: SizeInt; PaddingChar: TStringChar): TStringType;
  769. Var
  770. L : SizeInt;
  771. begin
  772. Result:=Self;
  773. L:=ATotalWidth-Length;
  774. If L>0 then
  775. Result:=StringOfChar(PaddingChar,L)+Result;
  776. end;
  777. function TStringHelper.PadRight(ATotalWidth: SizeInt): TStringType;
  778. begin
  779. Result:=PadRight(ATotalWidth,' ');
  780. end;
  781. function TStringHelper.PadRight(ATotalWidth: SizeInt; PaddingChar: TStringChar
  782. ): TStringType;
  783. Var
  784. L : SizeInt;
  785. begin
  786. Result:=Self;
  787. L:=ATotalWidth-Length;
  788. If L>0 then
  789. Result:=Result+StringOfChar(PaddingChar,L);
  790. end;
  791. function TStringHelper.QuotedString: TStringType;
  792. begin
  793. Result:=QuotedStr(Self);
  794. end;
  795. function TStringHelper.QuotedString(const AQuoteChar: TStringChar): TStringType;
  796. begin
  797. Result:=AnsiQuotedStr(Self,AQuoteChar);
  798. end;
  799. function TStringHelper.Remove(StartIndex: SizeInt): TStringType;
  800. begin
  801. Result:=Remove(StartIndex,Self.Length-StartIndex);
  802. end;
  803. function TStringHelper.Remove(StartIndex: SizeInt; ACount: SizeInt): TStringType;
  804. begin
  805. Result:=Self;
  806. System.Delete(Result,StartIndex+1,ACount);
  807. end;
  808. function TStringHelper.Replace(OldChar: TStringChar; NewChar: TStringChar): TStringType;
  809. begin
  810. Result:=Replace(OldChar,NewChar,[rfReplaceAll]);
  811. end;
  812. function TStringHelper.Replace(OldChar: TStringChar; NewChar: TStringChar;
  813. ReplaceFlags: TReplaceFlags): TStringType;
  814. var
  815. Sp,Se,Rp : PTStringChar;
  816. Ofs : SizeInt;
  817. begin
  818. if rfIgnoreCase in ReplaceFlags then
  819. exit(StringReplace(Self,OldChar,NewChar,ReplaceFlags));
  820. {$IFDEF IS_SHORTSTRINGHELPER}
  821. Sp:=PTStringChar(@Self[1]);
  822. {$ELSE}
  823. Sp:=PTStringChar(Pointer(Self));
  824. {$ENDIF}
  825. Se:=Sp+System.Length(Self);
  826. Ofs:=IndexChar(Sp^,Se-Sp,OldChar);
  827. if Ofs<0 then
  828. exit(Self);
  829. SetLength(Result,Se-Sp);
  830. {$IFDEF IS_SHORTSTRINGHELPER}
  831. Rp:=PTStringChar(@Result[1]);
  832. {$ELSE}
  833. Rp:=PTStringChar(Pointer(Result));
  834. {$ENDIF}
  835. repeat
  836. Move(Sp^,Rp^,Ofs*sizeof(TStringChar));
  837. Sp:=Sp+Ofs+1;
  838. Rp[Ofs]:=NewChar;
  839. Rp:=Rp+Ofs+1;
  840. if not (rfReplaceAll in ReplaceFlags) then
  841. break;
  842. { This loop can be removed entirely, but greatly speeds up replacing streaks of characters. }
  843. while (Sp<Se) and (Sp^=OldChar) do
  844. begin
  845. Rp^:=NewChar;
  846. Sp:=Sp+1;
  847. Rp:=Rp+1;
  848. end;
  849. Ofs:=IndexChar(Sp^,Se-Sp,OldChar);
  850. until Ofs<0;
  851. Move(Sp^,Rp^,(Se-Sp)*sizeof(TStringChar));
  852. end;
  853. function TStringHelper.Replace(const OldValue: TStringType; const NewValue: TStringType
  854. ): TStringType;
  855. begin
  856. Result:=Replace(OldValue,NewValue,[rfReplaceAll]);
  857. end;
  858. function TStringHelper.Replace(const OldValue: TStringType; const NewValue: TStringType;
  859. ReplaceFlags: TReplaceFlags): TStringType;
  860. begin
  861. Result:=StringReplace(Self,OldValue,NewValue,ReplaceFlags);
  862. end;
  863. function TStringHelper.Split(const Separators: array of TStringChar): TSHStringArray;
  864. begin
  865. Result:=Split(Separators,#0,#0,Length+1,TStringSplitOptions.None);
  866. end;
  867. function TStringHelper.Split(const Separators: array of TStringChar; ACount: SizeInt
  868. ): TSHStringArray;
  869. begin
  870. Result:=Split(Separators,#0,#0,ACount,TStringSplitOptions.None);
  871. end;
  872. function TStringHelper.Split(const Separators: array of TStringChar;
  873. Options: TStringSplitOptions): TSHStringArray;
  874. begin
  875. Result:=Split(Separators,Length+1,Options);
  876. end;
  877. function TStringHelper.Split(const Separators: array of TStringChar; ACount: SizeInt;
  878. Options: TStringSplitOptions): TSHStringArray;
  879. begin
  880. Result:=Split(Separators,#0,#0,ACount,Options);
  881. end;
  882. function TStringHelper.Split(const Separators: array of TStringType): TSHStringArray;
  883. begin
  884. Result:=Split(Separators,Length+1);
  885. end;
  886. function TStringHelper.Split(const Separators: array of TStringType; ACount: SizeInt
  887. ): TSHStringArray;
  888. begin
  889. Result:=Split(Separators,ACount,TStringSplitOptions.None);
  890. end;
  891. function TStringHelper.Split(const Separators: array of TStringType;
  892. Options: TStringSplitOptions): TSHStringArray;
  893. begin
  894. Result:=Split(Separators,Length+1,Options);
  895. end;
  896. function TStringHelper.Split(const Separators: array of TStringType;
  897. ACount: SizeInt; Options: TStringSplitOptions): TSHStringArray;
  898. begin
  899. Result:=Split(Separators,#0,#0,ACount,Options);
  900. end;
  901. function TStringHelper.Split(const Separators: array of TStringChar; AQuote: TStringChar
  902. ): TSHStringArray;
  903. begin
  904. Result:=Split(Separators,AQuote,AQuote);
  905. end;
  906. function TStringHelper.Split(const Separators: array of TStringChar; AQuoteStart,
  907. AQuoteEnd: TStringChar): TSHStringArray;
  908. begin
  909. Result:=Split(Separators,AQuoteStart,AQuoteEnd,TStringSplitOptions.None);
  910. end;
  911. function TStringHelper.Split(const Separators: array of TStringChar; AQuoteStart,
  912. AQuoteEnd: TStringChar; Options: TStringSplitOptions): TSHStringArray;
  913. begin
  914. Result:=Split(Separators,AQuoteStart,AQuoteEnd,Length+1,Options);
  915. end;
  916. function TStringHelper.Split(const Separators: array of TStringChar; AQuoteStart,
  917. AQuoteEnd: TStringChar; ACount: SizeInt): TSHStringArray;
  918. begin
  919. Result:=Split(Separators,AQuoteStart,AQuoteEnd,ACount,TStringSplitOptions.None);
  920. end;
  921. function TStringHelper.Split(const Separators: array of TStringChar; AQuoteStart,
  922. AQuoteEnd: TStringChar; ACount: SizeInt; Options: TStringSplitOptions): TSHStringArray;
  923. Function NextSep(StartIndex : SizeInt) : SizeInt;
  924. begin
  925. if (AQuoteStart<>#0) then
  926. Result:=Self.IndexOfAnyUnQuoted(Separators,AQuoteStart,AQuoteEnd,StartIndex)
  927. else
  928. Result:=Self.IndexOfAny(Separators,StartIndex);
  929. end;
  930. Procedure MaybeGrow(Curlen : SizeInt);
  931. begin
  932. if System.Length(Result)<=CurLen then
  933. SetLength(Result,System.Length(Result)+4+SizeInt(SizeUint(System.Length(Result)) div 4));
  934. end;
  935. Var
  936. Sep,LastSep,Len : SizeInt;
  937. begin
  938. Result:=nil;
  939. Len:=0;
  940. LastSep:=0;
  941. While ((ACount=0) or (Len<ACount)) and (LastSep<=System.Length(Self)) do
  942. begin
  943. Sep:=NextSep(LastSep);
  944. if Sep<0 then
  945. Sep:=System.Length(Self);
  946. // Writeln('Examining >',T,'< at pos ',LastSep,', till pos ',Sep);
  947. If (Sep>LastSep) or (not (TStringSplitOptions.ExcludeEmpty=Options)) then
  948. begin
  949. MaybeGrow(Len);
  950. Result[Len]:=SubString(LastSep,Sep-LastSep);
  951. Inc(Len);
  952. end;
  953. LastSep:=Sep+1;
  954. end;
  955. if (TStringSplitOptions.ExcludeLastEmpty=Options) then
  956. if (Len > 0) and (Result[Len-1] = '') then
  957. dec(Len);
  958. SetLength(Result,Len);
  959. end;
  960. function TStringHelper.Split(const Separators: array of TStringType; AQuote: TStringChar
  961. ): TSHStringArray;
  962. begin
  963. Result:=SPlit(Separators,AQuote,AQuote);
  964. end;
  965. function TStringHelper.Split(const Separators: array of TStringType; AQuoteStart,
  966. AQuoteEnd: TStringChar): TSHStringArray;
  967. begin
  968. Result:=SPlit(Separators,AQuoteStart,AQuoteEnd,Length+1,TStringSplitOptions.None);
  969. end;
  970. function TStringHelper.Split(const Separators: array of TStringType; AQuoteStart,
  971. AQuoteEnd: TStringChar; Options: TStringSplitOptions): TSHStringArray;
  972. begin
  973. Result:=SPlit(Separators,AQuoteStart,AQuoteEnd,Length+1,Options);
  974. end;
  975. function TStringHelper.Split(const Separators: array of TStringType; AQuoteStart,
  976. AQuoteEnd: TStringChar; ACount: SizeInt): TSHStringArray;
  977. begin
  978. Result:=SPlit(Separators,AQuoteStart,AQuoteEnd,ACount,TStringSplitOptions.None);
  979. end;
  980. function TStringHelper.Split(const Separators: array of TStringType; AQuoteStart,
  981. AQuoteEnd: TStringChar; ACount: SizeInt; Options: TStringSplitOptions): TSHStringArray;
  982. Const
  983. BlockSize = 10;
  984. Function NextSep(StartIndex : SizeInt; out Match : SizeInt) : SizeInt;
  985. begin
  986. if (AQuoteStart<>#0) then
  987. Result:=Self.IndexOfAnyUnQuoted(Separators,AQuoteStart,AQuoteEnd,StartIndex,Match)
  988. else
  989. Result:=Self.IndexOfAny(Separators,StartIndex,Length,Match);
  990. if Result<>-1 then
  991. end;
  992. Procedure MaybeGrow(Curlen : SizeInt);
  993. begin
  994. if System.Length(Result)<=CurLen then
  995. SetLength(Result,System.Length(Result)+BlockSize);
  996. end;
  997. Var
  998. Sep,LastSep,Len,Match : SizeInt;
  999. T : TStringType;
  1000. begin
  1001. SetLength(Result,BlockSize);
  1002. Len:=0;
  1003. LastSep:=0;
  1004. Sep:=NextSep(0,Match);
  1005. While (Sep<>-1) and ((ACount=0) or (Len<ACount)) do
  1006. begin
  1007. T:=SubString(LastSep,Sep-LastSep);
  1008. If (T<>'') or (not (TStringSplitOptions.ExcludeEmpty=Options)) then
  1009. begin
  1010. MaybeGrow(Len);
  1011. Result[Len]:=T;
  1012. Inc(Len);
  1013. end;
  1014. LastSep:=Sep+System.Length(Separators[Match]);
  1015. Sep:=NextSep(LastSep,Match);
  1016. end;
  1017. if (LastSep<=Length) and ((ACount=0) or (Len<ACount)) then
  1018. begin
  1019. T:=SubString(LastSep);
  1020. // Writeln('Examining >',T,'< at pos,',LastSep,' till pos ',Sep);
  1021. If (T<>'') or (not (TStringSplitOptions.ExcludeEmpty=Options)) then
  1022. begin
  1023. MaybeGrow(Len);
  1024. Result[Len]:=T;
  1025. Inc(Len);
  1026. end;
  1027. end;
  1028. If (TStringSplitOptions.ExcludeLastEmpty=Options) then
  1029. if (Len > 0) and (Result[Len-1] = '') then
  1030. dec(Len);
  1031. SetLength(Result,Len);
  1032. end;
  1033. function TStringHelper.StartsWith(const AValue: TStringType): Boolean;
  1034. begin
  1035. Result:=StartsWith(AValue,False);
  1036. end;
  1037. function TStringHelper.StartsWith(const AValue: TStringType; IgnoreCase: Boolean
  1038. ): Boolean;
  1039. Var
  1040. L : SizeInt;
  1041. begin
  1042. L:=System.Length(AValue);
  1043. Result:=L<=System.Length(Self);
  1044. if Result then
  1045. if IgnoreCase then
  1046. Result:=SameText(System.Copy(Self,1,L),AValue)
  1047. else
  1048. {$IFDEF IS_SHORTSTRINGHELPER}
  1049. Result:=CompareChar(PTStringChar(@Self[1])^,PTStringChar(@AValue[1])^,L)=0;
  1050. {$ELSE}
  1051. Result:=CompareChar(PTStringChar(Pointer(Self))^,PTStringChar(Pointer(AValue))^,L)=0;
  1052. {$ENDIF}
  1053. end;
  1054. function TStringHelper.Substring(AStartIndex: SizeInt): TStringType;
  1055. begin
  1056. Result:=Self.SubString(AStartIndex,Self.Length-AStartIndex);
  1057. end;
  1058. function TStringHelper.Substring(AStartIndex: SizeInt; ALen: SizeInt): TStringType;
  1059. begin
  1060. Result:=system.Copy(Self,AStartIndex+1,ALen);
  1061. end;
  1062. function TStringHelper.ToBoolean: Boolean;
  1063. begin
  1064. Result:=StrToBool(Self);
  1065. end;
  1066. function TStringHelper.ToInteger: Integer;
  1067. begin
  1068. Result:=StrToInt(Self);
  1069. end;
  1070. function TStringHelper.ToInt64: Int64;
  1071. begin
  1072. Result:=StrToInt64(Self);
  1073. end;
  1074. function TStringHelper.ToSingle: Single;
  1075. begin
  1076. Result:=StrToFLoat(Self);
  1077. end;
  1078. function TStringHelper.ToDouble: Double;
  1079. begin
  1080. Result:=StrToFLoat(Self);
  1081. end;
  1082. function TStringHelper.ToExtended: Extended;
  1083. begin
  1084. Result:=StrToFLoat(Self);
  1085. end;
  1086. function TStringHelper.ToCharArray: TCharArray;
  1087. begin
  1088. Result:=ToCharArray(0,Self.Length);
  1089. end;
  1090. function TStringHelper.ToCharArray(AStartIndex: SizeInt; ALen: SizeInt
  1091. ): TCharArray;
  1092. Var
  1093. I : SizeInt;
  1094. begin
  1095. SetLength(Result,ALen);
  1096. For I:=0 to ALen-1 do
  1097. Result[I]:=Self[AStartIndex+I+1];
  1098. end;
  1099. function TStringHelper.ToLower: TStringType;
  1100. begin
  1101. Result:=LowerCase(Self);
  1102. end;
  1103. function TStringHelper.ToLowerInvariant: TStringType;
  1104. begin
  1105. Result:=LowerCase(Self);
  1106. end;
  1107. function TStringHelper.ToUpper: TStringType;
  1108. begin
  1109. Result:=UpperCase(Self);
  1110. end;
  1111. function TStringHelper.ToUpperInvariant: TStringType;
  1112. begin
  1113. Result:=UpperCase(Self);
  1114. end;
  1115. function TStringHelper.Trim: TStringType;
  1116. begin
  1117. Result:=SysUtils.Trim(Self);
  1118. end;
  1119. function TStringHelper.TrimLeft: TStringType;
  1120. begin
  1121. Result:=SysUtils.TrimLeft(Self);
  1122. end;
  1123. function TStringHelper.TrimRight: TStringType;
  1124. begin
  1125. Result:=SysUtils.TrimRight(Self);
  1126. end;
  1127. function TStringHelper.Trim(const ATrimChars: array of TStringChar): TStringType;
  1128. begin
  1129. Result:=Self.TrimLeft(ATrimChars).TrimRight(ATrimChars);
  1130. end;
  1131. function TStringHelper.TrimLeft(const ATrimChars: array of TStringChar): TStringType;
  1132. Var
  1133. I,Len : SizeInt;
  1134. begin
  1135. I:=1;
  1136. Len:=Self.Length;
  1137. While (I<=Len) and HaveChar(Self[i],ATrimChars) do Inc(I);
  1138. if I=1 then
  1139. Result:=Self
  1140. else if I>Len then
  1141. Result:=''
  1142. else
  1143. Result:=system.Copy(Self,I,Len-I+1);
  1144. end;
  1145. function TStringHelper.TrimRight(const ATrimChars: array of TStringChar): TStringType;
  1146. Var
  1147. I,Len : SizeInt;
  1148. begin
  1149. Len:=Self.Length;
  1150. I:=Len;
  1151. While (I>=1) and HaveChar(Self[i],ATrimChars) do Dec(I);
  1152. if I<1 then
  1153. Result:=''
  1154. else if I=Len then
  1155. Result:=Self
  1156. else
  1157. Result:=system.Copy(Self,1,I);
  1158. end;
  1159. function TStringHelper.TrimEnd(const ATrimChars: array of TStringChar): TStringType;
  1160. begin
  1161. Result:=TrimRight(ATrimChars);
  1162. end;
  1163. function TStringHelper.TrimStart(const ATrimChars: array of TStringChar): TStringType;
  1164. begin
  1165. Result:=TrimLeft(ATrimChars);
  1166. end;