syssb.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. {%MainUnit sysutils.pp}
  2. { TGenericStringBuilder }
  3. constructor TGenericStringBuilder.Create;
  4. begin
  5. Create(DefaultCapacity,Maxint);
  6. end;
  7. constructor TGenericStringBuilder.Create(const AValue: SBString; aCapacity: Integer);
  8. begin
  9. Create(aCapacity,Maxint);
  10. if (system.Length(AValue)>0) then
  11. Append(AValue);
  12. end;
  13. constructor TGenericStringBuilder.Create(const AValue: SBString; StartIndex, Alength,
  14. aCapacity: Integer);
  15. begin
  16. Create(Copy(AValue,StartIndex+1,Alength), aCapacity);
  17. end;
  18. constructor TGenericStringBuilder.Create(aCapacity, aMaxCapacity: Integer);
  19. begin
  20. FMaxCapacity:=aMaxCapacity;
  21. Capacity:=aCapacity;
  22. FLength:=0;
  23. end;
  24. constructor TGenericStringBuilder.Create(aCapacity: Integer);
  25. begin
  26. Create(aCapacity,MaxInt);
  27. end;
  28. constructor TGenericStringBuilder.Create(const AValue: SBString);
  29. begin
  30. Create(aValue,DefaultCapacity);
  31. end;
  32. { Enumerator }
  33. function TGenericStringBuilder.TCharEnumerator.GetCurrent: SBChar;
  34. begin
  35. Result := FCurrentPosition^;
  36. Inc(FCurrentPosition);
  37. end;
  38. function TGenericStringBuilder.TCharEnumerator.MoveNext: Boolean;
  39. begin
  40. Result := FCurrentPosition < FEndPosition;
  41. end;
  42. function TGenericStringBuilder.GetEnumerator: TCharEnumerator;
  43. var
  44. StartPosition: PSBChar;
  45. begin
  46. StartPosition := @FData[0];
  47. Result.FCurrentPosition := StartPosition;
  48. Result.FEndPosition := StartPosition + Length;
  49. end;
  50. { Property getter/setter }
  51. function TGenericStringBuilder.GetLength: Integer;
  52. begin
  53. Result:=FLength;
  54. end;
  55. function TGenericStringBuilder.GetCapacity: Integer;
  56. begin
  57. Result:=System.Length(FData);
  58. end;
  59. function TGenericStringBuilder.GetC(Index: Integer): SBChar;
  60. begin
  61. CheckNegative(Index,'Index');
  62. CheckRange(Index,0,Length);
  63. Result:=FData[Index];
  64. end;
  65. procedure TGenericStringBuilder.SetC(Index: Integer; AValue: SBChar);
  66. begin
  67. CheckNegative(Index,'Index');
  68. CheckRange(Index,0,Length-1);
  69. FData[Index]:=AValue;
  70. end;
  71. procedure TGenericStringBuilder.SetLength(AValue: Integer);
  72. begin
  73. CheckNegative(AValue,'AValue');
  74. CheckRange(AValue,0,MaxCapacity);
  75. While AValue>Capacity do
  76. Grow;
  77. Flength:=AValue;
  78. end;
  79. { Check functions }
  80. procedure TGenericStringBuilder.CheckRange(Idx, Count, MaxLen: Integer);
  81. begin
  82. if (Idx<0) or (Idx+Count>MaxLen) then
  83. Raise ERangeError.CreateFmt(SListIndexError,[Idx]);
  84. end;
  85. procedure TGenericStringBuilder.CheckNegative(const AValue: Integer;
  86. const AName: SBString);
  87. begin
  88. if (AValue<0) then
  89. Raise ERangeError.CreateFmt(SParamIsNegative,[AName])
  90. end;
  91. { These do the actual Appending/Inserting }
  92. procedure TGenericStringBuilder.DoAppend(const S: {$IFDEF SBUNICODE}SBString{$ELSE}RawByteString{$ENDIF});
  93. Var
  94. L,SL : Integer;
  95. begin
  96. SL:=System.Length(S);
  97. if SL>0 then
  98. begin
  99. L:=Length;
  100. Length:=L+SL;
  101. Move(S[1], FData[L],SL*SizeOf(SBChar));
  102. end;
  103. end;
  104. procedure TGenericStringBuilder.DoAppend(const AValue: TSBCharArray; Idx, aCount: Integer
  105. );
  106. Var
  107. L : integer;
  108. begin
  109. L:=Length;
  110. CheckRange(Idx,aCount,System.Length(AValue));
  111. Length:=L+aCount;
  112. Move(AValue[Idx],FData[L],aCount*SizeOf(SBChar));
  113. end;
  114. procedure TGenericStringBuilder.DoInsert(Index: Integer; const AValue: SBString);
  115. Var
  116. ShiftLen,LV : Integer;
  117. begin
  118. CheckRange(Index,0,Length-1);
  119. LV:=System.Length(AValue);
  120. ShiftLen:=Length-Index;
  121. Length:=Length+LV;
  122. Move(FData[Index],FData[Index+LV],ShiftLen*SizeOf(SBChar));
  123. Move(AValue[1],FData[Index],LV*SizeOf(SBChar));
  124. end;
  125. procedure TGenericStringBuilder.DoInsert(Index: Integer; const AValue: TSBCharArray;
  126. StartIndex, SBCharCount: Integer);
  127. Var
  128. ShiftLen : Integer;
  129. begin
  130. CheckRange(Index,0,Length-1);
  131. CheckNegative(StartIndex,'StartIndex');
  132. CheckNegative(SBCharCount,'SBCharCount');
  133. CheckRange(StartIndex,SBCharCount,System.Length(AValue));
  134. Length:=Length+SBCharCount;
  135. ShiftLen:=Length-Index;
  136. if ShiftLen> 0 then
  137. Move(FData[Index], FData[Index+SBCharCount],ShiftLen*SizeOf(SBChar));
  138. Move(AValue[StartIndex],FData[Index],SBCharCount*SizeOf(SBChar));
  139. end;
  140. { Public routines for appending }
  141. function TGenericStringBuilder.Append(const AValue: UInt64): TGenericStringBuilder;
  142. begin
  143. DoAppend(IntToStr(AValue));
  144. Result:=self;
  145. end;
  146. function TGenericStringBuilder.Append(const AValue: TSBCharArray): TGenericStringBuilder;
  147. var
  148. I,L: Integer;
  149. begin
  150. I:=-1;
  151. L:=System.Length(AValue);
  152. If L=0 then
  153. Exit(Self);
  154. Repeat
  155. Inc(I);
  156. Until (I>=L) or (AValue[I]=#0);
  157. DoAppend(AValue,0,I);
  158. Result:=Self;
  159. end;
  160. function TGenericStringBuilder.Append(const AValue: Single): TGenericStringBuilder;
  161. begin
  162. DoAppend(FloatToStr(AValue));
  163. Result:=self;
  164. end;
  165. function TGenericStringBuilder.Append(const AValue: Word): TGenericStringBuilder;
  166. begin
  167. Append(IntToStr(AValue));
  168. Result:=self;
  169. end;
  170. function TGenericStringBuilder.Append(const AValue: Cardinal): TGenericStringBuilder;
  171. begin
  172. DoAppend(IntToStr(AValue));
  173. Result:=self;
  174. end;
  175. function TGenericStringBuilder.Append(const AValue: SBChar; RepeatCount: Integer
  176. ): TGenericStringBuilder;
  177. begin
  178. DoAppend(StringOfChar(AValue,RepeatCount));
  179. Result:=Self;
  180. end;
  181. function TGenericStringBuilder.Append(const AValue: Shortint): TGenericStringBuilder;
  182. begin
  183. DoAppend(IntToStr(AValue));
  184. Result:=Self;
  185. end;
  186. function TGenericStringBuilder.Append(const AValue: SBChar): TGenericStringBuilder;
  187. begin
  188. DoAppend(AValue);
  189. Result:=Self;
  190. end;
  191. {$IFDEF SBUNICODE}
  192. function TGenericStringBuilder.Append(const AValue: AnsiChar): TGenericStringBuilder;
  193. begin
  194. DoAppend(WideChar(AValue));
  195. Result:=Self;
  196. end;
  197. {$ELSE}
  198. function TGenericStringBuilder.Append(const AValue: WideChar): TGenericStringBuilder;
  199. begin
  200. // We don't know what the target encoding is ?
  201. DoAppend(AnsiChar(AValue));
  202. Result:=Self;
  203. end;
  204. function TGenericStringBuilder.Append(const AValue: UnicodeString): TGenericStringBuilder;
  205. begin
  206. // We don't know what the target encoding is ?
  207. DoAppend(AnsiString(AValue));
  208. Result:=Self;
  209. end;
  210. {$ENDIF}
  211. function TGenericStringBuilder.Append(const AValue: Currency): TGenericStringBuilder;
  212. begin
  213. DoAppend(CurrToStr(AValue));
  214. Result:=Self;
  215. end;
  216. function TGenericStringBuilder.Append(const AValue: Boolean): TGenericStringBuilder;
  217. begin
  218. DoAppend(BoolToStr(AValue, True));
  219. Result:=Self;
  220. end;
  221. function TGenericStringBuilder.Append(const AValue: Byte): TGenericStringBuilder;
  222. begin
  223. DoAppend(IntToStr(AValue));
  224. Result:=Self;
  225. end;
  226. function TGenericStringBuilder.Append(const AValue: Double): TGenericStringBuilder;
  227. begin
  228. DoAppend(FloatToStr(AValue));
  229. Result:=Self;
  230. end;
  231. function TGenericStringBuilder.Append(const AValue: Int64): TGenericStringBuilder;
  232. begin
  233. DoAppend(IntToStr(AValue));
  234. Result:=Self;
  235. end;
  236. function TGenericStringBuilder.Append(const AValue: TObject): TGenericStringBuilder;
  237. begin
  238. DoAppend(AValue.ToString);
  239. Result:=Self;
  240. end;
  241. function TGenericStringBuilder.Append(const AValue: Smallint): TGenericStringBuilder;
  242. begin
  243. DoAppend(IntToStr(AValue));
  244. Result:=Self;
  245. end;
  246. function TGenericStringBuilder.Append(const AValue: LongInt): TGenericStringBuilder;
  247. begin
  248. DoAppend(IntToStr(AValue));
  249. Result:=Self;
  250. end;
  251. Function TGenericStringBuilder.Append(const AValue: TSBCharArray; StartIndex, SBCharCount: Integer): TGenericStringBuilder;
  252. begin
  253. DoAppend(AValue,StartIndex,SBCharCount);
  254. Result:=Self;
  255. end;
  256. Function TGenericStringBuilder.Append(const AValue: SBString; StartIndex, Count: Integer): TGenericStringBuilder;
  257. begin
  258. CheckRange(StartIndex,Count,System.Length(AValue));
  259. DoAppend(Copy(AValue,StartIndex+1,Count));
  260. Result:=Self;
  261. end;
  262. function TGenericStringBuilder.Append(const AValue: PSBChar): TGenericStringBuilder;
  263. begin
  264. DoAppend(AnsiString(AValue));
  265. Result:=Self;
  266. end;
  267. {$IFDEF SBUNICODE}
  268. function TGenericStringBuilder.Append(const AValue: SBString): TGenericStringBuilder;
  269. begin
  270. DoAppend(AValue);
  271. Result:=Self;
  272. end;
  273. {$ENDIF}
  274. function TGenericStringBuilder.Append(const AValue: RawByteString): TGenericStringBuilder;
  275. begin
  276. {$IFDEF SBUNICODE}
  277. DoAppend(SBString(AValue));
  278. {$ELSE}
  279. DoAppend(AValue);
  280. {$ENDIF}
  281. Result:=Self;
  282. end;
  283. function TGenericStringBuilder.AppendFormat(const Fmt: SBString;
  284. const Args: array of const): TGenericStringBuilder;
  285. begin
  286. DoAppend(Format(Fmt,Args));
  287. Result:=Self;
  288. end;
  289. function TGenericStringBuilder.Append(const Fmt: SBString;
  290. const Args: array of const): TGenericStringBuilder;
  291. begin
  292. DoAppend(Format(Fmt,Args));
  293. Result:=Self;
  294. end;
  295. function TGenericStringBuilder.AppendLine: TGenericStringBuilder;
  296. begin
  297. DoAppend(sLineBreak);
  298. Result:=Self;
  299. end;
  300. function TGenericStringBuilder.AppendLine(const AValue: RawByteString): TGenericStringBuilder;
  301. begin
  302. DoAppend(AValue);
  303. Result:=AppendLine();
  304. end;
  305. procedure TGenericStringBuilder.Clear;
  306. begin
  307. Length:=0;
  308. Capacity:=DefaultCapacity;
  309. end;
  310. procedure TGenericStringBuilder.CopyTo(SourceIndex: Integer;
  311. Var Destination: TSBCharArray; DestinationIndex: Integer; Count: Integer);
  312. begin
  313. CheckNegative(Count,'Count');
  314. CheckNegative(DestinationIndex,'DestinationIndex');
  315. CheckRange(DestinationIndex,Count,System.Length(Destination));
  316. if Count>0 then
  317. begin
  318. CheckRange(SourceIndex,Count,Length);
  319. Move(FData[SourceIndex],Destination[DestinationIndex],Count * SizeOf(SBChar));
  320. end;
  321. end;
  322. function TGenericStringBuilder.EnsureCapacity(aCapacity: Integer): Integer;
  323. begin
  324. CheckRange(aCapacity,0,MaxCapacity);
  325. if Capacity<aCapacity then
  326. Capacity:=aCapacity;
  327. Result:=Capacity;
  328. end;
  329. function TGenericStringBuilder.Equals(StringBuilder: TGenericStringBuilder): Boolean;
  330. begin
  331. Result:=(StringBuilder<>nil);
  332. if Result then
  333. Result:=(Length=StringBuilder.Length)
  334. and (MaxCapacity=StringBuilder.MaxCapacity)
  335. and CompareMem(@FData[0],@StringBuilder.FData[0],Length*SizeOf(SBChar));
  336. end;
  337. procedure TGenericStringBuilder.Grow;
  338. var
  339. NewCapacity: SizeInt;
  340. begin
  341. NewCapacity:=Capacity*2;
  342. if NewCapacity>MaxCapacity then
  343. NewCapacity:=MaxCapacity;
  344. Capacity:=NewCapacity;
  345. end;
  346. function TGenericStringBuilder.Insert(Index: Integer; const AValue: TObject
  347. ): TGenericStringBuilder;
  348. begin
  349. DoInsert(Index,AValue.ToString());
  350. Result:=Self;
  351. end;
  352. function TGenericStringBuilder.Insert(Index: Integer; const AValue: Int64
  353. ): TGenericStringBuilder;
  354. begin
  355. DoInsert(Index,IntToStr(AValue));
  356. Result:=Self;
  357. end;
  358. function TGenericStringBuilder.Insert(Index: Integer; const AValue: Single
  359. ): TGenericStringBuilder;
  360. begin
  361. DoInsert(Index,FloatToStr(AValue));
  362. Result:=Self;
  363. end;
  364. function TGenericStringBuilder.Insert(Index: Integer; const AValue: SBString
  365. ): TGenericStringBuilder;
  366. begin
  367. DoInsert(Index,AValue);
  368. Result:=Self;
  369. end;
  370. function TGenericStringBuilder.Insert(Index: Integer; const AValue: Word
  371. ): TGenericStringBuilder;
  372. begin
  373. DoInsert(Index,IntToStr(AValue));
  374. Result:=Self;
  375. end;
  376. function TGenericStringBuilder.Insert(Index: Integer; const AValue: Shortint
  377. ): TGenericStringBuilder;
  378. begin
  379. DoInsert(Index, IntToStr(AValue));
  380. Result:=Self;
  381. end;
  382. function TGenericStringBuilder.Insert(Index: Integer; const AValue: Currency
  383. ): TGenericStringBuilder;
  384. begin
  385. DoInsert(Index,CurrToStr(AValue));
  386. Result:=Self;
  387. end;
  388. function TGenericStringBuilder.Insert(Index: Integer; const AValue: SBChar
  389. ): TGenericStringBuilder;
  390. begin
  391. DoInsert(Index,AValue);
  392. Result:=Self;
  393. end;
  394. function TGenericStringBuilder.Insert(Index: Integer; const AValue: Byte
  395. ): TGenericStringBuilder;
  396. begin
  397. DoInsert(Index,IntToStr(AValue));
  398. Result:=Self;
  399. end;
  400. function TGenericStringBuilder.Insert(Index: Integer; const AValue: Double
  401. ): TGenericStringBuilder;
  402. begin
  403. DoInsert(Index,FloatToStr(AValue));
  404. Result:=Self;
  405. end;
  406. function TGenericStringBuilder.Insert(Index: Integer; const AValue: LongInt
  407. ): TGenericStringBuilder;
  408. begin
  409. DoInsert(Index,IntToStr(AValue));
  410. Result:=Self;
  411. end;
  412. function TGenericStringBuilder.Insert(Index: Integer; const AValue: Smallint
  413. ): TGenericStringBuilder;
  414. begin
  415. DoInsert(Index,IntToStr(AValue));
  416. Result:=Self;
  417. end;
  418. function TGenericStringBuilder.Insert(Index: Integer; const AValue: Boolean
  419. ): TGenericStringBuilder;
  420. begin
  421. DoInsert(Index,BoolToStr(AValue,True));
  422. Result:=Self;
  423. end;
  424. function TGenericStringBuilder.Insert(Index: Integer; const AValue: SBString;
  425. const aRepeatCount: Integer): TGenericStringBuilder;
  426. var
  427. I: Integer;
  428. begin
  429. for I:=0 to aRepeatCount-1 do
  430. DoInsert(Index,AValue);
  431. Result:=Self;
  432. end;
  433. function TGenericStringBuilder.Insert(Index: Integer; const AValue: TSBCharArray
  434. ): TGenericStringBuilder;
  435. begin
  436. DoInsert(Index,AValue,0,System.Length(AValue));
  437. Result:=Self;
  438. end;
  439. function TGenericStringBuilder.Insert(Index: Integer; const AValue: TSBCharArray;
  440. startIndex: Integer; SBCharCount: Integer): TGenericStringBuilder;
  441. begin
  442. DoInsert(Index,AValue,StartIndex,SBCharCount);
  443. Result:=Self;
  444. end;
  445. function TGenericStringBuilder.Insert(Index: Integer; const AValue: Cardinal
  446. ): TGenericStringBuilder;
  447. begin
  448. DoInsert(Index,IntToStr(AValue));
  449. Result:=self;
  450. end;
  451. function TGenericStringBuilder.Insert(Index: Integer; const AValue: UInt64
  452. ): TGenericStringBuilder;
  453. begin
  454. DoInsert(Index,IntToStr(AValue));
  455. Result:=self;
  456. end;
  457. procedure TGenericStringBuilder.Shrink;
  458. begin
  459. if (Capacity div 4)>=Length then
  460. Capacity:=Capacity div 2;
  461. end;
  462. function TGenericStringBuilder.Remove(StartIndex: Integer; RemLength: Integer
  463. ): TGenericStringBuilder;
  464. Var
  465. MoveIndex : Integer;
  466. begin
  467. if (RemLength=0) then
  468. exit(Self);
  469. CheckNegative(RemLength,'RemLength');
  470. CheckRange(StartIndex,0,Length);
  471. MoveIndex:=StartIndex+RemLength;
  472. CheckRange(MoveIndex,0,Length);
  473. if (Length-Moveindex)>0 then
  474. Move(FData[MoveIndex],FData[StartIndex],(Length-MoveIndex)*SizeOf(SBChar));
  475. Length:=Length-RemLength;
  476. Shrink;
  477. Result:=Self;
  478. end;
  479. Function TGenericStringBuilder.Replace(const OldValue, NewValue: SBRawString; StartIndex, Count: Integer): TGenericStringBuilder;
  480. var
  481. Cur : PSBChar;
  482. CurIndex,MaxIndex : Integer;
  483. OldLen, NewLen, Delta : Integer;
  484. BC : SBChar;
  485. begin
  486. if Count=0 then
  487. Exit(Self);
  488. // Some checks.
  489. CheckNegative(StartIndex,'StartIndex');
  490. CheckNegative(Count,'Count');
  491. CheckRange(Startindex,Count,Length);
  492. // Init
  493. OldLen:=System.Length(OldValue);
  494. NewLen:=System.Length(NewValue);
  495. Delta:=NewLen-OldLen;
  496. MaxIndex:=StartIndex+Count;
  497. CurIndex:=StartIndex;
  498. BC:=OldValue[1];
  499. Cur:=@FData[StartIndex];
  500. // Loop
  501. while (CurIndex<Length-OldLen+1) and (CurIndex<MaxIndex) do
  502. begin
  503. if (Cur^=BC) then
  504. begin
  505. if 0=StrLComp(@FData[CurIndex],PSBChar(OldValue),OldLen) then
  506. begin
  507. // Do actual replace.
  508. DoReplace(CurIndex,OldValue,NewValue);
  509. Inc(CurIndex,NewLen-1);
  510. // DoReplace may have reallocated memory, so changed pointers, reset pointer
  511. Cur:=@FData[CurIndex];
  512. // The max index must be increased/decreased with Delta
  513. // 0123456789012
  514. // 'zzbczzedeafzz' replace('e','qqqq',6,3) -> zzbczzqqqqdqqqqafzz
  515. Inc(MaxIndex,Delta);
  516. end;
  517. end;
  518. Inc(CurIndex);
  519. Inc(Cur);
  520. end;
  521. Result:=Self;
  522. end;
  523. Function TGenericStringBuilder.Replace(const OldChar, NewChar: SBChar; StartIndex,
  524. Count: Integer): TGenericStringBuilder;
  525. var
  526. I : Integer;
  527. Cur : PSBChar;
  528. begin
  529. if Count=0 then
  530. Exit(Self);
  531. CheckNegative(StartIndex,'StartIndex');
  532. CheckNegative(Count,'Count');
  533. CheckRange(StartIndex,Count-1,Length);
  534. Cur:=@FData[StartIndex];
  535. For I:=1 to Count do
  536. begin
  537. if Cur^=OldChar then
  538. Cur^:=NewChar;
  539. Inc(Cur);
  540. end;
  541. Result:=Self;
  542. end;
  543. Function TGenericStringBuilder.Replace(const OldChar, NewChar: SBChar): TGenericStringBuilder;
  544. begin
  545. Result:=Replace(OldChar,NewChar,0,Length);
  546. end;
  547. Function TGenericStringBuilder.Replace(const OldValue, NewValue: SBRawString): TGenericStringBuilder;
  548. begin
  549. Result:=Replace(OldValue,NewValue,0,Length);
  550. end;
  551. procedure TGenericStringBuilder.SetCapacity(AValue: Integer);
  552. begin
  553. if (AValue>FMaxCapacity) then
  554. Raise ERangeError.CreateFmt(SListCapacityError,[AValue]);
  555. if (AValue<Length) then
  556. Raise ERangeError.CreateFmt(SListCapacityError,[AValue]);
  557. System.SetLength(FData,AValue);
  558. end;
  559. {$IFDEF UNICODERTL}
  560. function TGenericStringBuilder.ToString: RTLString;
  561. {$ELSE}
  562. function TGenericStringBuilder.ToString: SBString;
  563. {$ENDIF}
  564. begin
  565. Result:=ToString(0,Length);
  566. end;
  567. function TGenericStringBuilder.ToString(aStartIndex: Integer; aLength: Integer
  568. ): SBString;
  569. begin
  570. if (aLength=0) then
  571. Result:=''
  572. else
  573. begin
  574. CheckNegative(aStartIndex,'aStartIndex');
  575. CheckNegative(aLength,'aLength');
  576. CheckRange(aStartIndex,aLength,Length);
  577. System.SetLength(Result,aLength);
  578. Move(FData[aStartIndex],Result[1],aLength*SizeOf(SBChar));
  579. end;
  580. end;
  581. {$IFDEF UNICODERTL}
  582. function TGenericStringBuilder.ToString(UpdateCapacity: Boolean): RTLString;
  583. {$ELSE}
  584. function TGenericStringBuilder.ToString(UpdateCapacity: Boolean): SBString;
  585. {$ENDIF}
  586. begin
  587. if (Length<>Capacity) and UpdateCapacity then
  588. SetCapacity(Length);
  589. Result:=ToString;
  590. end;
  591. procedure TGenericStringBuilder.DoReplace(Index: Integer; const Old, New: SBString);
  592. var
  593. NVLen,OVLen,OLen,Delta,TailStart: Integer;
  594. begin
  595. NVLen:=System.Length(New);
  596. OVLen:=System.Length(Old);
  597. Delta:=NVLen-OVLen;
  598. if (Delta<>0) then
  599. begin
  600. OLen:=Length;
  601. if (Delta>0) then
  602. Length:=OLen+Delta;
  603. TailStart:=Index+OVlen;
  604. Move(FData[TailStart],FData[Index+NVLen],(OLen-TailStart)*SizeOf(SBChar));
  605. if (Delta<0) then
  606. Length:=OLen+Delta;
  607. end;
  608. Move(New[1],FData[Index],NVLen*SizeOf(SBChar));
  609. end;