-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathNeslib.Xml.Types.pas
525 lines (427 loc) · 13.3 KB
/
Neslib.Xml.Types.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
unit Neslib.Xml.Types;
{< Common XML types. }
{$INCLUDE 'Neslib.inc'}
interface
type
{ XmlChar and XmlString map to WideChar and String repectively.
When compiled with the XML_UTF8 define, all strings and characters are
UTF-8-based. This reduces memory and can increase speed, but may result in
implicit conversions when using in your code. }
{$IFDEF XML_UTF8}
XmlChar = UTF8Char;
PXmlChar = PUTF8Char;
XmlString = UTF8String;
{$ELSE}
XmlChar = WideChar;
PXmlChar = PWideChar;
XmlString = String;
{$ENDIF}
type
{ Support encodings of XML source files. }
TXmlEncoding = (
{ UTF-8. This is the default encoding when no BOM is present, or the
document starts with an UTF-8 BOM }
Utf8,
{ Little-Endian UTF-16.
Used when a document starts with a UTF-16 LE BOM. }
Utf16,
{ Big-Endian UTF-16.
Used when a document starts with a UTF-16 BE BOM. }
Utf16BigEndian,
{ Little-Endian UTF-32.
Used when a document starts with a UTF-32 LE BOM. }
Utf32,
{ Big-Endian UTF-32.
Used when a document starts with a UTF-32 BE BOM. }
Utf32BigEndian);
type
{ Options to control XML output (eg. for IXmlDocument.Save and
IXmlDocument.ToXml) }
TXmlOutputOption = (
{ Whether you want indented (aka pretty-printed) output. If set, child nodes
will be indented and line breaks will be inserted.
Set by default.}
Indent,
{ Whether to write and XML declaration (<?xml...>) at the beginning of the
output.
Set by default. }
WriteDeclaration);
TXmlOutputOptions = set of TXmlOutputOption;
const
{ Default XML output options }
DEFAULT_XML_OUTPUT_OPTIONS = [TXmlOutputOption.Indent, TXmlOutputOption.WriteDeclaration];
type
{ A string interning pool.
Is used internally to efficiently keep track of element and attribute
names. You usually don't need to use this class yourself. }
TXmlStringInternPool = class
{$REGION 'Internal Declarations'}
private type
TItem = packed record
HashCode: Integer;
Index: Integer;
end;
private
FItems: TArray<TItem>;
FStrings: TArray<XmlString>;
FCount: Integer;
FGrowThreshold: Integer;
{$IFDEF CPU64BITS}
FAdditionalStrings: TArray<XmlString>;
FAdditionalCount: Integer;
{$ENDIF}
private
procedure Resize(ANewSize: Integer);
{$ENDREGION 'Internal Declarations'}
public
constructor Create;
{ Clears to pool. }
procedure Clear;
{ Retrieves the index of a string in the pool, or adds the string in case
it is not in the pool yet.
Parameters:
AString: the string to get or add.
Returns:
The index of the (existing or newly added) string.
To lookup a string without adding it, use the Find method instead. }
function Get(const AString: XmlString): Integer; overload;
{ Retrieves the index of a string in the pool, or adds the string in case
it is not in the pool yet.
Parameters:
AString: pointer to the first character in the string.
ALength: number of characters in the string.
Returns:
The index of the (existing or newly added) string.
To lookup a string without adding it, use the Find method instead. }
function Get(const AString: PXmlChar; const ALength: Integer): Integer; overload;
{ Retrieves a string from the pool by index.
Parameters:
AIndex: the index of the string (as previously returned by one of the
other Get methods). Must be between 0 and the number of strings in the
pool. This is checked with an assertion.
Returns:
The string at this index. }
function Get(const AIndex: Integer): XmlString; overload;
{ Retrieves the index of a string in the pool.
Parameters:
AString: the string to find.
Returns:
The index of the string, or -1 in case the pool does not contain the
string.
Use the Get method if you want to add the string to the pool in case it
does not exist in the pool yet. }
function Find(const AString: XmlString): Integer;
{$IFDEF CPU64BITS}
{ Adds an "additional string" to the pool.
An additional string is the value of an attribute or text/comment/cdata
node, whose pointer doesn't fit into 32-bits (which should be very rare).
In that case, this method is used to store the string and return its
index.
Parameters:
AString: the string to add.
Returns:
The index of the added string.
Note that these strings are stored seperately, and are not part of the
main string interning pool. }
function AddAdditionalString(const AString: XmlString): Integer;
{ Retrieves an "additional string" previously added using
AddAdditionalString.
Parameters:
AIndex: the index of the string (as previously returned by
AddAdditionalString). Must be between 0 and the number of additionl
strings. This is checked with an assertion.
Returns:
The string at this index.
See AddAdditionalString for more details. }
function GetAdditionalString(const AIndex: Integer): XmlString;
{$ENDIF}
{ The number of strings in the pool (excluding "additional strings"). }
property Count: Integer read FCount;
end;
type
{ Uses internally to map pointers.
You don't need to use this class yourself. }
TXmlPointerMap = class
{$REGION 'Internal Declarations'}
private type
TItem = packed record
HashCode: Integer;
Key: UIntPtr;
Value: Pointer;
end;
private
FItems: TArray<TItem>;
FCount: Integer;
FGrowThreshold: Integer;
private
class function Hash(const AKey: UIntPtr): Integer; inline; static;
private
procedure Resize(ANewSize: Integer);
{$ENDREGION 'Internal Declarations'}
public
{ Clears the map }
procedure Clear;
{ Maps a pointer.
Parameters:
AKey: the key
AID: an identifier (between 0 and 7) that, in combination with AKey,
uniquely identifies AValue
AValue: the value to map to the combination of AKey and AID.
If the map already contains an AKey/AID combination, then its value is
overwritten. }
procedure Map(const AKey: Pointer; const AID: Integer;
const AValue: Pointer);
{ Retrieves a value.
Parameters:
AKey: the key
AID: an identifier (between 0 and 7) that, in combination with AKey,
uniquely identifies AValue
Returns:
The value for the AKey/AID combination, or nil in case the map does
not contain the AKey/AID combination. }
function Get(const AKey: Pointer; const AID: Integer): Pointer;
end;
implementation
uses
System.SysUtils,
{$IFDEF XML_UTF8}
System.AnsiStrings,
{$ENDIF}
Neslib.Hash;
const
EMPTY_HASH = -1;
{ TXmlStringInternPool }
function TXmlStringInternPool.Get(const AString: XmlString): Integer;
begin
if (FCount >= FGrowThreshold) then
Resize(Length(FItems) * 2);
var HashCode := MurmurHash2(Pointer(AString)^, Length(AString) * SizeOf(XmlChar));
var Mask := Length(FItems) - 1;
var Index := HashCode and Mask;
while True do
begin
var HC := FItems[Index].HashCode;
if (HC = EMPTY_HASH) then
Break;
if (HC = HashCode) and (FStrings[FItems[Index].Index] = AString) then
Exit(FItems[Index].Index);
Index := (Index + 1) and Mask;
end;
FStrings[FCount] := AString;
FItems[Index].HashCode := HashCode;
FItems[Index].Index := FCount;
Result := FCount;
Inc(FCount);
Assert(FCount <= (1 shl 17));
end;
{$IFDEF CPU64BITS}
function TXmlStringInternPool.AddAdditionalString(
const AString: XmlString): Integer;
begin
Result := FAdditionalCount;
if (FAdditionalCount >= Length(FAdditionalStrings)) then
SetLength(FAdditionalStrings, GrowCollection(Length(FAdditionalStrings), FAdditionalCount + 1));
FAdditionalStrings[FAdditionalCount] := AString;
Inc(FAdditionalCount);
end;
{$ENDIF}
procedure TXmlStringInternPool.Clear;
begin
FItems := nil;
FStrings := nil;
FCount := 0;
FGrowThreshold := 0;
{ Index 0 should be an empty string }
Get('');
{$IFDEF CPU64BITS}
FAdditionalStrings := nil;
FAdditionalCount := 0;
AddAdditionalString('');
{$ENDIF}
end;
constructor TXmlStringInternPool.Create;
begin
inherited;
Clear;
end;
function TXmlStringInternPool.Get(const AString: PXmlChar;
const ALength: Integer): Integer;
begin
if (FCount >= FGrowThreshold) then
Resize(Length(FItems) * 2);
var HashCode := MurmurHash2(AString^, ALength * SizeOf(XmlChar));
var Mask := Length(FItems) - 1;
var Index := HashCode and Mask;
while True do
begin
var HC := FItems[Index].HashCode;
if (HC = EMPTY_HASH) then
Break;
if (HC = HashCode) and (System.{$IFDEF XML_UTF8}AnsiStrings{$ELSE}SysUtils{$ENDIF}.StrLComp(PXmlChar(FStrings[FItems[Index].Index]), AString, ALength) = 0) then
Exit(FItems[Index].Index);
Index := (Index + 1) and Mask;
end;
SetString(FStrings[FCount], AString, ALength);
FItems[Index].HashCode := HashCode;
FItems[Index].Index := FCount;
Result := FCount;
Inc(FCount);
Assert(FCount <= (1 shl 17));
end;
function TXmlStringInternPool.Find(const AString: XmlString): Integer;
begin
var HashCode := MurmurHash2(Pointer(AString)^, Length(AString) * SizeOf(XmlChar));
var Mask := Length(FItems) - 1;
var Index := HashCode and Mask;
while True do
begin
var HC := FItems[Index].HashCode;
if (HC = EMPTY_HASH) then
Break;
if (HC = HashCode) and (FStrings[FItems[Index].Index] = AString) then
Exit(FItems[Index].Index);
Index := (Index + 1) and Mask;
end;
Result := -1;
end;
function TXmlStringInternPool.Get(const AIndex: Integer): XmlString;
begin
Assert(Cardinal(AIndex) < Cardinal(FCount));
Result := FStrings[AIndex];
end;
{$IFDEF CPU64BITS}
function TXmlStringInternPool.GetAdditionalString(
const AIndex: Integer): XmlString;
begin
Assert(Cardinal(AIndex) < Cardinal(FAdditionalCount));
Result := FAdditionalStrings[AIndex];
end;
{$ENDIF}
procedure TXmlStringInternPool.Resize(ANewSize: Integer);
var
OldItems, NewItems: TArray<TItem>;
begin
if (ANewSize < 4) then
ANewSize := 4;
var NewMask := ANewSize - 1;
SetLength(NewItems, ANewSize);
SetLength(FStrings, ANewSize); // TODO : Could be less
for var I := 0 to ANewSize - 1 do
NewItems[I].HashCode := EMPTY_HASH;
OldItems := FItems;
for var I := 0 to Length(OldItems) - 1 do
begin
if (OldItems[I].HashCode <> EMPTY_HASH) then
begin
var NewIndex := OldItems[I].HashCode and NewMask;
while (NewItems[NewIndex].HashCode <> EMPTY_HASH) do
NewIndex := (NewIndex + 1) and NewMask;
NewItems[NewIndex] := OldItems[I];
end;
end;
FItems := NewItems;
FGrowThreshold := (ANewSize * 3) shr 2; // 75%
end;
{ TXmlPointerMap }
procedure TXmlPointerMap.Clear;
begin
FItems := nil;
FCount := 0;
FGrowThreshold := 0;
end;
function TXmlPointerMap.Get(const AKey: Pointer; const AID: Integer): Pointer;
begin
if (FCount = 0) then
Exit(nil);
var Key := UIntPtr(AKey) + Cardinal(AID);
var HashCode := Hash(Key);
var Mask := Length(FItems) - 1;
var Index := HashCode and Mask;
while True do
begin
var HC := FItems[Index].HashCode;
if (HC = EMPTY_HASH) then
Exit(nil);
if (HC = HashCode) and (FItems[Index].Key = Key) then
Exit(FItems[Index].Value);
Index := (Index + 1) and Mask;
end;
end;
{$IFOPT Q+}
{$DEFINE Q_ON}
{$OVERFLOWCHECKS OFF}
{$ENDIF}
class function TXmlPointerMap.Hash(const AKey: UIntPtr): Integer;
var
H: UIntPtr;
begin
// MurmurHash3 finalizer
{$IFDEF CPU32BITS}
H := AKey xor (AKey shr 16);
H := H * $85ebca6b;
H := H xor (H shr 13);
H := H * $c2b2ae35;
Result := (H xor (H shr 16)) and $7FFFFFFF;
{$ELSE}
H := AKey xor (AKey shr 33);
H := H * $ff51afd7ed558ccd;
H := H xor (H shr 33);
H := H * $c4ceb9fe1a85ec53;
Result := (H xor (H shr 33)) and $7FFFFFFF;
{$ENDIF}
end;
{$IFDEF Q_ON}
{$OVERFLOWCHECKS ON}
{$ENDIF}
procedure TXmlPointerMap.Map(const AKey: Pointer; const AID: Integer;
const AValue: Pointer);
begin
if (FCount >= FGrowThreshold) then
Resize(Length(FItems) * 2);
var Key := UIntPtr(AKey) + Cardinal(AID);
var HashCode := Hash(Key);
var Mask := Length(FItems) - 1;
var Index := HashCode and Mask;
while True do
begin
var HC := FItems[Index].HashCode;
if (HC = EMPTY_HASH) then
Break;
if (HC = HashCode) and (FItems[Index].Key = Key) then
begin
{ Update }
FItems[Index].Value := AValue;
Exit;
end;
Index := (Index + 1) and Mask;
end;
FItems[Index].HashCode := HashCode;
FItems[Index].Key := Key;
FItems[Index].Value := AValue;
Inc(FCount);
end;
procedure TXmlPointerMap.Resize(ANewSize: Integer);
var
OldItems, NewItems: TArray<TItem>;
begin
if (ANewSize < 4) then
ANewSize := 4;
var NewMask := ANewSize - 1;
SetLength(NewItems, ANewSize);
for var I := 0 to ANewSize - 1 do
NewItems[I].HashCode := EMPTY_HASH;
OldItems := FItems;
for var I := 0 to Length(OldItems) - 1 do
begin
if (OldItems[I].HashCode <> EMPTY_HASH) then
begin
var NewIndex := OldItems[I].HashCode and NewMask;
while (NewItems[NewIndex].HashCode <> EMPTY_HASH) do
NewIndex := (NewIndex + 1) and NewMask;
NewItems[NewIndex] := OldItems[I];
end;
end;
FItems := NewItems;
FGrowThreshold := (ANewSize * 3) shr 2; // 75%
end;
end.