📄 rm_jclwidestrings.pas.~1~
字号:
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: WStrUtils.PAS, released on 2004-01-25.
The Initial Developers of the Original Code are Andreas Hausladen <Andreas dott Hausladen att gmx dott de>
and Mike Lischke (WideQuotedStr & WideExtractQuotedStr from Unicode.pas).
All Rights Reserved.
Contributors:
Robert Marquardt (marquardt)
Robert Rossmair (rrossmair)
You may retrieve the latest version of this file at the Project JEDI's JCL home page,
located at http://jcl.sourceforge.net
This is a lightweight Unicode unit. For more features use JclUnicode.
Known Issues:
-----------------------------------------------------------------------------}
unit rm_JclWideStrings;
{$I rm_jcl.inc}
interface
{$IFNDEF DELPHI10_UP}
uses
Classes, SysUtils;
const
// definitions of often used characters:
// Note: Use them only for tests of a certain character not to determine character
// classes (like white spaces) as in Unicode are often many code points defined
// being in a certain class. Hence your best option is to use the various
// UnicodeIs* functions.
WideNull = WideChar(#0);
WideTabulator = WideChar(#9);
WideSpace = WideChar(#32);
// logical line breaks
WideLF = WideChar(#10);
WideLineFeed = WideChar(#10);
WideVerticalTab = WideChar(#11);
WideFormFeed = WideChar(#12);
WideCR = WideChar(#13);
WideCarriageReturn = WideChar(#13);
WideCRLF: WideString = #13#10;
WideLineSeparator = WideChar($2028);
WideParagraphSeparator = WideChar($2029);
BOM_LSB_FIRST = WideChar($FEFF);
BOM_MSB_FIRST = WideChar($FFFE);
type
TWideFileOptionsType =
(
foAnsiFile, // loads/writes an ANSI file
foUnicodeLB // reads/writes BOM_LSB_FIRST/BOM_MSB_FIRST
);
TWideFileOptions = set of TWideFileOptionsType;
TSearchFlag = (
sfCaseSensitive, // match letter case
sfIgnoreNonSpacing, // ignore non-spacing characters in search
sfSpaceCompress, // handle several consecutive white spaces as one white space
// (this applies to the pattern as well as the search text)
sfWholeWordOnly // match only text at end/start and/or surrounded by white spaces
);
TSearchFlags = set of TSearchFlag;
TWStrings = class;
TWStringList = class;
TWStringListSortCompare = function(List: TWStringList; Index1, Index2: Integer): Integer;
TWStrings = class(TPersistent)
private
FDelimiter: WideChar;
FQuoteChar: WideChar;
FNameValueSeparator: WideChar;
FLineSeparator: WideString;
FUpdateCount: Integer;
function GetCommaText: WideString;
function GetDelimitedText: WideString;
function GetName(Index: Integer): WideString;
function GetValue(const Name: WideString): WideString;
procedure ReadData(Reader: TReader);
procedure SetCommaText(const Value: WideString);
procedure SetDelimitedText(const Value: WideString);
procedure SetValue(const Name, Value: WideString);
procedure WriteData(Writer: TWriter);
function GetValueFromIndex(Index: Integer): WideString;
procedure SetValueFromIndex(Index: Integer; const Value: WideString);
protected
procedure DefineProperties(Filer: TFiler); override;
function ExtractName(const S: WideString): WideString;
function GetP(Index: Integer): PWideString; virtual; abstract;
function Get(Index: Integer): WideString;
function GetCapacity: Integer; virtual;
function GetCount: Integer; virtual; abstract;
function GetObject(Index: Integer): TObject; virtual;
function GetTextStr: WideString; virtual;
procedure Put(Index: Integer; const S: WideString); virtual; abstract;
procedure PutObject(Index: Integer; AObject: TObject); virtual; abstract;
procedure SetCapacity(NewCapacity: Integer); virtual;
procedure SetTextStr(const Value: WideString); virtual;
procedure SetUpdateState(Updating: Boolean); virtual;
property UpdateCount: Integer read FUpdateCount;
function CompareStrings(const S1, S2: WideString): Integer; virtual;
procedure AssignTo(Dest: TPersistent); override;
public
constructor Create;
function Add(const S: WideString): Integer; virtual;
function AddObject(const S: WideString; AObject: TObject): Integer; virtual;
procedure Append(const S: WideString);
procedure AddStrings(Strings: TWStrings); overload; virtual;
procedure AddStrings(Strings: TStrings); overload; virtual;
procedure Assign(Source: TPersistent); override;
function CreateAnsiStringList: TStrings;
procedure AddStringsTo(Dest: TStrings); virtual;
procedure BeginUpdate;
procedure Clear; virtual; abstract;
procedure Delete(Index: Integer); virtual; abstract;
procedure EndUpdate;
function Equals(Strings: TWStrings): Boolean; overload;
function Equals(Strings: TStrings): Boolean; overload;
procedure Exchange(Index1, Index2: Integer); virtual;
function GetText: PWideChar; virtual;
function IndexOf(const S: WideString): Integer; virtual;
function IndexOfName(const Name: WideString): Integer; virtual;
function IndexOfObject(AObject: TObject): Integer; virtual;
procedure Insert(Index: Integer; const S: WideString); virtual;
procedure InsertObject(Index: Integer; const S: WideString;
AObject: TObject); virtual;
procedure LoadFromFile(const FileName: AnsiString;
WideFileOptions: TWideFileOptions = []); virtual;
procedure LoadFromStream(Stream: TStream;
WideFileOptions: TWideFileOptions = []); virtual;
procedure Move(CurIndex, NewIndex: Integer); virtual;
procedure SaveToFile(const FileName: AnsiString;
WideFileOptions: TWideFileOptions = []); virtual;
procedure SaveToStream(Stream: TStream;
WideFileOptions: TWideFileOptions = []); virtual;
procedure SetText(Text: PWideChar); virtual;
function GetDelimitedTextEx(ADelimiter, AQuoteChar: WideChar): WideString;
procedure SetDelimitedTextEx(ADelimiter, AQuoteChar: WideChar; const Value: WideString);
property Capacity: Integer read GetCapacity write SetCapacity;
property CommaText: WideString read GetCommaText write SetCommaText;
property Count: Integer read GetCount;
property Delimiter: WideChar read FDelimiter write FDelimiter;
property DelimitedText: WideString read GetDelimitedText write SetDelimitedText;
property Names[Index: Integer]: WideString read GetName;
property Objects[Index: Integer]: TObject read GetObject write PutObject;
property QuoteChar: WideChar read FQuoteChar write FQuoteChar;
property Values[const Name: WideString]: WideString read GetValue write SetValue;
property ValueFromIndex[Index: Integer]: WideString read GetValueFromIndex write SetValueFromIndex;
property NameValueSeparator: WideChar read FNameValueSeparator write FNameValueSeparator;
property LineSeparator: WideString read FLineSeparator write FLineSeparator;
property PStrings[Index: Integer]: PWideString read GetP;
property Strings[Index: Integer]: WideString read Get write Put; default;
property Text: WideString read GetTextStr write SetTextStr;
end;
// do not replace by JclUnicode.TWideStringList (speed and size issue)
PWStringItem = ^TWStringItem;
TWStringItem = record
FString: WideString;
FObject: TObject;
end;
TWStringList = class(TWStrings)
private
FList: TList;
FSorted: Boolean;
FDuplicates: TDuplicates;
FCaseSensitive: Boolean;
FOnChange: TNotifyEvent;
FOnChanging: TNotifyEvent;
procedure SetSorted(Value: Boolean);
procedure SetCaseSensitive(const Value: Boolean);
protected
function GetItem(Index: Integer): PWStringItem;
procedure Changed; virtual;
procedure Changing; virtual;
function GetP(Index: Integer): PWideString; override;
function GetCapacity: Integer; override;
function GetCount: Integer; override;
function GetObject(Index: Integer): TObject; override;
procedure Put(Index: Integer; const Value: WideString); override;
procedure PutObject(Index: Integer; AObject: TObject); override;
procedure SetCapacity(NewCapacity: Integer); override;
procedure SetUpdateState(Updating: Boolean); override;
function CompareStrings(const S1, S2: WideString): Integer; override;
public
constructor Create;
destructor Destroy; override;
function AddObject(const S: WideString; AObject: TObject): Integer; override;
procedure Clear; override;
procedure Delete(Index: Integer); override;
procedure Exchange(Index1, Index2: Integer); override;
function Find(const S: WideString; var Index: Integer): Boolean; virtual;
// Find() also works with unsorted lists
function IndexOf(const S: WideString): Integer; override;
procedure InsertObject(Index: Integer; const S: WideString;
AObject: TObject); override;
procedure Sort; virtual;
procedure CustomSort(Compare: TWStringListSortCompare); virtual;
property Duplicates: TDuplicates read FDuplicates write FDuplicates;
property Sorted: Boolean read FSorted write SetSorted;
property CaseSensitive: Boolean read FCaseSensitive write SetCaseSensitive;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
property OnChanging: TNotifyEvent read FOnChanging write FOnChanging;
end;
TWideStringList = TWStringList;
TWideStrings = TWStrings;
// WideChar functions
function CharToWideChar(Ch: AnsiChar): WideChar;
function WideCharToChar(Ch: WideChar): AnsiChar;
// PWideChar functions
procedure MoveWideChar(const Source; var Dest; Count: Integer);
function StrLenW(const Str: PWideChar): Cardinal;
function StrEndW(const Str: PWideChar): PWideChar;
function StrMoveW(Dest: PWideChar; const Source: PWideChar; Count: Cardinal): PWideChar;
function StrCopyW(Dest: PWideChar; const Source: PWideChar): PWideChar;
function StrECopyW(Dest: PWideChar; const Source: PWideChar): PWideChar;
function StrLCopyW(Dest: PWideChar; const Source: PWideChar; MaxLen: Cardinal): PWideChar;
function StrPCopyWW(Dest: PWideChar; const Source: WideString): PWideChar;
function StrPCopyW(Dest: PWideChar; const Source: string): PWideChar;
function StrPLCopyWW(Dest: PWideChar; const Source: WideString; MaxLen: Cardinal): PWideChar;
function StrPLCopyW(Dest: PWideChar; const Source: string; MaxLen: Cardinal): PWideChar;
function StrCatW(Dest: PWideChar; const Source: PWideChar): PWideChar;
function StrLCatW(Dest: PWideChar; const Source: PWideChar; MaxLen: Cardinal): PWideChar;
function StrCompW(const Str1, Str2: PWideChar): Integer;
function StrICompW(const Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;
function StrLCompW(const Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;
function StrLICompW(const Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;
function StrLICompW2(const Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;
function StrNScanW(const Str1, Str2: PWideChar): Integer;
function StrRNScanW(const Str1, Str2: PWideChar): Integer;
function StrScanW(const Str: PWideChar; Ch: WideChar): PWideChar; overload;
function StrScanW(Str: PWideChar; Chr: WideChar; StrLen: Cardinal): PWideChar; overload;
function StrRScanW(const Str: PWideChar; Chr: WideChar): PWideChar;
function StrPosW(const Str, SubStr: PWideChar): PWideChar;
function StrAllocW(WideSize: Cardinal): PWideChar;
function StrBufSizeW(const Str: PWideChar): Cardinal;
function StrNewW(const Str: PWideChar): PWideChar; overload;
function StrNewW(const Str: WideString): PWideChar; overload;
procedure StrDisposeW(Str: PWideChar);
procedure StrDisposeAndNilW(var Str: PWideChar);
procedure StrSwapByteOrder(Str: PWideChar);
// WideString functions
function WidePos(const SubStr, S: WideString): Integer;
function WideQuotedStr(const S: WideString; Quote: WideChar): WideString;
function WideExtractQuotedStr(var Src: PWideChar; Quote: WideChar): WideString;
{$IFNDEF RTL140_UP}
function WideCompareText(const S1, S2: WideString): Integer;
function WideCompareStr(const S1, S2: WideString): Integer;
function WideUpperCase(const S: WideString): WideString;
function WideLowerCase(const S: WideString): WideString;
{$ENDIF ~RTL140_UP}
function TrimW(const S: WideString): WideString;
function TrimLeftW(const S: WideString): WideString;
function TrimRightW(const S: WideString): WideString;
function TrimLeftLengthW(const S: WideString): Integer;
function TrimRightLengthW(const S: WideString): Integer;
{$ENDIF}
implementation
{$IFNDEF DELPHI10_UP}
uses
{$IFDEF HAS_UNIT_RTLCONSTS}
RTLConsts,
{$ELSE}
Consts,
{$ENDIF HAS_UNIT_RTLCONSTS}
{$IFDEF MSWINDOWS}
Windows,
{$ENDIF MSWINDOWS}
Math;
procedure SwapWordByteOrder(P: PChar; Len: Cardinal);
var
B: Char;
begin
while Len > 0 do
begin
B := P[0];
P[0] := P[1];
P[1] := B;
Inc(P, 2);
Dec(Len);
end;
end;
//=== WideChar functions =====================================================
function CharToWideChar(Ch: Char): WideChar;
var
WS: WideString;
begin
WS := Ch;
Result := WS[1];
end;
function WideCharToChar(Ch: WideChar): AnsiChar;
var
S: AnsiString;
begin
S := Ch;
Result := S[1];
end;
//=== PWideChar functions ====================================================
procedure MoveWideChar(const Source; var Dest; Count: Integer);
begin
Move(Source, Dest, Count * SizeOf(WideChar));
end;
function StrAllocW(WideSize: Cardinal): PWideChar;
begin
WideSize := SizeOf(WideChar) * WideSize + SizeOf(Cardinal);
Result := AllocMem(WideSize);
Cardinal(Pointer(Result)^) := WideSize;
Inc(Result, SizeOf(Cardinal) div SizeOf(WideChar));
end;
function StrNewW(const Str: PWideChar): PWideChar;
// Duplicates the given string (if not nil) and returns the address of the new string.
var
Size: Cardinal;
begin
if Str = nil then
Result := nil
else
begin
Size := StrLenW(Str) + 1;
Result := StrMoveW(StrAllocW(Size), Str, Size);
end;
end;
function StrNewW(const Str: WideString): PWideChar;
begin
Result := StrNewW(PWideChar(Str));
end;
procedure StrDisposeW(Str: PWideChar);
// releases a string allocated with StrNewW or StrAllocW
begin
if Str <> nil then
begin
Dec(Str, SizeOf(Cardinal) div SizeOf(WideChar));
FreeMem(Str);
end;
end;
procedure StrDisposeAndNilW(var Str: PWideChar);
begin
StrDisposeW(Str);
Str := nil;
end;
function StrLICompW(const Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;
var
P1, P2: WideString;
begin
SetString(P1, Str1, Min(MaxLen, StrLenW(Str1)));
SetString(P2, Str2, Min(MaxLen, StrLenW(Str2)));
Result := WideCompareText(P1, P2);
end;
function StrLICompW2(const Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;
var
P1, P2: WideString;
begin
// faster than the JclUnicode.StrLICompW function
SetString(P1, Str1, Min(MaxLen, StrLenW(Str1)));
SetString(P2, Str2, Min(MaxLen, StrLenW(Str2)));
Result := WideCompareText(P1, P2);
end;
function StrCompW(const Str1, Str2: PWideChar): Integer;
var
NullWide: WideChar;
SA, SB: PWideChar;
begin
Result := 0;
if Str1 = Str2 then // "equal" and "nil" case
Exit;
NullWide := #0;
if Str1 = nil then
SA := @NullWide
else
SA := Str1;
if Str2 = nil then
SB := @NullWide
else
SB := Str2;
while (SA^ = SB^) and (SA^ <> #0) and (SB^ <> #0) do
begin
Inc(SA);
Inc(SB);
end;
Result := Integer(SA^) - Integer(SB^);
end;
function StrLCompW(const Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;
var
NullWide: WideChar;
SA, SB: PWideChar;
begin
Result := 0;
if Str1 = Str2 then // "equal" and "nil" case
Exit;
NullWide := #0;
if Str1 = nil then
SA := @NullWide
else
SA := Str1;
if Str2 = nil then
SB := @NullWide
else
SB := Str2;
while (MaxLen > 0) and (SA^ = SB^) and (SA^ <> #0) and (SB^ <> #0) do
begin
Inc(SA);
Inc(SB);
Dec(MaxLen);
end;
if MaxLen > 0 then
Result := Integer(SA^) - Integer(SB^)
else
Result := 0;
end;
function StrICompW(const Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;
begin
Result := StrLICompW(Str1, Str2, Max(StrLenW(Str1), StrLenW(Str2)));
end;
function StrPosW(const Str, SubStr: PWideChar): PWideChar;
var
P: PWideChar;
I: Integer;
begin
Result := nil;
if (Str = nil) or (SubStr = nil) or (Str^ = #0) or (SubStr^ = #0) then
Exit;
Result := Str;
while Result^ <> #0 do
begin
if Result^ <> SubStr^ then
Inc(Result)
else
begin
P := Result + 1;
I := 1;
while (P^ <> #0) and (P^ = SubStr[I]) do
begin
Inc(I);
Inc(P);
end;
if SubStr[I] = #0 then
Exit
else
Inc(Result);
end;
end;
Result := nil;
end;
function StrLenW(const Str: PWideChar): Cardinal;
begin
Result := 0;
if Str <> nil then
while Str[Result] <> #0 do
Inc(Result);
end;
function StrScanW(const Str: PWideChar; Ch: WideChar): PWideChar;
begin
Result := Str;
if Result <> nil then
begin
while (Result^ <> #0) and (Result^ <> Ch) do
Inc(Result);
if (Result^ = #0) and (Ch <> #0) then
Result := nil;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -