* SAX cleanup: removed {$ifdef UseDynArrays}, left variant without dynarrays, they don't provide any significant advantage.

* Changed TList to TFPList, removed redundant FLength field.

git-svn-id: trunk@20879 -
This commit is contained in:
sergei 2012-04-14 22:44:45 +00:00
parent f0b7ce8afe
commit 86f0a4177d

View File

@ -77,27 +77,18 @@ type
AttrType: String; AttrType: String;
end; end;
{$IFNDEF UseDynArrays}
PSAXAttributeData = ^TSAXAttributeData; PSAXAttributeData = ^TSAXAttributeData;
{$ENDIF}
TSAXAttributes = class TSAXAttributes = class
protected protected
FLength: Integer; FData: TFPList;
{$IFDEF UseDynArrays}
Data: array of TSAXAttributeData;
{$ELSE}
FData: TList;
function GetData(Index: Integer): PSAXAttributeData; function GetData(Index: Integer): PSAXAttributeData;
property Data[Index:Integer]: PSAXAttributeData read GetData; property Data[Index:Integer]: PSAXAttributeData read GetData;
{$ENDIF}
procedure BadIndex(Index: Integer); procedure BadIndex(Index: Integer);
public public
constructor Create; overload; constructor Create; overload;
constructor Create(Atts: TSAXAttributes); overload; constructor Create(Atts: TSAXAttributes); overload;
{$IFNDEF UseDynArrays}
destructor Destroy; override; destructor Destroy; override;
{$ENDIF}
function GetIndex(const QName: SAXString): Integer; overload; function GetIndex(const QName: SAXString): Integer; overload;
function GetIndex(const URI, LocalPart: SAXString): Integer; overload; function GetIndex(const URI, LocalPart: SAXString): Integer; overload;
@ -313,33 +304,27 @@ end;
constructor TSAXAttributes.Create; constructor TSAXAttributes.Create;
begin begin
inherited Create; inherited Create;
{$IFNDEF UseDynArrays} FData := TFPList.Create;
FData := TList.Create;
{$ENDIF}
end; end;
constructor TSAXAttributes.Create(Atts: TSAXAttributes); constructor TSAXAttributes.Create(Atts: TSAXAttributes);
begin begin
inherited Create; inherited Create;
{$IFNDEF UseDynArrays} FData := TFPList.Create;
FData := TList.Create;
{$ENDIF}
SetAttributes(Atts); SetAttributes(Atts);
end; end;
{$IFNDEF UseDynArrays}
destructor TSAXAttributes.Destroy; destructor TSAXAttributes.Destroy;
begin begin
Clear; Clear;
FData.Free; FData.Free;
inherited Destroy; inherited Destroy;
end; end;
{$ENDIF}
function TSAXAttributes.GetIndex(const QName: SAXString): Integer; function TSAXAttributes.GetIndex(const QName: SAXString): Integer;
begin begin
Result := 0; Result := 0;
while Result < FLength do while Result < FData.Count do
begin begin
if Data[Result]^.QName = QName then if Data[Result]^.QName = QName then
exit; exit;
@ -351,7 +336,7 @@ end;
function TSAXAttributes.GetIndex(const URI, LocalPart: SAXString): Integer; function TSAXAttributes.GetIndex(const URI, LocalPart: SAXString): Integer;
begin begin
Result := 0; Result := 0;
while Result < FLength do while Result < FData.Count do
begin begin
if (Data[Result]^.URI = URI) and (Data[Result]^.LocalName = LocalPart) then if (Data[Result]^.URI = URI) and (Data[Result]^.LocalName = LocalPart) then
exit; exit;
@ -362,12 +347,12 @@ end;
function TSAXAttributes.GetLength: Integer; function TSAXAttributes.GetLength: Integer;
begin begin
Result := FLength; Result := FData.Count;
end; end;
function TSAXAttributes.GetLocalName(Index: Integer): SAXString; function TSAXAttributes.GetLocalName(Index: Integer): SAXString;
begin begin
if (Index >= 0) and (Index < FLength) then if (Index >= 0) and (Index < FData.Count) then
Result := Data[Index]^.LocalName Result := Data[Index]^.LocalName
else else
SetLength(Result, 0); SetLength(Result, 0);
@ -375,7 +360,7 @@ end;
function TSAXAttributes.GetQName(Index: Integer): SAXString; function TSAXAttributes.GetQName(Index: Integer): SAXString;
begin begin
if (Index >= 0) and (Index < FLength) then if (Index >= 0) and (Index < FData.Count) then
Result := Data[Index]^.QName Result := Data[Index]^.QName
else else
SetLength(Result, 0); SetLength(Result, 0);
@ -383,7 +368,7 @@ end;
function TSAXAttributes.GetType(Index: Integer): String; function TSAXAttributes.GetType(Index: Integer): String;
begin begin
if (Index >= 0) and (Index < FLength) then if (Index >= 0) and (Index < FData.Count) then
Result := Data[Index]^.AttrType Result := Data[Index]^.AttrType
else else
SetLength(Result, 0); SetLength(Result, 0);
@ -393,7 +378,7 @@ function TSAXAttributes.GetType(const QName: SAXString): String;
var var
i: Integer; i: Integer;
begin begin
for i := 0 to FLength - 1 do for i := 0 to FData.Count - 1 do
if Data[i]^.QName = QName then if Data[i]^.QName = QName then
begin begin
Result := Data[i]^.AttrType; Result := Data[i]^.AttrType;
@ -406,7 +391,7 @@ function TSAXAttributes.GetType(const URI, LocalName: SAXString): String;
var var
i: Integer; i: Integer;
begin begin
for i := 0 to FLength - 1 do for i := 0 to FData.Count - 1 do
if (Data[i]^.URI = URI) and (Data[i]^.LocalName = LocalName) then if (Data[i]^.URI = URI) and (Data[i]^.LocalName = LocalName) then
begin begin
Result := Data[i]^.AttrType; Result := Data[i]^.AttrType;
@ -417,15 +402,15 @@ end;
function TSAXAttributes.GetURI(Index: Integer): SAXString; function TSAXAttributes.GetURI(Index: Integer): SAXString;
begin begin
if (Index >= 0) and (Index < FLength) then if (Index >= 0) and (Index < FData.Count) then
Result := Data[Index * 5]^.URI Result := Data[Index]^.URI
else else
SetLength(Result, 0); SetLength(Result, 0);
end; end;
function TSAXAttributes.GetValue(Index: Integer): SAXString; function TSAXAttributes.GetValue(Index: Integer): SAXString;
begin begin
if (Index >= 0) and (Index < FLength) then if (Index >= 0) and (Index < FData.Count) then
Result := Data[Index]^.Value Result := Data[Index]^.Value
else else
SetLength(Result, 0); SetLength(Result, 0);
@ -435,7 +420,7 @@ function TSAXAttributes.GetValue(const QName: SAXString): SAXString;
var var
i: Integer; i: Integer;
begin begin
for i := 0 to FLength - 1 do for i := 0 to FData.Count - 1 do
if Data[i]^.QName = QName then if Data[i]^.QName = QName then
begin begin
Result := Data[i]^.Value; Result := Data[i]^.Value;
@ -448,7 +433,7 @@ function TSAXAttributes.GetValue(const URI, LocalName: SAXString): SAXString;
var var
i: Integer; i: Integer;
begin begin
for i := 0 to FLength - 1 do for i := 0 to FData.Count - 1 do
if (Data[i]^.URI = URI) and (Data[i]^.LocalName = LocalName) then if (Data[i]^.URI = URI) and (Data[i]^.LocalName = LocalName) then
begin begin
Result := Data[i]^.Value; Result := Data[i]^.Value;
@ -458,39 +443,20 @@ begin
end; end;
procedure TSAXAttributes.Clear; procedure TSAXAttributes.Clear;
{$IFDEF UseDynArrays}
begin
SetLength(Data, 0);
end;
{$ELSE}
var var
i: Integer; i: Integer;
p: PSAXAttributeData;
begin begin
for i := 0 to FData.Count - 1 do for i := 0 to FData.Count - 1 do
begin Dispose(PSAXAttributeData(FData[i]));
p := PSAXAttributeData(FData[i]);
Dispose(p);
end;
end; end;
{$ENDIF}
procedure TSAXAttributes.SetAttributes(Atts: TSAXAttributes); procedure TSAXAttributes.SetAttributes(Atts: TSAXAttributes);
var var
i: Integer; i: Integer;
begin begin
FLength := Atts.Length; FData.Count := Atts.Length;
{$IFDEF UseDynArrays} for i := 0 to FData.Count - 1 do
SetLength(Data, FLength);
{$ELSE}
FData.Count := FLength;
{$ENDIF}
for i := 0 to FLength - 1 do
{$IFDEF UseDynArrays}
with Data[i] do
{$ELSE}
with Data[i]^ do with Data[i]^ do
{$ENDIF}
begin begin
URI := Atts.URIs[i]; URI := Atts.URIs[i];
LocalName := Atts.LocalNames[i]; LocalName := Atts.LocalNames[i];
@ -502,42 +468,24 @@ end;
procedure TSAXAttributes.AddAttribute(const AURI, ALocalName, AQName: SAXString; procedure TSAXAttributes.AddAttribute(const AURI, ALocalName, AQName: SAXString;
const AType: String; const AValue: SAXString); const AType: String; const AValue: SAXString);
{$IFNDEF UseDynArrays}
var var
p: PSAXAttributeData; p: PSAXAttributeData;
{$ENDIF}
begin begin
Inc(FLength);
{$IFDEF UseDynArrays}
SetLength(Data, FLength);
{$ELSE}
New(p); New(p);
FData.Add(p); FData.Add(p);
{$ENDIF} p^.URI := AURI;
{$IFDEF UseDynArrays} p^.LocalName := ALocalName;
with Data[FLength - 1] do p^.QName := AQName;
{$ELSE} p^.AttrType := AType;
with Data[FLength - 1]^ do p^.Value := AValue;
{$ENDIF}
begin
URI := AURI;
LocalName := ALocalName;
QName := AQName;
AttrType := AType;
Value := AValue;
end;
end; end;
procedure TSAXAttributes.SetAttribute(Index: Integer; procedure TSAXAttributes.SetAttribute(Index: Integer;
const AURI, ALocalName, AQName: SAXString; const AType: String; const AURI, ALocalName, AQName: SAXString; const AType: String;
const AValue: SAXString); const AValue: SAXString);
begin begin
if (Index >= 0) and (Index < FLength) then if (Index >= 0) and (Index < FData.Count) then
{$IFDEF UseDynArrays}
with Data[Index] do
{$ELSE}
with Data[Index]^ do with Data[Index]^ do
{$ENDIF}
begin begin
URI := AURI; URI := AURI;
LocalName := ALocalName; LocalName := ALocalName;
@ -550,29 +498,17 @@ begin
end; end;
procedure TSAXAttributes.RemoveAttribute(Index: Integer); procedure TSAXAttributes.RemoveAttribute(Index: Integer);
{$IFDEF UseDynArrays}
var
i: Integer;
{$ENDIF}
begin begin
if (Index >= 0) and (Index < FLength) then if (Index >= 0) and (Index < FData.Count) then
begin begin
{$IFDEF UseDynArrays}
for i := Index to FLength - 1 do
Data[i] := Data[i + 1];
Dec(FLength);
SetLength(Data, FLength);
{$ELSE}
FData.Delete(Index); FData.Delete(Index);
Dec(FLength);
{$ENDIF}
end else end else
BadIndex(Index); BadIndex(Index);
end; end;
procedure TSAXAttributes.SetURI(Index: Integer; const AURI: SAXString); procedure TSAXAttributes.SetURI(Index: Integer; const AURI: SAXString);
begin begin
if (Index >= 0) and (Index < FLength) then if (Index >= 0) and (Index < FData.Count) then
Data[Index]^.URI := AURI Data[Index]^.URI := AURI
else else
BadIndex(Index); BadIndex(Index);
@ -581,7 +517,7 @@ end;
procedure TSAXAttributes.SetLocalName(Index: Integer; procedure TSAXAttributes.SetLocalName(Index: Integer;
const ALocalName: SAXString); const ALocalName: SAXString);
begin begin
if (Index >= 0) and (Index < FLength) then if (Index >= 0) and (Index < FData.Count) then
Data[Index]^.LocalName := ALocalName Data[Index]^.LocalName := ALocalName
else else
BadIndex(Index); BadIndex(Index);
@ -589,7 +525,7 @@ end;
procedure TSAXAttributes.SetQName(Index: Integer; const AQName: SAXString); procedure TSAXAttributes.SetQName(Index: Integer; const AQName: SAXString);
begin begin
if (Index >= 0) and (Index < FLength) then if (Index >= 0) and (Index < FData.Count) then
Data[Index]^.QName := AQName Data[Index]^.QName := AQName
else else
BadIndex(Index); BadIndex(Index);
@ -597,7 +533,7 @@ end;
procedure TSAXAttributes.SetType(Index: Integer; const AType: String); procedure TSAXAttributes.SetType(Index: Integer; const AType: String);
begin begin
if (Index >= 0) and (Index < FLength) then if (Index >= 0) and (Index < FData.Count) then
Data[Index]^.AttrType := AType Data[Index]^.AttrType := AType
else else
BadIndex(Index); BadIndex(Index);
@ -605,18 +541,16 @@ end;
procedure TSAXAttributes.SetValue(Index: Integer; const AValue: SAXString); procedure TSAXAttributes.SetValue(Index: Integer; const AValue: SAXString);
begin begin
if (Index >= 0) and (Index < FLength) then if (Index >= 0) and (Index < FData.Count) then
Data[Index]^.Value := AValue Data[Index]^.Value := AValue
else else
BadIndex(Index); BadIndex(Index);
end; end;
{$IFNDEF UseDynArrays}
function TSAXAttributes.GetData(Index: Integer): PSAXAttributeData; function TSAXAttributes.GetData(Index: Integer): PSAXAttributeData;
begin begin
Result := PSAXAttributeData(FData[Index]); Result := PSAXAttributeData(FData[Index]);
end; end;
{$ENDIF}
procedure TSAXAttributes.BadIndex(Index: Integer); procedure TSAXAttributes.BadIndex(Index: Integer);
begin begin