* Add SplitCommandLine

git-svn-id: trunk@42269 -
This commit is contained in:
michael 2019-06-22 13:37:47 +00:00
parent fb387da807
commit bf54543518

View File

@ -266,6 +266,15 @@ Function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceF
Function StringReplace(const S, OldPattern, NewPattern: unicodestring; Flags: TReplaceFlags): unicodestring; overload;
Function StringReplace(const S, OldPattern, NewPattern: widestring; Flags: TReplaceFlags): widestring; overload;
Type
TRawByteStringArray = Array of RawByteString;
TUnicodeStringArray = Array of UnicodeString;
Function SplitCommandLine(S : RawByteString) : TRawByteStringArray;
Function SplitCommandLine(S : UnicodeString) : TUnicodeStringArray;
implementation
(*
@ -3271,4 +3280,148 @@ begin
end;
Function SplitCommandLine(S : RawByteString) : TRawByteStringArray;
Function GetNextWord : RawByteString;
Const
WhiteSpace = [' ',#9,#10,#13];
Literals = ['"',''''];
Var
Wstart,wend : Integer;
InLiteral : Boolean;
LastLiteral : AnsiChar;
Procedure AppendToResult;
begin
Result:=Result+Copy(S,WStart,WEnd-WStart);
WStart:=Wend+1;
end;
begin
Result:='';
WStart:=1;
While (WStart<=Length(S)) and charinset(S[WStart],WhiteSpace) do
Inc(WStart);
WEnd:=WStart;
InLiteral:=False;
LastLiteral:=#0;
While (Wend<=Length(S)) and (Not charinset(S[Wend],WhiteSpace) or InLiteral) do
begin
if charinset(S[Wend],Literals) then
If InLiteral then
begin
InLiteral:=Not (S[Wend]=LastLiteral);
if not InLiteral then
AppendToResult;
end
else
begin
InLiteral:=True;
LastLiteral:=S[Wend];
AppendToResult;
end;
inc(wend);
end;
AppendToResult;
While (WEnd<=Length(S)) and (S[Wend] in WhiteSpace) do
inc(Wend);
Delete(S,1,WEnd-1);
end;
Var
W : RawByteString;
len : Integer;
begin
Len:=0;
Result:=Default(TRawByteStringArray);
SetLength(Result,(Length(S) div 2)+1);
While Length(S)>0 do
begin
W:=GetNextWord;
If (W<>'') then
begin
Result[Len]:=W;
Inc(Len);
end;
end;
SetLength(Result,Len);
end;
Function SplitCommandLine(S : UnicodeString) : TUnicodeStringArray;
Function GetNextWord : UnicodeString;
Const
WhiteSpace = [' ',#9,#10,#13];
Literals = ['"',''''];
Var
Wstart,wend : Integer;
InLiteral : Boolean;
LastLiteral : AnsiChar;
Procedure AppendToResult;
begin
Result:=Result+Copy(S,WStart,WEnd-WStart);
WStart:=Wend+1;
end;
begin
Result:='';
WStart:=1;
While (WStart<=Length(S)) and charinset(S[WStart],WhiteSpace) do
Inc(WStart);
WEnd:=WStart;
InLiteral:=False;
LastLiteral:=#0;
While (Wend<=Length(S)) and (Not charinset(S[Wend],WhiteSpace) or InLiteral) do
begin
if charinset(S[Wend],Literals) then
If InLiteral then
begin
InLiteral:=Not (S[Wend]=LastLiteral);
if not InLiteral then
AppendToResult;
end
else
begin
InLiteral:=True;
LastLiteral:=S[Wend];
AppendToResult;
end;
inc(wend);
end;
AppendToResult;
While (WEnd<=Length(S)) and (S[Wend] in WhiteSpace) do
inc(Wend);
Delete(S,1,WEnd-1);
end;
Var
W : UnicodeString;
len : Integer;
begin
Len:=0;
Result:=Default(TUnicodeStringArray);
SetLength(Result,(Length(S) div 2)+1);
While Length(S)>0 do
begin
W:=GetNextWord;
If (W<>'') then
begin
Result[Len]:=W;
Inc(Len);
end;
end;
SetLength(Result,Len);
end;
end.