2
0

stringl.inc 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499
  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.Contains(const S: string): Boolean;
  1022. begin
  1023. Result:=Indexof(S)<>-1;
  1024. end;
  1025. Function TStrings.IndexOf(const S: string): Integer;
  1026. begin
  1027. Result:=0;
  1028. While (Result<Count) and (DoCompareText(Strings[Result],S)<>0) do Result:=Result+1;
  1029. if Result=Count then Result:=-1;
  1030. end;
  1031. function TStrings.IndexOf(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. While (Result<Count) and (DoCompareText(Strings[Result],S)<>0) do Result:=Result+1;
  1041. if Result=Count then Result:=-1;
  1042. end;
  1043. Function TStrings.ContainsName(const Name: string): Boolean;
  1044. begin
  1045. Result:=IndexOfName(Name)<>-1;
  1046. end;
  1047. Function TStrings.IndexOfName(const Name: string): Integer;
  1048. Var
  1049. len : longint;
  1050. S : String;
  1051. begin
  1052. CheckSpecialChars;
  1053. Result:=0;
  1054. while (Result<Count) do
  1055. begin
  1056. S:=Strings[Result];
  1057. len:=pos(FNameValueSeparator,S)-1;
  1058. if (len>=0) and (DoCompareText(Name,Copy(S,1,Len))=0) then
  1059. exit;
  1060. inc(result);
  1061. end;
  1062. result:=-1;
  1063. end;
  1064. Function TStrings.IndexOfObject(AObject: TObject): Integer;
  1065. begin
  1066. Result:=0;
  1067. While (Result<count) and (Objects[Result]<>AObject) do Result:=Result+1;
  1068. If Result=Count then Result:=-1;
  1069. end;
  1070. Procedure TStrings.InsertObject(Index: Integer; const S: string;
  1071. AObject: TObject);
  1072. begin
  1073. BeginUpdate;
  1074. try
  1075. Insert (Index,S);
  1076. Objects[Index]:=AObject;
  1077. finally
  1078. EndUpdate;
  1079. end;
  1080. end;
  1081. function TStrings.LastIndexOf(const S: string): Integer;
  1082. begin
  1083. Result:=LastIndexOf(S,Count-1);
  1084. end;
  1085. function TStrings.LastIndexOf(const S: string; aStart : Integer): Integer;
  1086. begin
  1087. if aStart<0 then
  1088. begin
  1089. aStart:=Count+aStart;
  1090. if aStart<0 then
  1091. aStart:=0;
  1092. end;
  1093. Result:=aStart;
  1094. if Result>=Count-1 then
  1095. Result:=Count-1;
  1096. While (Result>=0) and (DoCompareText(Strings[Result],S)<>0) do
  1097. Result:=Result-1;
  1098. end;
  1099. Procedure TStrings.LoadFromFile(const FileName: string);
  1100. begin
  1101. LoadFromFile(FileName,False)
  1102. end;
  1103. Procedure TStrings.LoadFromFile(const FileName: string; IgnoreEncoding : Boolean);
  1104. Var
  1105. TheStream : TFileStream;
  1106. begin
  1107. TheStream:=TFileStream.Create(FileName,fmOpenRead or fmShareDenyWrite);
  1108. try
  1109. LoadFromStream(TheStream, IgnoreEncoding);
  1110. finally
  1111. TheStream.Free;
  1112. end;
  1113. end;
  1114. Procedure TStrings.LoadFromFile(const FileName: string; AEncoding: TEncoding);
  1115. Var
  1116. TheStream : TFileStream;
  1117. begin
  1118. TheStream:=TFileStream.Create(FileName,fmOpenRead or fmShareDenyWrite);
  1119. try
  1120. LoadFromStream(TheStream,AEncoding);
  1121. finally
  1122. TheStream.Free;
  1123. end;
  1124. end;
  1125. Procedure TStrings.LoadFromStream(Stream: TStream);
  1126. begin
  1127. LoadFromStream(Stream,False);
  1128. end;
  1129. Const
  1130. LoadBufSize = 1024;
  1131. LoadMaxGrow = MaxInt Div 2;
  1132. Procedure TStrings.LoadFromStream(Stream: TStream; IgnoreEncoding : Boolean);
  1133. {
  1134. Borlands method is no good, since a pipe for
  1135. instance doesn't have a size.
  1136. So we must do it the hard way.
  1137. }
  1138. Var
  1139. Buffer : AnsiString;
  1140. BufLen : SizeInt;
  1141. BytesRead, I, BufDelta : Longint;
  1142. begin
  1143. if not IgnoreEncoding then
  1144. begin
  1145. LoadFromStream(Stream,Nil);
  1146. Exit;
  1147. end;
  1148. // reread into a buffer
  1149. beginupdate;
  1150. try
  1151. Buffer:='';
  1152. BufLen:=0;
  1153. I:=1;
  1154. Repeat
  1155. BufDelta:=LoadBufSize*I;
  1156. SetLength(Buffer,BufLen+BufDelta);
  1157. BytesRead:=Stream.Read(Buffer[BufLen+1],BufDelta);
  1158. inc(BufLen,BufDelta);
  1159. If I<LoadMaxGrow then
  1160. I:=I shl 1;
  1161. Until BytesRead<>BufDelta;
  1162. SetLength(Buffer, BufLen-BufDelta+BytesRead);
  1163. SetTextStr(Buffer);
  1164. SetLength(Buffer,0);
  1165. finally
  1166. EndUpdate;
  1167. end;
  1168. if soPreserveBOM in FOptions then
  1169. WriteBOM:=False;
  1170. end;
  1171. Procedure TStrings.LoadFromStream(Stream: TStream; AEncoding: TEncoding);
  1172. {
  1173. Borlands method is no good, since a pipe for
  1174. instance doesn't have a size.
  1175. So we must do it the hard way.
  1176. }
  1177. Var
  1178. Buffer : TBytes;
  1179. T : string;
  1180. BufLen : SizeInt;
  1181. BytesRead, I, BufDelta, PreambleLength : Longint;
  1182. begin
  1183. // reread into a buffer
  1184. beginupdate;
  1185. try
  1186. SetLength(Buffer,0);
  1187. BufLen:=0;
  1188. I:=1;
  1189. Repeat
  1190. BufDelta:=LoadBufSize*I;
  1191. SetLength(Buffer,BufLen+BufDelta);
  1192. BytesRead:=Stream.Read(Buffer[BufLen],BufDelta);
  1193. inc(BufLen,BufDelta);
  1194. If I<LoadMaxGrow then
  1195. I:=I shl 1;
  1196. Until BytesRead<>BufDelta;
  1197. SetLength(Buffer,BufLen-BufDelta+BytesRead);
  1198. PreambleLength:=TEncoding.GetBufferEncoding(Buffer,AEncoding,FDefaultEncoding);
  1199. T:=AEncoding.GetAnsiString(Buffer,PreambleLength,Length(Buffer)-PreambleLength);
  1200. if soPreserveBOM in FOptions then
  1201. WriteBOM:=PreambleLength>0;
  1202. SetEncoding(AEncoding);
  1203. SetLength(Buffer,0);
  1204. SetTextStr(T);
  1205. finally
  1206. EndUpdate;
  1207. end;
  1208. end;
  1209. Procedure TStrings.Move(CurIndex, NewIndex: Integer);
  1210. Var
  1211. Obj : TObject;
  1212. Str : String;
  1213. begin
  1214. if (CurIndex=NewIndex) then
  1215. Exit;
  1216. BeginUpdate;
  1217. Try
  1218. Obj:=Objects[CurIndex];
  1219. Str:=Strings[CurIndex];
  1220. Objects[CurIndex]:=Nil; // Prevent Delete from freeing.
  1221. Delete(Curindex);
  1222. InsertObject(NewIndex,Str,Obj);
  1223. finally
  1224. EndUpdate;
  1225. end;
  1226. end;
  1227. function TStrings.Pop: string;
  1228. var
  1229. C : Integer;
  1230. begin
  1231. Result:='';
  1232. C:=Count-1;
  1233. if (C>=0) then
  1234. begin
  1235. Result:=Strings[C];
  1236. Delete(C);
  1237. end;
  1238. end;
  1239. function TStrings.Shift: String;
  1240. begin
  1241. Result:='';
  1242. if (Count > 0) then
  1243. begin
  1244. Result:=Strings[0];
  1245. Delete(0);
  1246. end;
  1247. end;
  1248. Procedure TStrings.SaveToFile(const FileName: string);
  1249. Var TheStream : TFileStream;
  1250. begin
  1251. TheStream:=TFileStream.Create(FileName,fmCreate);
  1252. try
  1253. SaveToStream(TheStream);
  1254. finally
  1255. TheStream.Free;
  1256. end;
  1257. end;
  1258. Procedure TStrings.SaveToFile(const FileName: string; IgnoreEncoding : Boolean);
  1259. Var TheStream : TFileStream;
  1260. begin
  1261. TheStream:=TFileStream.Create(FileName,fmCreate);
  1262. try
  1263. SaveToStream(TheStream, IgnoreEncoding);
  1264. finally
  1265. TheStream.Free;
  1266. end;
  1267. end;
  1268. Procedure TStrings.SaveToFile(const FileName: string; AEncoding: TEncoding);
  1269. Var TheStream : TFileStream;
  1270. begin
  1271. TheStream:=TFileStream.Create(FileName,fmCreate);
  1272. try
  1273. SaveToStream(TheStream,AEncoding);
  1274. finally
  1275. TheStream.Free;
  1276. end;
  1277. end;
  1278. Procedure TStrings.SaveToStream(Stream: TStream);
  1279. begin
  1280. SaveToStream(Stream,False)
  1281. end;
  1282. Procedure TStrings.SaveToStream(Stream: TStream; IgnoreEncoding: Boolean);
  1283. Var
  1284. I,L,NLS : SizeInt;
  1285. S,NL : String;
  1286. begin
  1287. if not IgnoreEncoding then
  1288. begin
  1289. SaveToStream(Stream,FEncoding);
  1290. Exit;
  1291. end;
  1292. NL:=GetLineBreakCharLBS;
  1293. NLS:=Length(NL)*SizeOf(Char);
  1294. For i:=0 To count-1 do
  1295. begin
  1296. S:=Strings[I];
  1297. L:=Length(S);
  1298. if L<>0 then
  1299. Stream.WriteBuffer(S[1], L*SizeOf(Char));
  1300. if (I<Count-1) or Not SkipLastLineBreak then
  1301. Stream.WriteBuffer(NL[1], NLS);
  1302. end;
  1303. end;
  1304. Procedure TStrings.SaveToStream(Stream: TStream; AEncoding: TEncoding);
  1305. Var B,BNL : TBytes;
  1306. NL,S: string;
  1307. i,BNLS: SizeInt;
  1308. begin
  1309. if AEncoding=nil then
  1310. AEncoding:=FDefaultEncoding;
  1311. if WriteBOM then
  1312. begin
  1313. B:=AEncoding.GetPreamble;
  1314. if Length(B)>0 then
  1315. Stream.WriteBuffer(B[0],Length(B));
  1316. end;
  1317. NL := GetLineBreakCharLBS;
  1318. {$if sizeof(char)=1}
  1319. BNL:=AEncoding.GetAnsiBytes(NL);
  1320. {$else}
  1321. BNL:=AEncoding.GetBytes(NL);
  1322. {$endif}
  1323. BNLS:=Length(BNL);
  1324. For i:=0 To count-1 do
  1325. begin
  1326. S:=Strings[I];
  1327. if S<>'' then
  1328. begin
  1329. {$if sizeof(char)=1}
  1330. B:=AEncoding.GetAnsiBytes(S);
  1331. {$else}
  1332. B:=AEncoding.GetBytes(S);
  1333. {$endif}
  1334. Stream.WriteBuffer(B[0],Length(B));
  1335. end;
  1336. if (I<Count-1) or Not SkipLastLineBreak then
  1337. Stream.WriteBuffer(BNL[0],BNLS);
  1338. end;
  1339. end;
  1340. Procedure TStrings.SetText(TheText: PChar);
  1341. Var S : String;
  1342. begin
  1343. If TheText<>Nil then
  1344. S:=StrPas(TheText)
  1345. else
  1346. S:='';
  1347. SetTextStr(S);
  1348. end;
  1349. {****************************************************************************}
  1350. {* TStringList *}
  1351. {****************************************************************************}
  1352. {$if not defined(FPC_TESTGENERICS)}
  1353. procedure TStringList.ExchangeItemsInt(Index1, Index2: Integer);
  1354. Var P1,P2 : Pointer;
  1355. begin
  1356. P1:=Pointer(Flist^[Index1].FString);
  1357. P2:=Pointer(Flist^[Index1].FObject);
  1358. Pointer(Flist^[Index1].Fstring):=Pointer(Flist^[Index2].Fstring);
  1359. Pointer(Flist^[Index1].FObject):=Pointer(Flist^[Index2].FObject);
  1360. Pointer(Flist^[Index2].Fstring):=P1;
  1361. Pointer(Flist^[Index2].FObject):=P2;
  1362. end;
  1363. function TStringList.GetSorted: Boolean;
  1364. begin
  1365. Result:=FSortStyle in [sslUser,sslAuto];
  1366. end;
  1367. procedure TStringList.ExchangeItems(Index1, Index2: Integer);
  1368. begin
  1369. ExchangeItemsInt(Index1, Index2);
  1370. end;
  1371. procedure TStringList.Grow;
  1372. Var
  1373. NC : Integer;
  1374. begin
  1375. NC:=FCapacity;
  1376. If NC>=256 then
  1377. NC:=NC+(NC Div 4)
  1378. else if NC=0 then
  1379. NC:=4
  1380. else
  1381. NC:=NC*4;
  1382. SetCapacity(NC);
  1383. end;
  1384. procedure TStringList.InternalClear(FromIndex: Integer; ClearOnly: Boolean);
  1385. Var
  1386. I: Integer;
  1387. begin
  1388. if FromIndex < FCount then
  1389. begin
  1390. if FOwnsObjects then
  1391. begin
  1392. For I:=FromIndex to FCount-1 do
  1393. begin
  1394. Flist^[I].FString:='';
  1395. freeandnil(Flist^[i].FObject);
  1396. end;
  1397. end
  1398. else
  1399. begin
  1400. For I:=FromIndex to FCount-1 do
  1401. Flist^[I].FString:='';
  1402. end;
  1403. FCount:=FromIndex;
  1404. end;
  1405. if Not ClearOnly then
  1406. SetCapacity(0);
  1407. end;
  1408. procedure TStringList.InsertItem(Index: Integer; const S: string);
  1409. begin
  1410. InsertItem(Index, S, nil);
  1411. end;
  1412. procedure TStringList.InsertItem(Index: Integer; const S: string; O: TObject);
  1413. begin
  1414. Changing;
  1415. If FCount=Fcapacity then Grow;
  1416. If Index<FCount then
  1417. System.Move (FList^[Index],FList^[Index+1],
  1418. (FCount-Index)*SizeOf(TStringItem));
  1419. Pointer(Flist^[Index].Fstring):=Nil; // Needed to initialize...
  1420. Flist^[Index].FString:=S;
  1421. Flist^[Index].FObject:=O;
  1422. Inc(FCount);
  1423. Changed;
  1424. end;
  1425. procedure TStringList.SetSorted(Value: Boolean);
  1426. begin
  1427. If Value then
  1428. SortStyle:=sslAuto
  1429. else
  1430. SortStyle:=sslNone
  1431. end;
  1432. procedure TStringList.Changed;
  1433. begin
  1434. If (FUpdateCount=0) Then
  1435. begin
  1436. If Assigned(FOnChange) then
  1437. FOnchange(Self);
  1438. FPONotifyObservers(Self,ooChange,Nil);
  1439. end;
  1440. end;
  1441. procedure TStringList.Changing;
  1442. begin
  1443. If FUpdateCount=0 then
  1444. if Assigned(FOnChanging) then
  1445. FOnchanging(Self);
  1446. end;
  1447. function TStringList.Get(Index: Integer): string;
  1448. begin
  1449. CheckIndex(Index);
  1450. Result:=Flist^[Index].FString;
  1451. end;
  1452. function TStringList.GetCapacity: Integer;
  1453. begin
  1454. Result:=FCapacity;
  1455. end;
  1456. function TStringList.GetCount: Integer;
  1457. begin
  1458. Result:=FCount;
  1459. end;
  1460. function TStringList.GetObject(Index: Integer): TObject;
  1461. begin
  1462. CheckIndex(Index);
  1463. Result:=Flist^[Index].FObject;
  1464. end;
  1465. procedure TStringList.Put(Index: Integer; const S: string);
  1466. begin
  1467. If Sorted then
  1468. Error(SSortedListError,0);
  1469. CheckIndex(Index);
  1470. Changing;
  1471. Flist^[Index].FString:=S;
  1472. Changed;
  1473. end;
  1474. procedure TStringList.PutObject(Index: Integer; AObject: TObject);
  1475. begin
  1476. CheckIndex(Index);
  1477. Changing;
  1478. Flist^[Index].FObject:=AObject;
  1479. Changed;
  1480. end;
  1481. procedure TStringList.SetCapacity(NewCapacity: Integer);
  1482. Var NewList : Pointer;
  1483. MSize : Longint;
  1484. begin
  1485. If (NewCapacity<0) then
  1486. Error (SListCapacityError,NewCapacity);
  1487. If NewCapacity>FCapacity then
  1488. begin
  1489. GetMem (NewList,NewCapacity*SizeOf(TStringItem));
  1490. If NewList=Nil then
  1491. Error (SListCapacityError,NewCapacity);
  1492. If Assigned(FList) then
  1493. begin
  1494. MSize:=FCapacity*Sizeof(TStringItem);
  1495. System.Move (FList^,NewList^,MSize);
  1496. FillWord (PAnsiChar(NewList)[MSize],(NewCapacity-FCapacity)*(SizeOf(TStringItem) div SizeOf(Word)), 0);
  1497. FreeMem (Flist,MSize);
  1498. end;
  1499. Flist:=NewList;
  1500. FCapacity:=NewCapacity;
  1501. end
  1502. else if NewCapacity<FCapacity then
  1503. begin
  1504. if NewCapacity = 0 then
  1505. begin
  1506. if FCount > 0 then
  1507. InternalClear(0,True);
  1508. FreeMem(FList);
  1509. FList := nil;
  1510. end else
  1511. begin
  1512. InternalClear(NewCapacity,True);
  1513. GetMem(NewList, NewCapacity * SizeOf(TStringItem));
  1514. System.Move(FList^, NewList^, NewCapacity * SizeOf(TStringItem));
  1515. FreeMem(FList);
  1516. FList := NewList;
  1517. end;
  1518. FCapacity:=NewCapacity;
  1519. end;
  1520. end;
  1521. procedure TStringList.SetUpdateState(Updating: Boolean);
  1522. begin
  1523. If Updating then
  1524. Changing
  1525. else
  1526. Changed
  1527. end;
  1528. Constructor TStringList.Create;
  1529. begin
  1530. inherited Create;
  1531. end;
  1532. Constructor TStringList.Create(anOwnsObjects : Boolean);
  1533. begin
  1534. inherited Create;
  1535. FOwnsObjects:=anOwnsObjects;
  1536. end;
  1537. destructor TStringList.Destroy;
  1538. begin
  1539. InternalClear;
  1540. Inherited destroy;
  1541. end;
  1542. function TStringList.DoAddObject(const S: string; AObject: TObject): Integer;
  1543. begin
  1544. If (SortStyle<>sslAuto) then
  1545. Result:=FCount
  1546. else
  1547. If Find (S,Result) then
  1548. Case Duplicates of
  1549. DupIgnore : Exit;
  1550. DupError : Error(SDuplicateString,0)
  1551. end;
  1552. BeginUpdate;
  1553. try
  1554. InsertItem (Result,S);
  1555. if (aObject<>Nil) then
  1556. Objects[Result]:=AObject;
  1557. finally
  1558. EndUpdate;
  1559. end;
  1560. end;
  1561. function TStringList.Add(const S: string): Integer;
  1562. begin
  1563. Result:=DoAddObject(S, nil);
  1564. end;
  1565. function TStringList.AddObject(const S: string; AObject: TObject): Integer;
  1566. begin
  1567. Result:=DoAddObject(S,aObject);
  1568. end;
  1569. procedure TStringList.Clear;
  1570. begin
  1571. if FCount = 0 then Exit;
  1572. Changing;
  1573. InternalClear;
  1574. Changed;
  1575. end;
  1576. procedure TStringList.Delete(Index: Integer);
  1577. begin
  1578. CheckIndex(Index);
  1579. Changing;
  1580. Flist^[Index].FString:='';
  1581. if FOwnsObjects then
  1582. FreeAndNil(Flist^[Index].FObject);
  1583. Dec(FCount);
  1584. If Index<FCount then
  1585. System.Move(Flist^[Index+1],
  1586. Flist^[Index],
  1587. (Fcount-Index)*SizeOf(TStringItem));
  1588. Changed;
  1589. end;
  1590. procedure TStringList.Exchange(Index1, Index2: Integer);
  1591. begin
  1592. CheckIndex(Index1);
  1593. CheckIndex(Index2);
  1594. Changing;
  1595. ExchangeItemsInt(Index1,Index2);
  1596. changed;
  1597. end;
  1598. procedure TStringList.SetCaseSensitive(b : boolean);
  1599. begin
  1600. if b=FCaseSensitive then
  1601. Exit;
  1602. FCaseSensitive:=b;
  1603. if FSortStyle=sslAuto then
  1604. begin
  1605. FForceSort:=True;
  1606. try
  1607. Sort;
  1608. finally
  1609. FForceSort:=False;
  1610. end;
  1611. end;
  1612. end;
  1613. procedure TStringList.SetSortStyle(AValue: TStringsSortStyle);
  1614. begin
  1615. if FSortStyle=AValue then Exit;
  1616. if (AValue=sslAuto) then
  1617. Sort;
  1618. FSortStyle:=AValue;
  1619. end;
  1620. procedure TStringList.CheckIndex(AIndex: Integer);
  1621. begin
  1622. If (AIndex<0) or (AIndex>=FCount) then
  1623. Error(SListIndexError,AIndex);
  1624. end;
  1625. function TStringList.DoCompareText(const s1, s2: string): PtrInt;
  1626. begin
  1627. if FCaseSensitive then
  1628. begin
  1629. if UseLocale then
  1630. result:=AnsiCompareStr(s1,s2)
  1631. else
  1632. result:=CompareStr(s1,s2);
  1633. end else
  1634. begin
  1635. if UseLocale then
  1636. result:=AnsiCompareText(s1,s2)
  1637. else
  1638. result:=CompareText(s1,s2);
  1639. end;
  1640. end;
  1641. function TStringList.Find(const S: string; out Index: Integer): Boolean;
  1642. var
  1643. L, R, I: Integer;
  1644. CompareRes: PtrInt;
  1645. begin
  1646. Result := false;
  1647. Index:=-1;
  1648. if Not Sorted then
  1649. Raise EListError.Create(SErrFindNeedsSortedList);
  1650. // Use binary search.
  1651. L := 0;
  1652. R := Count - 1;
  1653. while (L<=R) do
  1654. begin
  1655. I := L + (R - L) div 2;
  1656. CompareRes := DoCompareText(S, Flist^[I].FString);
  1657. if (CompareRes>0) then
  1658. L := I+1
  1659. else begin
  1660. R := I-1;
  1661. if (CompareRes=0) then begin
  1662. Result := true;
  1663. if (Duplicates<>dupAccept) then
  1664. L := I; // forces end of while loop
  1665. end;
  1666. end;
  1667. end;
  1668. Index := L;
  1669. end;
  1670. function TStringList.IndexOf(const S: string): Integer;
  1671. begin
  1672. If Not Sorted then
  1673. Result:=Inherited indexOf(S)
  1674. else
  1675. // faster using binary search...
  1676. If Not Find (S,Result) then
  1677. Result:=-1;
  1678. end;
  1679. procedure TStringList.Insert(Index: Integer; const S: string);
  1680. begin
  1681. If SortStyle=sslAuto then
  1682. Error (SSortedListError,0)
  1683. else
  1684. begin
  1685. If (Index<0) or (Index>FCount) then
  1686. Error(SListIndexError,Index); // Cannot use CheckIndex, because there >= FCount...
  1687. InsertItem (Index,S);
  1688. end;
  1689. end;
  1690. procedure TStringList.CustomSort(CompareFn: TStringListSortCompare);
  1691. begin
  1692. CustomSort(CompareFn, {$IFDEF FPC_DOTTEDUNITS}System.{$ENDIF}SortBase.DefaultSortingAlgorithm);
  1693. end;
  1694. type
  1695. PStringList_CustomSort_Context = ^TStringList_CustomSort_Context;
  1696. TStringList_CustomSort_Context = record
  1697. List: TStringList;
  1698. ListStartPtr: Pointer;
  1699. CompareFn: TStringListSortCompare;
  1700. end;
  1701. function TStringList_CustomSort_Comparer(Item1, Item2, Context: Pointer): Integer;
  1702. begin
  1703. with PStringList_CustomSort_Context(Context)^ do
  1704. Result := CompareFn(List,
  1705. (Item1 - ListStartPtr) div SizeOf(TStringItem),
  1706. (Item2 - ListStartPtr) div SizeOf(TStringItem));
  1707. end;
  1708. procedure TStringList_CustomSort_Exchanger(Item1, Item2, Context: Pointer);
  1709. begin
  1710. with PStringList_CustomSort_Context(Context)^ do
  1711. List.ExchangeItems((Item1 - ListStartPtr) div SizeOf(TStringItem),
  1712. (Item2 - ListStartPtr) div SizeOf(TStringItem));
  1713. end;
  1714. procedure TStringList.CustomSort(CompareFn: TStringListSortCompare; SortingAlgorithm: PSortingAlgorithm);
  1715. var
  1716. Context: TStringList_CustomSort_Context;
  1717. begin
  1718. If (FCount>1) and (FForceSort or (FSortStyle<>sslAuto)) then
  1719. begin
  1720. Changing;
  1721. Context.List := Self;
  1722. Context.ListStartPtr := FList;
  1723. Context.CompareFn := CompareFn;
  1724. //if ExchangeItems is overriden call that, else call (faster) ItemListSorter_ContextComparer
  1725. if TMethod(@Self.ExchangeItems).Code = CodePointer(@TStringList.ExchangeItems) then
  1726. SortingAlgorithm^.ItemListSorter_ContextComparer(
  1727. FList, FCount, SizeOf(TStringItem), @TStringList_CustomSort_Comparer,
  1728. @Context)
  1729. else
  1730. SortingAlgorithm^.ItemListSorter_CustomItemExchanger_ContextComparer(
  1731. FList, FCount, SizeOf(TStringItem), @TStringList_CustomSort_Comparer,
  1732. @TStringList_CustomSort_Exchanger, @Context);
  1733. Changed;
  1734. end;
  1735. end;
  1736. function StringListAnsiCompare(List: TStringList; Index1, Index: Integer): Integer;
  1737. begin
  1738. Result := List.DoCompareText(List.FList^[Index1].FString,
  1739. List.FList^[Index].FString);
  1740. end;
  1741. procedure TStringList.Sort;
  1742. begin
  1743. CustomSort(@StringListAnsiCompare);
  1744. end;
  1745. procedure TStringList.Sort(SortingAlgorithm: PSortingAlgorithm);
  1746. begin
  1747. CustomSort(@StringListAnsiCompare, SortingAlgorithm);
  1748. end;
  1749. {$else}
  1750. { generics based implementation of TStringList follows }
  1751. function StringListAnsiCompare(List: TStringList; Index1, Index2: Integer): Integer;
  1752. begin
  1753. Result := List.DoCompareText(List.Strings[Index1], List.Strings[Index2]);
  1754. end;
  1755. constructor TStringList.Create;
  1756. begin
  1757. inherited;
  1758. FOwnsObjects:=false;
  1759. FMap := TFPStrObjMap.Create;
  1760. FMap.OnPtrCompare := @MapPtrCompare;
  1761. FOnCompareText := @DefaultCompareText;
  1762. NameValueSeparator:='=';
  1763. CheckSpecialChars;
  1764. end;
  1765. destructor TStringList.Destroy;
  1766. begin
  1767. FMap.Free;
  1768. inherited;
  1769. end;
  1770. function TStringList.GetDuplicates: TDuplicates;
  1771. begin
  1772. Result := FMap.Duplicates;
  1773. end;
  1774. function TStringList.GetSorted: boolean;
  1775. begin
  1776. Result := FMap.Sorted;
  1777. end;
  1778. procedure TStringList.SetDuplicates(NewDuplicates: TDuplicates);
  1779. begin
  1780. FMap.Duplicates := NewDuplicates;
  1781. end;
  1782. procedure TStringList.SetSorted(NewSorted: Boolean);
  1783. begin
  1784. FMap.Sorted := NewSorted;
  1785. end;
  1786. procedure TStringList.Changed;
  1787. begin
  1788. if FUpdateCount = 0 then
  1789. if Assigned(FOnChange) then
  1790. FOnChange(Self);
  1791. end;
  1792. procedure TStringList.Changing;
  1793. begin
  1794. if FUpdateCount = 0 then
  1795. if Assigned(FOnChanging) then
  1796. FOnChanging(Self);
  1797. end;
  1798. function TStringList.Get(Index: Integer): string;
  1799. begin
  1800. Result := FMap.Keys[Index];
  1801. end;
  1802. function TStringList.GetCapacity: Integer;
  1803. begin
  1804. Result := FMap.Capacity;
  1805. end;
  1806. function TStringList.GetCount: Integer;
  1807. begin
  1808. Result := FMap.Count;
  1809. end;
  1810. function TStringList.GetObject(Index: Integer): TObject;
  1811. begin
  1812. Result := FMap.Data[Index];
  1813. end;
  1814. procedure TStringList.Put(Index: Integer; const S: string);
  1815. begin
  1816. Changing;
  1817. FMap.Keys[Index] := S;
  1818. Changed;
  1819. end;
  1820. procedure TStringList.PutObject(Index: Integer; AObject: TObject);
  1821. begin
  1822. Changing;
  1823. FMap.Data[Index] := AObject;
  1824. Changed;
  1825. end;
  1826. procedure TStringList.SetCapacity(NewCapacity: Integer);
  1827. begin
  1828. FMap.Capacity := NewCapacity;
  1829. end;
  1830. procedure TStringList.SetUpdateState(Updating: Boolean);
  1831. begin
  1832. if Updating then
  1833. Changing
  1834. else
  1835. Changed
  1836. end;
  1837. function TStringList.Add(const S: string): Integer;
  1838. begin
  1839. Result := FMap.Add(S);
  1840. end;
  1841. procedure TStringList.Clear;
  1842. begin
  1843. if FMap.Count = 0 then exit;
  1844. Changing;
  1845. FMap.Clear;
  1846. Changed;
  1847. end;
  1848. procedure TStringList.Delete(Index: Integer);
  1849. begin
  1850. if (Index < 0) or (Index >= FMap.Count) then
  1851. Error(SListIndexError, Index);
  1852. Changing;
  1853. FMap.Delete(Index);
  1854. Changed;
  1855. end;
  1856. procedure TStringList.Exchange(Index1, Index2: Integer);
  1857. begin
  1858. if (Index1 < 0) or (Index1 >= FMap.Count) then
  1859. Error(SListIndexError, Index1);
  1860. if (Index2 < 0) or (Index2 >= FMap.Count) then
  1861. Error(SListIndexError, Index2);
  1862. Changing;
  1863. FMap.InternalExchange(Index1, Index2);
  1864. Changed;
  1865. end;
  1866. procedure TStringList.SetCaseSensitive(NewSensitive: Boolean);
  1867. begin
  1868. if NewSensitive <> FCaseSensitive then
  1869. begin
  1870. FCaseSensitive := NewSensitive;
  1871. if Sorted then
  1872. Sort;
  1873. end;
  1874. end;
  1875. function TStringList.MapPtrCompare(Key1, Key2: Pointer): Integer;
  1876. begin
  1877. Result := FOnCompareText(string(Key1^), string(Key2^));
  1878. end;
  1879. function TStringList.DefaultCompareText(const s1, s2: string): PtrInt;
  1880. begin
  1881. if FCaseSensitive then
  1882. Result := AnsiCompareStr(s1, s2)
  1883. else
  1884. Result := AnsiCompareText(s1, s2);
  1885. end;
  1886. function TStringList.DoCompareText(const s1, s2: string): PtrInt;
  1887. begin
  1888. Result := FOnCompareText(s1, s2);
  1889. end;
  1890. function TStringList.Find(const S: string; var Index: Integer): Boolean;
  1891. begin
  1892. Result := FMap.Find(S, Index);
  1893. end;
  1894. function TStringList.IndexOf(const S: string): Integer;
  1895. begin
  1896. Result := FMap.IndexOf(S);
  1897. end;
  1898. procedure TStringList.Insert(Index: Integer; const S: string);
  1899. begin
  1900. if not Sorted and (0 <= Index) and (Index < FMap.Count) then
  1901. Changing;
  1902. FMap.InsertKey(Index, S);
  1903. Changed;
  1904. end;
  1905. type
  1906. PStringList_CustomSort_Context = ^TStringList_CustomSort_Context;
  1907. TStringList_CustomSort_Context = record
  1908. List: TStringList;
  1909. ListStartPtr: Pointer;
  1910. ItemSize: SizeUInt;
  1911. IndexBase: Integer;
  1912. CompareFn: TStringListSortCompare;
  1913. end;
  1914. function TStringList_CustomSort_Comparer(Item1, Item2, Context: Pointer): Integer;
  1915. begin
  1916. with PStringList_CustomSort_Context(Context)^ do
  1917. Result := CompareFn(List,
  1918. ((Item1 - ListStartPtr) div ItemSize) + IndexBase,
  1919. ((Item2 - ListStartPtr) div ItemSize) + IndexBase);
  1920. end;
  1921. procedure TStringList_CustomSort_Exchanger(Item1, Item2, Context: Pointer);
  1922. begin
  1923. with PStringList_CustomSort_Context(Context)^ do
  1924. List.Exchange(((Item1 - ListStartPtr) div ItemSize) + IndexBase,
  1925. ((Item2 - ListStartPtr) div ItemSize) + IndexBase);
  1926. end;
  1927. procedure TStringList.QuickSort(L, R: Integer; CompareFn: TStringListSortCompare);
  1928. var
  1929. Context: TStringList_CustomSort_Context;
  1930. begin
  1931. if L > R then
  1932. exit;
  1933. Context.List := Self;
  1934. Context.ListStartPtr := FMap.Items[L];
  1935. Context.CompareFn := CompareFn;
  1936. Context.ItemSize := FMap.KeySize + FMap.DataSize;
  1937. Context.IndexBase := L;
  1938. DefaultSortingAlgorithm^.ItemListSorter_CustomItemExchanger_ContextComparer(
  1939. Context.ListStartPtr, R - L + 1, Context.ItemSize, @TStringList_CustomSort_Comparer,
  1940. @TStringList_CustomSort_Exchanger, @Context);
  1941. end;
  1942. procedure TStringList.CustomSort(CompareFn: TStringListSortCompare);
  1943. begin
  1944. if not Sorted and (FMap.Count > 1) then
  1945. begin
  1946. Changing;
  1947. QuickSort(0, FMap.Count-1, CompareFn);
  1948. Changed;
  1949. end;
  1950. end;
  1951. procedure TStringList.CustomSort(CompareFn: TStringListSortCompare; SortingAlgorithm: PSortingAlgorithm);
  1952. var
  1953. Context: TStringList_CustomSort_Context;
  1954. begin
  1955. if not Sorted and (FMap.Count > 1) then
  1956. begin
  1957. Changing;
  1958. Context.List := Self;
  1959. Context.ListStartPtr := FMap.Items[0];
  1960. Context.CompareFn := CompareFn;
  1961. Context.ItemSize := FMap.KeySize + FMap.DataSize;
  1962. Context.IndexBase := 0;
  1963. SortingAlgorithm^.ItemListSorter_CustomItemExchanger_ContextComparer(
  1964. Context.ListStartPtr, FMap.Count, Context.ItemSize, @TStringList_CustomSort_Comparer,
  1965. @TStringList_CustomSort_Exchanger, @Context);
  1966. Changed;
  1967. end;
  1968. end;
  1969. procedure TStringList.Sort;
  1970. begin
  1971. if not Sorted and (FMap.Count > 1) then
  1972. begin
  1973. Changing;
  1974. FMap.Sort;
  1975. Changed;
  1976. end;
  1977. end;
  1978. procedure TStringList.Sort(SortingAlgorithm: PSortingAlgorithm);
  1979. begin
  1980. if not Sorted and (FMap.Count > 1) then
  1981. begin
  1982. Changing;
  1983. FMap.Sort(SortingAlgorithm);
  1984. Changed;
  1985. end;
  1986. end;
  1987. {$endif}