-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWIDESTR.PAS
227 lines (203 loc) · 4.69 KB
/
WIDESTR.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
{$O+,F+}
unit WideStr;
interface
uses
Objects,
Consts,
Utils,
Strings;
type
PWideString = ^TWideString;
TWideString = object
Data: PChar;
Len: word;
Capacity: word;
constructor Create;
constructor CreateString(st: string);
constructor CreateBuffer(size: word);
destructor Done; virtual;
function ToString: string; virtual;
function SubString(index: integer; length: byte): string; virtual;
function GetChar(pos: word): char; virtual;
procedure SetString(st: string); virtual;
procedure SetCapacity(newCapacity: word); virtual;
procedure Append(st: string); virtual;
procedure Insert(index: integer; st: string); virtual;
procedure Delete(index: integer);
end;
PStringList = ^TStringList;
TStringList = object(TList)
procedure Init; virtual;
function ToString: string; virtual;
function GetItem(index: integer): PWideString;
function IndexOf(item: PWideString): integer;
function Add(item: PWideString): integer;
procedure Insert(index: integer; item: PWideString);
end;
implementation
constructor TWideString.Create;
begin
Data := nil;
Len := 0;
Capacity := 0;
end;
function TWideString.GetChar(pos: word): char;
var
dataPtr: PChar;
begin
dataPtr := Data;
Inc(dataPtr, pos);
GetChar := dataPtr^;
end;
procedure TWideString.SetCapacity(newCapacity: word);
var
newData: pointer;
max: word;
dataSize: word;
begin
if (Capacity = newCapacity) then exit;
dataSize := newCapacity * SizeOf(Pointer);
GetMem(newData, dataSize);
FillChar(newData^, dataSize, 0);
if (newCapacity > 0) and (Data <> nil) then
begin
if (newCapacity > Capacity) then
Move(Data^, NewData^, Capacity)
else
Move(Data^, NewData^, newCapacity);
FreeMem(Data, Capacity * SizeOf(pointer));
end;
Data := newData;
Capacity := newCapacity;
if Len > Capacity then
Len := Capacity;
end;
constructor TWideString.CreateString(st: string);
begin
Data := nil;
Len := 0;
Capacity := 0;
SetCapacity(Length(st) * 2);
SetString(st);
end;
constructor TWideString.CreateBuffer(size: word);
begin
Len := 0;
SetCapacity(size);
end;
function TWideString.ToString: string;
var
result: string;
begin
Move(Data^, result[1], Lo(Len));
result[0] := Chr(Lo(Len));
ToString := result;
end;
procedure TWideString.SetString(st: string);
var
strPtr: PChar;
begin
if (Data <> nil) then
FreeMem(Data, Capacity * SizeOf(pointer));
Data := nil;
Len := Length(st);
Capacity := 0;
SetCapacity(Len);
strPtr := @st;
Inc(strPtr, 1);
Move(strPtr^, Data^, Len);
end;
procedure TWideString.Append(st: string);
var
strPtr: PChar;
dataPtr: PChar;
begin
if (Len + Length(st) > Capacity) then
SetCapacity(MaxL(Capacity * 2, Capacity + Length(st) * 2));
strPtr := @st;
Inc(strPtr, 1);
dataPtr := Data;
Inc(dataPtr, Len);
Move(strPtr^, dataPtr^, Length(st));
Inc(Len, Length(st));
end;
procedure TWideString.Insert(index: integer; st: string);
var
strPtr: PChar;
dataPtr: PChar;
nextPtr: PChar;
begin
if (Len + Length(st) > Capacity) then
SetCapacity(MaxL(Capacity * 2, Capacity + Length(st) * 2));
strPtr := @st;
Inc(strPtr, 1);
dataPtr := Data;
Inc(dataPtr, index - 1);
nextPtr := dataPtr;
Inc(nextPtr, 1);
Move(dataPtr^, nextPtr^, Len - index + 1);
Move(strPtr^, dataPtr^, Length(st));
Inc(Len, Length(st));
end;
procedure TWideString.Delete(index: integer);
var
dataPtr: PChar;
nextPtr: PChar;
begin
if (Len < 1) or (index < 0) or (index > Len - 1) then exit;
dataPtr := Data;
Inc(dataPtr, index);
nextPtr := dataPtr;
Inc(nextPtr);
Move(nextPtr^, dataPtr^, Len - index - 1);
Dec(Len);
end;
function TWideString.SubString(index: integer; length: byte): string;
var
pos: integer;
result: string;
dataPtr: PChar;
begin
result := '';
SubString := '';
if (index < 0) or (length < 1) or (Len = 0) then exit;
if (index + length > len) then
length := len - index;
result[0] := Chr(length);
dataPtr := Data;
Inc(dataPtr, index);
Move(dataPtr^, result[1], length);
SubString := result;
end;
destructor TWideString.Done;
begin
if (Data <> nil) then
FreeMem(Data, Capacity);
end;
procedure TStringList.Init;
begin
TList.Init;
TypeName := 'TStringList';
end;
function TStringList.ToString: string;
begin
TList.Init;
ToString := TypeName;
end;
function TStringList.GetItem(index: integer): PWideString;
begin
GetItem := TList.GetItem(index);
end;
function TStringList.IndexOf(item: PWideString): integer;
begin
IndexOf := TList.IndexOf(item);
end;
function TStringList.Add(item: PWideString): integer;
begin
TList.Add(item);
end;
procedure TStringList.Insert(index: integer; item: PWideString);
begin
TList.Insert(index, item);
end;
end.