stringl.inc 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {****************************************************************************}
  11. {* TStringsEnumerator *}
  12. {****************************************************************************}
  13. constructor TStringsEnumerator.Create(AStrings: TStrings);
  14. begin
  15. inherited Create;
  16. FStrings := AStrings;
  17. FPosition := -1;
  18. end;
  19. function TStringsEnumerator.GetCurrent: String;
  20. begin
  21. Result := FStrings[FPosition];
  22. end;
  23. function TStringsEnumerator.MoveNext: Boolean;
  24. begin
  25. Inc(FPosition);
  26. Result := FPosition < FStrings.Count;
  27. end;
  28. {****************************************************************************}
  29. {* TStrings *}
  30. {****************************************************************************}
  31. // Function to quote text. Should move maybe to sysutils !!
  32. // Also, it is not clear at this point what exactly should be done.
  33. { //!! is used to mark unsupported things. }
  34. Function QuoteString (Const S : String; Const Quote : String) : String;
  35. Var
  36. I,J : Integer;
  37. begin
  38. J:=0;
  39. Result:=S;
  40. for i:=1 to length(s) do
  41. begin
  42. inc(j);
  43. if S[i]=Quote then
  44. begin
  45. System.Insert(Quote,Result,J);
  46. inc(j);
  47. end;
  48. end;
  49. Result:=Quote+Result+Quote;
  50. end;
  51. {
  52. For compatibility we can't add a Constructor to TSTrings to initialize
  53. the special characters. Therefore we add a routine which is called whenever
  54. the special chars are needed.
  55. }
  56. Procedure Tstrings.CheckSpecialChars;
  57. begin
  58. If Not FSpecialCharsInited then
  59. begin
  60. FQuoteChar:='"';
  61. FDelimiter:=',';
  62. FNameValueSeparator:='=';
  63. FLBS:=DefaultTextLineBreakStyle;
  64. FSpecialCharsInited:=true;
  65. FLineBreak:=sLineBreak;
  66. end;
  67. end;
  68. Function TStrings.GetSkipLastLineBreak : Boolean;
  69. begin
  70. CheckSpecialChars;
  71. Result:=FSkipLastLineBreak;
  72. end;
  73. procedure TStrings.SetSkipLastLineBreak(const AValue : Boolean);
  74. begin
  75. CheckSpecialChars;
  76. FSkipLastLineBreak:=AValue;
  77. end;
  78. Function TStrings.GetLBS : TTextLineBreakStyle;
  79. begin
  80. CheckSpecialChars;
  81. Result:=FLBS;
  82. end;
  83. Procedure TStrings.SetLBS (AValue : TTextLineBreakStyle);
  84. begin
  85. CheckSpecialChars;
  86. FLBS:=AValue;
  87. end;
  88. procedure TStrings.SetDelimiter(c:Char);
  89. begin
  90. CheckSpecialChars;
  91. FDelimiter:=c;
  92. end;
  93. Procedure TStrings.SetEncoding(const AEncoding: TEncoding);
  94. begin
  95. if (FEncoding<>nil) and not TEncoding.IsStandardEncoding(FEncoding) then
  96. FEncoding.Free;
  97. if TEncoding.IsStandardEncoding(AEncoding) then
  98. FEncoding:=AEncoding
  99. else if AEncoding<>nil then
  100. FEncoding:=AEncoding.Clone
  101. else
  102. FEncoding:=nil;
  103. end;
  104. Function TStrings.GetDelimiter : Char;
  105. begin
  106. CheckSpecialChars;
  107. Result:=FDelimiter;
  108. end;
  109. procedure TStrings.SetLineBreak(Const S : String);
  110. begin
  111. CheckSpecialChars;
  112. FLineBreak:=S;
  113. end;
  114. Function TStrings.GetLineBreak : String;
  115. begin
  116. CheckSpecialChars;
  117. Result:=FLineBreak;
  118. end;
  119. procedure TStrings.SetQuoteChar(c:Char);
  120. begin
  121. CheckSpecialChars;
  122. FQuoteChar:=c;
  123. end;
  124. Function TStrings.GetQuoteChar :Char;
  125. begin
  126. CheckSpecialChars;
  127. Result:=FQuoteChar;
  128. end;
  129. procedure TStrings.SetNameValueSeparator(c:Char);
  130. begin
  131. CheckSpecialChars;
  132. FNameValueSeparator:=c;
  133. end;
  134. Function TStrings.GetNameValueSeparator :Char;
  135. begin
  136. CheckSpecialChars;
  137. Result:=FNameValueSeparator;
  138. end;
  139. function TStrings.GetCommaText: string;
  140. Var
  141. C1,C2 : Char;
  142. FSD : Boolean;
  143. begin
  144. CheckSpecialChars;
  145. FSD:=StrictDelimiter;
  146. C1:=Delimiter;
  147. C2:=QuoteChar;
  148. Delimiter:=',';
  149. QuoteChar:='"';
  150. StrictDelimiter:=False;
  151. Try
  152. Result:=GetDelimitedText;
  153. Finally
  154. Delimiter:=C1;
  155. QuoteChar:=C2;
  156. StrictDelimiter:=FSD;
  157. end;
  158. end;
  159. Function TStrings.GetDelimitedText: string;
  160. Var
  161. I : integer;
  162. p : pchar;
  163. BreakChars : set of char;
  164. S : String;
  165. doQuote : Boolean;
  166. begin
  167. CheckSpecialChars;
  168. result:='';
  169. if StrictDelimiter then
  170. BreakChars:=[#0,QuoteChar,Delimiter]
  171. else
  172. BreakChars:=[#0..' ',QuoteChar,Delimiter];
  173. // Check for break characters and quote if required.
  174. For i:=0 to count-1 do
  175. begin
  176. S:=Strings[i];
  177. doQuote:=FAlwaysQuote;
  178. If not DoQuote then
  179. begin
  180. p:=pchar(S);
  181. //Quote strings that include BreakChars:
  182. while not(p^ in BreakChars) do
  183. inc(p);
  184. DoQuote:=(p<>pchar(S)+length(S));
  185. end;
  186. if DoQuote then
  187. Result:=Result+QuoteString(S,QuoteChar)
  188. else
  189. Result:=Result+S;
  190. if I<Count-1 then
  191. Result:=Result+Delimiter;
  192. end;
  193. // Quote empty string:
  194. If (Length(Result)=0) and (Count=1) then
  195. Result:=QuoteChar+QuoteChar;
  196. end;
  197. procedure TStrings.GetNameValue(Index : Integer; Out AName,AValue : String);
  198. Var L : longint;
  199. begin
  200. CheckSpecialChars;
  201. AValue:=Strings[Index];
  202. L:=Pos(FNameValueSeparator,AValue);
  203. If L<>0 then
  204. begin
  205. AName:=Copy(AValue,1,L-1);
  206. System.Delete(AValue,1,L);
  207. end
  208. else
  209. AName:='';
  210. end;
  211. function TStrings.ExtractName(const s:String):String;
  212. var
  213. L: Longint;
  214. begin
  215. CheckSpecialChars;
  216. L:=Pos(FNameValueSeparator,S);
  217. If L<>0 then
  218. Result:=Copy(S,1,L-1)
  219. else
  220. Result:='';
  221. end;
  222. function TStrings.GetName(Index: Integer): string;
  223. Var
  224. V : String;
  225. begin
  226. GetNameValue(Index,Result,V);
  227. end;
  228. Function TStrings.GetValue(const Name: string): string;
  229. Var
  230. L : longint;
  231. N : String;
  232. begin
  233. Result:='';
  234. L:=IndexOfName(Name);
  235. If L<>-1 then
  236. GetNameValue(L,N,Result);
  237. end;
  238. Function TStrings.GetValueFromIndex(Index: Integer): string;
  239. Var
  240. N : String;
  241. begin
  242. GetNameValue(Index,N,Result);
  243. end;
  244. Procedure TStrings.SetValueFromIndex(Index: Integer; const Value: string);
  245. begin
  246. If (Value='') then
  247. Delete(Index)
  248. else
  249. begin
  250. If (Index<0) then
  251. Index:=Add('');
  252. CheckSpecialChars;
  253. Strings[Index]:=GetName(Index)+FNameValueSeparator+Value;
  254. end;
  255. end;
  256. procedure TStrings.ReadData(Reader: TReader);
  257. begin
  258. Reader.ReadListBegin;
  259. BeginUpdate;
  260. try
  261. Clear;
  262. while not Reader.EndOfList do
  263. Add(Reader.ReadString);
  264. finally
  265. EndUpdate;
  266. end;
  267. Reader.ReadListEnd;
  268. end;
  269. Procedure TStrings.SetDelimitedText(const AValue: string);
  270. var i,j:integer;
  271. aNotFirst:boolean;
  272. begin
  273. CheckSpecialChars;
  274. BeginUpdate;
  275. i:=1;
  276. j:=1;
  277. aNotFirst:=false;
  278. { Paraphrased from Delphi XE2 help:
  279. Strings must be separated by Delimiter characters or spaces.
  280. They may be enclosed in QuoteChars.
  281. QuoteChars in the string must be repeated to distinguish them from the QuoteChars enclosing the string.
  282. }
  283. try
  284. Clear;
  285. If StrictDelimiter then
  286. begin
  287. while i<=length(AValue) do begin
  288. // skip delimiter
  289. if aNotFirst and (i<=length(AValue)) and (AValue[i]=FDelimiter) then inc(i);
  290. // read next string
  291. if i<=length(AValue) then begin
  292. if AValue[i]=FQuoteChar then begin
  293. // next string is quoted
  294. j:=i+1;
  295. while (j<=length(AValue)) and
  296. ( (AValue[j]<>FQuoteChar) or
  297. ( (j+1<=length(AValue)) and (AValue[j+1]=FQuoteChar) ) ) do begin
  298. if (j<=length(AValue)) and (AValue[j]=FQuoteChar) then inc(j,2)
  299. else inc(j);
  300. end;
  301. // j is position of closing quote
  302. Add( StringReplace (Copy(AValue,i+1,j-i-1),
  303. FQuoteChar+FQuoteChar,FQuoteChar, [rfReplaceAll]));
  304. i:=j+1;
  305. end else begin
  306. // next string is not quoted; read until delimiter
  307. j:=i;
  308. while (j<=length(AValue)) and
  309. (AValue[j]<>FDelimiter) do inc(j);
  310. Add( Copy(AValue,i,j-i));
  311. i:=j;
  312. end;
  313. end else begin
  314. if aNotFirst then Add('');
  315. end;
  316. aNotFirst:=true;
  317. end;
  318. end
  319. else
  320. begin
  321. while i<=length(AValue) do begin
  322. // skip delimiter
  323. if aNotFirst and (i<=length(AValue)) and (AValue[i]=FDelimiter) then inc(i);
  324. // skip spaces
  325. while (i<=length(AValue)) and (Ord(AValue[i])<=Ord(' ')) do inc(i);
  326. // read next string
  327. if i<=length(AValue) then begin
  328. if AValue[i]=FQuoteChar then begin
  329. // next string is quoted
  330. j:=i+1;
  331. while (j<=length(AValue)) and
  332. ( (AValue[j]<>FQuoteChar) or
  333. ( (j+1<=length(AValue)) and (AValue[j+1]=FQuoteChar) ) ) do begin
  334. if (j<=length(AValue)) and (AValue[j]=FQuoteChar) then inc(j,2)
  335. else inc(j);
  336. end;
  337. // j is position of closing quote
  338. Add( StringReplace (Copy(AValue,i+1,j-i-1),
  339. FQuoteChar+FQuoteChar,FQuoteChar, [rfReplaceAll]));
  340. i:=j+1;
  341. end else begin
  342. // next string is not quoted; read until control character/space/delimiter
  343. j:=i;
  344. while (j<=length(AValue)) and
  345. (Ord(AValue[j])>Ord(' ')) and
  346. (AValue[j]<>FDelimiter) do inc(j);
  347. Add( Copy(AValue,i,j-i));
  348. i:=j;
  349. end;
  350. end else begin
  351. if aNotFirst then Add('');
  352. end;
  353. // skip spaces
  354. while (i<=length(AValue)) and (Ord(AValue[i])<=Ord(' ')) do inc(i);
  355. aNotFirst:=true;
  356. end;
  357. end;
  358. finally
  359. EndUpdate;
  360. end;
  361. end;
  362. Procedure TStrings.SetCommaText(const Value: string);
  363. Var
  364. C1,C2 : Char;
  365. begin
  366. CheckSpecialChars;
  367. C1:=Delimiter;
  368. C2:=QuoteChar;
  369. Delimiter:=',';
  370. QuoteChar:='"';
  371. Try
  372. SetDelimitedText(Value);
  373. Finally
  374. Delimiter:=C1;
  375. QuoteChar:=C2;
  376. end;
  377. end;
  378. Procedure TStrings.SetStringsAdapter(const Value: IStringsAdapter);
  379. begin
  380. end;
  381. Procedure TStrings.SetDefaultEncoding(const ADefaultEncoding: TEncoding);
  382. begin
  383. if (FDefaultEncoding<>nil) and not TEncoding.IsStandardEncoding(FDefaultEncoding) then
  384. FDefaultEncoding.Free;
  385. if TEncoding.IsStandardEncoding(ADefaultEncoding) then
  386. FDefaultEncoding:=ADefaultEncoding
  387. else if ADefaultEncoding<>nil then
  388. FDefaultEncoding:=ADefaultEncoding.Clone
  389. else
  390. FDefaultEncoding:=TEncoding.Default;
  391. end;
  392. Procedure TStrings.SetValue(const Name, Value: string);
  393. Var L : longint;
  394. begin
  395. CheckSpecialChars;
  396. L:=IndexOfName(Name);
  397. if L=-1 then
  398. Add (Name+FNameValueSeparator+Value)
  399. else
  400. Strings[L]:=Name+FNameValueSeparator+value;
  401. end;
  402. procedure TStrings.WriteData(Writer: TWriter);
  403. var
  404. i: Integer;
  405. begin
  406. Writer.WriteListBegin;
  407. for i := 0 to Count - 1 do
  408. Writer.WriteString(Strings[i]);
  409. Writer.WriteListEnd;
  410. end;
  411. procedure TStrings.DefineProperties(Filer: TFiler);
  412. var
  413. HasData: Boolean;
  414. begin
  415. if Assigned(Filer.Ancestor) then
  416. // Only serialize if string list is different from ancestor
  417. if Filer.Ancestor.InheritsFrom(TStrings) then
  418. HasData := not Equals(TStrings(Filer.Ancestor))
  419. else
  420. HasData := True
  421. else
  422. HasData := Count > 0;
  423. Filer.DefineProperty('Strings', @ReadData, @WriteData, HasData);
  424. end;
  425. Procedure TStrings.Error(const Msg: string; Data: Integer);
  426. begin
  427. Raise EStringListError.CreateFmt(Msg,[Data]) at get_caller_addr(get_frame), get_caller_frame(get_frame);
  428. end;
  429. Procedure TStrings.Error(const Msg: pstring; Data: Integer);
  430. begin
  431. Raise EStringListError.CreateFmt(Msg^,[Data]) at get_caller_addr(get_frame), get_caller_frame(get_frame);
  432. end;
  433. Function TStrings.GetCapacity: Integer;
  434. begin
  435. Result:=Count;
  436. end;
  437. Function TStrings.GetObject(Index: Integer): TObject;
  438. begin
  439. Result:=Nil;
  440. end;
  441. Function TStrings.GetTextStr: string;
  442. Var P : Pchar;
  443. I,L,NLS : Longint;
  444. S,NL : String;
  445. begin
  446. CheckSpecialChars;
  447. // Determine needed place
  448. if FLineBreak<>sLineBreak then
  449. NL:=FLineBreak
  450. else
  451. Case FLBS of
  452. tlbsLF : NL:=#10;
  453. tlbsCRLF : NL:=#13#10;
  454. tlbsCR : NL:=#13;
  455. end;
  456. L:=0;
  457. NLS:=Length(NL);
  458. For I:=0 to count-1 do
  459. L:=L+Length(Strings[I])+NLS;
  460. if SkipLastLineBreak then
  461. Dec(L,NLS);
  462. Setlength(Result,L);
  463. P:=Pointer(Result);
  464. For i:=0 To count-1 do
  465. begin
  466. S:=Strings[I];
  467. L:=Length(S);
  468. if L<>0 then
  469. System.Move(Pointer(S)^,P^,L);
  470. P:=P+L;
  471. if (I<Count-1) or Not SkipLastLineBreak then
  472. For L:=1 to NLS do
  473. begin
  474. P^:=NL[L];
  475. inc(P);
  476. end;
  477. end;
  478. end;
  479. Procedure TStrings.Put(Index: Integer; const S: string);
  480. Var Obj : TObject;
  481. begin
  482. Obj:=Objects[Index];
  483. Delete(Index);
  484. InsertObject(Index,S,Obj);
  485. end;
  486. Procedure TStrings.PutObject(Index: Integer; AObject: TObject);
  487. begin
  488. // Empty.
  489. end;
  490. Procedure TStrings.SetCapacity(NewCapacity: Integer);
  491. begin
  492. // Empty.
  493. end;
  494. Class Function TStrings.GetNextLine (Const Value : String; Var S : String; Var P : Integer) : Boolean;
  495. Var
  496. PS : PChar;
  497. IP,L : Integer;
  498. begin
  499. L:=Length(Value);
  500. S:='';
  501. Result:=False;
  502. If ((L-P)<0) then
  503. exit;
  504. if ((L-P)=0) and (not (value[P] in [#10,#13])) Then
  505. Begin
  506. s:=value[P];
  507. inc(P);
  508. Exit(True);
  509. End;
  510. PS:=PChar(Value)+P-1;
  511. IP:=P;
  512. While ((L-P)>=0) and (not (PS^ in [#10,#13])) do
  513. begin
  514. P:=P+1;
  515. Inc(PS);
  516. end;
  517. SetLength (S,P-IP);
  518. System.Move (Value[IP],Pointer(S)^,P-IP);
  519. If (P<=L) and (Value[P]=#13) then
  520. Inc(P);
  521. If (P<=L) and (Value[P]=#10) then
  522. Inc(P); // Point to character after #10(#13)
  523. Result:=True;
  524. end;
  525. Function TStrings.GetNextLineBreak (Const Value : String; Var S : String; Var P : Integer) : Boolean;
  526. Var
  527. PS,PC,PP : PChar;
  528. begin
  529. S:='';
  530. Result:=False;
  531. If ((Length(Value)-P)<0) then
  532. exit;
  533. PS:=@Value[P];
  534. PC:=PS;
  535. PP:=AnsiStrPos(PS,PChar(FLineBreak));
  536. // Stop on #0.
  537. While (PC^<>#0) and (PC<>PP) do
  538. Inc(PC);
  539. P:=P+(PC-PS)+Length(FLineBreak);
  540. SetString(S,PS,PC-PS);
  541. Result:=True;
  542. end;
  543. Procedure TStrings.DoSetTextStr(const Value: string; DoClear : Boolean);
  544. Var
  545. S : String;
  546. P : Integer;
  547. begin
  548. Try
  549. beginUpdate;
  550. if DoClear then
  551. Clear;
  552. P:=1;
  553. if FLineBreak=sLineBreak then
  554. begin
  555. While GetNextLine (Value,S,P) do
  556. Add(S)
  557. end
  558. else
  559. While GetNextLineBreak (Value,S,P) do
  560. Add(S);
  561. finally
  562. EndUpdate;
  563. end;
  564. end;
  565. Procedure TStrings.SetTextStr(const Value: string);
  566. begin
  567. CheckSpecialChars;
  568. DoSetTextStr(Value,True);
  569. end;
  570. Procedure TStrings.AddText(const S: string);
  571. begin
  572. CheckSpecialChars;
  573. DoSetTextStr(S,False);
  574. end;
  575. Procedure TStrings.SetUpdateState(Updating: Boolean);
  576. begin
  577. FPONotifyObservers(Self,ooChange,Nil);
  578. end;
  579. destructor TSTrings.Destroy;
  580. begin
  581. if (FEncoding<>nil) and not TEncoding.IsStandardEncoding(FEncoding) then
  582. FreeAndNil(FEncoding);
  583. if (FDefaultEncoding<>nil) and not TEncoding.IsStandardEncoding(FDefaultEncoding) then
  584. FreeAndNil(FDefaultEncoding);
  585. inherited destroy;
  586. end;
  587. constructor TStrings.Create;
  588. begin
  589. inherited Create;
  590. FDefaultEncoding:=TEncoding.Default;
  591. FEncoding:=nil;
  592. FWriteBOM:=True;
  593. FAlwaysQuote:=False;
  594. end;
  595. Function TStrings.Add(const S: string): Integer;
  596. begin
  597. Result:=Count;
  598. Insert (Count,S);
  599. end;
  600. function TStrings.Add(const Fmt : string; const Args : Array of const): Integer;
  601. begin
  602. Result:=Add(Format(Fmt,Args));
  603. end;
  604. Function TStrings.AddObject(const S: string; AObject: TObject): Integer;
  605. begin
  606. Result:=Add(S);
  607. Objects[result]:=AObject;
  608. end;
  609. function TStrings.AddObject(const Fmt: string; Args : Array of const; AObject: TObject): Integer;
  610. begin
  611. Result:=AddObject(Format(Fmt,Args),AObject);
  612. end;
  613. function TStrings.AddPair(const AName, AValue: string): TStrings;
  614. begin
  615. Result:=AddPair(AName,AValue,Nil);
  616. end;
  617. function TStrings.AddPair(const AName, AValue: string; AObject: TObject): TStrings;
  618. begin
  619. Result := Self;
  620. AddObject(Concat(AName, NameValueSeparator, AValue), AObject);
  621. end;
  622. Procedure TStrings.Append(const S: string);
  623. begin
  624. Add (S);
  625. end;
  626. Procedure TStrings.AddStrings(TheStrings: TStrings; ClearFirst : Boolean);
  627. begin
  628. beginupdate;
  629. try
  630. if ClearFirst then
  631. Clear;
  632. AddStrings(TheStrings);
  633. finally
  634. EndUpdate;
  635. end;
  636. end;
  637. Procedure TStrings.AddStrings(TheStrings: TStrings);
  638. Var Runner : longint;
  639. begin
  640. For Runner:=0 to TheStrings.Count-1 do
  641. self.AddObject (Thestrings[Runner],TheStrings.Objects[Runner]);
  642. end;
  643. Procedure TStrings.AddStrings(const TheStrings: array of string);
  644. Var Runner : longint;
  645. begin
  646. if Count + High(TheStrings)+1 > Capacity then
  647. Capacity := Count + High(TheStrings)+1;
  648. For Runner:=Low(TheStrings) to High(TheStrings) do
  649. self.Add(Thestrings[Runner]);
  650. end;
  651. Procedure TStrings.AddStrings(const TheStrings: array of string; ClearFirst : Boolean);
  652. begin
  653. beginupdate;
  654. try
  655. if ClearFirst then
  656. Clear;
  657. AddStrings(TheStrings);
  658. finally
  659. EndUpdate;
  660. end;
  661. end;
  662. Procedure TStrings.Assign(Source: TPersistent);
  663. Var
  664. S : TStrings;
  665. begin
  666. If Source is TStrings then
  667. begin
  668. S:=TStrings(Source);
  669. BeginUpdate;
  670. Try
  671. clear;
  672. FSpecialCharsInited:=S.FSpecialCharsInited;
  673. FQuoteChar:=S.FQuoteChar;
  674. FDelimiter:=S.FDelimiter;
  675. FNameValueSeparator:=S.FNameValueSeparator;
  676. FLBS:=S.FLBS;
  677. FLineBreak:=S.FLineBreak;
  678. FWriteBOM:=S.FWriteBOM;
  679. DefaultEncoding:=S.DefaultEncoding;
  680. SetEncoding(S.Encoding);
  681. AddStrings(S);
  682. finally
  683. EndUpdate;
  684. end;
  685. end
  686. else
  687. Inherited Assign(Source);
  688. end;
  689. Procedure TStrings.BeginUpdate;
  690. begin
  691. if FUpdateCount = 0 then SetUpdateState(true);
  692. inc(FUpdateCount);
  693. end;
  694. Procedure TStrings.EndUpdate;
  695. begin
  696. If FUpdateCount>0 then
  697. Dec(FUpdateCount);
  698. if FUpdateCount=0 then
  699. SetUpdateState(False);
  700. end;
  701. Function TStrings.Equals(Obj: TObject): Boolean;
  702. begin
  703. if Obj is TStrings then
  704. Result := Equals(TStrings(Obj))
  705. else
  706. Result := inherited Equals(Obj);
  707. end;
  708. Function TStrings.Equals(TheStrings: TStrings): Boolean;
  709. Var Runner,Nr : Longint;
  710. begin
  711. Result:=False;
  712. Nr:=Self.Count;
  713. if Nr<>TheStrings.Count then exit;
  714. For Runner:=0 to Nr-1 do
  715. If Strings[Runner]<>TheStrings[Runner] then exit;
  716. Result:=True;
  717. end;
  718. Procedure TStrings.Exchange(Index1, Index2: Integer);
  719. Var
  720. Obj : TObject;
  721. Str : String;
  722. begin
  723. beginUpdate;
  724. Try
  725. Obj:=Objects[Index1];
  726. Str:=Strings[Index1];
  727. Objects[Index1]:=Objects[Index2];
  728. Strings[Index1]:=Strings[Index2];
  729. Objects[Index2]:=Obj;
  730. Strings[Index2]:=Str;
  731. finally
  732. EndUpdate;
  733. end;
  734. end;
  735. function TStrings.GetEnumerator: TStringsEnumerator;
  736. begin
  737. Result:=TStringsEnumerator.Create(Self);
  738. end;
  739. Function TStrings.GetText: PChar;
  740. begin
  741. Result:=StrNew(Pchar(Self.Text));
  742. end;
  743. Function TStrings.DoCompareText(const s1,s2 : string) : PtrInt;
  744. begin
  745. result:=CompareText(s1,s2);
  746. end;
  747. Function TStrings.IndexOf(const S: string): Integer;
  748. begin
  749. Result:=0;
  750. While (Result<Count) and (DoCompareText(Strings[Result],S)<>0) do Result:=Result+1;
  751. if Result=Count then Result:=-1;
  752. end;
  753. Function TStrings.IndexOfName(const Name: string): Integer;
  754. Var
  755. len : longint;
  756. S : String;
  757. begin
  758. CheckSpecialChars;
  759. Result:=0;
  760. while (Result<Count) do
  761. begin
  762. S:=Strings[Result];
  763. len:=pos(FNameValueSeparator,S)-1;
  764. if (len>=0) and (DoCompareText(Name,Copy(S,1,Len))=0) then
  765. exit;
  766. inc(result);
  767. end;
  768. result:=-1;
  769. end;
  770. Function TStrings.IndexOfObject(AObject: TObject): Integer;
  771. begin
  772. Result:=0;
  773. While (Result<count) and (Objects[Result]<>AObject) do Result:=Result+1;
  774. If Result=Count then Result:=-1;
  775. end;
  776. Procedure TStrings.InsertObject(Index: Integer; const S: string;
  777. AObject: TObject);
  778. begin
  779. Insert (Index,S);
  780. Objects[Index]:=AObject;
  781. end;
  782. Procedure TStrings.LoadFromFile(const FileName: string);
  783. begin
  784. LoadFromFile(FileName,False)
  785. end;
  786. Procedure TStrings.LoadFromFile(const FileName: string; IgnoreEncoding : Boolean);
  787. Var
  788. TheStream : TFileStream;
  789. begin
  790. TheStream:=TFileStream.Create(FileName,fmOpenRead or fmShareDenyWrite);
  791. try
  792. LoadFromStream(TheStream, IgnoreEncoding);
  793. finally
  794. TheStream.Free;
  795. end;
  796. end;
  797. Procedure TStrings.LoadFromFile(const FileName: string; AEncoding: TEncoding);
  798. Var
  799. TheStream : TFileStream;
  800. begin
  801. TheStream:=TFileStream.Create(FileName,fmOpenRead or fmShareDenyWrite);
  802. try
  803. LoadFromStream(TheStream,AEncoding);
  804. finally
  805. TheStream.Free;
  806. end;
  807. end;
  808. Procedure TStrings.LoadFromStream(Stream: TStream);
  809. begin
  810. LoadFromStream(Stream,False);
  811. end;
  812. Procedure TStrings.LoadFromStream(Stream: TStream; IgnoreEncoding : Boolean);
  813. {
  814. Borlands method is no good, since a pipe for
  815. instance doesn't have a size.
  816. So we must do it the hard way.
  817. }
  818. Const
  819. BufSize = 1024;
  820. MaxGrow = 1 shl 29;
  821. Var
  822. Buffer : AnsiString;
  823. BytesRead,
  824. BufLen,
  825. I,BufDelta : Longint;
  826. begin
  827. if not IgnoreEncoding then
  828. begin
  829. LoadFromStream(Stream,Nil);
  830. Exit;
  831. end;
  832. // reread into a buffer
  833. beginupdate;
  834. try
  835. Buffer:='';
  836. BufLen:=0;
  837. I:=1;
  838. Repeat
  839. BufDelta:=BufSize*I;
  840. SetLength(Buffer,BufLen+BufDelta);
  841. BytesRead:=Stream.Read(Buffer[BufLen+1],BufDelta);
  842. inc(BufLen,BufDelta);
  843. If I<MaxGrow then
  844. I:=I shl 1;
  845. Until BytesRead<>BufDelta;
  846. SetLength(Buffer, BufLen-BufDelta+BytesRead);
  847. SetTextStr(Buffer);
  848. SetLength(Buffer,0);
  849. finally
  850. EndUpdate;
  851. end;
  852. end;
  853. Procedure TStrings.LoadFromStream(Stream: TStream; AEncoding: TEncoding);
  854. {
  855. Borlands method is no good, since a pipe for
  856. instance doesn't have a size.
  857. So we must do it the hard way.
  858. }
  859. Const
  860. BufSize = 1024;
  861. MaxGrow = 1 shl 29;
  862. Var
  863. Buffer : TBytes;
  864. T : string;
  865. BytesRead,
  866. BufLen,
  867. I,BufDelta,
  868. PreambleLength : Longint;
  869. begin
  870. // reread into a buffer
  871. beginupdate;
  872. try
  873. SetLength(Buffer,0);
  874. BufLen:=0;
  875. I:=1;
  876. Repeat
  877. BufDelta:=BufSize*I;
  878. SetLength(Buffer,BufLen+BufDelta);
  879. BytesRead:=Stream.Read(Buffer[BufLen],BufDelta);
  880. inc(BufLen,BufDelta);
  881. If I<MaxGrow then
  882. I:=I shl 1;
  883. Until BytesRead<>BufDelta;
  884. SetLength(Buffer,BufLen-BufDelta+BytesRead);
  885. PreambleLength:=TEncoding.GetBufferEncoding(Buffer,AEncoding,FDefaultEncoding);
  886. T:=AEncoding.GetAnsiString(Buffer,PreambleLength,Length(Buffer)-PreambleLength);
  887. SetEncoding(AEncoding);
  888. SetLength(Buffer,0);
  889. SetTextStr(T);
  890. finally
  891. EndUpdate;
  892. end;
  893. end;
  894. Procedure TStrings.Move(CurIndex, NewIndex: Integer);
  895. Var
  896. Obj : TObject;
  897. Str : String;
  898. begin
  899. BeginUpdate;
  900. Try
  901. Obj:=Objects[CurIndex];
  902. Str:=Strings[CurIndex];
  903. Objects[CurIndex]:=Nil; // Prevent Delete from freeing.
  904. Delete(Curindex);
  905. InsertObject(NewIndex,Str,Obj);
  906. finally
  907. EndUpdate;
  908. end;
  909. end;
  910. Procedure TStrings.SaveToFile(const FileName: string);
  911. Var TheStream : TFileStream;
  912. begin
  913. TheStream:=TFileStream.Create(FileName,fmCreate);
  914. try
  915. SaveToStream(TheStream);
  916. finally
  917. TheStream.Free;
  918. end;
  919. end;
  920. Procedure TStrings.SaveToFile(const FileName: string; AEncoding: TEncoding);
  921. Var TheStream : TFileStream;
  922. begin
  923. TheStream:=TFileStream.Create(FileName,fmCreate);
  924. try
  925. SaveToStream(TheStream,AEncoding);
  926. finally
  927. TheStream.Free;
  928. end;
  929. end;
  930. Procedure TStrings.SaveToStream(Stream: TStream);
  931. Var
  932. S : String;
  933. begin
  934. if Encoding<>nil then
  935. SaveToStream(Stream,Encoding)
  936. else
  937. begin
  938. S:=Text;
  939. if S = '' then Exit;
  940. Stream.WriteBuffer(Pointer(S)^,Length(S));
  941. end;
  942. end;
  943. Procedure TStrings.SaveToStream(Stream: TStream; AEncoding: TEncoding);
  944. Var B : TBytes;
  945. begin
  946. if AEncoding=nil then
  947. AEncoding:=FDefaultEncoding;
  948. if FWriteBOM then
  949. begin
  950. B:=AEncoding.GetPreamble;
  951. if Length(B)>0 then
  952. Stream.WriteBuffer(B[0],Length(B));
  953. end;
  954. B:=AEncoding.GetAnsiBytes(Text);
  955. if Length(B)>0 then
  956. Stream.WriteBuffer(B[0],Length(B));
  957. end;
  958. Procedure TStrings.SetText(TheText: PChar);
  959. Var S : String;
  960. begin
  961. If TheText<>Nil then
  962. S:=StrPas(TheText)
  963. else
  964. S:='';
  965. SetTextStr(S);
  966. end;
  967. {****************************************************************************}
  968. {* TStringList *}
  969. {****************************************************************************}
  970. {$if not defined(FPC_TESTGENERICS)}
  971. procedure TStringList.ExchangeItemsInt(Index1, Index2: Integer);
  972. Var P1,P2 : Pointer;
  973. begin
  974. P1:=Pointer(Flist^[Index1].FString);
  975. P2:=Pointer(Flist^[Index1].FObject);
  976. Pointer(Flist^[Index1].Fstring):=Pointer(Flist^[Index2].Fstring);
  977. Pointer(Flist^[Index1].FObject):=Pointer(Flist^[Index2].FObject);
  978. Pointer(Flist^[Index2].Fstring):=P1;
  979. Pointer(Flist^[Index2].FObject):=P2;
  980. end;
  981. function TStringList.GetSorted: Boolean;
  982. begin
  983. Result:=FSortStyle in [sslUser,sslAuto];
  984. end;
  985. procedure TStringList.ExchangeItems(Index1, Index2: Integer);
  986. begin
  987. ExchangeItemsInt(Index1, Index2);
  988. end;
  989. procedure TStringList.Grow;
  990. Var
  991. NC : Integer;
  992. begin
  993. NC:=FCapacity;
  994. If NC>=256 then
  995. NC:=NC+(NC Div 4)
  996. else if NC=0 then
  997. NC:=4
  998. else
  999. NC:=NC*4;
  1000. SetCapacity(NC);
  1001. end;
  1002. procedure TStringList.InternalClear(FromIndex: Integer; ClearOnly: Boolean);
  1003. Var
  1004. I: Integer;
  1005. begin
  1006. if FromIndex < FCount then
  1007. begin
  1008. if FOwnsObjects then
  1009. begin
  1010. For I:=FromIndex to FCount-1 do
  1011. begin
  1012. Flist^[I].FString:='';
  1013. freeandnil(Flist^[i].FObject);
  1014. end;
  1015. end
  1016. else
  1017. begin
  1018. For I:=FromIndex to FCount-1 do
  1019. Flist^[I].FString:='';
  1020. end;
  1021. FCount:=FromIndex;
  1022. end;
  1023. if Not ClearOnly then
  1024. SetCapacity(0);
  1025. end;
  1026. procedure TStringList.QuickSort(L, R: Integer; CompareFn: TStringListSortCompare
  1027. );
  1028. var
  1029. Pivot, vL, vR: Integer;
  1030. ExchangeProc: procedure(Left, Right: Integer) of object;
  1031. begin
  1032. //if ExchangeItems is override call that, else call (faster) ExchangeItemsInt
  1033. if TMethod(@Self.ExchangeItems).Code = CodePointer(@TStringList.ExchangeItems) then
  1034. ExchangeProc := @ExchangeItemsInt
  1035. else
  1036. ExchangeProc := @ExchangeItems;
  1037. if R - L <= 1 then begin // a little bit of time saver
  1038. if L < R then
  1039. if CompareFn(Self, L, R) > 0 then
  1040. ExchangeProc(L, R);
  1041. Exit;
  1042. end;
  1043. vL := L;
  1044. vR := R;
  1045. Pivot := L + Random(R - L); // they say random is best
  1046. while vL < vR do begin
  1047. while (vL < Pivot) and (CompareFn(Self, vL, Pivot) <= 0) do
  1048. Inc(vL);
  1049. while (vR > Pivot) and (CompareFn(Self, vR, Pivot) > 0) do
  1050. Dec(vR);
  1051. ExchangeProc(vL, vR);
  1052. if Pivot = vL then // swap pivot if we just hit it from one side
  1053. Pivot := vR
  1054. else if Pivot = vR then
  1055. Pivot := vL;
  1056. end;
  1057. if Pivot - 1 >= L then
  1058. QuickSort(L, Pivot - 1, CompareFn);
  1059. if Pivot + 1 <= R then
  1060. QuickSort(Pivot + 1, R, CompareFn);
  1061. end;
  1062. procedure TStringList.InsertItem(Index: Integer; const S: string);
  1063. begin
  1064. InsertItem(Index, S, nil);
  1065. end;
  1066. procedure TStringList.InsertItem(Index: Integer; const S: string; O: TObject);
  1067. begin
  1068. Changing;
  1069. If FCount=Fcapacity then Grow;
  1070. If Index<FCount then
  1071. System.Move (FList^[Index],FList^[Index+1],
  1072. (FCount-Index)*SizeOf(TStringItem));
  1073. Pointer(Flist^[Index].Fstring):=Nil; // Needed to initialize...
  1074. Flist^[Index].FString:=S;
  1075. Flist^[Index].FObject:=O;
  1076. Inc(FCount);
  1077. Changed;
  1078. end;
  1079. procedure TStringList.SetSorted(Value: Boolean);
  1080. begin
  1081. If Value then
  1082. SortStyle:=sslAuto
  1083. else
  1084. SortStyle:=sslNone
  1085. end;
  1086. procedure TStringList.Changed;
  1087. begin
  1088. If (FUpdateCount=0) Then
  1089. begin
  1090. If Assigned(FOnChange) then
  1091. FOnchange(Self);
  1092. FPONotifyObservers(Self,ooChange,Nil);
  1093. end;
  1094. end;
  1095. procedure TStringList.Changing;
  1096. begin
  1097. If FUpdateCount=0 then
  1098. if Assigned(FOnChanging) then
  1099. FOnchanging(Self);
  1100. end;
  1101. function TStringList.Get(Index: Integer): string;
  1102. begin
  1103. CheckIndex(Index);
  1104. Result:=Flist^[Index].FString;
  1105. end;
  1106. function TStringList.GetCapacity: Integer;
  1107. begin
  1108. Result:=FCapacity;
  1109. end;
  1110. function TStringList.GetCount: Integer;
  1111. begin
  1112. Result:=FCount;
  1113. end;
  1114. function TStringList.GetObject(Index: Integer): TObject;
  1115. begin
  1116. CheckIndex(Index);
  1117. Result:=Flist^[Index].FObject;
  1118. end;
  1119. procedure TStringList.Put(Index: Integer; const S: string);
  1120. begin
  1121. If Sorted then
  1122. Error(SSortedListError,0);
  1123. CheckIndex(Index);
  1124. Changing;
  1125. Flist^[Index].FString:=S;
  1126. Changed;
  1127. end;
  1128. procedure TStringList.PutObject(Index: Integer; AObject: TObject);
  1129. begin
  1130. CheckIndex(Index);
  1131. Changing;
  1132. Flist^[Index].FObject:=AObject;
  1133. Changed;
  1134. end;
  1135. procedure TStringList.SetCapacity(NewCapacity: Integer);
  1136. Var NewList : Pointer;
  1137. MSize : Longint;
  1138. begin
  1139. If (NewCapacity<0) then
  1140. Error (SListCapacityError,NewCapacity);
  1141. If NewCapacity>FCapacity then
  1142. begin
  1143. GetMem (NewList,NewCapacity*SizeOf(TStringItem));
  1144. If NewList=Nil then
  1145. Error (SListCapacityError,NewCapacity);
  1146. If Assigned(FList) then
  1147. begin
  1148. MSize:=FCapacity*Sizeof(TStringItem);
  1149. System.Move (FList^,NewList^,MSize);
  1150. FillWord (Pchar(NewList)[MSize],(NewCapacity-FCapacity)*(SizeOf(TStringItem) div SizeOf(Word)), 0);
  1151. FreeMem (Flist,MSize);
  1152. end;
  1153. Flist:=NewList;
  1154. FCapacity:=NewCapacity;
  1155. end
  1156. else if NewCapacity<FCapacity then
  1157. begin
  1158. if NewCapacity = 0 then
  1159. begin
  1160. if FCount > 0 then
  1161. InternalClear(0,True);
  1162. FreeMem(FList);
  1163. FList := nil;
  1164. end else
  1165. begin
  1166. InternalClear(NewCapacity,True);
  1167. GetMem(NewList, NewCapacity * SizeOf(TStringItem));
  1168. System.Move(FList^, NewList^, NewCapacity * SizeOf(TStringItem));
  1169. FreeMem(FList);
  1170. FList := NewList;
  1171. end;
  1172. FCapacity:=NewCapacity;
  1173. end;
  1174. end;
  1175. procedure TStringList.SetUpdateState(Updating: Boolean);
  1176. begin
  1177. If Updating then
  1178. Changing
  1179. else
  1180. Changed
  1181. end;
  1182. destructor TStringList.Destroy;
  1183. begin
  1184. InternalClear;
  1185. Inherited destroy;
  1186. end;
  1187. function TStringList.Add(const S: string): Integer;
  1188. begin
  1189. If Not (SortStyle=sslAuto) then
  1190. Result:=FCount
  1191. else
  1192. If Find (S,Result) then
  1193. Case DUplicates of
  1194. DupIgnore : Exit;
  1195. DupError : Error(SDuplicateString,0)
  1196. end;
  1197. InsertItem (Result,S);
  1198. end;
  1199. procedure TStringList.Clear;
  1200. begin
  1201. if FCount = 0 then Exit;
  1202. Changing;
  1203. InternalClear;
  1204. Changed;
  1205. end;
  1206. procedure TStringList.Delete(Index: Integer);
  1207. begin
  1208. CheckIndex(Index);
  1209. Changing;
  1210. Flist^[Index].FString:='';
  1211. if FOwnsObjects then
  1212. FreeAndNil(Flist^[Index].FObject);
  1213. Dec(FCount);
  1214. If Index<FCount then
  1215. System.Move(Flist^[Index+1],
  1216. Flist^[Index],
  1217. (Fcount-Index)*SizeOf(TStringItem));
  1218. Changed;
  1219. end;
  1220. procedure TStringList.Exchange(Index1, Index2: Integer);
  1221. begin
  1222. CheckIndex(Index1);
  1223. CheckIndex(Index2);
  1224. Changing;
  1225. ExchangeItemsInt(Index1,Index2);
  1226. changed;
  1227. end;
  1228. procedure TStringList.SetCaseSensitive(b : boolean);
  1229. begin
  1230. if b=FCaseSensitive then
  1231. Exit;
  1232. FCaseSensitive:=b;
  1233. if FSortStyle=sslAuto then
  1234. begin
  1235. FForceSort:=True;
  1236. try
  1237. Sort;
  1238. finally
  1239. FForceSort:=False;
  1240. end;
  1241. end;
  1242. end;
  1243. procedure TStringList.SetSortStyle(AValue: TStringsSortStyle);
  1244. begin
  1245. if FSortStyle=AValue then Exit;
  1246. if (AValue=sslAuto) then
  1247. Sort;
  1248. FSortStyle:=AValue;
  1249. end;
  1250. procedure TStringList.CheckIndex(AIndex: Integer);
  1251. begin
  1252. If (AIndex<0) or (AIndex>=FCount) then
  1253. Error(SListIndexError,AIndex);
  1254. end;
  1255. function TStringList.DoCompareText(const s1, s2: string): PtrInt;
  1256. begin
  1257. if FCaseSensitive then
  1258. result:=AnsiCompareStr(s1,s2)
  1259. else
  1260. result:=AnsiCompareText(s1,s2);
  1261. end;
  1262. function TStringList.CompareStrings(const s1,s2 : string) : Integer;
  1263. begin
  1264. Result := DoCompareText(s1, s2);
  1265. end;
  1266. function TStringList.Find(const S: string; out Index: Integer): Boolean;
  1267. var
  1268. L, R, I: Integer;
  1269. CompareRes: PtrInt;
  1270. begin
  1271. Result := false;
  1272. Index:=-1;
  1273. if Not Sorted then
  1274. Raise EListError.Create(SErrFindNeedsSortedList);
  1275. // Use binary search.
  1276. L := 0;
  1277. R := Count - 1;
  1278. while (L<=R) do
  1279. begin
  1280. I := L + (R - L) div 2;
  1281. CompareRes := DoCompareText(S, Flist^[I].FString);
  1282. if (CompareRes>0) then
  1283. L := I+1
  1284. else begin
  1285. R := I-1;
  1286. if (CompareRes=0) then begin
  1287. Result := true;
  1288. if (Duplicates<>dupAccept) then
  1289. L := I; // forces end of while loop
  1290. end;
  1291. end;
  1292. end;
  1293. Index := L;
  1294. end;
  1295. function TStringList.IndexOf(const S: string): Integer;
  1296. begin
  1297. If Not Sorted then
  1298. Result:=Inherited indexOf(S)
  1299. else
  1300. // faster using binary search...
  1301. If Not Find (S,Result) then
  1302. Result:=-1;
  1303. end;
  1304. procedure TStringList.Insert(Index: Integer; const S: string);
  1305. begin
  1306. If SortStyle=sslAuto then
  1307. Error (SSortedListError,0)
  1308. else
  1309. begin
  1310. If (Index<0) or (Index>FCount) then
  1311. Error(SListIndexError,Index); // Cannot use CheckIndex, because there >= FCount...
  1312. InsertItem (Index,S);
  1313. end;
  1314. end;
  1315. procedure TStringList.CustomSort(CompareFn: TStringListSortCompare);
  1316. begin
  1317. If (FForceSort or (Not (FSortStyle=sslAuto))) and (FCount>1) then
  1318. begin
  1319. Changing;
  1320. QuickSort(0,FCount-1, CompareFn);
  1321. Changed;
  1322. end;
  1323. end;
  1324. function StringListAnsiCompare(List: TStringList; Index1, Index: Integer): Integer;
  1325. begin
  1326. Result := List.DoCompareText(List.FList^[Index1].FString,
  1327. List.FList^[Index].FString);
  1328. end;
  1329. procedure TStringList.Sort;
  1330. begin
  1331. CustomSort(@StringListAnsiCompare);
  1332. end;
  1333. {$else}
  1334. { generics based implementation of TStringList follows }
  1335. function StringListAnsiCompare(List: TStringList; Index1, Index2: Integer): Integer;
  1336. begin
  1337. Result := List.DoCompareText(List.Strings[Index1], List.Strings[Index2]);
  1338. end;
  1339. constructor TStringList.Create;
  1340. begin
  1341. inherited;
  1342. FOwnsObjects:=false;
  1343. FMap := TFPStrObjMap.Create;
  1344. FMap.OnPtrCompare := @MapPtrCompare;
  1345. FOnCompareText := @DefaultCompareText;
  1346. NameValueSeparator:='=';
  1347. CheckSpecialChars;
  1348. end;
  1349. destructor TStringList.Destroy;
  1350. begin
  1351. FMap.Free;
  1352. inherited;
  1353. end;
  1354. function TStringList.GetDuplicates: TDuplicates;
  1355. begin
  1356. Result := FMap.Duplicates;
  1357. end;
  1358. function TStringList.GetSorted: boolean;
  1359. begin
  1360. Result := FMap.Sorted;
  1361. end;
  1362. procedure TStringList.SetDuplicates(NewDuplicates: TDuplicates);
  1363. begin
  1364. FMap.Duplicates := NewDuplicates;
  1365. end;
  1366. procedure TStringList.SetSorted(NewSorted: Boolean);
  1367. begin
  1368. FMap.Sorted := NewSorted;
  1369. end;
  1370. procedure TStringList.Changed;
  1371. begin
  1372. if FUpdateCount = 0 then
  1373. if Assigned(FOnChange) then
  1374. FOnChange(Self);
  1375. end;
  1376. procedure TStringList.Changing;
  1377. begin
  1378. if FUpdateCount = 0 then
  1379. if Assigned(FOnChanging) then
  1380. FOnChanging(Self);
  1381. end;
  1382. function TStringList.Get(Index: Integer): string;
  1383. begin
  1384. Result := FMap.Keys[Index];
  1385. end;
  1386. function TStringList.GetCapacity: Integer;
  1387. begin
  1388. Result := FMap.Capacity;
  1389. end;
  1390. function TStringList.GetCount: Integer;
  1391. begin
  1392. Result := FMap.Count;
  1393. end;
  1394. function TStringList.GetObject(Index: Integer): TObject;
  1395. begin
  1396. Result := FMap.Data[Index];
  1397. end;
  1398. procedure TStringList.Put(Index: Integer; const S: string);
  1399. begin
  1400. Changing;
  1401. FMap.Keys[Index] := S;
  1402. Changed;
  1403. end;
  1404. procedure TStringList.PutObject(Index: Integer; AObject: TObject);
  1405. begin
  1406. Changing;
  1407. FMap.Data[Index] := AObject;
  1408. Changed;
  1409. end;
  1410. procedure TStringList.SetCapacity(NewCapacity: Integer);
  1411. begin
  1412. FMap.Capacity := NewCapacity;
  1413. end;
  1414. procedure TStringList.SetUpdateState(Updating: Boolean);
  1415. begin
  1416. if Updating then
  1417. Changing
  1418. else
  1419. Changed
  1420. end;
  1421. function TStringList.Add(const S: string): Integer;
  1422. begin
  1423. Result := FMap.Add(S);
  1424. end;
  1425. procedure TStringList.Clear;
  1426. begin
  1427. if FMap.Count = 0 then exit;
  1428. Changing;
  1429. FMap.Clear;
  1430. Changed;
  1431. end;
  1432. procedure TStringList.Delete(Index: Integer);
  1433. begin
  1434. if (Index < 0) or (Index >= FMap.Count) then
  1435. Error(SListIndexError, Index);
  1436. Changing;
  1437. FMap.Delete(Index);
  1438. Changed;
  1439. end;
  1440. procedure TStringList.Exchange(Index1, Index2: Integer);
  1441. begin
  1442. if (Index1 < 0) or (Index1 >= FMap.Count) then
  1443. Error(SListIndexError, Index1);
  1444. if (Index2 < 0) or (Index2 >= FMap.Count) then
  1445. Error(SListIndexError, Index2);
  1446. Changing;
  1447. FMap.InternalExchange(Index1, Index2);
  1448. Changed;
  1449. end;
  1450. procedure TStringList.SetCaseSensitive(NewSensitive: Boolean);
  1451. begin
  1452. if NewSensitive <> FCaseSensitive then
  1453. begin
  1454. FCaseSensitive := NewSensitive;
  1455. if Sorted then
  1456. Sort;
  1457. end;
  1458. end;
  1459. function TStringList.MapPtrCompare(Key1, Key2: Pointer): Integer;
  1460. begin
  1461. Result := FOnCompareText(string(Key1^), string(Key2^));
  1462. end;
  1463. function TStringList.DefaultCompareText(const s1, s2: string): PtrInt;
  1464. begin
  1465. if FCaseSensitive then
  1466. Result := AnsiCompareStr(s1, s2)
  1467. else
  1468. Result := AnsiCompareText(s1, s2);
  1469. end;
  1470. function TStringList.DoCompareText(const s1, s2: string): PtrInt;
  1471. begin
  1472. Result := FOnCompareText(s1, s2);
  1473. end;
  1474. function TStringList.Find(const S: string; var Index: Integer): Boolean;
  1475. begin
  1476. Result := FMap.Find(S, Index);
  1477. end;
  1478. function TStringList.IndexOf(const S: string): Integer;
  1479. begin
  1480. Result := FMap.IndexOf(S);
  1481. end;
  1482. procedure TStringList.Insert(Index: Integer; const S: string);
  1483. begin
  1484. if not Sorted and (0 <= Index) and (Index < FMap.Count) then
  1485. Changing;
  1486. FMap.InsertKey(Index, S);
  1487. Changed;
  1488. end;
  1489. procedure TStringList.QuickSort(L, R: Integer; CompareFn: TStringListSortCompare);
  1490. var
  1491. I, J, Pivot: Integer;
  1492. begin
  1493. repeat
  1494. I := L;
  1495. J := R;
  1496. Pivot := (L + R) div 2;
  1497. repeat
  1498. while CompareFn(Self, I, Pivot) < 0 do Inc(I);
  1499. while CompareFn(Self, J, Pivot) > 0 do Dec(J);
  1500. if I <= J then
  1501. begin
  1502. FMap.InternalExchange(I, J); // No check, indices are correct.
  1503. if Pivot = I then
  1504. Pivot := J
  1505. else if Pivot = J then
  1506. Pivot := I;
  1507. Inc(I);
  1508. Dec(j);
  1509. end;
  1510. until I > J;
  1511. if L < J then
  1512. QuickSort(L,J, CompareFn);
  1513. L := I;
  1514. until I >= R;
  1515. end;
  1516. procedure TStringList.CustomSort(CompareFn: TStringListSortCompare);
  1517. begin
  1518. if not Sorted and (FMap.Count > 1) then
  1519. begin
  1520. Changing;
  1521. QuickSort(0, FMap.Count-1, CompareFn);
  1522. Changed;
  1523. end;
  1524. end;
  1525. procedure TStringList.Sort;
  1526. begin
  1527. if not Sorted and (FMap.Count > 1) then
  1528. begin
  1529. Changing;
  1530. FMap.Sort;
  1531. Changed;
  1532. end;
  1533. end;
  1534. {$endif}