syshelps.inc 33 KB

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