• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

TstringBuilderDelphi2007版

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

 

unit DxStringBuilder;

interface
uses RTLConsts,Classes,SysUtils;
type
  EExternal 
= class(Exception)
  
public
{$IFDEF MSWINDOWS}
    ExceptionRecord: PExceptionRecord platform;
{$ENDIF}
{$IF defined(LINUX) or defined(MACOSX)}
    ExceptionAddress: LongWord platform;
    AccessAddress: LongWord platform;
    SignalNumber: Integer platform;
{$IFEND LINUX or MACOSX}
  
end;
  
  EIntError 
= class(EExternal);
  ERangeError 
= class(EIntError);
  TCharArray 
= array of Char;
  TStringBuilder 
= class
  
private
    
const DefaultCapacity = $10;
    
function GetCapacity: Integer;
    
procedure SetCapacity(const Value: Integer);
    
function GetLength: Integer;
    
procedure Set_Length(const Value: Integer);
    
function GetMaxCapacity: Integer;

    
procedure ReduceCapacity;
    
procedure ExpandCapacity;
    
procedure CheckBounds(Index: Integer);
    
function _Replace(Index: Integer; const Old, New: string): Boolean;
    
function GetChars(index: Integer): Char;
    
procedure SetChars(index: Integer; const Value: Char);
  
protected
    FData: TCharArray;
    FLength: Integer;
    FMaxCapacity: Integer;
  
public
    
constructor Create; overload;
    
constructor Create(aCapacity: Integer); overload;
    
constructor Create(const Value: string); overload;
    
function Append(const Value: Boolean): TStringBuilder; overload;
    
function Append(const Value: Byte): TStringBuilder; overload;
    
function Append(const Value: Char): TStringBuilder; overload;
    
function Append(const Value: Currency): TStringBuilder; overload;
    
function Append(const Value: Double): TStringBuilder; overload;
    
function Append(const Value: Smallint): TStringBuilder; overload;
    
function Append(const Value: Integer): TStringBuilder; overload;
    
function Append(const Value: Int64): TStringBuilder; overload;
    
function Append(const Value: TObject): TStringBuilder; overload;
    
function Append(const Value: Shortint): TStringBuilder; overload;
    
function Append(const Value: Single): TStringBuilder; overload;
    
function Append(const Value: string): TStringBuilder; overload;
    
function Append(const Value: UInt64): TStringBuilder; overload;
    
function Append(const Value: TCharArray): TStringBuilder; overload;
    
function Append(const Value: Word): TStringBuilder; overload;
    
function Append(const Value: Cardinal): TStringBuilder; overload;
    
function Append(const Value: Char; RepeatCount: Integer): TStringBuilder; overload;
    
function Append(const Value: TCharArray; StartIndex: Integer; CharCount: Integer): TStringBuilder; overload;
    
function Append(const Value: string; StartIndex: Integer; Count: Integer): TStringBuilder; overload;

    
function AppendFormat(const Format: stringconst Args: array of const): TStringBuilder; overload;
    
function AppendLine: TStringBuilder; overload;
    
function AppendLine(const Value: string): TStringBuilder; overload;

    
procedure Clear;
    
procedure CopyTo(SourceIndex: Integer; const Destination: TCharArray; DestinationIndex: Integer; Count: Integer);
    
function EnsureCapacity(aCapacity: Integer): Integer;
    
function Equals(StringBuilder: TStringBuilder): Boolean; reintroduce;

    
function Insert(Index: Integer; const Value: Boolean): TStringBuilder; overload;
    
function Insert(Index: Integer; const Value: Byte): TStringBuilder; overload;
    
function Insert(Index: Integer; const Value: Char): TStringBuilder; overload;
    
function Insert(Index: Integer; const Value: Currency): TStringBuilder; overload;
    
function Insert(Index: Integer; const Value: Double): TStringBuilder; overload;
    
function Insert(Index: Integer; const Value: Smallint): TStringBuilder; overload;
    
function Insert(Index: Integer; const Value: Integer): TStringBuilder; overload;
    
function Insert(Index: Integer; const Value: TCharArray): TStringBuilder; overload;
    
function Insert(Index: Integer; const Value: Int64): TStringBuilder; overload;
    
function Insert(Index: Integer; const Value: TObject): TStringBuilder; overload;
    
function Insert(Index: Integer; const Value: Shortint): TStringBuilder; overload;
    
function Insert(Index: Integer; const Value: Single): TStringBuilder; overload;
    
function Insert(Index: Integer; const Value: string): TStringBuilder; overload;
    
function Insert(Index: Integer; const Value: Word): TStringBuilder; overload;
    
function Insert(Index: Integer; const Value: Cardinal): TStringBuilder; overload;
    
function Insert(Index: Integer; const Value: UInt64): TStringBuilder; overload;
    
function Insert(Index: Integer; const Value: string; count: Integer): TStringBuilder; overload;
    
function Insert(Index: Integer; const Value: TCharArray; startIndex: Integer; charCount: Integer): TStringBuilder; overload;

    
function Remove(StartIndex: Integer; RemLength: Integer): TStringBuilder;

    
function Replace(const OldChar: Char; const NewChar: Char): TStringBuilder; overload;
    
function Replace(const OldValue: stringconst NewValue: string): TStringBuilder; overload;
    
function Replace(const OldChar: Char; const NewChar: Char; StartIndex: Integer; Count: Integer): TStringBuilder; overload;
    
function Replace(const OldValue: stringconst NewValue: string; StartIndex: Integer; Count: Integer): TStringBuilder; overload;
    
function ToString: stringoverload;
    
function ToString(StartIndex: Integer; StrLength: Integer): stringreintroduceoverload;

    
procedure SaveToStream(Stream: TStream);
    
procedure SaveToFile(FileName: string);
    
procedure LoadFromStream(Stream: TStream);
    
procedure LoadFromFile(FileName: string);

    
property Capacity: Integer read GetCapacity write SetCapacity;
    
property Chars[index: Integer]: Char read GetChars write SetChars; default;
    
property Length: Integer read GetLength write Set_Length;
    
property MaxCapacity: Integer read GetMaxCapacity;
  
end;


function UIntToStr(Value: Cardinal): stringoverload;
function UIntToStr(Value: UInt64): stringoverload;

resourcestring
  SParamIsNegative 
= 'Parameter %s cannot be a negative value';
  SInputBufferExceed 
= 'Input buffer exceeded for %s = %d, %s = %d';
implementation

function UIntToStr(Value: Cardinal): string;
begin
  FmtStr(Result, 
'%u', [Value]);
end;

function UIntToStr(Value: UInt64): string;
begin
  FmtStr(Result, 
'%u', [Value]);
end;

{ TStringBuilder }

constructor TStringBuilder.Create;
begin
  
inherited Create;
  FMaxCapacity :
= MaxInt;
  Capacity :
= DefaultCapacity;
  FLength :
= 0;
end;

constructor TStringBuilder.Create(aCapacity: Integer);
begin
  
inherited Create;
  FMaxCapacity :
= MaxInt;
  Capacity :
= aCapacity;
  FLength :
= 0;
end;

function TStringBuilder.Append(const Value: string): TStringBuilder;
begin
  Length :
= Length + System.Length(Value);
  Move(PChar(Value)^, FData[Length 
- System.Length(Value)], System.Length(Value) * SizeOf(Char));
  Result :
= self;
end;

function TStringBuilder.Append(const Value: Currency): TStringBuilder;
begin
  Append(CurrToStr(Value));
  Result :
= Self;
end;

function TStringBuilder.Append(const Value: Double): TStringBuilder;
begin
  Append(FloatToStr(Value));
  Result :
= Self;
end;

function TStringBuilder.Append(const Value: Char): TStringBuilder;
begin
  Length :
= Length + 1;
  FData[Length 
- 1] := Value;
  Result :
= Self;
end;

function TStringBuilder.Append(const Value: Boolean): TStringBuilder;
begin
  Append(BoolToStr(Value, True));
  Result :
= Self;
end;

function TStringBuilder.Append(const Value: Byte): TStringBuilder;
begin
  Append(IntToStr(Value));
  Result :
= Self;
end;

function TStringBuilder.Append(const Value: Smallint): TStringBuilder;
begin
  Append(IntToStr(Value));
  Result :
= Self;
end;

function TStringBuilder.Append(const Value: Shortint): TStringBuilder;
begin
  Append(IntToStr(Value));
  Result :
= Self;
end;

function TStringBuilder.Append(const Value: Single): TStringBuilder;
begin
  Append(FloatToStr(Value));
  Result :
= self;
end;

function TStringBuilder.Append(const Value: TObject): TStringBuilder;
begin
{$if CompilerVersion >= 19}
  Append(Value.ToString());
{$else}
  
if Value.InheritsFrom(TComponent) then
    Append(TComponent(Value).Name
+''+Value.ClassName)
  
else Append(Value.ClassName);
{$ifend}
  Result :
= Self;
end;

function TStringBuilder.Append(const Value: Integer): TStringBuilder;
begin
  Append(IntToStr(Value));
  Result :
= Self;
end;

function TStringBuilder.Append(const Value: Int64): TStringBuilder;
begin
  Append(IntToStr(Value));
  Result :
= Self;
end;

procedure TStringBuilder.CheckBounds(Index: Integer);
begin
  
if Cardinal(Index) >= Cardinal(Length) then
    
raise ERangeError.CreateResFmt(@SListIndexError, [Index]);
end;

procedure TStringBuilder.Clear;
begin
  Length :
