📄 ststrl.pas
字号:
function HexWL(W : Word) : AnsiString;
{-Return the hex string for a word.}
begin
SetLength(Result, 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 HexLL(L : LongInt) : AnsiString;
{-Return the hex string for a long integer.}
begin
SetLength(Result, 8);
Result := HexWL(HiWord(DWORD(L))) + HexWL(LoWord(DWORD(L))); {!!.02}
end;
function HexPtrL(P : Pointer) : AnsiString;
{-Return the hex string for a pointer.}
begin
SetLength(Result, 9);
Result := ':' + HexLL(LongInt(P));
end;
function BinaryBL(B : Byte) : AnsiString;
{-Return a binary string for a byte.}
var
I, N : Word;
begin
N := 1;
SetLength(Result, 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 BinaryWL(W : Word) : AnsiString;
{-Return the binary string for a word.}
var
I, N : Word;
begin
N := 1;
SetLength(Result, 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 BinaryLL(L : LongInt) : AnsiString;
{-Return the binary string for a long integer.}
var
I : Longint;
N : Byte;
begin
N := 1;
SetLength(Result, 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 OctalBL(B : Byte) : AnsiString;
{-Return an octal string for a byte.}
var
I : Word;
begin
SetLength(Result, 3);
for I := 0 to 2 do begin
Result[3-I] := StHexDigits[B and 7];
B := B shr 3;
end;
end;
function OctalWL(W : Word) : AnsiString;
{-Return an octal string for a word.}
var
I : Word;
begin
SetLength(Result, 6);
for I := 0 to 5 do begin
Result[6-I] := StHexDigits[W and 7];
W := W shr 3;
end;
end;
function OctalLL(L : LongInt) : AnsiString;
{-Return an octal string for a long integer.}
var
I : Word;
begin
SetLength(Result, 12);
for I := 0 to 11 do begin
Result[12-I] := StHexDigits[L and 7];
L := L shr 3;
end;
end;
function Str2Int16L(const S : AnsiString; var I : SmallInt) : Boolean;
{-Convert a string to an SmallInt.}
var
ec : Integer;
begin
{note the automatic string conversion}
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 Str2WordL(const S : AnsiString; var I : Word) : Boolean;
{-Convert a string to a word.}
var
ec : Integer;
begin
{note the automatic string conversion}
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 Str2LongL(const S : AnsiString; var I : LongInt) : Boolean;
{-Convert a string to a long integer.}
var
ec : Integer;
begin
{note the automatic string conversion}
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 Str2RealL(const S : AnsiString; var R : Double) : Boolean;
{$ELSE}
function Str2RealL(const S : AnsiString; var R : Real) : Boolean;
{$ENDIF}
{-Convert a string to a real.}
var
Code : Integer;
St : AnsiString;
begin
Result := False;
if S = '' then Exit;
St := TrimTrailL(S);
if St = '' then Exit;
Val(ValPrepL(St), R, Code);
if Code <> 0 then begin
R := Code;
end else
Result := True;
end;
function Str2ExtL(const S : AnsiString; var R : Extended) : Boolean;
{-Convert a string to an extended.}
var
Code : Integer;
P : AnsiString;
begin
Result := False;
if S = '' then Exit;
P := TrimTrailL(S);
if P = '' then Exit;
Val(ValPrepL(P), R, Code);
if Code <> 0 then begin
R := Code - 1;
end else
Result := True;
end;
function Long2StrL(L : LongInt) : AnsiString;
{-Convert an integer type to a string.}
begin
Str(L, Result);
end;
function Real2StrL(R : Double; Width : Byte; Places : ShortInt) : AnsiString;
{-Convert a real to a string.}
begin
Str(R:Width:Places, Result);
end;
function Ext2StrL(R : Extended; Width : Byte; Places : ShortInt) : AnsiString;
{-Convert an extended to a string.}
begin
Str(R:Width:Places, Result);
end;
function ValPrepL(const S : AnsiString) : AnsiString;
{-Prepares a string for calling Val.}
var
P : Cardinal;
C : Longint;
begin
Result := TrimSpacesL(S);
if Result <> '' then begin
if StrChPosL(Result, DecimalSeparator, P) then begin
C := P;
Result[C] := '.';
if C = Length(Result) then
SetLength(Result, Pred(C));
end;
end else
Result := '0';
end;
{-------- General purpose string manipulation --------}
function CharStrL(C : AnsiChar; Len : Cardinal) : AnsiString;
{-Return a string filled with the specified character.}
begin
SetLength(Result, Len);
if Len <> 0 then begin
{SetLength(Result, Len);}
FillChar(Result[1], Len, C);
end;
end;
function PadChL(const S : AnsiString; C : AnsiChar; Len : Cardinal) : AnsiString;
{-Pad a string on the right with a specified character.}
begin
if Length(S) >= LongInt(Len) then
Result := S
else begin
SetLength(Result, Len);
{ copy current contents (if any) of S to Result }
if (Length(S) > 0) then {!!.01}
Move(S[1], Result[1], Length(S));
{ add pad chars }
FillChar(Result[Succ(Length(S))], LongInt(Len)-Length(S), C);
end;
end;
function PadL(const S : AnsiString; Len : Cardinal) : AnsiString;
{-Pad a string on the right with spaces.}
begin
Result := PadChL(S, ' ', Len);
end;
function LeftPadChL(const S : AnsiString; C : AnsiChar; Len : Cardinal) : AnsiString;
{-Pad a string on the left with a specified character.}
begin
if Length(S) >= LongInt(Len) then
Result := S
else if Length(S) < MaxLongInt then begin
SetLength(Result, Len);
{ copy current contents (if any) of S to Result }
if (Length(S) > 0) then {!!.01}
Move(S[1], Result[Succ(Word(Len))-Length(S)], Length(S));
{ add pad chars }
FillChar(Result[1], LongInt(Len)-Length(S), C);
end;
end;
function LeftPadL(const S : AnsiString; Len : Cardinal) : AnsiString;
{-Pad a string on the left with spaces.}
begin
Result := LeftPadChL(S, ' ', Len);
end;
function TrimLeadL(const S : AnsiString) : AnsiString;
{-Return a string with leading white space removed}
var
I : Longint;
begin
I := 1;
while (I <= Length(S)) and (S[I] <= ' ') do
Inc(I);
SetLength(Result, Length(S)-Pred(I));
if Length(Result) > 0 then {!!.01}
Move(S[I], Result[1], Length(S)-Pred(I));
end;
function TrimTrailL(const S : AnsiString) : AnsiString;
{-Return a string with trailing white space removed.}
begin
Result := S;
while (Length(Result) > 0) and (Result[Length(Result)] <= ' ') do
SetLength(Result, Pred(Length(Result)));
end;
function TrimL(const S : AnsiString) : AnsiString;
{-Return a string with leading and trailing white space removed.}
var
I : Longint;
begin
Result := S;
while (Length(Result) > 0) and (Result[Length(Result)] <= ' ') do
SetLength(Result, Pred(Length(Result)));
I := 1;
while (I <= Length(Result)) and (Result[I] <= ' ') do
Inc(I);
Dec(I);
if I > 0 then
System.Delete(Result, 1, I);
end;
function TrimSpacesL(const S : AnsiString) : AnsiString;
{-Return a string with leading and trailing spaces removed.}
var
I : Longint;
begin
Result := S;
while (Length(Result) > 0) and (Result[Length(Result)] = ' ') do
SetLength(Result, Pred(Length(Result)));
I := 1;
while (I <= Length(Result)) and (S[I] = ' ') do
Inc(I);
Dec(I);
if I > 0 then
System.Delete(Result, 1, I);
end;
function CenterChL(const S : AnsiString; C : AnsiChar; Len : Cardinal) : AnsiString;
{-Pad a string on the left and right with a specified character.}
begin
if Length(S) >= LongInt(Len) then
Result := S
else if Length(S) < MaxLongInt then begin
SetLength(Result, Len);
FillChar(Result[1], Len, C);
if Length(S) > 0 then {!!.01}
Move(S[1], Result[Succ((LongInt(Len)-Length(S)) shr 1)], Length(S));
end;
end;
function CenterL(const S : AnsiString; Len : Cardinal) : AnsiString;
{-Pad a string on the left and right with spaces.}
begin
Result := CenterChL(S, ' ', Len);
end;
function EntabL(const S : AnsiString; TabSize : Byte) : AnsiString;
{-Convert blanks in a string to tabs.}
var
InLen, OutLen : Cardinal;
begin
if S = '' then Exit;
InLen := Length(S);
OutLen := 0;
SetLength(Result, InLen);
asm
push ebx { Save registers }
push edi
push esi
mov edi, [Result]
mov edi, [edi]
xor ecx, ecx
add cl, TabSize
jz @@Done
mov esi, S
xor ebx, ebx { Zero EBX and EDX }
xor edx, edx
inc edx { Set output length to 1 }
@@Next:
or ebx, ebx
je @@NoTab { Jump to NoTab if spacecount is zero }
mov eax, edx { IPos to EAX }
push edx
xor edx, edx
div ecx
cmp edx, 1 { Is mod = 1? }
pop edx
jne @@NoTab { If not, no tab }
sub edi, ebx
sub OutLen, ebx
inc OutLen
xor ebx, ebx { Reset spacecount }
mov byte ptr [edi], 9h { Store a tab }
inc edi
@@NoTab:
mov al, [esi] { Get next input character }
inc esi
cmp edx, InLen { End of string? }
jg @@Done { Yes, done }
inc ebx { Increment SpaceCount }
cmp al, 20h { Is character a space? }
jz @@Store { Yes, store it for now }
xor ebx, ebx { Reset SpaceCount }
cmp al, 27h { Is it a quote? }
jz @@Quotes { Yep, enter quote loop }
cmp al, 22h { Is it a doublequote? }
jnz @@Store { Nope, store it }
@@Quotes:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -