📄 qexport4emswidestrutils.pas
字号:
SearchStr, Patt, NewStr: Widestring;
Offset: Integer;
begin
if rfIgnoreCase in Flags then
begin
SearchStr := QEUpperCase(S);
Patt := QEUpperCase(OldPattern);
end else
begin
SearchStr := S;
Patt := OldPattern;
end;
NewStr := S;
Result := '';
while SearchStr <> '' do
begin
Offset := Pos(Patt, SearchStr);
if Offset = 0 then
begin
Result := Result + NewStr;
Break;
end;
Result := Result + Copy(NewStr, 1, Offset - 1) + NewPattern;
NewStr := Copy(NewStr, Offset + Length(OldPattern), MaxInt);
if not (rfReplaceAll in Flags) then
begin
Result := Result + NewStr;
Break;
end;
SearchStr := Copy(SearchStr, Offset + Length(Patt), MaxInt);
end;
end;
{$ENDIF}
function QEStringReplace(const S, OldPattern, NewPattern: WideString;
Flags: TReplaceFlags): WideString;
begin
Result := WideStringReplace(S, OldPattern, NewPattern, Flags);
end;
{$ELSE}
function QEStringReplace(const S, OldPattern, NewPattern: string;
Flags: TReplaceFlags): string;
begin
Result := SysUtils.StringReplace(S, OldPattern, NewPattern, Flags);
end;
{$ENDIF}
{$IFDEF QE_UNICODE}
{$IFNDEF VCL10}
function WideQuotedStr(const S: WideString; Quote: WideChar): WideString;
var
P, Src,
Dest: PWideChar;
AddCount: Integer;
begin
AddCount := 0;
P := WStrScan(PWideChar(S), Quote);
while (P <> nil) do
begin
Inc(P);
Inc(AddCount);
P := WStrScan(P, Quote);
end;
if AddCount = 0 then
Result := Quote + S + Quote
else
begin
SetLength(Result, Length(S) + AddCount + 2);
Dest := PWideChar(Result);
Dest^ := Quote;
Inc(Dest);
Src := PWideChar(S);
P := WStrScan(Src, Quote);
repeat
Inc(P);
Move(Src^, Dest^, 2 * (P - Src));
Inc(Dest, P - Src);
Dest^ := Quote;
Inc(Dest);
Src := P;
P := WStrScan(Src, Quote);
until P = nil;
P := WStrEnd(Src);
Move(Src^, Dest^, 2 * (P - Src));
Inc(Dest, P - Src);
Dest^ := Quote;
end;
end;
{$ENDIF}
function QEQuotedStr(const S: WideString; Quote: WideChar): WideString;
begin
Result := WideQuotedStr(S, Quote);
end;
{$ELSE}
function QEQuotedStr(const S: string; Quote: Char): string;
begin
Result := SysUtils.AnsiQuotedStr(S, Quote);
end;
{$ENDIF}
{$IFDEF QE_UNICODE}
{$IFNDEF VCL9}
function WideFormat(const Format: WideString; const Args: array of const): WideString;
begin
WideFmtStr(Result, Format, Args);
end;
function WideReplaceStr(const AText, AFromText, AToText: WideString): WideString;
begin
Result := WideStringReplace(AText, AFromText, AToText, [rfReplaceAll]);
end;
function WideReplaceText(const AText, AFromText, AToText: WideString): WideString;
begin
Result := WideStringReplace(AText, AFromText, AToText, [rfReplaceAll, rfIgnoreCase]);
end;
function WStrAlloc(Size: Cardinal): PWideChar;
begin
Size := SizeOf(Cardinal) + (Size * SizeOf(WideChar));
GetMem(Result, Size);
PCardinal(Result)^ := Size;
Inc(PAnsiChar(Result), SizeOf(Cardinal));
end;
function WStrBufSize(const Str: PWideChar): Cardinal;
var
P: PWideChar;
begin
P := Str;
Dec(PAnsiChar(P), SizeOf(Cardinal));
Result := PCardinal(P)^ - SizeOf(Cardinal);
Result := Result div SizeOf(WideChar);
end;
{$ENDIF}
{$IFNDEF VCL10}
function WStrMove(Dest: PWideChar; const Source: PWideChar; Count: Cardinal): PWideChar;
var
Length: Integer;
begin
Result := Dest;
Length := Count * SizeOf(WideChar);
Move(Source^, Dest^, Length);
end;
{$ENDIF}
{$IFNDEF VCL9}
function WStrNew(const Str: PWideChar): PWideChar;
var
Size: Cardinal;
begin
if Str = nil then Result := nil else
begin
Size := WStrLen(Str) + 1;
Result := WStrMove(WStrAlloc(Size), Str, Size);
end;
end;
procedure WStrDispose(Str: PWideChar);
begin
if Str <> nil then
begin
Dec(PAnsiChar(Str), SizeOf(Cardinal));
FreeMem(Str, Cardinal(Pointer(Str)^));
end;
end;
{$ENDIF}
{$IFNDEF VCL9}
function WStrLen(Str: PWideChar): Cardinal;
begin
Result := WStrEnd(Str) - Str;
end;
function WStrEnd(Str: PWideChar): PWideChar;
begin
Result := Str;
While Result^ <> #0 do
Inc(Result);
end;
{$ENDIF}
{$IFNDEF VCL10}
function WStrCat(Dest: PWideChar; const Source: PWideChar): PWideChar;
begin
Result := Dest;
WStrCopy(WStrEnd(Dest), Source);
end;
{$ENDIF}
{$IFNDEF VCL9}
function WStrCopy(Dest, Source: PWideChar): PWideChar;
begin
Result := WStrLCopy(Dest, Source, MaxInt);
end;
function WStrLCopy(Dest, Source: PWideChar; MaxLen: Cardinal): PWideChar;
var
Count: Cardinal;
begin
Result := Dest;
Count := 0;
while (Count < MaxLen) and (Source^ <> #0) do
begin
Dest^ := Source^;
Inc(Source);
Inc(Dest);
Inc(Count);
end;
Dest^ := #0;
end;
function WStrPCopy(Dest: PWideChar; const Source: AnsiString): PWideChar;
begin
Result := WStrPLCopy(Dest, Source, MaxInt);
end;
function WStrPLCopy(Dest: PWideChar; const Source: AnsiString; MaxLen: Cardinal): PWideChar;
begin
Result := WStrLCopy(Dest, PWideChar(WideString(Source)), MaxLen);
end;
{$ENDIF}
{$IFNDEF VCL10}
function WStrScan(const Str: PWideChar; Chr: WideChar): PWideChar;
begin
Result := Str;
while Result^ <> Chr do
begin
if Result^ = #0 then
begin
Result := nil;
Exit;
end;
Inc(Result);
end;
end;
function WStrComp(Str1, Str2: PWideChar): Integer;
begin
Result := WStrLComp(Str1, Str2, MaxInt);
end;
function WStrPos(Str, SubStr: PWideChar): PWideChar;
var
PSave: PWideChar;
P: PWideChar;
PSub: PWideChar;
begin
Result := nil;
if (Str <> nil) and (Str^ <> #0) and (SubStr <> nil) and (SubStr^ <> #0) then
begin
P := Str;
while P^ <> #0 do
begin
if P^ = SubStr^ then
begin
PSave := P;
PSub := SubStr;
while (P^ = PSub^) do
begin
Inc(P);
Inc(PSub);
if (PSub^ = #0) then
begin
Result := PSave;
exit;
end;
if (P^ = #0) then
Exit;
end;
P := PSave;
end;
Inc(P);
end;
end;
end;
{$ENDIF}
function WStrECopy(Dest, Source: PWideChar): PWideChar;
begin
Result := WStrEnd(WStrCopy(Dest, Source));
end;
function WStrComp_EX(Str1, Str2: PWideChar; MaxLen: Cardinal; dwCmpFlags: Cardinal): Integer;
var
Len1, Len2: Integer;
begin
if MaxLen = Cardinal(MaxInt) then
begin
Len1 := -1;
Len2 := -1;
end
else begin
Len1 := Min(WStrLen(Str1), MaxLen);
Len2 := Min(WStrLen(Str2), MaxLen);
end;
Result := CompareStringW(GetThreadLocale, dwCmpFlags, Str1, Len1, Str2, Len2) - 2;
end;
function WStrLComp(Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;
begin
Result := WStrComp_EX(Str1, Str2, MaxLen, 0);
end;
function WStrLIComp(Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;
begin
Result := WStrComp_EX(Str1, Str2, MaxLen, NORM_IGNORECASE);
end;
function WStrIComp(Str1, Str2: PWideChar): Integer;
begin
Result := WStrLIComp(Str1, Str2, MaxInt);
end;
function WStrLower(Str: PWideChar): PWideChar;
begin
Result := Str;
CharLowerBuffW(Str, WStrLen(Str))
end;
function WStrUpper(Str: PWideChar): PWideChar;
begin
Result := Str;
CharUpperBuffW(Str, WStrLen(Str))
end;
function WStrRScan(const Str: PWideChar; Chr: WideChar): PWideChar;
var
MostRecentFound: PWideChar;
begin
if Chr = #0 then
Result := WStrEnd(Str)
else
begin
Result := nil;
MostRecentFound := Str;
while True do
begin
while MostRecentFound^ <> Chr do
begin
if MostRecentFound^ = #0 then
Exit;
Inc(MostRecentFound);
end;
Result := MostRecentFound;
Inc(MostRecentFound);
end;
end;
end;
function WStrLCat(Dest: PWideChar; const Source: PWideChar; MaxLen: Cardinal): PWideChar;
begin
Result := Dest;
WStrLCopy(WStrEnd(Dest), Source, MaxLen - WStrLen(Dest));
end;
function WStrPas(const Str: PWideChar): WideString;
begin
Result := Str;
end;
{$IFNDEF VCL10}
function WideLastChar(const S: WideString): PWideChar;
begin
if S = '' then
Result := nil
else
Result := @S[Length(S)];
end;
{$ENDIF}
{$IFNDEF VCL9}
function WideExtractQuotedStr(var Src: PWideChar; Quote: WideChar): Widestring;
var
P, Dest: PWideChar;
DropCount: Integer;
begin
Result := '';
if (Src = nil) or (Src^ <> Quote) then Exit;
Inc(Src);
DropCount := 1;
P := Src;
Src := WStrScan(Src, Quote);
while Src <> nil do
begin
Inc(Src);
if Src^ <> Quote then Break;
Inc(Src);
Inc(DropCount);
Src := WStrScan(Src, Quote);
end;
if Src = nil then Src := WStrEnd(P);
if ((Src - P) <= 1) then Exit;
if DropCount = 1 then
SetString(Result, P, Src - P - 1)
else
begin
SetLength(Result, Src - P - DropCount);
Dest := PWideChar(Result);
Src := WStrScan(P, Quote);
while Src <> nil do
begin
Inc(Src);
if Src^ <> Quote then Break;
Move(P^, Dest^, (Src - P) * SizeOf(WideChar));
Inc(Dest, Src - P);
Inc(Src);
P := Src;
Src := WStrScan(Src, Quote);
end;
if Src = nil then Src := WStrEnd(P);
Move(P^, Dest^, (Src - P - 1) * SizeOf(WideChar));
end;
end;
{$ENDIF}
{$IFNDEF VCL10}
function WideDequotedStr(const S: WideString; AQuote: WideChar): WideString;
var
LText : PWideChar;
begin
LText := PWideChar(S);
Result := WideExtractQuotedStr(LText, AQuote);
if Result = '' then
Result := S;
end;
{$ENDIF}
{$ENDIF}
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -