stringl.inc 49 KB

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