深入学习System.SysUtils.pas

深入学习System.SysUtils.pas

 

1、initialization和finalization的写法和其中的内容

initialization
  if ModuleIsCpp then HexDisplayPrefix := '0x';
  InitMonitorSupport;
{$IFNDEF NEXTGEN}
  AddModuleUnloadProc (ModuleUnloaded);
{$ENDIF !NEXTGEN}

{$IF (DEFINED(MSWINDOWS) and DEFINED(NEXTGEN)) or DEFINED(POSIX)}
  SafeCallErrorProc := @SafeCallError;
{$ENDIF (MSWINDOWS and NEXTGEN) or POSIX}
{$IFDEF LINUX}
  CheckLocale;
{$ENDIF LINUX}

{$IFDEF MSWINDOWS}
  InitPlatformIdLock := TObject.Create;
  InitDriveSpaceLock := TObject.Create;
  GetDiskFreeSpaceEx := @GetDiskFreeSpaceExThunk;
  DefaultFallbackLanguages := GetLocaleOverride('');
  NTQueryObject := nil;
  InternalFormatSettingsLock := TObject.Create;
{$ENDIF MSWINDOWS }
  GetFormatSettings ; { Win implementation uses platform id }
finalization
{$IFDEF MSWINDOWS}
  InternalFormatSettingsLock. Free ;
  InitDriveSpaceLock.Free;
  InitPlatformIdLock.Free;
{$ENDIF MSWINDOWS}
{$IFDEF POSIX}
  if libuuidHandle <> 0 then
    dlclose(libuuid Handle);
{$ENDIF}
{$IFNDEF NEXTGEN}
  RemoveModuleUnloadProc (ModuleUnloaded);
  ClearHashTables;
{$ENDIF !NEXTGEN}
  FreeTerminateProcs ;
  DoneMonitorSupport ;
  ClearFormatSettings ;
end.

2、

// 求Unicode字符串的字节长度:

function ByteLength (const S: string): Integer; inline;

3、

const
  netapi = 'netapi32.dll';

  NERR_Success = 0;

//Win32获取当前计算机所加入的工作组(WorkGroup):

function GetNetWkstaMajorMinor (var MajorVersion, MinorVersion: DWORD): Boolean; //调用下面返回:

function NetWkstaGetInfo100(ServerName: LPWSTR; Level: DWORD; var BufPtr: LPWKSTA_INFO_100): Longint;

4、var  HexDisplayPrefix : string = '$'; //16进制默认全局前缀变量

 

5、{ 多字节字符集的字节类型MultiByte Character Set ( MBCS ) byte type
        单字节字符集SBCS  }
  TMbcsByteType = (mbSingleByte, mbLeadByte, mbTrailByte);

function ByteType (const S: UnicodeString; Index: Integer): TMbcsByteType ; overload;
 

6、字符串替换工具

type
  TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase);

function StringReplace (const Source, OldPattern, NewPattern: string;
  Flags: TReplaceFlags): string; overload;

7、字符串文本断词分割符封装工具

