zoukankan      html  css  js  c++  java
  • TstringBuilder Delphi2007版

           2010中的StringBuilder对象用的比较爽快!于是稍作了一些修改(增加了几个函数和属性)然后移植到D2007中来使用了!效果不错,共享一下!

            

    [delphi] view plain copy
     
    1. unit DxStringBuilder;  
    2.   
    3. interface  
    4. uses RTLConsts,Classes,SysUtils;  
    5. type  
    6.   EExternal = class(Exception)  
    7.   public  
    8. {$IFDEF MSWINDOWS}  
    9.     ExceptionRecord: PExceptionRecord platform;  
    10. {$ENDIF}  
    11. {$IF defined(LINUX) or defined(MACOSX)}  
    12.     ExceptionAddress: LongWord platform;  
    13.     AccessAddress: LongWord platform;  
    14.     SignalNumber: Integer platform;  
    15. {$IFEND LINUX or MACOSX}  
    16.   end;  
    17.     
    18.   EIntError = class(EExternal);  
    19.   ERangeError = class(EIntError);  
    20.   TCharArray = array of Char;  
    21.   TStringBuilder = class  
    22.   private  
    23.     const DefaultCapacity = $10;  
    24.     function GetCapacity: Integer;  
    25.     procedure SetCapacity(const Value: Integer);  
    26.     function GetLength: Integer;  
    27.     procedure Set_Length(const Value: Integer);  
    28.     function GetMaxCapacity: Integer;  
    29.   
    30.     procedure ReduceCapacity;  
    31.     procedure ExpandCapacity;  
    32.     procedure CheckBounds(Index: Integer);  
    33.     function _Replace(Index: Integer; const Old, New: string): Boolean;  
    34.     function GetChars(index: Integer): Char;  
    35.     procedure SetChars(index: Integer; const Value: Char);  
    36.   protected  
    37.     FData: TCharArray;  
    38.     FLength: Integer;  
    39.     FMaxCapacity: Integer;  
    40.   public  
    41.     constructor Create; overload;  
    42.     constructor Create(aCapacity: Integer); overload;  
    43.     constructor Create(const Value: string); overload;  
    44.     function Append(const Value: Boolean): TStringBuilder; overload;  
    45.     function Append(const Value: Byte): TStringBuilder; overload;  
    46.     function Append(const Value: Char): TStringBuilder; overload;  
    47.     function Append(const Value: Currency): TStringBuilder; overload;  
    48.     function Append(const Value: Double): TStringBuilder; overload;  
    49.     function Append(const Value: Smallint): TStringBuilder; overload;  
    50.     function Append(const Value: Integer): TStringBuilder; overload;  
    51.     function Append(const Value: Int64): TStringBuilder; overload;  
    52.     function Append(const Value: TObject): TStringBuilder; overload;  
    53.     function Append(const Value: Shortint): TStringBuilder; overload;  
    54.     function Append(const Value: Single): TStringBuilder; overload;  
    55.     function Append(const Value: string): TStringBuilder; overload;  
    56.     function Append(const Value: UInt64): TStringBuilder; overload;  
    57.     function Append(const Value: TCharArray): TStringBuilder; overload;  
    58.     function Append(const Value: Word): TStringBuilder; overload;  
    59.     function Append(const Value: Cardinal): TStringBuilder; overload;  
    60.     function Append(const Value: Char; RepeatCount: Integer): TStringBuilder; overload;  
    61.     function Append(const Value: TCharArray; StartIndex: Integer; CharCount: Integer): TStringBuilder; overload;  
    62.     function Append(const Value: string; StartIndex: Integer; Count: Integer): TStringBuilder; overload;  
    63.   
    64.     function AppendFormat(const Format: string; const Args: array of const): TStringBuilder; overload;  
    65.     function AppendLine: TStringBuilder; overload;  
    66.     function AppendLine(const Value: string): TStringBuilder; overload;  
    67.   
    68.     procedure Clear;  
    69.     procedure CopyTo(SourceIndex: Integer; const Destination: TCharArray; DestinationIndex: Integer; Count: Integer);  
    70.     function EnsureCapacity(aCapacity: Integer): Integer;  
    71.     function Equals(StringBuilder: TStringBuilder): Boolean; reintroduce;  
    72.   
    73.     function Insert(Index: Integer; const Value: Boolean): TStringBuilder; overload;  
    74.     function Insert(Index: Integer; const Value: Byte): TStringBuilder; overload;  
    75.     function Insert(Index: Integer; const Value: Char): TStringBuilder; overload;  
    76.     function Insert(Index: Integer; const Value: Currency): TStringBuilder; overload;  
    77.     function Insert(Index: Integer; const Value: Double): TStringBuilder; overload;  
    78.     function Insert(Index: Integer; const Value: Smallint): TStringBuilder; overload;  
    79.     function Insert(Index: Integer; const Value: Integer): TStringBuilder; overload;  
    80.     function Insert(Index: Integer; const Value: TCharArray): TStringBuilder; overload;  
    81.     function Insert(Index: Integer; const Value: Int64): TStringBuilder; overload;  
    82.     function Insert(Index: Integer; const Value: TObject): TStringBuilder; overload;  
    83.     function Insert(Index: Integer; const Value: Shortint): TStringBuilder; overload;  
    84.     function Insert(Index: Integer; const Value: Single): TStringBuilder; overload;  
    85.     function Insert(Index: Integer; const Value: string): TStringBuilder; overload;  
    86.     function Insert(Index: Integer; const Value: Word): TStringBuilder; overload;  
    87.     function Insert(Index: Integer; const Value: Cardinal): TStringBuilder; overload;  
    88.     function Insert(Index: Integer; const Value: UInt64): TStringBuilder; overload;  
    89.     function Insert(Index: Integer; const Value: string; count: Integer): TStringBuilder; overload;  
    90.     function Insert(Index: Integer; const Value: TCharArray; startIndex: Integer; charCount: Integer): TStringBuilder; overload;  
    91.   
    92.     function Remove(StartIndex: Integer; RemLength: Integer): TStringBuilder;  
    93.   
    94.     function Replace(const OldChar: Char; const NewChar: Char): TStringBuilder; overload;  
    95.     function Replace(const OldValue: string; const NewValue: string): TStringBuilder; overload;  
    96.     function Replace(const OldChar: Char; const NewChar: Char; StartIndex: Integer; Count: Integer): TStringBuilder; overload;  
    97.     function Replace(const OldValue: string; const NewValue: string; StartIndex: Integer; Count: Integer): TStringBuilder; overload;  
    98.     function ToString: string; overload;  
    99.     function ToString(StartIndex: Integer; StrLength: Integer): string; reintroduce; overload;  
    100.   
    101.     procedure SaveToStream(Stream: TStream);  
    102.     procedure SaveToFile(FileName: string);  
    103.     procedure LoadFromStream(Stream: TStream);  
    104.     procedure LoadFromFile(FileName: string);  
    105.   
    106.     property Capacity: Integer read GetCapacity write SetCapacity;  
    107.     property Chars[index: Integer]: Char read GetChars write SetChars; default;  
    108.     property Length: Integer read GetLength write Set_Length;  
    109.     property MaxCapacity: Integer read GetMaxCapacity;  
    110.   end;  
    111.   
    112.   
    113. function UIntToStr(Value: Cardinal): string; overload;  
    114. function UIntToStr(Value: UInt64): string; overload;  
    115.   
    116. resourcestring  
    117.   SParamIsNegative = 'Parameter %s cannot be a negative value';  
    118.   SInputBufferExceed = 'Input buffer exceeded for %s = %d, %s = %d';  
    119. implementation  
    120.   
    121. function UIntToStr(Value: Cardinal): string;  
    122. begin  
    123.   FmtStr(Result, '%u', [Value]);  
    124. end;  
    125.   
    126. function UIntToStr(Value: UInt64): string;  
    127. begin  
    128.   FmtStr(Result, '%u', [Value]);  
    129. end;  
    130.   
    131. { TStringBuilder }  
    132.   
    133. constructor TStringBuilder.Create;  
    134. begin  
    135.   inherited Create;  
    136.   FMaxCapacity := MaxInt;  
    137.   Capacity := DefaultCapacity;  
    138.   FLength := 0;  
    139. end;  
    140.   
    141. constructor TStringBuilder.Create(aCapacity: Integer);  
    142. begin  
    143.   inherited Create;  
    144.   FMaxCapacity := MaxInt;  
    145.   Capacity := aCapacity;  
    146.   FLength := 0;  
    147. end;  
    148.   
    149. function TStringBuilder.Append(const Value: string): TStringBuilder;  
    150. begin  
    151.   Length := Length + System.Length(Value);  
    152.   Move(PChar(Value)^, FData[Length - System.Length(Value)], System.Length(Value) * SizeOf(Char));  
    153.   Result := self;  
    154. end;  
    155.   
    156. function TStringBuilder.Append(const Value: Currency): TStringBuilder;  
    157. begin  
    158.   Append(CurrToStr(Value));  
    159.   Result := Self;  
    160. end;  
    161.   
    162. function TStringBuilder.Append(const Value: Double): TStringBuilder;  
    163. begin  
    164.   Append(FloatToStr(Value));  
    165.   Result := Self;  
    166. end;  
    167.   
    168. function TStringBuilder.Append(const Value: Char): TStringBuilder;  
    169. begin  
    170.   Length := Length + 1;  
    171.   FData[Length - 1] := Value;  
    172.   Result := Self;  
    173. end;  
    174.   
    175. function TStringBuilder.Append(const Value: Boolean): TStringBuilder;  
    176. begin  
    177.   Append(BoolToStr(Value, True));  
    178.   Result := Self;  
    179. end;  
    180.   
    181. function TStringBuilder.Append(const Value: Byte): TStringBuilder;  
    182. begin  
    183.   Append(IntToStr(Value));  
    184.   Result := Self;  
    185. end;  
    186.   
    187. function TStringBuilder.Append(const Value: Smallint): TStringBuilder;  
    188. begin  
    189.   Append(IntToStr(Value));  
    190.   Result := Self;  
    191. end;  
    192.   
    193. function TStringBuilder.Append(const Value: Shortint): TStringBuilder;  
    194. begin  
    195.   Append(IntToStr(Value));  
    196.   Result := Self;  
    197. end;  
    198.   
    199. function TStringBuilder.Append(const Value: Single): TStringBuilder;  
    200. begin  
    201.   Append(FloatToStr(Value));  
    202.   Result := self;  
    203. end;  
    204.   
    205. function TStringBuilder.Append(const Value: TObject): TStringBuilder;  
    206. begin  
    207. {$if CompilerVersion >= 19}  
    208.   Append(Value.ToString());  
    209. {$else}  
    210.   if Value.InheritsFrom(TComponent) then  
    211.     Append(TComponent(Value).Name+': '+Value.ClassName)  
    212.   else Append(Value.ClassName);  
    213. {$ifend}  
    214.   Result := Self;  
    215. end;  
    216.   
    217. function TStringBuilder.Append(const Value: Integer): TStringBuilder;  
    218. begin  
    219.   Append(IntToStr(Value));  
    220.   Result := Self;  
    221. end;  
    222.   
    223. function TStringBuilder.Append(const Value: Int64): TStringBuilder;  
    224. begin  
    225.   Append(IntToStr(Value));  
    226.   Result := Self;  
    227. end;  
    228.   
    229. procedure TStringBuilder.CheckBounds(Index: Integer);  
    230. begin  
    231.   if Cardinal(Index) >= Cardinal(Length) then  
    232.     raise ERangeError.CreateResFmt(@SListIndexError, [Index]);  
    233. end;  
    234.   
    235. procedure TStringBuilder.Clear;  
    236. begin  
    237.   Length := 0;  
    238.   Capacity := DefaultCapacity;  
    239. end;  
    240.   
    241. procedure TStringBuilder.CopyTo(SourceIndex: Integer;  
    242.   const Destination: TCharArray; DestinationIndex, Count: Integer);  
    243. begin  
    244.   if Count < then  
    245.     raise ERangeError.CreateResFmt(@SParamIsNegative, ['Count']); // DO NOT LOCALIZE  
    246.   if DestinationIndex < then  
    247.     raise ERangeError.CreateResFmt(@SParamIsNegative, ['DestinationIndex']); // DO NOT LOCALIZE  
    248.   if DestinationIndex + Count > System.Length(Destination) then  
    249.     raise ERangeError.CreateResFmt(@SInputBufferExceed,  
    250.       ['DestinationIndex', DestinationIndex, 'Count', Count]);  
    251.   
    252.   if Count > then  
    253.   begin  
    254.     CheckBounds(SourceIndex);  
    255.     CheckBounds(SourceIndex + Count - 1);  
    256.   
    257.     Move(FData[SourceIndex], Destination[DestinationIndex], Count * SizeOf(Char));  
    258.   end;  
    259. end;  
    260.   
    261. constructor TStringBuilder.Create(const Value: string);  
    262. begin  
    263.   Create;  
    264.   Append(Value);  
    265. end;  
    266.   
    267. function TStringBuilder.EnsureCapacity(aCapacity: Integer): Integer;  
    268. begin  
    269.   if Cardinal(aCapacity) > Cardinal(MaxCapacity) then  
    270.     raise ERangeError.CreateResFmt(@SListIndexError, [aCapacity]);  
    271.   
    272.   if Capacity < aCapacity then  
    273.     Capacity := aCapacity;  
    274.   
    275.   Result := Capacity;  
    276. end;  
    277.   
    278. function TStringBuilder.Equals(StringBuilder: TStringBuilder): Boolean;  
    279. begin  
    280.   Result := (StringBuilder <> nil) and (Length = StringBuilder.Length) and  
    281.     (MaxCapacity = StringBuilder.MaxCapacity) and  
    282.     CompareMem(@FData[0], @StringBuilder.FData[0], Length * SizeOf(Char));  
    283. end;  
    284.   
    285. procedure TStringBuilder.ExpandCapacity;  
    286. var  
    287.   NewCapacity: Integer;  
    288. begin  
    289.   NewCapacity := Capacity * 2;  
    290.   if Length > NewCapacity then  
    291.     NewCapacity := Length * 2; // this line may overflow NewCapacity to a negative value  
    292.   if NewCapacity > MaxCapacity then  
    293.     NewCapacity := MaxCapacity;  
    294.   if NewCapacity < then // if NewCapacity has been overflowed  
    295.     NewCapacity := Length;  
    296.   Capacity := NewCapacity;  
    297. end;  
    298.   
    299. function TStringBuilder.GetCapacity: Integer;  
    300. begin  
    301.   Result := System.Length(FData);  
    302. end;  
    303.   
    304. function TStringBuilder.GetChars(index: Integer): Char;  
    305. begin  
    306.   if Index < then  
    307.     raise ERangeError.CreateResFmt(@SParamIsNegative, ['Index']); // DO NOT LOCALIZE  
    308.   CheckBounds(Index);  
    309.   
    310.   Result := FData[Index];  
    311. end;  
    312.   
    313. function TStringBuilder.GetLength: Integer;  
    314. begin  
    315.   Result := FLength;  
    316. end;  
    317.   
    318. function TStringBuilder.GetMaxCapacity: Integer;  
    319. begin  
    320.   Result := FMaxCapacity;  
    321. end;  
    322.   
    323. function TStringBuilder.Insert(Index: Integer;  
    324.   const Value: Integer): TStringBuilder;  
    325. begin  
    326.   Insert(Index, IntToStr(Value));  
    327.   Result := Self;  
    328. end;  
    329.   
    330. function TStringBuilder.Insert(Index: Integer;  
    331.   const Value: Smallint): TStringBuilder;  
    332. begin  
    333.   Insert(Index, IntToStr(Value));  
    334.   Result := Self;  
    335. end;  
    336.   
    337. function TStringBuilder.Insert(Index: Integer;  
    338.   const Value: Int64): TStringBuilder;  
    339. begin  
    340.   Insert(Index, IntToStr(Value));  
    341.   Result := Self;  
    342. end;  
    343.   
    344. function TStringBuilder.Insert(Index: Integer;  
    345.   const Value: TCharArray): TStringBuilder;  
    346. begin  
    347.   if Index < then  
    348.     raise ERangeError.CreateResFmt(@SParamIsNegative, ['Index']); // DO NOT LOCALIZE  
    349.   if Index > Length then  
    350.     raise ERangeError.CreateResFmt(@SListIndexError, [Index]);  
    351.   
    352.   Length := Length + System.Length(Value);  
    353.   Move(FData[Index], FData[Index + System.Length(Value)], System.Length(Value) * SizeOf(Char));  
    354.   Move(Value[0], FData[Index], System.Length(Value) * SizeOf(Char));  
    355.   Result := Self;  
    356. end;  
    357.   
    358. function TStringBuilder.Insert(Index: Integer;  
    359.   const Value: Double): TStringBuilder;  
    360. begin  
    361.   Insert(Index, FloatToStr(Value));  
    362.   Result := Self;  
    363. end;  
    364.   
    365. function TStringBuilder.Insert(Index: Integer;  
    366.   const Value: Byte): TStringBuilder;  
    367. begin  
    368.   Insert(Index, IntToStr(Value));  
    369.   Result := Self;  
    370. end;  
    371.   
    372. function TStringBuilder.Insert(Index: Integer;  
    373.   const Value: Boolean): TStringBuilder;  
    374. begin  
    375.   Insert(Index, BoolToStr(Value, True));  
    376.   Result := Self;  
    377. end;  
    378.   
    379. function TStringBuilder.Insert(Index: Integer;  
    380.   const Value: Currency): TStringBuilder;  
    381. begin  
    382.   Insert(Index, CurrToStr(Value));  
    383.   Result := Self;  
    384. end;  
    385.   
    386. function TStringBuilder.Insert(Index: Integer;  
    387.   const Value: Char): TStringBuilder;  
    388. begin  
    389.   if Index < then  
    390.     raise ERangeError.CreateResFmt(@SParamIsNegative, ['Index']); // DO NOT LOCALIZE  
    391.   if Index > Length then  
    392.     raise ERangeError.CreateResFmt(@SListIndexError, [Index]);  
    393.   
    394.   Length := Length + 1;  
    395.   Move(FData[Index], FData[Index + 1], (Length - Index - 1) * SizeOf(Char));  
    396.   FData[Index] := Value;  
    397.   Result := Self;  
    398. end;  
    399.   
    400. function TStringBuilder.Insert(Index: Integer;  
    401.   const Value: UInt64): TStringBuilder;  
    402. begin  
    403.   Insert(Index, IntToStr(Value));  
    404.   Result := self;  
    405. end;  
    406.   
    407. function TStringBuilder.Insert(Index: Integer;  
    408.   const Value: Cardinal): TStringBuilder;  
    409. begin  
    410.   Insert(Index, IntToStr(Value));  
    411.   Result := self;  
    412. end;  
    413.   
    414. function TStringBuilder.Insert(Index: Integer; const Value: TCharArray;  
    415.   startIndex, charCount: Integer): TStringBuilder;  
    416. begin  
    417.   if Index - 1 >= Length then  
    418.     raise ERangeError.CreateResFmt(@SListIndexError, [Index])  
    419.   else if Index < then  
    420.     raise ERangeError.CreateResFmt(@SListIndexError, [Index]);  
    421.   if StartIndex < then  
    422.     raise ERangeError.CreateResFmt(@SParamIsNegative, ['StartIndex']); // DO NOT LOCALIZE  
    423.   if CharCount < then  
    424.     raise ERangeError.CreateResFmt(@SParamIsNegative, ['CharCount']); // DO NOT LOCALIZE  
    425.   if StartIndex + CharCount > System.Length(Value) then  
    426.     raise ERangeError.CreateResFmt(@SInputBufferExceed,  
    427.       ['StartIndex', StartIndex, 'CharCount', CharCount]);  
    428.   
    429.   Length := Length + CharCount;  
    430.   
    431.   if Length - Index > then  
    432.     Move(FData[Index], FData[Index + CharCount], (Length - Index) * SizeOf(Char));  
    433.   Move(Value[StartIndex], FData[Index], CharCount * SizeOf(Char));  
    434.   Result := Self;  
    435. end;  
    436.   
    437. procedure TStringBuilder.LoadFromFile(FileName: string);  
    438. var  
    439.   F: TFileStream;  
    440. begin  
    441.   F := TFileStream.Create(FileName,fmOpenRead);  
    442.   LoadFromStream(F);  
    443.   F.Free;  
    444. end;  
    445.   
    446. procedure TStringBuilder.LoadFromStream(Stream: TStream);  
    447. begin  
    448.   Capacity := Stream.Size;  
    449.   Stream.Position := 0;  
    450.   Stream.ReadBuffer(FData[0],Stream.Size);  
    451.   Length := Stream.Size;  
    452. end;  
    453.   
    454. procedure TStringBuilder.ReduceCapacity;  
    455. var  
    456.   NewCapacity: Integer;  
    457. begin  
    458.   if Length > Capacity div then  
    459.     Exit;  
    460.   
    461.   NewCapacity := Capacity div 2;  
    462.   if NewCapacity < Length then  
    463.     NewCapacity := Length;  
    464.   Capacity := NewCapacity;  
    465. end;  
    466.   
    467. function TStringBuilder.Remove(StartIndex, RemLength: Integer): TStringBuilder;  
    468. begin  
    469.   if RemLength <> then  
    470.   begin  
    471.     if StartIndex < then  
    472.       raise ERangeError.CreateResFmt(@SParamIsNegative, ['StartIndex']); // DO NOT LOCALIZE  
    473.     if RemLength < then  
    474.       raise ERangeError.CreateResFmt(@SParamIsNegative, ['RemLength']); // DO NOT LOCALIZE  
    475.     CheckBounds(StartIndex);  
    476.     CheckBounds(StartIndex + RemLength - 1);  
    477.   
    478.     if (Length - (StartIndex + RemLength)) > then  
    479.       Move(FData[StartIndex + RemLength], FData[StartIndex], (Length - (StartIndex + RemLength)) * SizeOf(Char));  
    480.     Length := Length - RemLength;  
    481.   
    482.     ReduceCapacity;  
    483.   end;  
    484.   Result := Self;  
    485. end;  
    486.   
    487. function TStringBuilder.Replace(const OldValue,  
    488.   NewValue: string): TStringBuilder;  
    489. begin  
    490.   Result := self;  
    491.   Replace(OldValue, NewValue, 0, Length);  
    492. end;  
    493.   
    494. function TStringBuilder.Replace(const OldChar, NewChar: Char): TStringBuilder;  
    495. var  
    496.   Ptr: PChar;  
    497.   EndPtr: PChar;  
    498. begin  
    499.   EndPtr := @FData[Length - 1];  
    500.   Ptr := @FData[0];  
    501.   while Ptr <= EndPtr do  
    502.   begin  
    503.     if Ptr^ = OldChar then  
    504.       Ptr^ := NewChar;  
    505.     Inc(Ptr);  
    506.   end;  
    507.   Result := Self;  
    508. end;  
    509.   
    510. function TStringBuilder.Replace(const OldValue, NewValue: string; StartIndex,  
    511.   Count: Integer): TStringBuilder;  
    512. var  
    513.   CurPtr: PChar;  
    514.   EndPtr: PChar;  
    515.   Index: Integer;  
    516.   EndIndex: Integer;  
    517.   oldLen, newLen: Integer;  
    518. begin  
    519.   Result := Self;  
    520.   
    521.   if Count <> then  
    522.   begin  
    523.     if StartIndex < then  
    524.       raise ERangeError.CreateResFmt(@SParamIsNegative, ['StartIndex']); // DO NOT LOCALIZE  
    525.     if Count < then  
    526.       raise ERangeError.CreateResFmt(@SParamIsNegative, ['Count']); // DO NOT LOCALIZE  
    527.     if StartIndex + Count > Length then  
    528.       raise ERangeError.CreateResFmt(@SInputBufferExceed,  
    529.         ['StartIndex', StartIndex, 'Count', Count]);  
    530.   
    531.     oldLen := System.Length(OldValue);  
    532.     newLen := System.Length(NewValue);  
    533.     Index := StartIndex;  
    534.     CurPtr := @FData[StartIndex];  
    535.     EndIndex := StartIndex + Count - oldLen;  
    536.     EndPtr := @FData[EndIndex];  
    537.   
    538.     while CurPtr <= EndPtr do  
    539.     begin  
    540.       if CurPtr^ = OldValue[1] then  
    541.       begin  
    542.         if StrLComp(CurPtr, PChar(OldValue), oldLen) = then  
    543.         begin  
    544.           if _Replace(Index, OldValue, NewValue) then  
    545.           begin  
    546.             CurPtr := @FData[Index];  
    547.             EndPtr := @FData[EndIndex];  
    548.           end;  
    549.           Inc(CurPtr, newLen - 1);  
    550.           Inc(Index, newLen - 1);  
    551.           Inc(EndPtr, newLen - oldLen);  
    552.           Inc(EndIndex, newLen - oldLen);  
    553.         end;  
    554.       end;  
    555.   
    556.       Inc(CurPtr);  
    557.       Inc(Index);  
    558.     end;  
    559.   end;  
    560. end;  
    561.   
    562. function TStringBuilder.Replace(const OldChar, NewChar: Char; StartIndex,  
    563.   Count: Integer): TStringBuilder;  
    564. var  
    565.   Ptr: PChar;  
    566.   EndPtr: PChar;  
    567. begin  
    568.   if Count <> then  
    569.   begin  
    570.     if StartIndex < then  
    571.       raise ERangeError.CreateResFmt(@SParamIsNegative, ['StartIndex']); // DO NOT LOCALIZE  
    572.     if Count < then  
    573.       raise ERangeError.CreateResFmt(@SParamIsNegative, ['Count']); // DO NOT LOCALIZE  
    574.     CheckBounds(StartIndex);  
    575.     CheckBounds(StartIndex + Count - 1);  
    576.   
    577.     EndPtr := @FData[StartIndex + Count - 1];  
    578.     Ptr := @FData[StartIndex];  
    579.     while Ptr <= EndPtr do  
    580.     begin  
    581.       if Ptr^ = OldChar then  
    582.         Ptr^ := NewChar;  
    583.       Inc(Ptr);  
    584.     end;  
    585.   end;  
    586.   Result := Self;  
    587. end;  
    588.   
    589. function TStringBuilder.Insert(Index: Integer; const Value: string;  
    590.   count: Integer): TStringBuilder;  
    591. var  
    592.   I: Integer;  
    593. begin  
    594.   for I := to Count - do  
    595.     Insert(Index, Value);  
    596.   Result := Self;  
    597. end;  
    598.   
    599. function TStringBuilder.Insert(Index: Integer;  
    600.   const Value: Word): TStringBuilder;  
    601. begin  
    602.   Insert(Index, IntToStr(Value));  
    603.   Result := Self;  
    604. end;  
    605.   
    606. function TStringBuilder.Insert(Index: Integer;  
    607.   const Value: Shortint): TStringBuilder;  
    608. begin  
    609.   Insert(Index, IntToStr(Value));  
    610.   Result := Self;  
    611. end;  
    612.   
    613. function TStringBuilder.Insert(Index: Integer;  
    614.   const Value: TObject): TStringBuilder;  
    615. begin  
    616. {$if CompilerVersion >= 19}  
    617.   Insert(Index, Value.ToString());  
    618. {$else}  
    619.   Insert(Index, IntToStr(Integer(Value)));  
    620. {$ifend}  
    621.   Result := Self;  
    622. end;  
    623.   
    624. function TStringBuilder.Insert(Index: Integer;  
    625.   const Value: string): TStringBuilder;  
    626. begin  
    627.   if Index < then  
    628.     raise ERangeError.CreateResFmt(@SParamIsNegative, ['Index']); // DO NOT LOCALIZE  
    629.   if Index > Length then  
    630.     raise ERangeError.CreateResFmt(@SListIndexError, [Index]);  
    631.   
    632.   Length := Length + System.Length(Value);  
    633.   Move(FData[Index], FData[Index + System.Length(Value)], (Length - System.Length(Value) - Index) * SizeOf(Char));  
    634.   Move(Value[1], FData[Index], System.Length(Value) * SizeOf(Char));  
    635.   Result := Self;  
    636. end;  
    637.   
    638. function TStringBuilder.Insert(Index: Integer;  
    639.   const Value: Single): TStringBuilder;  
    640. begin  
    641.   Insert(Index, FloatToStr(Value));  
    642.   Result := Self;  
    643. end;  
    644.   
    645. procedure TStringBuilder.SaveToFile(FileName: string);  
    646. var  
    647.   FileStream: TFileStream;  
    648. begin  
    649.   FileStream := TFileStream.Create(FileName,fmOpenWrite);  
    650.   SaveToStream(FileStream);  
    651.   FileStream.Free;  
    652. end;  
    653.   
    654. procedure TStringBuilder.SaveToStream(Stream: TStream);  
    655. begin  
    656.   Stream.WriteBuffer(FData[0],Length * SizeOf(Char));  
    657. end;  
    658.   
    659. procedure TStringBuilder.SetCapacity(const Value: Integer);  
    660. begin  
    661.   if Value < Length then  
    662.     raise ERangeError.CreateResFmt(@SListCapacityError, [Value]);  
    663.   if Value > FMaxCapacity then  
    664.     raise ERangeError.CreateResFmt(@SListCapacityError, [Value]);  
    665.       
    666.   SetLength(FData, Value);  
    667. end;  
    668.   
    669. procedure TStringBuilder.SetChars(index: Integer; const Value: Char);  
    670. begin  
    671.   if Index < then  
    672.     raise ERangeError.CreateResFmt(@SParamIsNegative, ['Index']); // DO NOT LOCALIZE  
    673.   CheckBounds(Index);  
    674.   
    675.   FData[Index] := Value;  
    676. end;  
    677.   
    678. procedure TStringBuilder.Set_Length(const Value: Integer);  
    679. var  
    680.   LOldLength: Integer;  
    681. begin  
    682.   if Value < then  
    683.     raise ERangeError.CreateResFmt(@SParamIsNegative, ['Value']); // DO NOT LOCALIZE  
    684.   if Value > MaxCapacity then  
    685.     raise ERangeError.CreateResFmt(@SListCapacityError, [Value]);  
    686.   
    687.   LOldLength := FLength;  
    688.   try  
    689.     FLength := Value;  
    690.     if FLength > Capacity then  
    691.       ExpandCapacity;  
    692.   except  
    693.     on E: EOutOfMemory do  
    694.       FLength := LOldLength;  
    695.   end;  
    696. end;  
    697.   
    698. function TStringBuilder.ToString: string;  
    699. begin  
    700.   SetLength(Result, Length);  
    701.   Move(FData[0], Result[1], Length * SizeOf(Char));  
    702. end;  
    703.   
    704. function TStringBuilder.ToString(StartIndex, StrLength: Integer): string;  
    705. begin  
    706.   if StrLength <> then  
    707.   begin  
    708.     if StartIndex < then  
    709.       raise ERangeError.CreateResFmt(@SParamIsNegative, ['StartIndex']); // DO NOT LOCALIZE  
    710.     if StrLength < then  
    711.       raise ERangeError.CreateResFmt(@SParamIsNegative, ['StrLength']); // DO NOT LOCALIZE  
    712.     CheckBounds(StartIndex);  
    713.     CheckBounds(StartIndex + StrLength - 1);  
    714.   
    715.     SetLength(Result, StrLength);  
    716.     Move(FData[StartIndex], Result[1], StrLength * SizeOf(Char));  
    717.   end  
    718.   else Result := '';  
    719. end;  
    720.   
    721. function TStringBuilder._Replace(Index: Integer; const Old,  
    722.   New: string): Boolean;  
    723. var  
    724.   OldCapacity: Integer;  
    725.   SizeChange: Integer;  
    726. begin  
    727.   Result := False;  
    728.   SizeChange := System.Length(New) - System.Length(Old);  
    729.   
    730.   if SizeChange = then  
    731.   begin  
    732.     Move(New[1], FData[Index], System.Length(New) * SizeOf(Char));  
    733.   end  
    734.   else  
    735.   begin  
    736.     if SizeChange > then  
    737.     begin  
    738.       OldCapacity := Capacity;  
    739.       Length := Length + SizeChange;  
    740.       if OldCapacity <> Capacity then  
    741.         Result := True;  
    742.     end;  
    743.   
    744.     Move(FData[Index + System.Length(Old)], FData[Index + System.Length(New)], (Length - (System.Length(Old) + Index)) * SizeOf(Char));  
    745.     Move(New[1], FData[Index], System.Length(New) * SizeOf(Char));  
    746.   
    747.     if SizeChange < then  
    748.       Length := Length + SizeChange;  
    749.   end;  
    750. end;  
    751.   
    752. function TStringBuilder.Append(const Value: Word): TStringBuilder;  
    753. begin  
    754.   Append(IntToStr(Value));  
    755.   Result := self;  
    756. end;  
    757.   
    758. function TStringBuilder.Append(const Value: TCharArray): TStringBuilder;  
    759. var  
    760.   I: Integer;  
    761. begin  
    762.   Result := self;  
    763.   
    764.   for I := to System.Length(Value) - do  
    765.     if Value[I] = #then  
    766.       Break;  
    767.   
    768.   Append(Value, 0, I);  
    769. end;  
    770.   
    771. function TStringBuilder.Append(const Value: UInt64): TStringBuilder;  
    772. begin  
    773.   Append(UIntToStr(Value));  
    774.   Result := self;  
    775. end;  
    776.   
    777. function TStringBuilder.Append(const Value: Cardinal): TStringBuilder;  
    778. begin  
    779.   Append(UIntToStr(Value));  
    780.   Result := self;  
    781. end;  
    782.   
    783. function TStringBuilder.Append(const Value: string; StartIndex,  
    784.   Count: Integer): TStringBuilder;  
    785. begin  
    786.   if StartIndex + Count > System.Length(Value) then  
    787.     raise ERangeError.CreateResFmt(@SListIndexError, [StartIndex]);  
    788.   if StartIndex < then  
    789.     raise ERangeError.CreateResFmt(@SListIndexError, [StartIndex]);  
    790.   
    791.   Length := Length + Count;  
    792.   Move(Value[StartIndex + 1], FData[Length - Count], Count * SizeOf(Char));  
    793.   Result := Self;  
    794. end;  
    795.   
    796. function TStringBuilder.AppendFormat(const Format: string;  
    797.   const Args: array of const): TStringBuilder;  
    798. begin  
    799.   Append(SysUtils.Format(Format, Args));  
    800.   Result := Self;  
    801. end;  
    802.   
    803. function TStringBuilder.AppendLine: TStringBuilder;  
    804. begin  
    805.   Append(sLineBreak);  
    806.   Result := Self;  
    807. end;  
    808.   
    809. function TStringBuilder.AppendLine(const Value: string): TStringBuilder;  
    810. begin  
    811.   Append(Value);  
    812.   AppendLine;  
    813.   Result := Self;  
    814. end;  
    815.   
    816. function TStringBuilder.Append(const Value: TCharArray; StartIndex,  
    817.   CharCount: Integer): TStringBuilder;  
    818. begin  
    819.   if StartIndex + CharCount > System.Length(Value) then  
    820.     raise ERangeError.CreateResFmt(@SListIndexError, [StartIndex]);  
    821.   if StartIndex < then  
    822.     raise ERangeError.CreateResFmt(@SListIndexError, [StartIndex]);  
    823.   
    824.   Length := Length + CharCount;  
    825.   Move(Value[StartIndex], FData[Length - CharCount], CharCount * SizeOf(Char));  
    826.   Result := self;  
    827. end;  
    828.   
    829. function TStringBuilder.Append(const Value: Char;  
    830.   RepeatCount: Integer): TStringBuilder;  
    831. begin  
    832.   Append(System.StringOfChar(Value, RepeatCount));  
    833.   Result := Self;  
    834. end;  
    835.   
    836. end.  


    转自:http://www.cnblogs.com/DxSoft/archive/2010/01/03/1638242.html

    http://blog.csdn.net/rznice/article/details/8016565

  • 相关阅读:
    实验四
    实验三 进程调度模拟程序
    实验二 调度
    一个完整的大作业
    数据结构化与保存
    爬取新闻列表
    用requests库和BeautifulSoup4库爬取新闻列表
    中文词频统计及词云制作
    组合数据类型练习,英文词频统计实例
    字符串操作练习:星座、凯撒密码、99乘法表、词频统计预处理
  • 原文地址:https://www.cnblogs.com/findumars/p/8207173.html
Copyright © 2011-2022 走看看