📄 ststrs.pas
字号:
begin
Result[0] := #2;
Result[1] := StHexDigits[B shr 4];
Result[2] := StHexDigits[B and $F];
end;
function HexWS(W : Word) : ShortString;
{-Return the hex string for a word.}
begin
Result[0] := #4;
Result[1] := StHexDigits[hi(W) shr 4];
Result[2] := StHexDigits[hi(W) and $F];
Result[3] := StHexDigits[lo(W) shr 4];
Result[4] := StHexDigits[lo(W) and $F];
end;
function HexLS(L : LongInt) : ShortString;
{-Return the hex string for a long integer.}
begin
Result := HexWS(HiWord(DWORD(L))) + HexWS(LoWord(DWORD(L))); {!!.02}
end;
function HexPtrS(P : Pointer) : ShortString;
{-Return the hex string for a pointer.}
begin
Result := HexLS(LongInt(P)); {!!.02}
end;
function BinaryBS(B : Byte) : ShortString;
{-Return a binary string for a byte.}
var
I, N : Cardinal;
begin
N := 1;
Result[0] := #8;
for I := 7 downto 0 do begin
Result[N] := StHexDigits[Ord(B and (1 shl I) <> 0)]; {0 or 1}
Inc(N);
end;
end;
function BinaryWS(W : Word) : ShortString;
{-Return the binary string for a word.}
var
I, N : Cardinal;
begin
N := 1;
Result[0] := #16;
for I := 15 downto 0 do begin
Result[N] := StHexDigits[Ord(W and (1 shl I) <> 0)]; {0 or 1}
Inc(N);
end;
end;
function BinaryLS(L : LongInt) : ShortString;
{-Return the binary string for a long integer.}
var
I : Longint;
N : Byte;
begin
N := 1;
Result[0] := #32;
for I := 31 downto 0 do begin
Result[N] := StHexDigits[Ord(L and LongInt(1 shl I) <> 0)]; {0 or 1}
Inc(N);
end;
end;
function OctalBS(B : Byte) : ShortString;
{-Return an octal string for a byte.}
var
I : Cardinal;
begin
Result[0] := #3;
for I := 0 to 2 do begin
Result[3-I] := StHexDigits[B and 7];
B := B shr 3;
end;
end;
function OctalWS(W : Word) : ShortString;
{-Return an octal string for a word.}
var
I : Cardinal;
begin
Result[0] := #6;
for I := 0 to 5 do begin
Result[6-I] := StHexDigits[W and 7];
W := W shr 3;
end;
end;
function OctalLS(L : LongInt) : ShortString;
{-Return an octal string for a long integer.}
var
I : Cardinal;
begin
Result[0] := #12;
for I := 0 to 11 do begin
Result[12-I] := StHexDigits[L and 7];
L := L shr 3;
end;
end;
function Str2Int16S(const S : ShortString; var I : SmallInt) : Boolean;
{-Convert a string to an SmallInt.}
var
ec : Integer;
begin
ValSmallint(S, I, ec);
if (ec = 0) then
Result := true
else begin
Result := false;
if (ec < 0) then
I := succ(length(S))
else
I := ec;
end;
end;
function Str2WordS(const S : ShortString; var I : Word) : Boolean;
{-Convert a string to a word.}
var
ec : Integer;
begin
ValWord(S, I, ec);
if (ec = 0) then
Result := true
else begin
Result := false;
if (ec < 0) then
I := succ(length(S))
else
I := ec;
end;
end;
function Str2LongS(const S : ShortString; var I : LongInt) : Boolean;
{-Convert a string to a long integer.}
var
ec : Integer;
begin
ValLongint(S, I, ec);
if (ec = 0) then
Result := true
else begin
Result := false;
if (ec < 0) then
I := succ(length(S))
else
I := ec;
end;
end;
{$IFDEF VER93}
function Str2RealS(const S : ShortString; var R : Double) : Boolean;
{$ELSE}
{-Convert a string to a real.}
function Str2RealS(const S : ShortString; var R : Real) : Boolean;
{$ENDIF}
{-Convert a string to a real.}
var
Code : Integer;
St : ShortString;
SLen : Byte absolute St;
begin
St := S;
{trim trailing blanks}
while St[SLen] = ' ' do
Dec(SLen);
Val(ValPrepS(St), R, Code);
if Code <> 0 then begin
R := Code;
Result := False;
end else
Result := True;
end;
function Str2ExtS(const S : ShortString; var R : Extended) : Boolean;
{-Convert a string to an extended.}
var
Code : Integer;
P : ShortString;
PLen : Byte absolute P;
begin
P := S;
{trim trailing blanks}
while P[PLen] = ' ' do
Dec(PLen);
Val(ValPrepS(P), R, Code);
if Code <> 0 then begin
R := Code;
Result := False;
end else
Result := True;
end;
function Long2StrS(L : LongInt) : ShortString;
{-Convert an integer type to a string.}
begin
Str(L, Result);
end;
function Real2StrS(R : Double; Width : Byte; Places : ShortInt) : ShortString;
{-Convert a real to a string.}
begin
Str(R:Width:Places, Result);
end;
function Ext2StrS(R : Extended; Width : Byte; Places : ShortInt) : ShortString;
{-Convert an extended to a string.}
begin
Str(R:Width:Places, Result);
end;
function ValPrepS(const S : ShortString) : ShortString;
{-Prepares a string for calling Val.}
var
P : Cardinal;
begin
Result := TrimSpacesS(S);
if Result <> '' then begin
if StrChPosS(Result, DecimalSeparator, P) then begin
Result[P] := '.';
if P = Byte(Result[0]) then
Result[0] := AnsiChar(Pred(P));
end;
end else begin
Result := '0';
end;
end;
{-------- General purpose string manipulation --------}
function CharStrS(C : AnsiChar; Len : Cardinal) : ShortString;
{-Return a string filled with the specified character.}
begin
if Len = 0 then
Result[0] := #0
else begin
Result[0] := Chr(Len);
FillChar(Result[1], Len, C);
end;
end;
function PadChS(const S : ShortString; C : AnsiChar; Len : Cardinal) : ShortString;
{-Pad a string on the right with a specified character.}
var
SLen : Byte absolute S;
begin
if Length(S) >= Len then
Result := S
else begin
if Len > 255 then Len := 255;
Result[0] := Chr(Len);
Move(S[1], Result[1], SLen);
if SLen < 255 then
FillChar(Result[Succ(SLen)], Len-SLen, C);
end;
end;
function PadS(const S : ShortString; Len : Cardinal) : ShortString;
{-Pad a string on the right with spaces.}
begin
Result := PadChS(S, ' ', Len);
end;
function LeftPadChS(const S : ShortString; C : AnsiChar; Len : Cardinal) : ShortString;
{-Pad a string on the left with a specified character.}
begin
if Length(S) >= Len then
Result := S
else if Length(S) < 255 then begin
if Len > 255 then Len := 255;
Result[0] := Chr(Len);
Move(S[1], Result[Succ(Word(Len))-Length(S)], Length(S));
FillChar(Result[1], Len-Length(S), C);
end;
end;
function LeftPadS(const S : ShortString; Len : Cardinal) : ShortString;
{-Pad a string on the left with spaces.}
begin
Result := LeftPadChS(S, ' ', Len);
end;
function TrimLeadS(const S : ShortString) : ShortString;
{-Return a string with leading white space removed}
var
I : Cardinal;
begin
{!!.03 - added }
if S = '' then begin
Result := '';
Exit;
end;
{!!.03 - added end }
I := 1;
while (I <= Length(S)) and (S[I] <= ' ') do
Inc(I);
Move(S[I], Result[1], Length(S)-I+1);
Result[0] := Char(Length(S)-I+1);
end;
function TrimTrailS(const S : ShortString) : ShortString;
{-Return a string with trailing white space removed.}
begin
Result := S;
while (Length(Result) > 0) and (Result[Length(Result)] <= ' ') do
Dec(Result[0]);
end;
function TrimS(const S : ShortString) : ShortString;
{-Return a string with leading and trailing white space removed.}
var
I : Cardinal;
SLen : Byte absolute Result;
begin
Result := S;
while (SLen > 0) and (Result[SLen] <= ' ') do
Dec(SLen);
I := 1;
while (I <= SLen) and (Result[I] <= ' ') do
Inc(I);
Dec(I);
if I > 0 then
Delete(Result, 1, I);
end;
function TrimSpacesS(const S : ShortString) : ShortString;
{-Return a string with leading and trailing spaces removed.}
var
I : Word;
begin
Result := S;
while (Length(Result) > 0) and (Result[Length(Result)] = ' ') do
Dec(Result[0]);
I := 1;
while (I <= Length(Result)) and (S[I] = ' ') do
Inc(I);
Dec(I);
if I > 0 then
Delete(Result, 1, I);
end;
function CenterChS(const S : ShortString; C : AnsiChar; Len : Cardinal) : ShortString;
{-Pad a string on the left and right with a specified character.}
begin
if Length(S) >= Len then
Result := S
else if Length(S) < 255 then begin
if Len > 255 then Len := 255;
Result[0] := Chr(Len);
FillChar(Result[1], Len, C);
Move(S[1], Result[Succ((Len-Length(S)) shr 1)], Length(S));
end;
end;
function CenterS(const S : ShortString; Len : Cardinal) : ShortString;
{-Pad a string on the left and right with spaces.}
begin
Result := CenterChS(S, ' ', Len);
end;
function EntabS(const S : ShortString; TabSize : Byte) : ShortString;
{-Convert blanks in a string to tabs.}
register;
asm
push ebx { Save registers }
push edi
push esi
mov esi, eax { ESI => input string }
mov edi, ecx { EDI => output string }
xor ebx, ebx { Initial SpaceCount = 0 }
xor ecx, ecx { Default input length = 0 }
and edx, 0FFh { Default output length = 0 in DH, TabSize in DL }
mov cl, [esi] { Get input length }
inc esi
or edx, edx { TabSize = 0? }
jnz @@DefLength
mov ecx, edx { Return zero length string if TabSize = 0 }
@@DefLength:
mov [edi], cl { Store default output length }
inc edi
or ecx, ecx
jz @@Done { Done if empty input string }
inc ch { Current input position=1 }
@@Next:
or ebx, ebx { Compare SpaceCount to 0 }
jz @@NoTab { If SpaceCount=0 then no tab insert here }
xor eax, eax
mov al, ch { Ipos to AL }
div dl { Ipos DIV TabSize }
cmp ah, 1 { Ipos MOD TabSize = 1 ? }
jnz @@NoTab { If not, no tab insert here }
sub edi, ebx { Remove unused characters from output string }
sub dh, bl { Reduce Olen by SpaceCount }
inc dh { Add one to output length }
xor ebx, ebx { Reset SpaceCount }
mov byte ptr [edi], 09h { Store a tab }
inc edi
@@NoTab:
mov al, [esi] { Get next input character }
inc esi
cmp cl, ch { End of string? }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -