2
0

syshelp.inc 39 KB

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