-
Notifications
You must be signed in to change notification settings - Fork 2
/
TinyWideStrings.pas
187 lines (159 loc) · 4.81 KB
/
TinyWideStrings.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
{$I Definition.Inc}
unit TinyWideStrings;
// Tiny WideStringList implementation for versions of Delphi prior to BDS2006
interface
uses Classes, SysUtils;
{ TWideStringList class }
type
PWideStringItem = ^TWideStringItem;
TWideStringItem = record
FString: WideString;
FObject: TObject;
end;
PWideStringItemList = ^TWideStringItemList;
TWideStringItemList = array[0..MaxListSize] of TWideStringItem;
TWideStringList = class(TPersistent)
private
FList: PWideStringItemList;
FCount: Integer;
FCapacity: Integer;
procedure Grow;
protected
procedure Error(const Msg: WideString; Data: Integer); overload;
procedure Error(Msg: PResStringRec; Data: Integer); overload;
function Get(Index: Integer): WideString;
function GetCapacity: Integer;
function GetCount: Integer;
function GetObject(Index: Integer): TObject;
procedure Put(Index: Integer; const S: WideString);
procedure PutObject(Index: Integer; AObject: TObject);
procedure SetCapacity(NewCapacity: Integer);
procedure InsertItem(Index: Integer; const S: WideString; AObject: TObject);
public
destructor Destroy; override;
function Add(const S: WideString): Integer;
function AddObject(const S: WideString; AObject: TObject): Integer;
procedure InsertObject(Index: Integer; const S: WideString;
AObject: TObject);
procedure Clear;
procedure Delete(Index: Integer);
procedure Insert(Index: Integer; const S: WideString);
property Capacity: Integer read GetCapacity write SetCapacity;
property Count: Integer read GetCount;
property Objects[Index: Integer]: TObject read GetObject write PutObject;
property Strings[Index: Integer]: WideString read Get write Put; default;
end;
implementation
uses
RTLConsts
;
{ TWideStringList }
procedure TWideStringList.Error(const Msg: WideString; Data: Integer);
begin
raise EStringListError.CreateFmt(Msg, [Data]) ;
end;
procedure TWideStringList.Error(Msg: PResStringRec; Data: Integer);
begin
Error(LoadResString(Msg), Data);
end;
destructor TWideStringList.Destroy;
begin
inherited Destroy;
if FCount <> 0 then Finalize(FList^[0], FCount);
FCount := 0;
SetCapacity(0);
end;
function TWideStringList.Add(const S: WideString): Integer;
begin
Result := AddObject(S, nil);
end;
function TWideStringList.AddObject(const S: WideString; AObject: TObject): Integer;
begin
Result := FCount;
InsertItem(Result, S, AObject);
end;
procedure TWideStringList.Clear;
begin
if FCount <> 0 then
begin
Finalize(FList^[0], FCount);
FCount := 0;
SetCapacity(0);
end;
end;
procedure TWideStringList.Delete(Index: Integer);
begin
if (Index < 0) or (Index >= FCount) then Error(@SListIndexError, Index);
Finalize(FList^[Index]);
Dec(FCount);
if Index < FCount then
System.Move(FList^[Index + 1], FList^[Index],
(FCount - Index) * SizeOf(TStringItem));
end;
function TWideStringList.Get(Index: Integer): WideString;
begin
if (Index < 0) or (Index >= FCount) then Error(@SListIndexError, Index);
Result := FList^[Index].FString;
end;
function TWideStringList.GetCapacity: Integer;
begin
Result := FCapacity;
end;
function TWideStringList.GetCount: Integer;
begin
Result := FCount;
end;
function TWideStringList.GetObject(Index: Integer): TObject;
begin
if (Index < 0) or (Index >= FCount) then Error(@SListIndexError, Index);
Result := FList^[Index].FObject;
end;
procedure TWideStringList.Grow;
var
Delta: Integer;
begin
if FCapacity > 64 then Delta := FCapacity div 4 else
if FCapacity > 8 then Delta := 16 else
Delta := 4;
SetCapacity(FCapacity + Delta);
end;
procedure TWideStringList.Insert(Index: Integer; const S: WideString);
begin
InsertObject(Index, S, nil);
end;
procedure TWideStringList.InsertObject(Index: Integer; const S: WideString;
AObject: TObject);
begin
if (Index < 0) or (Index > FCount) then Error(@SListIndexError, Index);
InsertItem(Index, S, AObject);
end;
procedure TWideStringList.InsertItem(Index: Integer; const S: WideString; AObject: TObject);
begin
if FCount = FCapacity then Grow;
if Index < FCount then
System.Move(FList^[Index], FList^[Index + 1],
(FCount - Index) * SizeOf(TStringItem));
with FList^[Index] do
begin
Pointer(FString) := nil;
FObject := AObject;
FString := S;
end;
Inc(FCount);
end;
procedure TWideStringList.Put(Index: Integer; const S: WideString);
begin
if (Index < 0) or (Index >= FCount) then Error(@SListIndexError, Index);
FList^[Index].FString := S;
end;
procedure TWideStringList.PutObject(Index: Integer; AObject: TObject);
begin
if (Index < 0) or (Index >= FCount) then Error(@SListIndexError, Index);
FList^[Index].FObject := AObject;
end;
procedure TWideStringList.SetCapacity(NewCapacity: Integer);
begin
ReallocMem(FList, NewCapacity * SizeOf(TStringItem));
FCapacity := NewCapacity;
end;
end.