* format support for ansistring (from mailinglist)

* fixed length checking in Trim()
This commit is contained in:
peter 1999-04-04 10:19:07 +00:00
parent 8630e6c954
commit d94060a283

View File

@ -245,13 +245,13 @@ end ;
function Trim(const S: string): string; function Trim(const S: string): string;
var Ofs, Len: integer; var Ofs, Len: integer;
begin begin
len := Length(S); len := Length(S);
while (S[Len] = ' ') and (Len > 0) do while (Len>0) and (S[Len] = ' ') do
dec(Len); dec(Len);
Ofs := 1; Ofs := 1;
while (S[Ofs] = ' ') and (Ofs <= Len) do while (Ofs<=Len) and (S[Ofs] = ' ') do
Inc(Ofs); Inc(Ofs);
result := Copy(S, Ofs, 1 + Len - Ofs); result := Copy(S, Ofs, 1 + Len - Ofs);
end ; end ;
{ TrimLeft returns a copy of S with all blank characters on the left stripped off } { TrimLeft returns a copy of S with all blank characters on the left stripped off }
@ -259,10 +259,11 @@ end ;
function TrimLeft(const S: string): string; function TrimLeft(const S: string): string;
var i,l:integer; var i,l:integer;
begin begin
l := length(s); l := length(s);
i := 1; i := 1;
while (s[i] = ' ') and (i <= l) do inc(i); while (i<=l) and (s[i] = ' ') do
Result := copy(s, i, l); inc(i);
Result := copy(s, i, l);
end ; end ;
{ TrimRight returns a copy of S with all blank characters on the right stripped off } { TrimRight returns a copy of S with all blank characters on the right stripped off }
@ -270,9 +271,10 @@ end ;
function TrimRight(const S: string): string; function TrimRight(const S: string): string;
var l:integer; var l:integer;
begin begin
l := length(s); l := length(s);
while (s[l] = ' ') and (l > 0) do dec(l); while (l>0) and (s[l] = ' ') do
result := copy(s,1,l); dec(l);
result := copy(s,1,l);
end ; end ;
{ QuotedStr returns S quoted left and right and every single quote in S { QuotedStr returns S quoted left and right and every single quote in S
@ -457,7 +459,7 @@ end;
Function Format (Const Fmt : String; const Args : Array of const) : String; Function Format (Const Fmt : String; const Args : Array of const) : String;
Var ChPos,OldPos,ArgPos,DoArg,Len : Longint; Var ChPos,OldPos,ArgPos,DoArg,Len : Longint;
ToAdd : String; Hs,ToAdd : String;
Index,Width,Prec : Longint; Index,Width,Prec : Longint;
Left : Boolean; Left : Boolean;
ExtVal: Extended; ExtVal: Extended;
@ -585,14 +587,15 @@ begin
Writeln (' Type : ',C); Writeln (' Type : ',C);
end; end;
Procedure Checkarg (AT : Longint); function Checkarg (AT : Longint;err:boolean):boolean;
{ {
Check if argument INDEX is of correct type (AT) Check if argument INDEX is of correct type (AT)
If Index=-1, ArgPos is used, and argpos is augmented with 1 If Index=-1, ArgPos is used, and argpos is augmented with 1
DoArg is set to the argument that must be used. DoArg is set to the argument that must be used.
} }
begin begin
If Index=-1 then result:=false;
if Index=-1 then
begin begin
DoArg:=Argpos; DoArg:=Argpos;
inc(ArgPos); inc(ArgPos);
@ -600,7 +603,12 @@ begin
else else
DoArg:=Index; DoArg:=Index;
If (Doarg>High(Args)) or (Args[Doarg].Vtype<>AT) then If (Doarg>High(Args)) or (Args[Doarg].Vtype<>AT) then
DoFormatError(feInvalidArgindex); begin
if err then
DoFormatError(feInvalidArgindex);
exit;
end;
result:=true;
end; end;
Const Zero = '000000000000000000000000000000000000000000000000000000000000000'; Const Zero = '000000000000000000000000000000000000000000000000000000000000000';
@ -625,7 +633,7 @@ begin
{$endif} {$endif}
Case FChar of Case FChar of
'D' : begin 'D' : begin
Checkarg(vtinteger); Checkarg(vtinteger,true);
Width:=Abs(width); Width:=Abs(width);
Str(Args[Doarg].VInteger,ToAdd); Str(Args[Doarg].VInteger,ToAdd);
While Length(ToAdd)<Prec do While Length(ToAdd)<Prec do
@ -636,33 +644,38 @@ begin
end; end;
end; end;
'E' : begin 'E' : begin
CheckArg(vtExtended); CheckArg(vtExtended,true);
If Prec=-1 then prec:=15; If Prec=-1 then prec:=15;
ExtVal:=Args[doarg].VExtended^; ExtVal:=Args[doarg].VExtended^;
Prec:=Prec+5; // correct dot, eXXX Prec:=Prec+5; // correct dot, eXXX
If ExtVal<0 then Inc(Prec); // Corect for minus sign If ExtVal<0 then Inc(Prec); // Corect for minus sign
If Abs(Extval)<1 then Inc(Prec); // correct for - in E If Abs(Extval)<1 then Inc(Prec); // correct for - in E
Writeln('STRING ',prec);
Str(Args[doarg].VExtended^:prec,ToAdd); Str(Args[doarg].VExtended^:prec,ToAdd);
WRITELN('DID');
end; end;
'F' : begin 'F' : begin
end; end;
'S' : begin 'S' : begin
CheckArg(vtString); if CheckArg(vtString,false) then
Index:=Length(Args[doarg].VString^); hs:=Args[doarg].VString^
If (Prec<>-1) and (Index>Prec) then else
Index:=Prec; begin
ToAdd:=Copy(Args[DoArg].VString^,1,Index); dec(argpos);
if CheckArg(vtAnsiString,true) then
hs:=ansistring(Args[doarg].VAnsiString);
end;
Index:=Length(hs);
If (Prec<>-1) and (Index>Prec) then
Index:=Prec;
ToAdd:=Copy(hs,1,Index);
end; end;
'P' : Begin 'P' : Begin
CheckArg(vtpointer); CheckArg(vtpointer,true);
ToAdd:=HexStr(Longint(Args[DoArg].VPointer),8); ToAdd:=HexStr(Longint(Args[DoArg].VPointer),8);
// Insert ':'. Is this needed in 32 bit ? No it isn't. // Insert ':'. Is this needed in 32 bit ? No it isn't.
// Insert(':',ToAdd,5); // Insert(':',ToAdd,5);
end; end;
'X' : begin 'X' : begin
Checkarg(vtinteger); Checkarg(vtinteger,true);
If Prec>32 then If Prec>32 then
ToAdd:=HexStr(Args[Doarg].VInteger,Prec) ToAdd:=HexStr(Args[Doarg].VInteger,Prec)
else else
@ -676,7 +689,6 @@ begin
ToAdd:=HexStr(Args[DoArg].VInteger,Prec); ToAdd:=HexStr(Args[DoArg].VInteger,Prec);
end; end;
end; end;
'%': ToAdd:='%'; '%': ToAdd:='%';
end; end;
If Width<>-1 then If Width<>-1 then
@ -957,7 +969,11 @@ const
{ {
$Log$ $Log$
Revision 1.14 1999-03-01 12:40:06 michael Revision 1.15 1999-04-04 10:19:07 peter
* format support for ansistring (from mailinglist)
* fixed length checking in Trim()
Revision 1.14 1999/03/01 12:40:06 michael
changed delete to system.delete changed delete to system.delete
Revision 1.13 1999/02/28 13:17:35 michael Revision 1.13 1999/02/28 13:17:35 michael