📄 ststrw.pas
字号:
(* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is TurboPower SysTools
*
* The Initial Developer of the Original Code is
* TurboPower Software
*
* Portions created by the Initial Developer are Copyright (C) 1996-2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** *)
{*********************************************************}
{* SysTools: StStrW.pas 4.03 *}
{*********************************************************}
{* SysTools: Wide (Unicode) string routines *}
{*********************************************************}
{$I StDefine.inc}
unit StStrW;
interface
uses
Windows,
Classes,
SysUtils,
StConst,
StBase;
{.Z+}
type
WStrRec = packed record
Length : Longint;
end;
const
StrOffset = SizeOf(WStrRec);
{.Z-}
{-------- Numeric conversion -----------}
function HexBW(B : Byte) : WideString;
{-Return the hex string for a byte.}
function HexWW(W : Word) : WideString;
{-Return the hex string for a word.}
function HexLW(L : LongInt) : WideString;
{-Return the hex string for a long integer.}
function HexPtrW(P : Pointer) : WideString;
{-Return the hex string for a pointer.}
function BinaryBW(B : Byte) : WideString;
{-Return a binary string for a byte.}
function BinaryWW(W : Word) : WideString;
{-Return the binary string for a word.}
function BinaryLW(L : LongInt) : WideString;
{-Return the binary string for a long integer.}
function OctalBW(B : Byte) : WideString;
{-Return an octal string for a byte.}
function OctalWW(W : Word) : WideString;
{-Return an octal string for a word.}
function OctalLW(L : LongInt) : WideString;
{-Return an octal string for a long integer.}
function Str2Int16W(const S : WideString; var I : SmallInt) : Boolean;
{-Convert a string to an SmallInt.}
function Str2WordW(const S : WideString; var I : Word) : Boolean;
{-Convert a string to a word.}
function Str2LongW(const S : WideString; var I : LongInt) : Boolean;
{-Convert a string to a long integer.}
function Str2RealW(const S : WideString; var R : Double) : Boolean;
{-Convert a string to a real.}
function Str2ExtW(const S : WideString; var R : Extended) : Boolean;
{-Convert a string to an extended.}
function Long2StrW(L : LongInt) : WideString;
{-Convert an integer type to a string.}
function Real2StrW(R : Double; Width : Byte; Places : ShortInt) : WideString;
{-Convert a real to a string.}
function Ext2StrW(R : Extended; Width : Byte; Places : ShortInt) : WideString;
{-Convert an extended to a string.}
function ValPrepW(const S : WideString) : WideString;
{-Prepares a string for calling Val.}
{-------- General purpose string manipulation --------}
function CharStrW(C : WideChar; Len : Cardinal) : WideString;
{-Return a string filled with the specified character.}
function PadChW(const S : WideString; C : WideChar; Len : Cardinal) : WideString;
{-Pad a string on the right with a specified character.}
function PadW(const S : WideString; Len : Cardinal) : WideString;
{-Pad a string on the right with spaces.}
function LeftPadChW(const S : WideString; C : WideChar; Len : Cardinal) : WideString;
{-Pad a string on the left with a specified character.}
function LeftPadW(const S : WideString; Len : Cardinal) : WideString;
{-Pad a string on the left with spaces.}
function TrimLeadW(const S : WideString) : WideString;
{-Return a string with leading white space removed.}
function TrimTrailW(const S : WideString) : WideString;
{-Return a string with trailing white space removed.}
function TrimW(const S : WideString) : WideString;
{-Return a string with leading and trailing white space removed.}
function TrimSpacesW(const S : WideString) : WideString;
{-Return a string with leading and trailing spaces removed.}
function CenterChW(const S : WideString; C : WideChar; Len : Cardinal) : WideString;
{-Pad a string on the left and right with a specified character.}
function CenterW(const S : WideString; Len : Cardinal) : WideString;
{-Pad a string on the left and right with spaces.}
function EntabW(const S : WideString; TabSize : Byte) : WideString;
{-Convert blanks in a string to tabs.}
function DetabW(const S : WideString; TabSize : Byte) : WideString;
{-Expand tabs in a string to blanks.}
function ScrambleW(const S, Key : WideString) : WideString;
{-Encrypt / Decrypt string with enhanced XOR encryption.}
function SubstituteW(const S, FromStr, ToStr : WideString) : WideString;
{-Map the characters found in FromStr to the corresponding ones in ToStr.}
function FilterW(const S, Filters : WideString) : WideString;
{-Remove characters from a string. The characters to remove are specified in
ChSet.}
{--------------- Word / Char manipulation -------------------------}
function CharExistsW(const S : WideString; C : WideChar) : Boolean;
{-Determine whether a given character exists in a string. }
function CharCountW(const S : WideString; C : WideChar) : Cardinal;
{-Count the number of a given character in a string. }
function WordCountW(const S, WordDelims : WideString) : Cardinal;
{-Given an array of word delimiters, return the number of words in a string.}
function WordPositionW(N : Cardinal; const S, WordDelims : WideString;
var Pos : Cardinal) : Boolean;
{-Given an array of word delimiters, set Pos to the start position of the
N'th word in a string. Result indicates success/failure.}
function ExtractWordW(N : Cardinal; const S, WordDelims : WideString) : WideString;
{-Given an array of word delimiters, return the N'th word in a string.}
function AsciiCountW(const S, WordDelims : WideString; Quote : WideChar) : Cardinal;
{-Return the number of words in a string.}
function AsciiPositionW(N : Cardinal; const S, WordDelims : WideString;
Quote : WideChar; var Pos : Cardinal) : Boolean;
{-Return the position of the N'th word in a string.}
function ExtractAsciiW(N : Cardinal; const S, WordDelims : WideString;
Quote : WideChar) : WideString;
{-Given an array of word delimiters, return the N'th word in a string. Any
text within Quote characters is counted as one word.}
procedure WordWrapW(const InSt : WideString; var OutSt, Overlap : WideString;
Margin : Cardinal; PadToMargin : Boolean);
{-Wrap a text string at a specified margin.}
{--------------- String comparison and searching -----------------}
function CompStringW(const S1, S2 : WideString) : Integer;
{-Compare two strings.}
function CompUCStringW(const S1, S2 : WideString) : Integer;
{-Compare two strings. This compare is not case sensitive.}
{--------------- DOS pathname parsing -----------------}
function DefaultExtensionW(const Name, Ext : WideString) : WideString;
{-Return a file name with a default extension attached.}
function ForceExtensionW(const Name, Ext : WideString) : WideString;
{-Force the specified extension onto the file name.}
function JustFilenameW(const PathName : WideString) : WideString;
{-Return just the filename and extension of a pathname.}
function JustNameW(const PathName : WideString) : WideString;
{-Return just the filename (no extension, path, or drive) of a pathname.}
function JustExtensionW(const Name : WideString) : WideString;
{-Return just the extension of a pathname.}
function JustPathnameW(const PathName : WideString) : WideString;
{-Return just the drive and directory portion of a pathname.}
function AddBackSlashW(const DirName : WideString) : WideString;
{-Add a default backslash to a directory name.}
function CleanPathNameW(const PathName : WideString) : WideString;
{-Return a pathname cleaned up as DOS does it.}
function HasExtensionW(const Name : WideString; var DotPos : Cardinal) : Boolean;
{-Determine if a pathname contains an extension and, if so, return the
position of the dot in front of the extension.}
{------------------ Formatting routines --------------------}
function CommaizeW(L : LongInt) : WideString;
{-Convert a long integer to a string with commas.}
function CommaizeChW(L : Longint; Ch : WideChar) : WideString;
{-Convert a long integer to a string with Ch in comma positions.}
function FloatFormW(const Mask : WideString ; R : TstFloat ; const LtCurr,
RtCurr : WideString ; Sep, DecPt : WideChar) : WideString;
{-Return a formatted string with digits from R merged into mask.}
function LongIntFormW(const Mask : WideString ; L : LongInt ; const LtCurr,
RtCurr : WideString ; Sep : WideChar) : WideString;
{-Return a formatted string with digits from L merged into mask.}
function StrChPosW(const P : WideString; C : WideChar; var Pos : Cardinal) : Boolean;
{-Return the position of a specified character within a string.}
function StrStPosW(const P, S : WideString; var Pos : Cardinal) : Boolean;
{-Return the position of a specified substring within a string.}
function StrStCopyW(const S : WideString; Pos, Count : Cardinal) : WideString;
{-Copy characters at a specified position in a string.}
function StrChInsertW(const S : WideString; C : WideChar; Pos : Cardinal) : WideString;
{-Insert a character into a string at a specified position.}
function StrStInsertW(const S1, S2 : WideString; Pos : Cardinal) : WideString;
{-Insert a string into another string at a specified position.}
function StrChDeleteW(const S : WideString; Pos : Cardinal) : WideString;
{-Delete the character at a specified position in a string.}
function StrStDeleteW(const S : WideString; Pos, Count : Cardinal) : WideString;
{-Delete characters at a specified position in a string.}
{-------------------------- New Functions -----------------------------------}
function ContainsOnlyW(const S, Chars : WideString;
var BadPos : Cardinal) : Boolean;
function ContainsOtherThanW(const S, Chars : WideString;
var BadPos : Cardinal) : Boolean;
function CopyFromNthWordW(const S, WordDelims : WideString;
AWord : WideString; N : Cardinal;
var SubString : WideString) : Boolean;
function CopyFromToWordW(const S, WordDelims, Word1, Word2 : WideString;
N1, N2 : Cardinal;
var SubString : WideString) : Boolean;
function CopyLeftW(const S : WideString; Len : Cardinal) : WideString;
{-Return the left Len characters of a string}
function CopyMidW(const S : WideString; First, Len : Cardinal) : WideString;
{-Return the mid part of a string}
function CopyRightW(const S : WideString; First : Cardinal) : WideString;
{-Return the right Len characters of a string}
function CopyRightAbsW(const S : WideString; NumChars : Cardinal) : WideString;
{-Return NumChar characters starting from end}
function CopyWithinW(const S, Delimiter : WideString;
Strip : Boolean) : WideString;
function DeleteFromNthWordW(const S, WordDelims : WideString;
AWord : WideString; N : Cardinal;
var SubString : WideString) : Boolean;
function DeleteFromToWordW(const S, WordDelims, Word1, Word2 : WideString;
N1, N2 : Cardinal;
var SubString : WideString) : Boolean;
function DeleteWithinW(const S, Delimiter : WideString) : WideString;
function ExtractTokensW(const S, Delims : WideString;
QuoteChar : WideChar;
AllowNulls : Boolean;
Tokens : TStrings) : Cardinal;
function IsChAlphaW(C : WideChar) : Boolean;
{-Returns true if Ch is an alpha}
function IsChNumericW(C : WideChar; Numbers : WideString) : Boolean;
{-Returns true if Ch in numeric set}
function IsChAlphaNumericW(C : WideChar; Numbers : WideString) : Boolean;
{-Returns true if Ch is an alpha or numeric}
function IsStrAlphaW(const S : WideString) : Boolean;
{-Returns true if all characters in string are an alpha}
function IsStrNumericW(const S, Numbers : WideString) : Boolean;
{-Returns true if all characters in string are in numeric set}
function IsStrAlphaNumericW(const S, Numbers : WideString) : Boolean;
{-Returns true if all characters in string are alpha or numeric}
function KeepCharsW(const S, Chars : WideString) : WideString;
function LastStringW(const S, AString : WideString;
var Position : Cardinal) : Boolean;
function LastWordW(const S, WordDelims, AWord : WideString;
var Position : Cardinal) : Boolean;
function LastWordAbsW(const S, WordDelims : WideString;
var Position : Cardinal) : Boolean;
function LeftTrimCharsW(const S, Chars : WideString) : WideString;
function ReplaceWordW(const S, WordDelims, OldWord, NewWord : WideString;
N : Cardinal;
var Replacements : Cardinal) : WideString;
function ReplaceWordAllW(const S, WordDelims, OldWord, NewWord : WideString;
var Replacements : Cardinal) : WideString;
function ReplaceStringW(const S, OldString, NewString : WideString;
N : Cardinal;
var Replacements : Cardinal) : WideString;
function ReplaceStringAllW(const S, OldString, NewString : WideString;
var Replacements : Cardinal) : WideString;
function RepeatStringW(const RepeatString : WideString;
var Repetitions : Cardinal;
MaxLen : Cardinal) : WideString;
function RightTrimCharsW(const S, Chars : WideString) : WideString;
function StrWithinW(const S, SearchStr : WideString;
Start : Cardinal;
var Position : Cardinal) : boolean;
{-finds the position of a substring within a string starting at a given point}
function TrimCharsW(const S, Chars : WideString) : WideString;
function WordPosW(const S, WordDelims, AWord : WideString;
N : Cardinal; var Position : Cardinal) : Boolean;
{-returns the Occurrence instance of a word within a string}
implementation
uses
StUtils;
{-------- Numeric conversion -----------}
function HexBW(B : Byte) : WideString;
{-Return the hex string for a byte.}
begin
SetLength(Result, 2);
Result[1] := WideChar(StHexDigits[B shr 4]);
Result[2] := WideChar(StHexDigits[B and $F]);
end;
function HexWW(W : Word) : WideString;
{-Return the hex string for a word.}
begin
SetLength(Result, 4);
Result[1] := WideChar(StHexDigits[hi(W) shr 4]);
Result[2] := WideChar(StHexDigits[hi(W) and $F]);
Result[3] := WideChar(StHexDigits[lo(W) shr 4]);
Result[4] := WideChar(StHexDigits[lo(W) and $F]);
end;
function HexLW(L : LongInt) : WideString;
{-Return the hex string for a long integer.}
begin
SetLength(Result, 8);
Result := HexWW(HiWord(DWORD(L))) + HexWW(LoWord(DWORD(L))); {!!.02}
end;
function HexPtrW(P : Pointer) : WideString;
{-Return the hex string for a pointer.}
begin
SetLength(Result, 9);
Result := ':' + HexLW(LongInt(P));
end;
function BinaryBW(B : Byte) : WideString;
{-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] := WideChar(StHexDigits[Ord(B and (1 shl I) <> 0)]); {0 or 1}
Inc(N);
end;
end;
function BinaryWW(W : Word) : WideString;
{-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] := WideChar(StHexDigits[Ord(W and (1 shl I) <> 0)]); {0 or 1}
Inc(N);
end;
end;
function BinaryLW(L : LongInt) : WideString;
{-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] := WideChar(StHexDigits[Ord(L and LongInt(1 shl I) <> 0)]); {0 or 1}
Inc(N);
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -