2
0

syssbh.inc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. {%MainUnit sysutils.pp}
  2. { TGenericStringBuilder }
  3. TGenericStringBuilder = class
  4. private
  5. const
  6. DefaultCapacity = 64;
  7. type
  8. TCharEnumerator = record
  9. private
  10. FCurrentPosition: PSBChar;
  11. FEndPosition: PSBChar;
  12. Function GetCurrent: SBChar; inline;
  13. public
  14. Function MoveNext: Boolean; inline;
  15. property Current: SBChar read GetCurrent;
  16. end;
  17. private
  18. Function GetCapacity: Integer;
  19. Procedure SetCapacity(AValue: Integer);
  20. Function GetC(Index: Integer): SBChar;
  21. Procedure SetC(Index: Integer; AValue: SBChar);
  22. Function GetLength: Integer; inline;
  23. Procedure SetLength(AValue: Integer);
  24. protected
  25. FData: TSBCharArray;
  26. FLength: Integer;
  27. FMaxCapacity: Integer;
  28. // Raise error on range check.
  29. Procedure CheckRange(Idx,Count,MaxLen : Integer);inline;
  30. Procedure CheckNegative(Const AValue : Integer; Const AName: SBString); inline;
  31. // All appends/inserts pass through here.
  32. Procedure DoAppend(Const S : {$IFDEF SBUNICODE}SBString{$ELSE}RawByteString{$ENDIF});virtual;
  33. Procedure DoAppend(const AValue: TSBCharArray; Idx, aCount: Integer); virtual;
  34. Procedure DoInsert(Index: Integer; const AValue: SBString); virtual;
  35. Procedure DoInsert(Index: Integer; const AValue: TSBCharArray; StartIndex, SBCharCount: Integer); virtual;
  36. Procedure DoReplace(Index: Integer; const Old, New: SBString); virtual;
  37. Procedure Grow;
  38. Procedure Shrink;
  39. public
  40. Constructor Create;
  41. Constructor Create(aCapacity: Integer);
  42. Constructor Create(const AValue: SBString);
  43. Constructor Create(aCapacity: Integer; aMaxCapacity: Integer);
  44. Constructor Create(const AValue: SBString; aCapacity: Integer);
  45. Constructor Create(const AValue: SBString; StartIndex: Integer; aLength: Integer; aCapacity: Integer);
  46. Function Append(const AValue: Boolean): TGenericStringBuilder;
  47. Function Append(const AValue: Byte): TGenericStringBuilder;
  48. Function Append(const AValue: SBChar): TGenericStringBuilder;
  49. Function Append(const AValue: Currency): TGenericStringBuilder;
  50. Function Append(const AValue: Double): TGenericStringBuilder;
  51. Function Append(const AValue: Smallint): TGenericStringBuilder;
  52. Function Append(const AValue: LongInt): TGenericStringBuilder;
  53. Function Append(const AValue: Int64): TGenericStringBuilder;
  54. Function Append(const AValue: TObject): TGenericStringBuilder;
  55. Function Append(const AValue: Shortint): TGenericStringBuilder;
  56. Function Append(const AValue: Single): TGenericStringBuilder;
  57. Function Append(const AValue: UInt64): TGenericStringBuilder;
  58. Function Append(const AValue: TSBCharArray): TGenericStringBuilder;
  59. Function Append(const AValue: Word): TGenericStringBuilder;
  60. Function Append(const AValue: Cardinal): TGenericStringBuilder;
  61. Function Append(const AValue: PSBChar): TGenericStringBuilder;
  62. {$IFDEF SBUNICODE}
  63. // Do not use SBRawstring, we need 2 versions in case of unicode
  64. Function Append(const AValue: SBString): TGenericStringBuilder;
  65. Function Append(const AValue: AnsiChar): TGenericStringBuilder;
  66. {$ELSE}
  67. Function Append(const AValue: UnicodeString): TGenericStringBuilder;
  68. Function Append(const AValue: UnicodeChar): TGenericStringBuilder;
  69. {$ENDIF}
  70. Function Append(const AValue: RawByteString): TGenericStringBuilder;
  71. Function Append(const AValue: SBChar; RepeatCount: Integer): TGenericStringBuilder;
  72. Function Append(const AValue: TSBCharArray; StartIndex: Integer; SBCharCount: Integer): TGenericStringBuilder;
  73. Function Append(const AValue: SBString; StartIndex: Integer; Count: Integer): TGenericStringBuilder;
  74. Function Append(const Fmt: SBString; const Args: array of const): TGenericStringBuilder;
  75. Function AppendFormat(const Fmt: SBString; const Args: array of const): TGenericStringBuilder;
  76. Function AppendLine: TGenericStringBuilder;
  77. Function AppendLine(const AValue: RawByteString): TGenericStringBuilder;
  78. Procedure Clear;
  79. Procedure CopyTo(SourceIndex: Integer; Var Destination: TSBCharArray; DestinationIndex: Integer; Count: Integer);
  80. Function EnsureCapacity(aCapacity: Integer): Integer;
  81. Function Equals(StringBuilder: TGenericStringBuilder): Boolean; reintroduce;
  82. Function GetEnumerator: TCharEnumerator; inline;
  83. Function Insert(Index: Integer; const AValue: Boolean): TGenericStringBuilder;
  84. Function Insert(Index: Integer; const AValue: Byte): TGenericStringBuilder;
  85. Function Insert(Index: Integer; const AValue: SBChar): TGenericStringBuilder;
  86. Function Insert(Index: Integer; const AValue: Currency): TGenericStringBuilder;
  87. Function Insert(Index: Integer; const AValue: Double): TGenericStringBuilder;
  88. Function Insert(Index: Integer; const AValue: Smallint): TGenericStringBuilder;
  89. Function Insert(Index: Integer; const AValue: LongInt): TGenericStringBuilder;
  90. Function Insert(Index: Integer; const AValue: TSBCharArray): TGenericStringBuilder;
  91. Function Insert(Index: Integer; const AValue: Int64): TGenericStringBuilder;
  92. Function Insert(Index: Integer; const AValue: TObject): TGenericStringBuilder;
  93. Function Insert(Index: Integer; const AValue: Shortint): TGenericStringBuilder;
  94. Function Insert(Index: Integer; const AValue: Single): TGenericStringBuilder;
  95. Function Insert(Index: Integer; const AValue: SBString): TGenericStringBuilder;
  96. Function Insert(Index: Integer; const AValue: Word): TGenericStringBuilder;
  97. Function Insert(Index: Integer; const AValue: Cardinal): TGenericStringBuilder;
  98. Function Insert(Index: Integer; const AValue: UInt64): TGenericStringBuilder;
  99. Function Insert(Index: Integer; const AValue: SBString; const aRepeatCount: Integer): TGenericStringBuilder;
  100. Function Insert(Index: Integer; const AValue: TSBCharArray; startIndex: Integer; SBCharCount: Integer): TGenericStringBuilder;
  101. Function Remove(StartIndex: Integer; RemLength: Integer): TGenericStringBuilder;
  102. Function Replace(const OldChar, NewChar: SBChar): TGenericStringBuilder;
  103. Function Replace(const OldChar, NewChar: SBChar; StartIndex: Integer; Count: Integer): TGenericStringBuilder;
  104. Function Replace(const OldValue, NewValue: SBRawString): TGenericStringBuilder;
  105. Function Replace(const OldValue, NewValue: SBRawString; StartIndex: Integer; Count: Integer): TGenericStringBuilder;
  106. {$IFDEF UNICODERTL}
  107. Function ToString: RTLString; override;
  108. function ToString(UpdateCapacity: Boolean): RTLString; overload;
  109. {$ELSE}
  110. Function ToString: SBString; {$IFNDEF SBUNICODE} override; {$ELSE} reintroduce; {$ENDIF}
  111. function ToString(UpdateCapacity: Boolean): SBString; overload;
  112. {$ENDIF}
  113. Function ToString(aStartIndex: Integer; aLength: Integer): SBString; reintroduce;
  114. property Chars[index: Integer]: SBChar read GetC write SetC; default;
  115. property Length: Integer read GetLength write SetLength;
  116. property Capacity: Integer read GetCapacity write SetCapacity;
  117. property MaxCapacity: Integer read FMaxCapacity;
  118. end;