{ WrapText will scan a string for BreakChars and insert the BreakStr at the
  last BreakChar position before MaxCol. Will not insert a break into an
  embedded quoted string (both ''' and '"' supported) }

function WrapText (const Line, BreakStr: string; const BreakChars: TSysCharSet;
  MaxCol: Integer): string; overload;
function WrapText (const Line: string; MaxCol: Integer = 45): string; overload;


8、字符串字符分割工具

{ LastDelimiter returns the byte index in S of the rightmost whole
  character that matches any character in Delimiters (except null (#0)).
  S may contain multibyte characters; Delimiters must contain only single
  byte non-null characters.
  Example: LastDelimiter('\.:', 'c:\filename.ext') returns 12. }

function LastDelimiter(const Delimiters, S: string): Integer; overload;

{ FindDelimiter returns the index in S of the character that matches any of
  the characters in Delimiters (except null (#)). StartIdx specifies the
  index in S at which the search for delimiters will start. }

function FindDelimiter(const Delimiters, S: string; StartIdx: Integer = 1): Integer;

{ IsPathDelimiter returns True if the character at byte S[Index]
  is a PathDelimiter ('\' or '/'), and it is not a MBCS lead or trail byte. }

function IsPathDelimiter(const S: string; Index: Integer): Boolean; overload;

{ IsDelimiter returns True if the character at byte S[Index] matches any
  character in the Delimiters string, and the character is not a MBCS lead or
  trail byte.  S may contain multibyte characters; Delimiters must contain
  only single byte characters. }

function IsDelimiter(const Delimiters, S: string; Index: Integer): Boolean; overload;
 

9、{ 多字节函数MBCS functions: }
{ ByteType indicates what kind of byte exists at the Index'th byte in S.
  Western locales always return mbSingleByte.  Far East multibyte locales
  may also return mbLeadByte, indicating the byte is the first in a multibyte
  character sequence, and mbTrailByte, indicating that the byte is one of
  a sequence of bytes following a lead byte.  One or more trail bytes can
  follow a lead byte, depending on locale charset encoding and OS platform.
  Parameters are assumed to be valid. }

{$IFNDEF NEXTGEN}
function ByteType(const S: AnsiString; Index: Integer): TMbcsByteType; overload; deprecated 'Moved to the AnsiStrings unit';
{$ENDIF !NEXTGEN}
function ByteType(const S: UnicodeString; Index: Integer): TMbcsByteType; overload;

{ StrByteType works the same as ByteType, but on null-terminated PChar strings }

{$IFNDEF NEXTGEN}
function StrByteType(Str: PAnsiChar; Index: Cardinal): TMbcsByteType; overload; deprecated 'Moved to the AnsiStrings unit';
{$ENDIF !NEXTGEN}
function StrByteType(Str: PWideChar; Index: Cardinal): TMbcsByteType; overload;

{ ByteToCharLen returns the character length of a MBCS string, scanning the
  string for up to MaxLen bytes.  In multibyte character sets, the number of
  characters in a string may be less than the number of bytes.  }

{$IFNDEF NEXTGEN}
function ByteToCharLen(const S: AnsiString; MaxLen: Integer): Integer; overload; inline; deprecated 'Use ElementToCharLen';
{$ENDIF !NEXTGEN}
function ByteToCharLen(const S: UnicodeString; MaxLen: Integer): Integer; overload; inline; deprecated 'Use ElementToCharLen.';

{ CharToByteLen returns the byte length of a MBCS string, scanning the string
  for up to MaxLen characters. }

{$IFNDEF NEXTGEN}
function CharToByteLen(const S: AnsiString; MaxLen: Integer): Integer; overload; inline; deprecated 'Use CharToElementLen.';
{$ENDIF !NEXTGEN}
function CharToByteLen(const S: UnicodeString; MaxLen: Integer): Integer; overload; inline; deprecated 'Use CharToElementLen.';

{ ByteToCharIndex returns the 1-based character index of the Index'th byte in
  a MBCS string.  Returns zero if Index is out of range:
  (Index <= 0) or (Index > Length(S)) }

{$IFNDEF NEXTGEN}
function ByteToCharIndex(const S: AnsiString; Index: Integer): Integer; overload; inline; deprecated 'Use ElementToCharIndex.';
{$ENDIF !NEXTGEN}
function ByteToCharIndex(const S: UnicodeString; Index: Integer): Integer; overload; inline; deprecated 'Use ElementToCharIndex.';

{ CharToByteIndex returns the 1-based byte index of the Index'th character
  in a MBCS string.  Returns zero if Index or Result are out of range:
  (Index <= 0) or (Index > Length(S)) or (Result would be > Length(S)) }

{$IFNDEF NEXTGEN}
function CharToByteIndex(const S: AnsiString; Index: Integer): Integer; overload; inline; deprecated 'Use CharToElementIndex.';
{$ENDIF !NEXTGEN}
function CharToByteIndex(const S: UnicodeString; Index: Integer): Integer; overload; inline; deprecated 'Use CharToElementIndex.';
 

10.1、字符串构建工具  line=3926

// Record and Class for Delphi Native Stringbuilder

type
  TCharArray = TArray<Char>;

type
  TCharSearch = record
    ArrayPtr: PChar;
    MatchPtr: PChar;
  end;

  TStringBuilder = class

      Create  Append

      AppendFormat  AppendLine  Clear  EnsureCapacity  Equals  Insert 

      Remove  Replace  GetEnumerator  Capacity  

      Chars[index: Integer]  Length  MaxCapacity

10.2、基本数据类型助手  line=3926

  TCompareOption = (coLingIgnoreCase, coLingIgnoreDiacritic, coIgnoreCase,
    coIgnoreKanatype, coIgnoreNonSpace, coIgnoreSymbols, coIgnoreWidth,
    coLingCasing, coDigitAsNumbers, coStringSort);
  TCompareOptions = set of TCompareOption;
  {$SCOPEDENUMS ON}
  TStringSplitOptions = (None, ExcludeEmpty, ExcludeLastEmpty);
  {$SCOPEDENUMS OFF}

  TStringHelper = record helper for string    line=4030

  TSingleHelper = record helper for Single    line=4081

  public
    const
      Epsilon:Single = 1.4012984643248170709e-45;
      MaxValue:Single =  340282346638528859811704183484516925440.0;
      MinValue:Single = -340282346638528859811704183484516925440.0;
      PositiveInfinity:Single =  1.0 / 0.0;
      NegativeInfinity:Single = -1.0 / 0.0;
      NaN:Single = 0.0 / 0.0;

  TDoubleHelper = record helper for Double    line=4245
  public
    const
      Epsilon:Double = 4.9406564584124654418e-324;
      MaxValue:Double =  1.7976931348623157081e+308;
      MinValue:Double = -1.7976931348623157081e+308;
      PositiveInfinity:Double =  1.0 / 0.0;
      NegativeInfinity:Double = -1.0 / 0.0;
      NaN:Double = 0.0 / 0.0;

  TExtendedHelper = record helper for Extended   line=4311
  public
    const
      Epsilon:Extended = {$IFDEF EXTENDEDHAS10BYTES} 3.64519953188247460253e-4951 {$ELSE} 4.9406564584124654418e-324 {$ENDIF};
      MaxValue:Extended =  {$IFDEF EXTENDEDHAS10BYTES} 1.18973149535723176505e+4932 {$ELSE} 1.7976931348623157081e+308 {$ENDIF};
      MinValue:Extended = -{$IFDEF EXTENDEDHAS10BYTES} 1.18973149535723176505e+4932 {$ELSE} 1.7976931348623157081e+308 {$ENDIF};
      PositiveInfinity:Extended =  1.0 / 0.0;
      NegativeInfinity:Extended = -1.0 / 0.0;
      NaN:Extended = 0.0 / 0.0;

  TByteHelper = record helper for Byte    line=4375
  public
    const
      MaxValue = 255;
      MinValue = 0;

    ToString  ToBoolean  ToHexString  ToHexString(const MinDigits: Integer)  ToDouble  ToExtended  Size 

    ToString(const Value: Byte)  Parse(const S: string): Byte  TryParse(const S: string; out Value: Byte): Boolean

  TShortIntHelper = record helper for ShortInt   line=4395
  public
    const
      MaxValue = 127;
      MinValue = -128;

  TWordHelper = record helper for Word  line=4415
  public
    const
      MaxValue = 65535;
      MinValue = 0;

  TSmallIntHelper = record helper for SmallInt line=4435
  public
    const
      MaxValue = 32767;
      MinValue = -32768;

  TCardinalHelper = record helper for Cardinal { for LongWord type too } line=4455
  public
    const
      MaxValue = 4294967295;
      MinValue = 0;

    ToHexString(const MinDigits: Integer): string  ToHexString  ......

  TIntegerHelper = record helper for Integer { for LongInt type too }  line=4475
  public
    const
      MaxValue = 2147483647;
      MinValue = -2147483648;

  TUInt64Helper = record helper for UInt64  line=4495
  public
    const
      MaxValue = 18446744073709551615;
      MinValue = 0;

  TInt64Helper = record helper for Int64   line=4515
  public
    const
      MaxValue = 9223372036854775807;
      MinValue = -9223372036854775808;

  TNativeUIntHelper = record helper for NativeUInt   line=4535
  public
    const
{$IFDEF CPU64BITS}
      MaxValue = 18446744073709551615;
{$ELSE !CPU64BITS}
      MaxValue = 4294967295;
{$ENDIF CPU64BITS}
      MinValue = 0;

  TNativeIntHelper = record helper for NativeInt   line=4559
  public
    const
{$IFDEF CPU64BITS}
      MaxValue = 9223372036854775807;
      MinValue = -9223372036854775808;
{$ELSE !CPU64BITS}
      MaxValue = 2147483647;
      MinValue = -2147483648;
{$ENDIF CPU64BITS}

  TBooleanHelper = record helper for Boolean   line=4588
  public
    function ToInteger: Integer; inline;
    function ToString(UseBoolStrs: TUseBoolStrs = TUseBoolStrs.False): string; overload; inline;

    class function Size: Integer; inline; static;
    class function ToString(const Value: Boolean; UseBoolStrs: TUseBoolStrs = TUseBoolStrs.False): string; overload; inline; static;
    class function Parse(const S: string): Boolean; inline; static;
    class function TryToParse(const S: string; out Value: Boolean): Boolean; inline; static;

  TByteBoolHelper = record helper for ByteBool   line=4599
  public
    function ToInteger: Integer; inline;
    function ToString: string; overload; inline;

    class function Size: Integer; inline; static;
    class function ToString(const Value: Boolean): string; overload; inline; static;
    class function Parse(const S: string): Boolean; inline; static;
    class function TryToParse(const S: string; out Value: Boolean): Boolean; inline; static;
  end;

  TWordBoolHelper = record helper for WordBool  line=4610
  public

  TLongBoolHelper = record helper for LongBool  line=4621
  public

11、控制命令集合:

procedure Sleep(milliseconds: Cardinal);{$IFDEF MSWINDOWS} stdcall; {$ENDIF}
{$IFDEF MSWINDOWS}
(*$EXTERNALSYM Sleep*)
{$ENDIF}  //:同步暂停N毫秒

function GetModuleName(Module: HMODULE): string;

function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer;
  Buffer: PChar; Size: Integer): Integer;//:返回异常编号

procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);//:抛出异常提示!

procedure Abort//:谢谢哦那个该行中断执行

procedure OutOfMemoryError;

procedure Beep; inline;  //:给出Beep响声提示!

12、全局变量(带默认值):

{ Empty string and null string pointer. These constants are provided for
  backwards compatibility only.  }

  EmptyStr: string = ''; //空白字符串
{$IFNDEF NEXTGEN}
  NullStr: PString = @EmptyStr;

  EmptyWideStr: WideString = '';//空白字符串
  NullWideStr: PWideString = @EmptyWideStr;

  EmptyAnsiStr: AnsiString = '';//空白字符串
  NullAnsiStr: PAnsiString = @EmptyAnsiStr;
{$ENDIF !NEXTGEN}

 

未完待续,持续更新中......

别的博客对这个单元的翻译参考:

1、    https://blog.csdn.net/broker_chengchaoji/article/details/1540853?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-1.channel_param

猜你喜欢

转载自blog.csdn.net/pulledup/article/details/104576831
今日推荐