syshelps.inc 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  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:=SUT.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 (SUT.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:=SUT.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:=SUT.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:=SUT.Uppercase(S);
  275. end;
  276. function TStringHelper.CompareTo(const B: TStringType): Integer;
  277. begin
  278. // Order is important
  279. {$IFDEF IS_SHORTSTRINGHELPER}
  280. Result:=SUT.StrComp(PTStringChar(@Self[1]),PTStringChar(@B[1]));
  281. {$ELSE}
  282. Result:=SUT.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:=SUT.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:=SUT.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:=SUT.Trim(Self);
  1118. end;
  1119. function TStringHelper.TrimLeft: TStringType;
  1120. begin
  1121. Result:=SUT.TrimLeft(Self);
  1122. end;
  1123. function TStringHelper.TrimRight: TStringType;
  1124. begin
  1125. Result:=SUT.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;