= 0;
  Capacity :
= DefaultCapacity;
end;

procedure TStringBuilder.CopyTo(SourceIndex: Integer;
  
const Destination: TCharArray; DestinationIndex, Count: Integer);
begin
  
if Count < 0 then
    
raise ERangeError.CreateResFmt(@SParamIsNegative, ['Count']); // DO NOT LOCALIZE
  
if DestinationIndex < 0 then
    
raise ERangeError.CreateResFmt(@SParamIsNegative, ['DestinationIndex']); // DO NOT LOCALIZE
  
if DestinationIndex + Count > System.Length(Destination) then
    
raise ERangeError.CreateResFmt(@SInputBufferExceed,
      [
'DestinationIndex', DestinationIndex, 'Count', Count]);

  
if Count > 0 then
  
begin
    CheckBounds(SourceIndex);
    CheckBounds(SourceIndex 
+ Count - 1);

    Move(FData[SourceIndex], Destination[DestinationIndex], Count 
* SizeOf(Char));
  
end;
end;

constructor TStringBuilder.Create(const Value: string);
begin
  Create;
  Append(Value);
end;

function TStringBuilder.EnsureCapacity(aCapacity: Integer): Integer;
begin
  
if Cardinal(aCapacity) > Cardinal(MaxCapacity) then
    
raise ERangeError.CreateResFmt(@SListIndexError, [aCapacity]);

  
if Capacity < aCapacity then
    Capacity :
= aCapacity;

  Result :
= Capacity;
end;

function TStringBuilder.Equals(StringBuilder: TStringBuilder): Boolean;
begin
  Result :
= (StringBuilder <> niland (Length = StringBuilder.Length) and
    (MaxCapacity 
= StringBuilder.MaxCapacity) and
    CompareMem(@FData[
0], @StringBuilder.FData[0], Length * SizeOf(Char));
end;

procedure TStringBuilder.ExpandCapacity;
var
  NewCapacity: Integer;
begin
  NewCapacity :
= Capacity * 2;
  
if Length > NewCapacity then
    NewCapacity :
= Length * 2// this line may overflow NewCapacity to a negative value
  
if NewCapacity > MaxCapacity then
    NewCapacity :
= MaxCapacity;
  
if NewCapacity < 0 then // if NewCapacity has been overflowed
    NewCapacity :
= Length;
  Capacity :
= NewCapacity;
end;

function TStringBuilder.GetCapacity: Integer;
begin
  Result :
= System.Length(FData);
end;

function TStringBuilder.GetChars(index: Integer): Char;
begin
  
if Index < 0 then
    
raise ERangeError.CreateResFmt(@SParamIsNegative, ['Index']); // DO NOT LOCALIZE
  CheckBounds(Index);

  Result :
= FData[Index];
end;

function TStringBuilder.GetLength: Integer;
begin
  Result :
= FLength;
end;

function TStringBuilder.GetMaxCapacity: Integer;
begin
  Result :
= FMaxCapacity;
end;

function TStringBuilder.Insert(Index: Integer;
  
const Value: Integer): TStringBuilder;
begin
  Insert(Index, IntToStr(Value));
  Result :
= Self;
end;

function TStringBuilder.Insert(Index: Integer;
  
const Value: Smallint): TStringBuilder;
begin
  Insert(Index, IntToStr(Value));
  Result :
= Self;
end;

function TStringBuilder.Insert(Index: Integer;
  
const Value: Int64): TStringBuilder;
begin
  Insert(Index, IntToStr(Value));
  Result :
= Self;
end;

function TStringBuilder.Insert(Index: Integer;
  
const Value: TCharArray): TStringBuilder;
begin
  
if Index < 0 then
    
raise ERangeError.CreateResFmt(@SParamIsNegative, ['Index']); // DO NOT LOCALIZE
  
if Index > Length then
    
raise ERangeError.CreateResFmt(@SListIndexError, [Index]);

  Length :
= Length + System.Length(Value);
  Move(FData[Index], FData[Index 
+ System.Length(Value)], System.Length(Value) * SizeOf(Char));
  Move(Value[
0], FData[Index], System.Length(Value) * SizeOf(Char));
  Result :
= Self;
end;

function TStringBuilder.Insert(Index: Integer;
  
const Value: Double): TStringBuilder;
begin
  Insert(Index, FloatToStr(Value));
  Result :
= Self;
end;

function TStringBuilder.Insert(Index: Integer;
  
const Value: Byte): TStringBuilder;
begin
  Insert(Index, IntToStr(Value));
  Result :
= Self;
end;

function

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
用Delphi+GLScene轻松打造3D场景、动画之GLScene基础 - 山冈龙发布时间:2022-07-18
下一篇:
WIN锁屏+鼠标移动事件 - 疯狂delphi发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap