stringl.inc 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386
  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. Result:=not TrailingLineBreak;
  71. end;
  72. procedure TStrings.SetSkipLastLineBreak(const AValue : Boolean);
  73. begin
  74. TrailingLineBreak:=not AValue;
  75. end;
  76. Function TStrings.GetLBS : TTextLineBreakStyle;
  77. begin
  78. CheckSpecialChars;
  79. Result:=FLBS;
  80. end;
  81. Procedure TStrings.SetLBS (AValue : TTextLineBreakStyle);
  82. begin
  83. CheckSpecialChars;
  84. FLBS:=AValue;
  85. end;
  86. procedure TStrings.SetDelimiter(c:Char);
  87. begin
  88. CheckSpecialChars;
  89. FDelimiter:=c;
  90. end;
  91. Procedure TStrings.SetEncoding(const AEncoding: TEncoding);
  92. begin
  93. if (FEncoding<>nil) and not TEncoding.IsStandardEncoding(FEncoding) then
  94. FEncoding.Free;
  95. if TEncoding.IsStandardEncoding(AEncoding) then
  96. FEncoding:=AEncoding
  97. else if AEncoding<>nil then
  98. FEncoding:=AEncoding.Clone
  99. else
  100. FEncoding:=nil;
  101. end;
  102. Function TStrings.GetDelimiter : Char;
  103. begin
  104. CheckSpecialChars;
  105. Result:=FDelimiter;
  106. end;
  107. procedure TStrings.SetLineBreak(Const S : String);
  108. begin
  109. CheckSpecialChars;
  110. FLineBreak:=S;
  111. end;
  112. Function TStrings.GetLineBreak : String;
  113. begin
  114. CheckSpecialChars;
  115. Result:=FLineBreak;
  116. end;
  117. procedure TStrings.SetQuoteChar(c:Char);
  118. begin
  119. CheckSpecialChars;
  120. FQuoteChar:=c;
  121. end;
  122. Function TStrings.GetQuoteChar :Char;
  123. begin
  124. CheckSpecialChars;
  125. Result:=FQuoteChar;
  126. end;
  127. procedure TStrings.SetNameValueSeparator(c:Char);
  128. begin
  129. CheckSpecialChars;
  130. FNameValueSeparator:=c;
  131. end;
  132. Function TStrings.GetNameValueSeparator :Char;
  133. begin
  134. CheckSpecialChars;
  135. Result:=FNameValueSeparator;
  136. end;
  137. function TStrings.GetCommaText: string;
  138. Var
  139. C1,C2 : Char;
  140. FSD : Boolean;
  141. begin
  142. CheckSpecialChars;
  143. FSD:=StrictDelimiter;
  144. C1:=Delimiter;
  145. C2:=QuoteChar;
  146. Delimiter:=',';
  147. QuoteChar:='"';
  148. StrictDelimiter:=False;
  149. Try
  150. Result:=GetDelimitedText;
  151. Finally
  152. Delimiter:=C1;
  153. QuoteChar:=C2;
  154. StrictDelimiter:=FSD;
  155. end;
  156. end;
  157. function TStrings.GetLineBreakCharLBS: string;
  158. begin
  159. CheckSpecialChars;
  160. if FLineBreak<>sLineBreak then
  161. Result:=FLineBreak
  162. else
  163. Case FLBS of
  164. tlbsLF : Result:=#10;
  165. tlbsCRLF : Result:=#13#10;
  166. tlbsCR : Result:=#13;
  167. end;
  168. end;
  169. function TStrings.GetMissingNameValueSeparatorAction: TMissingNameValueSeparatorAction;
  170. begin
  171. CheckSpecialChars;
  172. Result:=FMissingNameValueSeparatorAction;
  173. end;
  174. Function TStrings.GetDelimitedText: string;
  175. Var
  176. I : integer;
  177. p : pchar;
  178. BreakChars : set of char;
  179. S : String;
  180. doQuote : Boolean;
  181. begin
  182. CheckSpecialChars;
  183. result:='';
  184. if StrictDelimiter then
  185. BreakChars:=[#0,QuoteChar,Delimiter]
  186. else
  187. BreakChars:=[#0..' ',QuoteChar,Delimiter];
  188. // Check for break characters and quote if required.
  189. For i:=0 to count-1 do
  190. begin
  191. S:=Strings[i];
  192. doQuote:=FAlwaysQuote;
  193. If not DoQuote then
  194. begin
  195. p:=pchar(S);
  196. //Quote strings that include BreakChars:
  197. while not(p^ in BreakChars) do
  198. inc(p);
  199. DoQuote:=(p<>pchar(S)+length(S));
  200. end;
  201. if DoQuote then
  202. Result:=Result+QuoteString(S,QuoteChar)
  203. else
  204. Result:=Result+S;
  205. if I<Count-1 then
  206. Result:=Result+Delimiter;
  207. end;
  208. // Quote empty string:
  209. If (Length(Result)=0) and (Count=1) then
  210. Result:=QuoteChar+QuoteChar;
  211. end;
  212. procedure TStrings.GetNameValue(Index : Integer; Out AName,AValue : String);
  213. Var L : longint;
  214. begin
  215. aName:='';
  216. CheckSpecialChars;
  217. AValue:=Strings[Index];
  218. L:=Pos(FNameValueSeparator,AValue);
  219. If L<>0 then
  220. begin
  221. AName:=Copy(AValue,1,L-1);
  222. System.Delete(AValue,1,L);
  223. end
  224. else
  225. case FMissingNameValueSeparatorAction of
  226. mnvaValue : ;
  227. mnvaName :
  228. begin
  229. aName:=aValue;
  230. aValue:='';
  231. end;
  232. mnvaEmpty :
  233. aValue:='';
  234. mnvaError :
  235. Raise EStringListError.CreateFmt(SErrNoNameValuePairAt,[Index]);
  236. end;
  237. end;
  238. function TStrings.ExtractName(const s:String):String;
  239. var
  240. L: Longint;
  241. begin
  242. CheckSpecialChars;
  243. L:=Pos(FNameValueSeparator,S);
  244. If L<>0 then
  245. Result:=Copy(S,1,L-1)
  246. else
  247. Result:='';
  248. end;
  249. procedure TStrings.Filter(aFilter: TStringsFilterMethod; aList: TStrings);
  250. var
  251. S : string;
  252. begin
  253. for S in self do
  254. if aFilter(S) then
  255. aList.Add(S);
  256. end;
  257. procedure TStrings.ForEach(aCallback: TStringsForeachMethod);
  258. var
  259. S : String;
  260. begin
  261. for S in self do
  262. aCallBack(S);
  263. end;
  264. procedure TStrings.ForEach(aCallback: TStringsForeachMethodEx);
  265. var
  266. i: integer;
  267. begin
  268. for i:=0 to Count-1 do
  269. aCallBack(Strings[i],i);
  270. end;
  271. procedure TStrings.ForEach(aCallback: TStringsForeachMethodExObj);
  272. var
  273. i: integer;
  274. begin
  275. for i:=0 to Count-1 do
  276. aCallback(Strings[i],i,Objects[i]);
  277. end;
  278. function TStrings.Filter(aFilter: TStringsFilterMethod): TStrings;
  279. begin
  280. Result:=TStringsClass(Self.ClassType).Create;
  281. try
  282. Filter(aFilter,Result);
  283. except
  284. FreeAndNil(Result);
  285. Raise;
  286. end;
  287. end;
  288. procedure TStrings.Fill(const aValue: String; aStart, aEnd: Integer);
  289. var
  290. i: integer;
  291. begin
  292. if aEnd<0 then
  293. aEnd:=Self.Count+aEnd;
  294. if aEnd>=Count then
  295. aEnd:=Count-1;
  296. for i:=aStart to aEnd do
  297. Strings[i]:=aValue;
  298. end;
  299. Procedure TStrings.Map(aMap: TStringsMapMethod; aList : TStrings);
  300. Var
  301. S : String;
  302. begin
  303. For S in self do
  304. aList.Add(aMap(S));
  305. end;
  306. Function TStrings.Map(aMap: TStringsMapMethod) : TStrings;
  307. begin
  308. Result:=TStringsClass(Self.ClassType).Create;
  309. try
  310. Map(aMap,Result);
  311. except
  312. FreeAndNil(Result);
  313. Raise;
  314. end;
  315. end;
  316. function TStrings.Reduce(aReduceMethod: TStringsReduceMethod; const startingValue: string): string;
  317. var
  318. S : String;
  319. begin
  320. Result:=startingValue;
  321. for S in self do
  322. Result:=aReduceMethod(Result, S);
  323. end;
  324. Function TStrings.Reverse : TStrings;
  325. begin
  326. Result:=TStringsClass(Self.ClassType).Create;
  327. try
  328. Reverse(Result);
  329. except
  330. FreeAndNil(Result);
  331. Raise;
  332. end;
  333. end;
  334. Procedure TStrings.Reverse(aList : TStrings);
  335. Var
  336. I : Integer;
  337. begin
  338. for I:=Count-1 downto 0 do
  339. aList.Add(Strings[i]);
  340. end;
  341. Procedure TStrings.Slice(fromIndex: integer; aList : TStrings);
  342. var
  343. i: integer;
  344. begin
  345. for i:=fromIndex to Count-1 do
  346. aList.Add(Self[i]);
  347. end;
  348. Function TStrings.Slice(fromIndex: integer) : TStrings;
  349. begin
  350. Result:=TStringsClass(Self.ClassType).Create;
  351. try
  352. Slice(FromIndex,Result);
  353. except
  354. FreeAndNil(Result);
  355. Raise;
  356. end;
  357. end;
  358. function TStrings.GetName(Index: Integer): string;
  359. Var
  360. V : String;
  361. begin
  362. GetNameValue(Index,Result,V);
  363. end;
  364. function TStrings.GetStrictDelimiter: Boolean;
  365. begin
  366. Result:=soStrictDelimiter in FOptions;
  367. end;
  368. function TStrings.GetTrailingLineBreak: Boolean;
  369. begin
  370. Result:=soTrailingLineBreak in FOptions;
  371. end;
  372. function TStrings.GetUseLocale: Boolean;
  373. begin
  374. Result:=soUseLocale in FOptions;
  375. end;
  376. function TStrings.GetWriteBOM: Boolean;
  377. begin
  378. Result:=soWriteBOM in FOptions;
  379. end;
  380. Function TStrings.GetValue(const Name: string): string;
  381. Var
  382. L : longint;
  383. N : String;
  384. begin
  385. Result:='';
  386. L:=IndexOfName(Name);
  387. If L<>-1 then
  388. GetNameValue(L,N,Result);
  389. end;
  390. Function TStrings.GetValueFromIndex(Index: Integer): string;
  391. Var
  392. N : String;
  393. begin
  394. GetNameValue(Index,N,Result);
  395. end;
  396. Procedure TStrings.SetValueFromIndex(Index: Integer; const Value: string);
  397. begin
  398. If (Value='') then
  399. Delete(Index)
  400. else
  401. begin
  402. If (Index<0) then
  403. Index:=Add('');
  404. CheckSpecialChars;
  405. Strings[Index]:=GetName(Index)+FNameValueSeparator+Value;
  406. end;
  407. end;
  408. procedure TStrings.ReadData(Reader: TReader);
  409. begin
  410. Reader.ReadListBegin;
  411. BeginUpdate;
  412. try
  413. Clear;
  414. while not Reader.EndOfList do
  415. Add(Reader.ReadString);
  416. finally
  417. EndUpdate;
  418. end;
  419. Reader.ReadListEnd;
  420. end;
  421. Procedure TStrings.SetDelimitedText(const AValue: string);
  422. begin
  423. CheckSpecialChars;
  424. DoSetDelimitedText(aValue,True,StrictDelimiter,FQuoteChar,FDelimiter);
  425. end;
  426. Procedure TStrings.DoSetDelimitedText(const AValue: string; DoClear,aStrictDelimiter : Boolean; aQuoteChar,aDelimiter : Char);
  427. var
  428. len,i,j: SizeInt;
  429. aNotFirst:boolean;
  430. Procedure AddQuoted;
  431. begin
  432. Add(StringReplace(Copy(AValue,i+1,j-i-1),aQuoteChar+aQuoteChar,aQuoteChar, [rfReplaceAll]));
  433. end;
  434. begin
  435. BeginUpdate;
  436. i:=1;
  437. j:=1;
  438. aNotFirst:=false;
  439. { Paraphrased from Delphi XE2 help:
  440. Strings must be separated by Delimiter characters or spaces.
  441. They may be enclosed in QuoteChars.
  442. QuoteChars in the string must be repeated to distinguish them from the QuoteChars enclosing the string.
  443. }
  444. try
  445. if DoClear then
  446. Clear;
  447. len:=length(AValue);
  448. If aStrictDelimiter then
  449. begin
  450. while i<=Len do begin
  451. // skip delimiter
  452. if aNotFirst and (i<=len) and (AValue[i]=aDelimiter) then
  453. inc(i);
  454. // read next string
  455. if i<=len then begin
  456. if AValue[i]=aQuoteChar then begin
  457. // next string is quoted
  458. j:=i+1;
  459. while (j<=len) and
  460. ((AValue[j]<>aQuoteChar) or
  461. ((j+1<=len) and (AValue[j+1]=aQuoteChar))) do
  462. begin
  463. if (j<=len) and (AValue[j]=aQuoteChar) then
  464. inc(j,2)
  465. else
  466. inc(j);
  467. end;
  468. AddQuoted;
  469. i:=j+1;
  470. end else begin
  471. // next string is not quoted; read until delimiter
  472. j:=i;
  473. while (j<=len) and
  474. (AValue[j]<>aDelimiter) do inc(j);
  475. Add( Copy(AValue,i,j-i));
  476. i:=j;
  477. end;
  478. end else begin
  479. if aNotFirst then Add('');
  480. end;
  481. aNotFirst:=true;
  482. end;
  483. end
  484. else
  485. begin
  486. while i<=len do begin
  487. // skip delimiter
  488. if aNotFirst and (i<=len) and (AValue[i]=aDelimiter) then inc(i);
  489. // skip spaces
  490. while (i<=len) and (Ord(AValue[i])<=Ord(' ')) do inc(i);
  491. // read next string
  492. if i<=len then begin
  493. if AValue[i]=aQuoteChar then begin
  494. // next string is quoted
  495. j:=i+1;
  496. while (j<=len) and
  497. ( (AValue[j]<>aQuoteChar) or
  498. ( (j+1<=len) and (AValue[j+1]=aQuoteChar) ) ) do begin
  499. if (j<=len) and (AValue[j]=aQuoteChar) then inc(j,2)
  500. else inc(j);
  501. end;
  502. AddQuoted;
  503. i:=j+1;
  504. end else begin
  505. // next string is not quoted; read until control character/space/delimiter
  506. j:=i;
  507. while (j<=len) and
  508. (Ord(AValue[j])>Ord(' ')) and
  509. (AValue[j]<>aDelimiter) do inc(j);
  510. Add( Copy(AValue,i,j-i));
  511. i:=j;
  512. end;
  513. end else begin
  514. if aNotFirst then Add('');
  515. end;
  516. // skip spaces
  517. while (i<=len) and (Ord(AValue[i])<=Ord(' ')) do inc(i);
  518. aNotFirst:=true;
  519. end;
  520. end;
  521. finally
  522. EndUpdate;
  523. end;
  524. end;
  525. Procedure TStrings.SetCommaText(const Value: string);
  526. begin
  527. CheckSpecialChars;
  528. DoSetDelimitedText(Value,True,StrictDelimiter,'"',',');
  529. end;
  530. procedure TStrings.SetMissingNameValueSeparatorAction(AValue: TMissingNameValueSeparatorAction);
  531. begin
  532. CheckSpecialChars;
  533. FMissingNameValueSeparatorAction:=aValue;
  534. end;
  535. Procedure TStrings.SetStringsAdapter(const Value: IStringsAdapter);
  536. begin
  537. end;
  538. procedure TStrings.SetStrictDelimiter(AValue: Boolean);
  539. begin
  540. if AValue then
  541. Include(FOptions,soStrictDelimiter)
  542. else
  543. Exclude(FOptions,soStrictDelimiter);
  544. end;
  545. procedure TStrings.SetTrailingLineBreak(AValue: Boolean);
  546. begin
  547. if AValue then
  548. Include(FOptions,soTrailingLineBreak)
  549. else
  550. Exclude(FOptions,soTrailingLineBreak);
  551. end;
  552. procedure TStrings.SetUseLocale(AValue: Boolean);
  553. begin
  554. if AValue then
  555. Include(FOptions,soUseLocale)
  556. else
  557. Exclude(FOptions,soUseLocale);
  558. end;
  559. procedure TStrings.SetWriteBOM(AValue: Boolean);
  560. begin
  561. if AValue then
  562. Include(FOptions,soWriteBOM)
  563. else
  564. Exclude(FOptions,soWriteBOM);
  565. end;
  566. Procedure TStrings.SetDefaultEncoding(const ADefaultEncoding: TEncoding);
  567. begin
  568. if (FDefaultEncoding<>nil) and not TEncoding.IsStandardEncoding(FDefaultEncoding) then
  569. FDefaultEncoding.Free;
  570. if TEncoding.IsStandardEncoding(ADefaultEncoding) then
  571. FDefaultEncoding:=ADefaultEncoding
  572. else if ADefaultEncoding<>nil then
  573. FDefaultEncoding:=ADefaultEncoding.Clone
  574. else
  575. FDefaultEncoding:=TEncoding.Default;
  576. end;
  577. Procedure TStrings.SetValue(const Name, Value: string);
  578. Var L : longint;
  579. begin
  580. CheckSpecialChars;
  581. L:=IndexOfName(Name);
  582. if L=-1 then
  583. Add (Name+FNameValueSeparator+Value)
  584. else
  585. Strings[L]:=Name+FNameValueSeparator+value;
  586. end;
  587. procedure TStrings.WriteData(Writer: TWriter);
  588. var
  589. i: Integer;
  590. begin
  591. Writer.WriteListBegin;
  592. for i := 0 to Count - 1 do
  593. Writer.WriteString(Strings[i]);
  594. Writer.WriteListEnd;
  595. end;
  596. function TStrings.CompareStrings(const s1,s2 : string) : Integer;
  597. begin
  598. Result := DoCompareText(s1, s2);
  599. end;
  600. procedure TStrings.DefineProperties(Filer: TFiler);
  601. var
  602. HasData: Boolean;
  603. begin
  604. if Assigned(Filer.Ancestor) then
  605. // Only serialize if string list is different from ancestor
  606. if Filer.Ancestor.InheritsFrom(TStrings) then
  607. HasData := not Equals(TStrings(Filer.Ancestor))
  608. else
  609. HasData := True
  610. else
  611. HasData := Count > 0;
  612. Filer.DefineProperty('Strings', @ReadData, @WriteData, HasData);
  613. end;
  614. Procedure TStrings.Error(const Msg: string; Data: Integer);
  615. begin
  616. Raise EStringListError.CreateFmt(Msg,[Data]) at get_caller_addr(get_frame), get_caller_frame(get_frame);
  617. end;
  618. Procedure TStrings.Error(const Msg: pstring; Data: Integer);
  619. begin
  620. Raise EStringListError.CreateFmt(Msg^,[Data]) at get_caller_addr(get_frame), get_caller_frame(get_frame);
  621. end;
  622. Function TStrings.GetCapacity: Integer;
  623. begin
  624. Result:=Count;
  625. end;
  626. Function TStrings.GetObject(Index: Integer): TObject;
  627. begin
  628. Result:=Nil;
  629. end;
  630. Function TStrings.GetTextStr: string;
  631. Var P : Pchar;
  632. I,L,NLS : SizeInt;
  633. S,NL : String;
  634. begin
  635. NL:=GetLineBreakCharLBS;
  636. // Determine needed place
  637. L:=0;
  638. NLS:=Length(NL);
  639. For I:=0 to count-1 do
  640. L:=L+Length(Strings[I])+NLS;
  641. if SkipLastLineBreak then
  642. Dec(L,NLS);
  643. Setlength(Result,L);
  644. P:=Pointer(Result);
  645. For i:=0 To count-1 do
  646. begin
  647. S:=Strings[I];
  648. L:=Length(S);
  649. if L<>0 then
  650. System.Move(Pointer(S)^,P^,L);
  651. P:=P+L;
  652. if (I<Count-1) or Not SkipLastLineBreak then
  653. For L:=1 to NLS do
  654. begin
  655. P^:=NL[L];
  656. inc(P);
  657. end;
  658. end;
  659. end;
  660. Procedure TStrings.Put(Index: Integer; const S: string);
  661. Var Obj : TObject;
  662. begin
  663. Obj:=Objects[Index];
  664. Delete(Index);
  665. InsertObject(Index,S,Obj);
  666. end;
  667. Procedure TStrings.PutObject(Index: Integer; AObject: TObject);
  668. begin
  669. // Empty.
  670. end;
  671. Procedure TStrings.SetCapacity(NewCapacity: Integer);
  672. begin
  673. // Empty.
  674. end;
  675. Class Function TStrings.GetNextLine (Const Value : String; Var S : String; Var P : SizeInt) : Boolean;
  676. var
  677. LengthOfValue: SizeInt;
  678. StartPos, FuturePos: SizeInt;
  679. begin
  680. LengthOfValue := Length(Value);
  681. StartPos := P;
  682. if (StartPos <= 0) or (StartPos > LengthOfValue) then // True for LengthOfValue <= 0
  683. begin
  684. S := '';
  685. Exit(False);
  686. end;
  687. FuturePos := StartPos;
  688. while (FuturePos <= LengthOfValue) and not (Value[FuturePos] in [#10, #13]) do
  689. Inc(FuturePos);
  690. // If we use S := Copy(Value, StartPos, FuturePos - StartPos); then compiler
  691. // generate TempS := Copy(...); S := TempS to eliminate side effects and
  692. // implicit "try finally" for TempS finalization
  693. // When we use SetString then no TempS, no try finally generated,
  694. // but we must check case when Value and S is same (side effects)
  695. if Pointer(S) = Pointer(Value) then
  696. System.Delete(S, FuturePos, High(FuturePos))
  697. else
  698. begin
  699. SetString(S, @Value[StartPos], FuturePos - StartPos);
  700. if (FuturePos <= LengthOfValue) and (Value[FuturePos] = #13) then
  701. Inc(FuturePos);
  702. if (FuturePos <= LengthOfValue) and (Value[FuturePos] = #10) then
  703. Inc(FuturePos);
  704. end;
  705. P := FuturePos;
  706. Result := True;
  707. end;
  708. Function TStrings.GetNextLineBreak (Const Value : String; Var S : String; Var P : SizeInt) : Boolean;
  709. var
  710. StartPos, FuturePos: SizeInt;
  711. begin
  712. StartPos := P;
  713. if (StartPos <= 0) or (StartPos > Length(Value)) then // True for Length <= 0
  714. begin
  715. S := '';
  716. Exit(False);
  717. end;
  718. FuturePos := Pos(FLineBreak, Value, StartPos); // Use PosEx in old RTL
  719. // Why we don't use Copy but use SetString read in GetNextLine
  720. if FuturePos = 0 then // No line breaks
  721. begin
  722. FuturePos := Length(Value) + 1;
  723. if Pointer(S) = Pointer(Value) then
  724. // Nothing to do
  725. else
  726. SetString(S, @Value[StartPos], FuturePos - StartPos)
  727. end
  728. else
  729. if Pointer(S) = Pointer(Value) then
  730. System.Delete(S, FuturePos, High(FuturePos))
  731. else
  732. begin
  733. SetString(S, @Value[StartPos], FuturePos - StartPos);
  734. Inc(FuturePos, Length(FLineBreak));
  735. end;
  736. P := FuturePos;
  737. Result := True;
  738. end;
  739. {$IF (SizeOf(Integer) < SizeOf(SizeInt)) }
  740. class function TStrings.GetNextLine(const Value: string; var S: string; var P: Integer) : Boolean;
  741. var
  742. LP: SizeInt;
  743. begin
  744. LP := P;
  745. Result := GetNextLine(Value, S, LP);
  746. P := LP;
  747. end;
  748. function TStrings.GetNextLineBreak(const Value: string; var S: string; var P: Integer) : Boolean;
  749. var
  750. LP: SizeInt;
  751. begin
  752. LP := P;
  753. Result := GetNextLineBreak(Value, S, LP);
  754. P := LP;
  755. end;
  756. {$IFEND}
  757. Procedure TStrings.DoSetTextStr(const Value: string; DoClear : Boolean);
  758. Var
  759. S : String;
  760. P : SizeInt;
  761. begin
  762. Try
  763. beginUpdate;
  764. if DoClear then
  765. Clear;
  766. P:=1;
  767. if FLineBreak=sLineBreak then
  768. begin
  769. While GetNextLine (Value,S,P) do
  770. Add(S)
  771. end
  772. else
  773. While GetNextLineBreak (Value,S,P) do
  774. Add(S);
  775. finally
  776. EndUpdate;
  777. end;
  778. end;
  779. Procedure TStrings.SetTextStr(const Value: string);
  780. begin
  781. CheckSpecialChars;
  782. DoSetTextStr(Value,True);
  783. end;
  784. Procedure TStrings.AddText(const S: string);
  785. begin
  786. CheckSpecialChars;
  787. DoSetTextStr(S,False);
  788. end;
  789. procedure TStrings.AddCommaText(const S: String);
  790. begin
  791. DoSetDelimitedText(S,False,StrictDelimiter,'"',',');
  792. end;
  793. procedure TStrings.AddDelimitedText(const S: String; ADelimiter: Char; AStrictDelimiter: Boolean);
  794. begin
  795. CheckSpecialChars;
  796. DoSetDelimitedText(S,False,AStrictDelimiter,FQuoteChar,ADelimiter);
  797. end;
  798. procedure TStrings.AddDelimitedText(const S: String);
  799. begin
  800. CheckSpecialChars;
  801. DoSetDelimitedText(S,False,StrictDelimiter,FQuoteChar,FDelimiter);
  802. end;
  803. Procedure TStrings.SetUpdateState(Updating: Boolean);
  804. begin
  805. FPONotifyObservers(Self,ooChange,Nil);
  806. end;
  807. destructor TSTrings.Destroy;
  808. begin
  809. if (FEncoding<>nil) and not TEncoding.IsStandardEncoding(FEncoding) then
  810. FreeAndNil(FEncoding);
  811. if (FDefaultEncoding<>nil) and not TEncoding.IsStandardEncoding(FDefaultEncoding) then
  812. FreeAndNil(FDefaultEncoding);
  813. inherited destroy;
  814. end;
  815. constructor TStrings.Create;
  816. begin
  817. inherited Create;
  818. FDefaultEncoding:=TEncoding.Default;
  819. FEncoding:=nil;
  820. FOptions := [soTrailingLineBreak,soUseLocale,soPreserveBOM];
  821. FAlwaysQuote:=False;
  822. end;
  823. Function TStrings.Add(const S: string): Integer;
  824. begin
  825. Result:=Count;
  826. Insert (Count,S);
  827. end;
  828. function TStrings.Add(const Fmt : string; const Args : Array of const): Integer;
  829. begin
  830. Result:=Add(Format(Fmt,Args));
  831. end;
  832. Function TStrings.AddObject(const S: string; AObject: TObject): Integer;
  833. begin
  834. Result:=Add(S);
  835. Objects[result]:=AObject;
  836. end;
  837. function TStrings.AddObject(const Fmt: string; Args : Array of const; AObject: TObject): Integer;
  838. begin
  839. Result:=AddObject(Format(Fmt,Args),AObject);
  840. end;
  841. function TStrings.AddPair(const AName, AValue: string): TStrings;
  842. begin
  843. Result:=AddPair(AName,AValue,Nil);
  844. end;
  845. function TStrings.AddPair(const AName, AValue: string; AObject: TObject): TStrings;
  846. begin
  847. Result := Self;
  848. AddObject(Concat(AName, NameValueSeparator, AValue), AObject);
  849. end;
  850. Procedure TStrings.Append(const S: string);
  851. begin
  852. Add (S);
  853. end;
  854. Procedure TStrings.AddStrings(TheStrings: TStrings; ClearFirst : Boolean);
  855. Var Runner : longint;
  856. begin
  857. beginupdate;
  858. try
  859. if ClearFirst then
  860. Clear;
  861. if Count + TheStrings.Count > Capacity then
  862. Capacity := Count + TheStrings.Count;
  863. For Runner:=0 to TheStrings.Count-1 do
  864. self.AddObject (Thestrings[Runner],TheStrings.Objects[Runner]);
  865. finally
  866. EndUpdate;
  867. end;
  868. end;
  869. Procedure TStrings.AddStrings(TheStrings: TStrings);
  870. begin
  871. AddStrings(TheStrings, False);
  872. end;
  873. Procedure TStrings.AddStrings(const TheStrings: array of string);
  874. begin
  875. AddStrings(TheStrings, False);
  876. end;
  877. Procedure TStrings.AddStrings(const TheStrings: array of string; ClearFirst : Boolean);
  878. Var Runner : longint;
  879. begin
  880. beginupdate;
  881. try
  882. if ClearFirst then
  883. Clear;
  884. if Count + High(TheStrings)+1 > Capacity then
  885. Capacity := Count + High(TheStrings)+1;
  886. For Runner:=Low(TheStrings) to High(TheStrings) do
  887. self.Add(Thestrings[Runner]);
  888. finally
  889. EndUpdate;
  890. end;
  891. end;
  892. Procedure TStrings.Assign(Source: TPersistent);
  893. Var
  894. S : TStrings;
  895. begin
  896. If Source is TStrings then
  897. begin
  898. S:=TStrings(Source);
  899. BeginUpdate;
  900. Try
  901. clear;
  902. FSpecialCharsInited:=S.FSpecialCharsInited;
  903. FQuoteChar:=S.FQuoteChar;
  904. FDelimiter:=S.FDelimiter;
  905. FNameValueSeparator:=S.FNameValueSeparator;
  906. FLBS:=S.FLBS;
  907. FLineBreak:=S.FLineBreak;
  908. FOptions:=S.FOptions;
  909. DefaultEncoding:=S.DefaultEncoding;
  910. SetEncoding(S.Encoding);
  911. AddStrings(S);
  912. finally
  913. EndUpdate;
  914. end;
  915. end
  916. else
  917. Inherited Assign(Source);
  918. end;
  919. Procedure TStrings.BeginUpdate;
  920. begin
  921. if FUpdateCount = 0 then SetUpdateState(true);
  922. inc(FUpdateCount);
  923. end;
  924. Procedure TStrings.EndUpdate;
  925. begin
  926. If FUpdateCount>0 then
  927. Dec(FUpdateCount);
  928. if FUpdateCount=0 then
  929. SetUpdateState(False);
  930. end;
  931. Function TStrings.Equals(Obj: TObject): Boolean;
  932. begin
  933. if Obj is TStrings then
  934. Result := Equals(TStrings(Obj))
  935. else
  936. Result := inherited Equals(Obj);
  937. end;
  938. Function TStrings.Equals(TheStrings: TStrings): Boolean;
  939. Var Runner,Nr : Longint;
  940. begin
  941. Result:=False;
  942. Nr:=Self.Count;
  943. if Nr<>TheStrings.Count then exit;
  944. For Runner:=0 to Nr-1 do
  945. If Strings[Runner]<>TheStrings[Runner] then exit;
  946. Result:=True;
  947. end;
  948. Procedure TStrings.Exchange(Index1, Index2: Integer);
  949. Var
  950. Obj : TObject;
  951. Str : String;
  952. begin
  953. beginUpdate;
  954. Try
  955. Obj:=Objects[Index1];
  956. Str:=Strings[Index1];
  957. Objects[Index1]:=Objects[Index2];
  958. Strings[Index1]:=Strings[Index2];
  959. Objects[Index2]:=Obj;
  960. Strings[Index2]:=Str;
  961. finally
  962. EndUpdate;
  963. end;
  964. end;
  965. function TStrings.GetEnumerator: TStringsEnumerator;
  966. begin
  967. Result:=TStringsEnumerator.Create(Self);
  968. end;
  969. Function TStrings.GetText: PChar;
  970. begin
  971. Result:=StrNew(Pchar(Self.Text));
  972. end;
  973. Function TStrings.DoCompareText(const s1,s2 : string) : PtrInt;
  974. begin
  975. if UseLocale then
  976. result:=AnsiCompareText(s1,s2)
  977. else
  978. result:=CompareText(s1,s2);
  979. end;
  980. Function TStrings.IndexOf(const S: string): Integer;
  981. begin
  982. Result:=0;
  983. While (Result<Count) and (DoCompareText(Strings[Result],S)<>0) do Result:=Result+1;
  984. if Result=Count then Result:=-1;
  985. end;
  986. function TStrings.IndexOf(const S: string; aStart: Integer): Integer;
  987. begin
  988. if aStart<0 then
  989. begin
  990. aStart:=Count+aStart;
  991. if aStart<0 then
  992. aStart:=0;
  993. end;
  994. Result:=aStart;
  995. While (Result<Count) and (DoCompareText(Strings[Result],S)<>0) do Result:=Result+1;
  996. if Result=Count then Result:=-1;
  997. end;
  998. Function TStrings.IndexOfName(const Name: string): Integer;
  999. Var
  1000. len : longint;
  1001. S : String;
  1002. begin
  1003. CheckSpecialChars;
  1004. Result:=0;
  1005. while (Result<Count) do
  1006. begin
  1007. S:=Strings[Result];
  1008. len:=pos(FNameValueSeparator,S)-1;
  1009. if (len>=0) and (DoCompareText(Name,Copy(S,1,Len))=0) then
  1010. exit;
  1011. inc(result);
  1012. end;
  1013. result:=-1;
  1014. end;
  1015. Function TStrings.IndexOfObject(AObject: TObject): Integer;
  1016. begin
  1017. Result:=0;
  1018. While (Result<count) and (Objects[Result]<>AObject) do Result:=Result+1;
  1019. If Result=Count then Result:=-1;
  1020. end;
  1021. Procedure TStrings.InsertObject(Index: Integer; const S: string;
  1022. AObject: TObject);
  1023. begin
  1024. Insert (Index,S);
  1025. Objects[Index]:=AObject;
  1026. end;
  1027. function TStrings.LastIndexOf(const S: string): Integer;
  1028. begin
  1029. Result:=LastIndexOf(S,Count-1);
  1030. end;
  1031. function TStrings.LastIndexOf(const S: string; aStart : Integer): Integer;
  1032. begin
  1033. if aStart<0 then
  1034. begin
  1035. aStart:=Count+aStart;
  1036. if aStart<0 then
  1037. aStart:=0;
  1038. end;
  1039. Result:=aStart;
  1040. if Result>=Count-1 then
  1041. Result:=Count-1;
  1042. While (Result>=0) and (DoCompareText(Strings[Result],S)<>0) do
  1043. Result:=Result-1;
  1044. end;
  1045. Procedure TStrings.LoadFromFile(const FileName: string);
  1046. begin
  1047. LoadFromFile(FileName,False)
  1048. end;
  1049. Procedure TStrings.LoadFromFile(const FileName: string; IgnoreEncoding : Boolean);
  1050. Var
  1051. TheStream : TFileStream;
  1052. begin
  1053. TheStream:=TFileStream.Create(FileName,fmOpenRead or fmShareDenyWrite);
  1054. try
  1055. LoadFromStream(TheStream, IgnoreEncoding);
  1056. finally
  1057. TheStream.Free;
  1058. end;
  1059. end;
  1060. Procedure TStrings.LoadFromFile(const FileName: string; AEncoding: TEncoding);
  1061. Var
  1062. TheStream : TFileStream;
  1063. begin
  1064. TheStream:=TFileStream.Create(FileName,fmOpenRead or fmShareDenyWrite);
  1065. try
  1066. LoadFromStream(TheStream,AEncoding);
  1067. finally
  1068. TheStream.Free;
  1069. end;
  1070. end;
  1071. Procedure TStrings.LoadFromStream(Stream: TStream);
  1072. begin
  1073. LoadFromStream(Stream,False);
  1074. end;
  1075. Const
  1076. LoadBufSize = 1024;
  1077. LoadMaxGrow = MaxInt Div 2;
  1078. Procedure TStrings.LoadFromStream(Stream: TStream; IgnoreEncoding : Boolean);
  1079. {
  1080. Borlands method is no good, since a pipe for
  1081. instance doesn't have a size.
  1082. So we must do it the hard way.
  1083. }
  1084. Var
  1085. Buffer : AnsiString;
  1086. BufLen : SizeInt;
  1087. BytesRead, I, BufDelta : Longint;
  1088. begin
  1089. if not IgnoreEncoding then
  1090. begin
  1091. LoadFromStream(Stream,Nil);
  1092. Exit;
  1093. end;
  1094. // reread into a buffer
  1095. beginupdate;
  1096. try
  1097. Buffer:='';
  1098. BufLen:=0;
  1099. I:=1;
  1100. Repeat
  1101. BufDelta:=LoadBufSize*I;
  1102. SetLength(Buffer,BufLen+BufDelta);
  1103. BytesRead:=Stream.Read(Buffer[BufLen+1],BufDelta);
  1104. inc(BufLen,BufDelta);
  1105. If I<LoadMaxGrow then
  1106. I:=I shl 1;
  1107. Until BytesRead<>BufDelta;
  1108. SetLength(Buffer, BufLen-BufDelta+BytesRead);
  1109. SetTextStr(Buffer);
  1110. SetLength(Buffer,0);
  1111. finally
  1112. EndUpdate;
  1113. end;
  1114. if soPreserveBOM in FOptions then
  1115. WriteBOM:=False;
  1116. end;
  1117. Procedure TStrings.LoadFromStream(Stream: TStream; AEncoding: TEncoding);
  1118. {
  1119. Borlands method is no good, since a pipe for
  1120. instance doesn't have a size.
  1121. So we must do it the hard way.
  1122. }
  1123. Var
  1124. Buffer : TBytes;
  1125. T : string;
  1126. BufLen : SizeInt;
  1127. BytesRead, I, BufDelta, PreambleLength : Longint;
  1128. begin
  1129. // reread into a buffer
  1130. beginupdate;
  1131. try
  1132. SetLength(Buffer,0);
  1133. BufLen:=0;
  1134. I:=1;
  1135. Repeat
  1136. BufDelta:=LoadBufSize*I;
  1137. SetLength(Buffer,BufLen+BufDelta);
  1138. BytesRead:=Stream.Read(Buffer[BufLen],BufDelta);
  1139. inc(BufLen,BufDelta);
  1140. If I<LoadMaxGrow then
  1141. I:=I shl 1;
  1142. Until BytesRead<>BufDelta;
  1143. SetLength(Buffer,BufLen-BufDelta+BytesRead);
  1144. PreambleLength:=TEncoding.GetBufferEncoding(Buffer,AEncoding,FDefaultEncoding);
  1145. T:=AEncoding.GetAnsiString(Buffer,PreambleLength,Length(Buffer)-PreambleLength);
  1146. if soPreserveBOM in FOptions then
  1147. WriteBOM:=PreambleLength>0;
  1148. SetEncoding(AEncoding);
  1149. SetLength(Buffer,0);
  1150. SetTextStr(T);
  1151. finally
  1152. EndUpdate;
  1153. end;
  1154. end;
  1155. Procedure TStrings.Move(CurIndex, NewIndex: Integer);
  1156. Var
  1157. Obj : TObject;
  1158. Str : String;
  1159. begin
  1160. BeginUpdate;
  1161. Try
  1162. Obj:=Objects[CurIndex];
  1163. Str:=Strings[CurIndex];
  1164. Objects[CurIndex]:=Nil; // Prevent Delete from freeing.
  1165. Delete(Curindex);
  1166. InsertObject(NewIndex,Str,Obj);
  1167. finally
  1168. EndUpdate;
  1169. end;
  1170. end;
  1171. function TStrings.Pop: string;
  1172. var
  1173. C : Integer;
  1174. begin
  1175. Result:='';
  1176. C:=Count-1;
  1177. if (C>=0) then
  1178. begin
  1179. Result:=Strings[C];
  1180. Delete(C);
  1181. end;
  1182. end;
  1183. function TStrings.Shift: String;
  1184. begin
  1185. Result:='';
  1186. if (Count > 0) then
  1187. begin
  1188. Result:=Strings[0];
  1189. Delete(0);
  1190. end;
  1191. end;
  1192. Procedure TStrings.SaveToFile(const FileName: string);
  1193. Var TheStream : TFileStream;
  1194. begin
  1195. TheStream:=TFileStream.Create(FileName,fmCreate);
  1196. try
  1197. SaveToStream(TheStream);
  1198. finally
  1199. TheStream.Free;
  1200. end;
  1201. end;
  1202. Procedure TStrings.SaveToFile(const FileName: string; IgnoreEncoding : Boolean);
  1203. Var TheStream : TFileStream;
  1204. begin
  1205. TheStream:=TFileStream.Create(FileName,fmCreate);
  1206. try
  1207. SaveToStream(TheStream, IgnoreEncoding);
  1208. finally
  1209. TheStream.Free;
  1210. end;
  1211. end;
  1212. Procedure TStrings.SaveToFile(const FileName: string; AEncoding: TEncoding);
  1213. Var TheStream : TFileStream;
  1214. begin
  1215. TheStream:=TFileStream.Create(FileName,fmCreate);
  1216. try
  1217. SaveToStream(TheStream,AEncoding);
  1218. finally
  1219. TheStream.Free;
  1220. end;
  1221. end;
  1222. Procedure TStrings.SaveToStream(Stream: TStream);
  1223. begin
  1224. SaveToStream(Stream,False)
  1225. end;
  1226. Procedure TStrings.SaveToStream(Stream: TStream; IgnoreEncoding: Boolean);
  1227. Var
  1228. I,L,NLS : SizeInt;
  1229. S,NL : String;
  1230. begin
  1231. if not IgnoreEncoding then
  1232. begin
  1233. SaveToStream(Stream,FEncoding);
  1234. Exit;
  1235. end;
  1236. NL:=GetLineBreakCharLBS;
  1237. NLS:=Length(NL)*SizeOf(Char);
  1238. For i:=0 To count-1 do
  1239. begin
  1240. S:=Strings[I];
  1241. L:=Length(S);
  1242. if L<>0 then
  1243. Stream.WriteBuffer(S[1], L*SizeOf(Char));
  1244. if (I<Count-1) or Not SkipLastLineBreak then
  1245. Stream.WriteBuffer(NL[1], NLS);
  1246. end;
  1247. end;
  1248. Procedure TStrings.SaveToStream(Stream: TStream; AEncoding: TEncoding);
  1249. Var B,BNL : TBytes;
  1250. NL,S: string;
  1251. i,BNLS: SizeInt;
  1252. begin
  1253. if AEncoding=nil then
  1254. AEncoding:=FDefaultEncoding;
  1255. if WriteBOM then
  1256. begin
  1257. B:=AEncoding.GetPreamble;
  1258. if Length(B)>0 then
  1259. Stream.WriteBuffer(B[0],Length(B));
  1260. end;
  1261. NL := GetLineBreakCharLBS;
  1262. BNL:=AEncoding.GetAnsiBytes(NL);
  1263. BNLS:=Length(BNL);
  1264. For i:=0 To count-1 do
  1265. begin
  1266. S:=Strings[I];
  1267. if S<>'' then
  1268. begin
  1269. B:=AEncoding.GetAnsiBytes(S);
  1270. Stream.WriteBuffer(B[0],Length(B));
  1271. end;
  1272. if (I<Count-1) or Not SkipLastLineBreak then
  1273. Stream.WriteBuffer(BNL[0],BNLS);
  1274. end;
  1275. end;
  1276. Procedure TStrings.SetText(TheText: PChar);
  1277. Var S : String;
  1278. begin
  1279. If TheText<>Nil then
  1280. S:=StrPas(TheText)
  1281. else
  1282. S:='';
  1283. SetTextStr(S);
  1284. end;
  1285. {****************************************************************************}
  1286. {* TStringList *}
  1287. {****************************************************************************}
  1288. {$if not defined(FPC_TESTGENERICS)}
  1289. procedure TStringList.ExchangeItemsInt(Index1, Index2: Integer);
  1290. Var P1,P2 : Pointer;
  1291. begin
  1292. P1:=Pointer(Flist^[Index1].FString);
  1293. P2:=Pointer(Flist^[Index1].FObject);
  1294. Pointer(Flist^[Index1].Fstring):=Pointer(Flist^[Index2].Fstring);
  1295. Pointer(Flist^[Index1].FObject):=Pointer(Flist^[Index2].FObject);
  1296. Pointer(Flist^[Index2].Fstring):=P1;
  1297. Pointer(Flist^[Index2].FObject):=P2;
  1298. end;
  1299. function TStringList.GetSorted: Boolean;
  1300. begin
  1301. Result:=FSortStyle in [sslUser,sslAuto];
  1302. end;
  1303. procedure TStringList.ExchangeItems(Index1, Index2: Integer);
  1304. begin
  1305. ExchangeItemsInt(Index1, Index2);
  1306. end;
  1307. procedure TStringList.Grow;
  1308. Var
  1309. NC : Integer;
  1310. begin
  1311. NC:=FCapacity;
  1312. If NC>=256 then
  1313. NC:=NC+(NC Div 4)
  1314. else if NC=0 then
  1315. NC:=4
  1316. else
  1317. NC:=NC*4;
  1318. SetCapacity(NC);
  1319. end;
  1320. procedure TStringList.InternalClear(FromIndex: Integer; ClearOnly: Boolean);
  1321. Var
  1322. I: Integer;
  1323. begin
  1324. if FromIndex < FCount then
  1325. begin
  1326. if FOwnsObjects then
  1327. begin
  1328. For I:=FromIndex to FCount-1 do
  1329. begin
  1330. Flist^[I].FString:='';
  1331. freeandnil(Flist^[i].FObject);
  1332. end;
  1333. end
  1334. else
  1335. begin
  1336. For I:=FromIndex to FCount-1 do
  1337. Flist^[I].FString:='';
  1338. end;
  1339. FCount:=FromIndex;
  1340. end;
  1341. if Not ClearOnly then
  1342. SetCapacity(0);
  1343. end;
  1344. procedure TStringList.InsertItem(Index: Integer; const S: string);
  1345. begin
  1346. InsertItem(Index, S, nil);
  1347. end;
  1348. procedure TStringList.InsertItem(Index: Integer; const S: string; O: TObject);
  1349. begin
  1350. Changing;
  1351. If FCount=Fcapacity then Grow;
  1352. If Index<FCount then
  1353. System.Move (FList^[Index],FList^[Index+1],
  1354. (FCount-Index)*SizeOf(TStringItem));
  1355. Pointer(Flist^[Index].Fstring):=Nil; // Needed to initialize...
  1356. Flist^[Index].FString:=S;
  1357. Flist^[Index].FObject:=O;
  1358. Inc(FCount);
  1359. Changed;
  1360. end;
  1361. procedure TStringList.SetSorted(Value: Boolean);
  1362. begin
  1363. If Value then
  1364. SortStyle:=sslAuto
  1365. else
  1366. SortStyle:=sslNone
  1367. end;
  1368. procedure TStringList.Changed;
  1369. begin
  1370. If (FUpdateCount=0) Then
  1371. begin
  1372. If Assigned(FOnChange) then
  1373. FOnchange(Self);
  1374. FPONotifyObservers(Self,ooChange,Nil);
  1375. end;
  1376. end;
  1377. procedure TStringList.Changing;
  1378. begin
  1379. If FUpdateCount=0 then
  1380. if Assigned(FOnChanging) then
  1381. FOnchanging(Self);
  1382. end;
  1383. function TStringList.Get(Index: Integer): string;
  1384. begin
  1385. CheckIndex(Index);
  1386. Result:=Flist^[Index].FString;
  1387. end;
  1388. function TStringList.GetCapacity: Integer;
  1389. begin
  1390. Result:=FCapacity;
  1391. end;
  1392. function TStringList.GetCount: Integer;
  1393. begin
  1394. Result:=FCount;
  1395. end;
  1396. function TStringList.GetObject(Index: Integer): TObject;
  1397. begin
  1398. CheckIndex(Index);
  1399. Result:=Flist^[Index].FObject;
  1400. end;
  1401. procedure TStringList.Put(Index: Integer; const S: string);
  1402. begin
  1403. If Sorted then
  1404. Error(SSortedListError,0);
  1405. CheckIndex(Index);
  1406. Changing;
  1407. Flist^[Index].FString:=S;
  1408. Changed;
  1409. end;
  1410. procedure TStringList.PutObject(Index: Integer; AObject: TObject);
  1411. begin
  1412. CheckIndex(Index);
  1413. Changing;
  1414. Flist^[Index].FObject:=AObject;
  1415. Changed;
  1416. end;
  1417. procedure TStringList.SetCapacity(NewCapacity: Integer);
  1418. Var NewList : Pointer;
  1419. MSize : Longint;
  1420. begin
  1421. If (NewCapacity<0) then
  1422. Error (SListCapacityError,NewCapacity);
  1423. If NewCapacity>FCapacity then
  1424. begin
  1425. GetMem (NewList,NewCapacity*SizeOf(TStringItem));
  1426. If NewList=Nil then
  1427. Error (SListCapacityError,NewCapacity);
  1428. If Assigned(FList) then
  1429. begin
  1430. MSize:=FCapacity*Sizeof(TStringItem);
  1431. System.Move (FList^,NewList^,MSize);
  1432. FillWord (Pchar(NewList)[MSize],(NewCapacity-FCapacity)*(SizeOf(TStringItem) div SizeOf(Word)), 0);
  1433. FreeMem (Flist,MSize);
  1434. end;
  1435. Flist:=NewList;
  1436. FCapacity:=NewCapacity;
  1437. end
  1438. else if NewCapacity<FCapacity then
  1439. begin
  1440. if NewCapacity = 0 then
  1441. begin
  1442. if FCount > 0 then
  1443. InternalClear(0,True);
  1444. FreeMem(FList);
  1445. FList := nil;
  1446. end else
  1447. begin
  1448. InternalClear(NewCapacity,True);
  1449. GetMem(NewList, NewCapacity * SizeOf(TStringItem));
  1450. System.Move(FList^, NewList^, NewCapacity * SizeOf(TStringItem));
  1451. FreeMem(FList);
  1452. FList := NewList;
  1453. end;
  1454. FCapacity:=NewCapacity;
  1455. end;
  1456. end;
  1457. procedure TStringList.SetUpdateState(Updating: Boolean);
  1458. begin
  1459. If Updating then
  1460. Changing
  1461. else
  1462. Changed
  1463. end;
  1464. destructor TStringList.Destroy;
  1465. begin
  1466. InternalClear;
  1467. Inherited destroy;
  1468. end;
  1469. function TStringList.Add(const S: string): Integer;
  1470. begin
  1471. If (SortStyle<>sslAuto) then
  1472. Result:=FCount
  1473. else
  1474. If Find (S,Result) then
  1475. Case DUplicates of
  1476. DupIgnore : Exit;
  1477. DupError : Error(SDuplicateString,0)
  1478. end;
  1479. InsertItem (Result,S);
  1480. end;
  1481. procedure TStringList.Clear;
  1482. begin
  1483. if FCount = 0 then Exit;
  1484. Changing;
  1485. InternalClear;
  1486. Changed;
  1487. end;
  1488. procedure TStringList.Delete(Index: Integer);
  1489. begin
  1490. CheckIndex(Index);
  1491. Changing;
  1492. Flist^[Index].FString:='';
  1493. if FOwnsObjects then
  1494. FreeAndNil(Flist^[Index].FObject);
  1495. Dec(FCount);
  1496. If Index<FCount then
  1497. System.Move(Flist^[Index+1],
  1498. Flist^[Index],
  1499. (Fcount-Index)*SizeOf(TStringItem));
  1500. Changed;
  1501. end;
  1502. procedure TStringList.Exchange(Index1, Index2: Integer);
  1503. begin
  1504. CheckIndex(Index1);
  1505. CheckIndex(Index2);
  1506. Changing;
  1507. ExchangeItemsInt(Index1,Index2);
  1508. changed;
  1509. end;
  1510. procedure TStringList.SetCaseSensitive(b : boolean);
  1511. begin
  1512. if b=FCaseSensitive then
  1513. Exit;
  1514. FCaseSensitive:=b;
  1515. if FSortStyle=sslAuto then
  1516. begin
  1517. FForceSort:=True;
  1518. try
  1519. Sort;
  1520. finally
  1521. FForceSort:=False;
  1522. end;
  1523. end;
  1524. end;
  1525. procedure TStringList.SetSortStyle(AValue: TStringsSortStyle);
  1526. begin
  1527. if FSortStyle=AValue then Exit;
  1528. if (AValue=sslAuto) then
  1529. Sort;
  1530. FSortStyle:=AValue;
  1531. end;
  1532. procedure TStringList.CheckIndex(AIndex: Integer);
  1533. begin
  1534. If (AIndex<0) or (AIndex>=FCount) then
  1535. Error(SListIndexError,AIndex);
  1536. end;
  1537. function TStringList.DoCompareText(const s1, s2: string): PtrInt;
  1538. begin
  1539. if FCaseSensitive then
  1540. begin
  1541. if UseLocale then
  1542. result:=AnsiCompareStr(s1,s2)
  1543. else
  1544. result:=CompareStr(s1,s2);
  1545. end else
  1546. begin
  1547. if UseLocale then
  1548. result:=AnsiCompareText(s1,s2)
  1549. else
  1550. result:=CompareText(s1,s2);
  1551. end;
  1552. end;
  1553. function TStringList.Find(const S: string; out Index: Integer): Boolean;
  1554. var
  1555. L, R, I: Integer;
  1556. CompareRes: PtrInt;
  1557. begin
  1558. Result := false;
  1559. Index:=-1;
  1560. if Not Sorted then
  1561. Raise EListError.Create(SErrFindNeedsSortedList);
  1562. // Use binary search.
  1563. L := 0;
  1564. R := Count - 1;
  1565. while (L<=R) do
  1566. begin
  1567. I := L + (R - L) div 2;
  1568. CompareRes := DoCompareText(S, Flist^[I].FString);
  1569. if (CompareRes>0) then
  1570. L := I+1
  1571. else begin
  1572. R := I-1;
  1573. if (CompareRes=0) then begin
  1574. Result := true;
  1575. if (Duplicates<>dupAccept) then
  1576. L := I; // forces end of while loop
  1577. end;
  1578. end;
  1579. end;
  1580. Index := L;
  1581. end;
  1582. function TStringList.IndexOf(const S: string): Integer;
  1583. begin
  1584. If Not Sorted then
  1585. Result:=Inherited indexOf(S)
  1586. else
  1587. // faster using binary search...
  1588. If Not Find (S,Result) then
  1589. Result:=-1;
  1590. end;
  1591. procedure TStringList.Insert(Index: Integer; const S: string);
  1592. begin
  1593. If SortStyle=sslAuto then
  1594. Error (SSortedListError,0)
  1595. else
  1596. begin
  1597. If (Index<0) or (Index>FCount) then
  1598. Error(SListIndexError,Index); // Cannot use CheckIndex, because there >= FCount...
  1599. InsertItem (Index,S);
  1600. end;
  1601. end;
  1602. procedure TStringList.CustomSort(CompareFn: TStringListSortCompare);
  1603. begin
  1604. CustomSort(CompareFn, SortBase.DefaultSortingAlgorithm);
  1605. end;
  1606. type
  1607. PStringList_CustomSort_Context = ^TStringList_CustomSort_Context;
  1608. TStringList_CustomSort_Context = record
  1609. List: TStringList;
  1610. ListStartPtr: Pointer;
  1611. CompareFn: TStringListSortCompare;
  1612. end;
  1613. function TStringList_CustomSort_Comparer(Item1, Item2, Context: Pointer): Integer;
  1614. begin
  1615. with PStringList_CustomSort_Context(Context)^ do
  1616. Result := CompareFn(List,
  1617. (Item1 - ListStartPtr) div SizeOf(TStringItem),
  1618. (Item2 - ListStartPtr) div SizeOf(TStringItem));
  1619. end;
  1620. procedure TStringList_CustomSort_Exchanger(Item1, Item2, Context: Pointer);
  1621. begin
  1622. with PStringList_CustomSort_Context(Context)^ do
  1623. List.ExchangeItems((Item1 - ListStartPtr) div SizeOf(TStringItem),
  1624. (Item2 - ListStartPtr) div SizeOf(TStringItem));
  1625. end;
  1626. procedure TStringList.CustomSort(CompareFn: TStringListSortCompare; SortingAlgorithm: PSortingAlgorithm);
  1627. var
  1628. Context: TStringList_CustomSort_Context;
  1629. begin
  1630. If (FCount>1) and (FForceSort or (FSortStyle<>sslAuto)) then
  1631. begin
  1632. Changing;
  1633. Context.List := Self;
  1634. Context.ListStartPtr := FList;
  1635. Context.CompareFn := CompareFn;
  1636. //if ExchangeItems is overriden call that, else call (faster) ItemListSorter_ContextComparer
  1637. if TMethod(@Self.ExchangeItems).Code = CodePointer(@TStringList.ExchangeItems) then
  1638. SortingAlgorithm^.ItemListSorter_ContextComparer(
  1639. FList, FCount, SizeOf(TStringItem), @TStringList_CustomSort_Comparer,
  1640. @Context)
  1641. else
  1642. SortingAlgorithm^.ItemListSorter_CustomItemExchanger_ContextComparer(
  1643. FList, FCount, SizeOf(TStringItem), @TStringList_CustomSort_Comparer,
  1644. @TStringList_CustomSort_Exchanger, @Context);
  1645. Changed;
  1646. end;
  1647. end;
  1648. function StringListAnsiCompare(List: TStringList; Index1, Index: Integer): Integer;
  1649. begin
  1650. Result := List.DoCompareText(List.FList^[Index1].FString,
  1651. List.FList^[Index].FString);
  1652. end;
  1653. procedure TStringList.Sort;
  1654. begin
  1655. CustomSort(@StringListAnsiCompare);
  1656. end;
  1657. procedure TStringList.Sort(SortingAlgorithm: PSortingAlgorithm);
  1658. begin
  1659. CustomSort(@StringListAnsiCompare, SortingAlgorithm);
  1660. end;
  1661. {$else}
  1662. { generics based implementation of TStringList follows }
  1663. function StringListAnsiCompare(List: TStringList; Index1, Index2: Integer): Integer;
  1664. begin
  1665. Result := List.DoCompareText(List.Strings[Index1], List.Strings[Index2]);
  1666. end;
  1667. constructor TStringList.Create;
  1668. begin
  1669. inherited;
  1670. FOwnsObjects:=false;
  1671. FMap := TFPStrObjMap.Create;
  1672. FMap.OnPtrCompare := @MapPtrCompare;
  1673. FOnCompareText := @DefaultCompareText;
  1674. NameValueSeparator:='=';
  1675. CheckSpecialChars;
  1676. end;
  1677. destructor TStringList.Destroy;
  1678. begin
  1679. FMap.Free;
  1680. inherited;
  1681. end;
  1682. function TStringList.GetDuplicates: TDuplicates;
  1683. begin
  1684. Result := FMap.Duplicates;
  1685. end;
  1686. function TStringList.GetSorted: boolean;
  1687. begin
  1688. Result := FMap.Sorted;
  1689. end;
  1690. procedure TStringList.SetDuplicates(NewDuplicates: TDuplicates);
  1691. begin
  1692. FMap.Duplicates := NewDuplicates;
  1693. end;
  1694. procedure TStringList.SetSorted(NewSorted: Boolean);
  1695. begin
  1696. FMap.Sorted := NewSorted;
  1697. end;
  1698. procedure TStringList.Changed;
  1699. begin
  1700. if FUpdateCount = 0 then
  1701. if Assigned(FOnChange) then
  1702. FOnChange(Self);
  1703. end;
  1704. procedure TStringList.Changing;
  1705. begin
  1706. if FUpdateCount = 0 then
  1707. if Assigned(FOnChanging) then
  1708. FOnChanging(Self);
  1709. end;
  1710. function TStringList.Get(Index: Integer): string;
  1711. begin
  1712. Result := FMap.Keys[Index];
  1713. end;
  1714. function TStringList.GetCapacity: Integer;
  1715. begin
  1716. Result := FMap.Capacity;
  1717. end;
  1718. function TStringList.GetCount: Integer;
  1719. begin
  1720. Result := FMap.Count;
  1721. end;
  1722. function TStringList.GetObject(Index: Integer): TObject;
  1723. begin
  1724. Result := FMap.Data[Index];
  1725. end;
  1726. procedure TStringList.Put(Index: Integer; const S: string);
  1727. begin
  1728. Changing;
  1729. FMap.Keys[Index] := S;
  1730. Changed;
  1731. end;
  1732. procedure TStringList.PutObject(Index: Integer; AObject: TObject);
  1733. begin
  1734. Changing;
  1735. FMap.Data[Index] := AObject;
  1736. Changed;
  1737. end;
  1738. procedure TStringList.SetCapacity(NewCapacity: Integer);
  1739. begin
  1740. FMap.Capacity := NewCapacity;
  1741. end;
  1742. procedure TStringList.SetUpdateState(Updating: Boolean);
  1743. begin
  1744. if Updating then
  1745. Changing
  1746. else
  1747. Changed
  1748. end;
  1749. function TStringList.Add(const S: string): Integer;
  1750. begin
  1751. Result := FMap.Add(S);
  1752. end;
  1753. procedure TStringList.Clear;
  1754. begin
  1755. if FMap.Count = 0 then exit;
  1756. Changing;
  1757. FMap.Clear;
  1758. Changed;
  1759. end;
  1760. procedure TStringList.Delete(Index: Integer);
  1761. begin
  1762. if (Index < 0) or (Index >= FMap.Count) then
  1763. Error(SListIndexError, Index);
  1764. Changing;
  1765. FMap.Delete(Index);
  1766. Changed;
  1767. end;
  1768. procedure TStringList.Exchange(Index1, Index2: Integer);
  1769. begin
  1770. if (Index1 < 0) or (Index1 >= FMap.Count) then
  1771. Error(SListIndexError, Index1);
  1772. if (Index2 < 0) or (Index2 >= FMap.Count) then
  1773. Error(SListIndexError, Index2);
  1774. Changing;
  1775. FMap.InternalExchange(Index1, Index2);
  1776. Changed;
  1777. end;
  1778. procedure TStringList.SetCaseSensitive(NewSensitive: Boolean);
  1779. begin
  1780. if NewSensitive <> FCaseSensitive then
  1781. begin
  1782. FCaseSensitive := NewSensitive;
  1783. if Sorted then
  1784. Sort;
  1785. end;
  1786. end;
  1787. function TStringList.MapPtrCompare(Key1, Key2: Pointer): Integer;
  1788. begin
  1789. Result := FOnCompareText(string(Key1^), string(Key2^));
  1790. end;
  1791. function TStringList.DefaultCompareText(const s1, s2: string): PtrInt;
  1792. begin
  1793. if FCaseSensitive then
  1794. Result := AnsiCompareStr(s1, s2)
  1795. else
  1796. Result := AnsiCompareText(s1, s2);
  1797. end;
  1798. function TStringList.DoCompareText(const s1, s2: string): PtrInt;
  1799. begin
  1800. Result := FOnCompareText(s1, s2);
  1801. end;
  1802. function TStringList.Find(const S: string; var Index: Integer): Boolean;
  1803. begin
  1804. Result := FMap.Find(S, Index);
  1805. end;
  1806. function TStringList.IndexOf(const S: string): Integer;
  1807. begin
  1808. Result := FMap.IndexOf(S);
  1809. end;
  1810. procedure TStringList.Insert(Index: Integer; const S: string);
  1811. begin
  1812. if not Sorted and (0 <= Index) and (Index < FMap.Count) then
  1813. Changing;
  1814. FMap.InsertKey(Index, S);
  1815. Changed;
  1816. end;
  1817. type
  1818. PStringList_CustomSort_Context = ^TStringList_CustomSort_Context;
  1819. TStringList_CustomSort_Context = record
  1820. List: TStringList;
  1821. ListStartPtr: Pointer;
  1822. ItemSize: SizeUInt;
  1823. IndexBase: Integer;
  1824. CompareFn: TStringListSortCompare;
  1825. end;
  1826. function TStringList_CustomSort_Comparer(Item1, Item2, Context: Pointer): Integer;
  1827. begin
  1828. with PStringList_CustomSort_Context(Context)^ do
  1829. Result := CompareFn(List,
  1830. ((Item1 - ListStartPtr) div ItemSize) + IndexBase,
  1831. ((Item2 - ListStartPtr) div ItemSize) + IndexBase);
  1832. end;
  1833. procedure TStringList_CustomSort_Exchanger(Item1, Item2, Context: Pointer);
  1834. begin
  1835. with PStringList_CustomSort_Context(Context)^ do
  1836. List.Exchange(((Item1 - ListStartPtr) div ItemSize) + IndexBase,
  1837. ((Item2 - ListStartPtr) div ItemSize) + IndexBase);
  1838. end;
  1839. procedure TStringList.QuickSort(L, R: Integer; CompareFn: TStringListSortCompare);
  1840. var
  1841. Context: TStringList_CustomSort_Context;
  1842. begin
  1843. if L > R then
  1844. exit;
  1845. Context.List := Self;
  1846. Context.ListStartPtr := FMap.Items[L];
  1847. Context.CompareFn := CompareFn;
  1848. Context.ItemSize := FMap.KeySize + FMap.DataSize;
  1849. Context.IndexBase := L;
  1850. DefaultSortingAlgorithm^.ItemListSorter_CustomItemExchanger_ContextComparer(
  1851. Context.ListStartPtr, R - L + 1, Context.ItemSize, @TStringList_CustomSort_Comparer,
  1852. @TStringList_CustomSort_Exchanger, @Context);
  1853. end;
  1854. procedure TStringList.CustomSort(CompareFn: TStringListSortCompare);
  1855. begin
  1856. if not Sorted and (FMap.Count > 1) then
  1857. begin
  1858. Changing;
  1859. QuickSort(0, FMap.Count-1, CompareFn);
  1860. Changed;
  1861. end;
  1862. end;
  1863. procedure TStringList.CustomSort(CompareFn: TStringListSortCompare; SortingAlgorithm: PSortingAlgorithm);
  1864. var
  1865. Context: TStringList_CustomSort_Context;
  1866. begin
  1867. if not Sorted and (FMap.Count > 1) then
  1868. begin
  1869. Changing;
  1870. Context.List := Self;
  1871. Context.ListStartPtr := FMap.Items[0];
  1872. Context.CompareFn := CompareFn;
  1873. Context.ItemSize := FMap.KeySize + FMap.DataSize;
  1874. Context.IndexBase := 0;
  1875. SortingAlgorithm^.ItemListSorter_CustomItemExchanger_ContextComparer(
  1876. Context.ListStartPtr, FMap.Count, Context.ItemSize, @TStringList_CustomSort_Comparer,
  1877. @TStringList_CustomSort_Exchanger, @Context);
  1878. Changed;
  1879. end;
  1880. end;
  1881. procedure TStringList.Sort;
  1882. begin
  1883. if not Sorted and (FMap.Count > 1) then
  1884. begin
  1885. Changing;
  1886. FMap.Sort;
  1887. Changed;
  1888. end;
  1889. end;
  1890. procedure TStringList.Sort(SortingAlgorithm: PSortingAlgorithm);
  1891. begin
  1892. if not Sorted and (FMap.Count > 1) then
  1893. begin
  1894. Changing;
  1895. FMap.Sort(SortingAlgorithm);
  1896. Changed;
  1897. end;
  1898. end;
  1899. {$endif}