📄 jclwidestrings.pas
字号:
case P^ of
WideChar(1)..WideChar(32):
Inc(P);
else
Break;
end;
end;
begin
BeginUpdate;
try
Clear;
P := PWideChar(Value);
IgnoreWhiteSpace(P);
while P[0] <> WideChar(0) do
begin
if P[0] = AQuoteChar then
S := WideExtractQuotedStr(P, AQuoteChar)
else
begin
P1 := P;
while (P[0] > WideChar(32)) and (P[0] <> ADelimiter) do
Inc(P);
SetString(S, P1, P - P1);
end;
Add(S);
IgnoreWhiteSpace(P);
if P[0] = ADelimiter then
begin
Inc(P);
IgnoreWhiteSpace(P);
end;
end;
finally
EndUpdate;
end;
end;
procedure TWStrings.SetText(Text: PWideChar);
begin
SetTextStr(Text);
end;
procedure TWStrings.SetTextStr(const Value: WideString);
var
P, Start: PWideChar;
S: WideString;
Len: Integer;
begin
BeginUpdate;
try
Clear;
if Value <> '' then
begin
P := PWideChar(Value);
if P <> nil then
begin
while P[0] <> WideChar(0) do
begin
Start := P;
while True do
begin
case P[0] of
WideChar(0), WideChar(10), WideChar(13):
Break;
end;
Inc(P);
end;
Len := P - Start;
if Len > 0 then
begin
SetString(S, Start, Len);
AddObject(S, nil); // consumes most time
end
else
AddObject('', nil);
if P[0] = WideChar(13) then
Inc(P);
if P[0] = WideChar(10) then
Inc(P);
end;
end;
end;
finally
EndUpdate;
end;
end;
procedure TWStrings.SetUpdateState(Updating: Boolean);
begin
end;
procedure TWStrings.SetValue(const Name, Value: WideString);
var
Idx: Integer;
begin
Idx := IndexOfName(Name);
if Idx >= 0 then
SetValueFromIndex(Idx, Value)
else
if Value <> '' then
Add(Name + NameValueSeparator + Value);
end;
procedure TWStrings.SetValueFromIndex(Index: Integer; const Value: WideString);
var
S: WideString;
I: Integer;
begin
if Value = '' then
Delete(Index)
else
begin
if Index < 0 then
Index := Add('');
S := GetP(Index)^;
I := WidePos(NameValueSeparator, S);
if I > 0 then
System.Delete(S, I, MaxInt);
S := S + NameValueSeparator + Value;
Put(Index, S);
end;
end;
procedure TWStrings.WriteData(Writer: TWriter);
var
I: Integer;
begin
Writer.WriteListBegin;
for I := 0 to Count - 1 do
Writer.WriteWideString(GetP(I)^);
Writer.WriteListEnd;
end;
//=== { TWStringList } =======================================================
constructor TWStringList.Create;
begin
inherited Create;
FList := TList.Create;
end;
destructor TWStringList.Destroy;
begin
FOnChange := nil;
FOnChanging := nil;
Inc(FUpdateCount); // do not call unnecessary functions
Clear;
FList.Free;
inherited Destroy;
end;
function TWStringList.AddObject(const S: WideString; AObject: TObject): Integer;
begin
if not Sorted then
Result := Count
else
if Find(S, Result) then
case Duplicates of
dupIgnore:
Exit;
dupError:
raise EListError.CreateRes(@SDuplicateString);
end;
InsertObject(Result, S, AObject);
end;
procedure TWStringList.Changed;
begin
if Assigned(FOnChange) then
FOnChange(Self);
end;
procedure TWStringList.Changing;
begin
if Assigned(FOnChanging) then
FOnChanging(Self);
end;
procedure TWStringList.Clear;
var
I: Integer;
Item: PWStringItem;
begin
if FUpdateCount = 0 then
Changing;
for I := 0 to Count - 1 do
begin
Item := PWStringItem(FList[I]);
Item.FString := '';
FreeMem(Item);
end;
FList.Clear;
if FUpdateCount = 0 then
Changed;
end;
function TWStringList.CompareStrings(const S1, S2: WideString): Integer;
begin
if CaseSensitive then
Result := WideCompareStr(S1, S2)
else
Result := WideCompareText(S1, S2);
end;
threadvar
CustomSortList: TWStringList;
CustomSortCompare: TWStringListSortCompare;
function WStringListCustomSort(Item1, Item2: Pointer): Integer;
begin
Result := CustomSortCompare(CustomSortList,
CustomSortList.FList.IndexOf(Item1),
CustomSortList.FList.IndexOf(Item2));
end;
procedure TWStringList.CustomSort(Compare: TWStringListSortCompare);
var
TempList: TWStringList;
TempCompare: TWStringListSortCompare;
begin
TempList := CustomSortList;
TempCompare := CustomSortCompare;
CustomSortList := Self;
CustomSortCompare := Compare;
try
Changing;
FList.Sort(WStringListCustomSort);
Changed;
finally
CustomSortList := TempList;
CustomSortCompare := TempCompare;
end;
end;
procedure TWStringList.Delete(Index: Integer);
var
Item: PWStringItem;
begin
if FUpdateCount = 0 then
Changing;
Item := PWStringItem(FList[Index]);
FList.Delete(Index);
Item.FString := '';
FreeMem(Item);
if FUpdateCount = 0 then
Changed;
end;
procedure TWStringList.Exchange(Index1, Index2: Integer);
begin
if FUpdateCount = 0 then
Changing;
FList.Exchange(Index1, Index2);
if FUpdateCount = 0 then
Changed;
end;
function TWStringList.Find(const S: WideString; var Index: Integer): Boolean;
var
L, H, I, C: Integer;
begin
Result := False;
if Sorted then
begin
L := 0;
H := Count - 1;
while L <= H do
begin
I := (L + H) shr 1;
C := CompareStrings(GetItem(I).FString, S);
if C < 0 then
L := I + 1
else
begin
H := I - 1;
if C = 0 then
begin
Result := True;
if Duplicates <> dupAccept then
L := I;
end;
end;
end;
Index := L;
end
else
begin
Index := IndexOf(S);
Result := Index <> -1;
end;
end;
function TWStringList.GetCapacity: Integer;
begin
Result := FList.Capacity;
end;
function TWStringList.GetCount: Integer;
begin
Result := FList.Count;
end;
function TWStringList.GetItem(Index: Integer): PWStringItem;
begin
Result := FList[Index];
end;
function TWStringList.GetObject(Index: Integer): TObject;
begin
Result := GetItem(Index).FObject;
end;
function TWStringList.GetP(Index: Integer): PWideString;
begin
Result := Addr(GetItem(Index).FString);
end;
function TWStringList.IndexOf(const S: WideString): Integer;
begin
if Sorted then
begin
if not Find(S, Result) then
Result := -1;
end
else
begin
for Result := 0 to Count - 1 do
if CompareStrings(GetItem(Result).FString, S) = 0 then
Exit;
Result := -1;
end;
end;
procedure TWStringList.InsertObject(Index: Integer; const S: WideString;
AObject: TObject);
var
P: PWStringItem;
begin
if FUpdateCount = 0 then
Changing;
FList.Insert(Index, nil); // error check
P := AllocMem(SizeOf(TWStringItem));
FList[Index] := P;
Put(Index, S);
if AObject <> nil then
PutObject(Index, AObject);
if FUpdateCount = 0 then
Changed;
end;
procedure TWStringList.Put(Index: Integer; const Value: WideString);
begin
if FUpdateCount = 0 then
Changing;
GetItem(Index).FString := Value;
if FUpdateCount = 0 then
Changed;
end;
procedure TWStringList.PutObject(Index: Integer; AObject: TObject);
begin
if FUpdateCount = 0 then
Changing;
GetItem(Index).FObject := AObject;
if FUpdateCount = 0 then
Changed;
end;
procedure TWStringList.SetCapacity(NewCapacity: Integer);
begin
FList.Capacity := NewCapacity;
end;
procedure TWStringList.SetCaseSensitive(const Value: Boolean);
begin
if Value <> FCaseSensitive then
begin
FCaseSensitive := Value;
if Sorted then
begin
Sorted := False;
Sorted := True; // re-sort
end;
end;
end;
procedure TWStringList.SetSorted(Value: Boolean);
begin
if Value <> FSorted then
begin
FSorted := Value;
if FSorted then
begin
FSorted := False;
Sort;
FSorted := True;
end;
end;
end;
procedure TWStringList.SetUpdateState(Updating: Boolean);
begin
if Updating then
Changing
else
Changed;
end;
function DefaultSort(List: TWStringList; Index1, Index2: Integer): Integer;
begin
Result := List.CompareStrings(List.GetItem(Index1).FString, List.GetItem(Index2).FString);
end;
procedure TWStringList.Sort;
begin
if not Sorted then
CustomSort(DefaultSort);
end;
// History:
// $Log: JclWideStrings.pas,v $
// Revision 1.11 2005/03/08 08:33:18 marquardt
// overhaul of exceptions and resourcestrings, minor style cleaning
//
// Revision 1.10 2005/03/01 15:37:40 marquardt
// addressing Mantis 0714, 0716, 0720, 0731, 0740 partly or completely
//
// Revision 1.9 2005/02/24 16:34:40 marquardt
// remove divider lines, add section lines (unfinished)
//
// Revision 1.8 2005/02/14 00:47:23 rrossmair
// - removed (redundant) comment in German language.
//
// Revision 1.7 2004/10/25 15:12:30 marquardt
// fix internal error
//
// Revision 1.6 2004/10/17 21:49:03 rrossmair
// added CVS Log entries
//
// Revision 1.5 rossmair
// fixed D6, FPC compatibility
//
// Revision 1.4 marquardt
// complete and fix PWideChar Str functions
//
// Revision 1.3 marquardt
// PH cleaning of JclStrings
//
// Revision 1.2 rrossmair
// replaced some conditional compilation symbols by more appropriate ones
//
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -