我对DELPHI写的几个基类型

类别:Delphi 点击:0 评论:0 推荐:

//用惯JAVA或C#的人可能对DELPHI非常生气,连基本的类型都没有,我平时工作中,经常会曾试着把一些函数集合在一起,也经常做一些属于自己的基础类型的函数,此处把它们弄出来,有不当之处,请大家点评.
unit BaseClass;

interface

uses
  SysUtils, Classes, StrUtils, IdGlobal, Math;

type
  TCharSet = set of char;
var
  TNormalCharSet: TCharSet = [#13, #10, #32, '.', ',', ';'];
type

  TString = class
  private
    FText: string;
  public
    function CharAt(APosition: Integer): Char;
      //指定位置的字母
    function toLowerCase: string; overload;
    class function toLowerCase(AString: string): string; overload;
      //小写

    function toUpperCase: string; overload;
    class function toUpperCase(AString: string): string; overload;
      //大写

    class function ValueOf(AValue: string): Boolean; overload;
    class function ValueOf(AValue: Boolean): string; overload;

    class function StringIn(AValue: string; AValues: array of string): Boolean;

    class function Left(AValue: string; ALength: Integer): string;
    class function Right(AValue: string; ALength: Integer): string;

    class function DeletePrefix(AValue: string; FixedString: TCharSet = [#32]): string; //删除前缀
    class function DeleteSuffix(AValue: string; FixedString: TCharSet = [#32]): string; //删除后缀
    //
    class function CompareString(AValue1: string; AValue2: string): Boolean;

    class function HashCode(AValue: string): Integer;

    class function LastChar(AValue: string): Char;

    class function StringReplace(const S, OldPattern, NewPattern: string;
                   Flags: TReplaceFlags): string;
    class function StringOfChar(Ch: Char; Count: Integer): string;

    class function SetString(var s: string; buffer: PChar; len: Integer): string;

    class function GetPy(AStr: string): string;
          //得到对应的拼音.
    class function IsAllChinese(AStr: string): Boolean;
    class function IsAllEnglish(AStr: string): Boolean;

    class function GetFirstWord(AValue: string; var AWord: string; ASeparator: TCharSet): Integer; overload;

    class function GetFirstWord(AValue: string; var AWord: string; ASeparator: string): Integer; overload;
                   //返回值为从开始到该单据的长度。

    class function GetAllWord(AValue: string; ASeparator: string): TStringList; overload;
                   //返回所有的关键字。使用完后,请将结果集Free;
                   //注:在以后的使用中,请不要使用该函数。请使用它的重载版
                   //GetAllWord(AValue: string; ASeparator: string; AStringList: TStringList);
    class procedure GetAllWord(AValue: string; ASeparator: string; AStringList: TStrings); overload;
                   //把所有的结果集装入AStringList;

    class procedure GetAllWordWithAll(AValue: string; ASeparator: string; AStringList: TStrings);
    class function StringToCharSet(AValue: string): TCharSet;
    class function CharSetToString(AValue: TCharSet): string;

    class function UpdateSentence(AOldString: string; //被操作字符串
                                  AUpdateSource: string; //查找的单词。
                                  AUpdateString: string; //替换的单据。
                                  ASentenceSeparator: string; //句子分隔符。
                                  AWordSeparator: string  //单据分隔符;
                                  ): string; //返回结果。
       //如 ' dbarcode ASC, dname DESC', 'dbarcode', '', ',', ' '的返回值为
            //' dname DESC';

    class function DeleteRepeat(AOldString: string;          //要处理字符
                                ADeleteString: Char;         //要删除的字符
                                ARepeat: Char): string;      //重复字符

    class function IfThen(AExpression: Boolean; ATrue: string; AFalse: string): string;
                   //根据表达式的值,返回相应的字符串。
    class function AbsoluteToRelate(AAbsolute: string; ACurrent: string): string;
                   //给定两个文件,将绝对路径转换成相对路径。
    class function RelateToAbsolute(ARelate: string; ACurrent: string): string;
    class function SimilarPosition(AOne, ATwo: string): Integer;

    class function GetCharNum(AString: string; AChar: Char): Integer;

    class function IndexOf(AString, ASubString: string): Integer;

    class function ZeroToInt(AString: string): Integer;
    class function ZeroToFloat(AString: string): Double;

    class function ZeroToStr(AString: string): string;

    class function SameText(AString, AString1: string): Boolean;

    class function Reverse(AString: string): string;

    class function IsValidIP(const S: String): Boolean;

    class function FillString(AChar: Char; ALength: Integer): string;

    class function StuffString(const AText: string; AStart, ALength: Cardinal;
       const ASubText: string): string;

    class function GetNextString(var SourceString: string; ASplitChar: string): string;
  end;

  //整型类。
  TInteger = class
    class function IntToStr(AInteger: Integer): string; overload;
    class function IntToStr(AInteger: Int64): string; overload;

    class function IsValidInt(AString: string): Boolean;
    class function IsValidInt64(AString: string): Boolean;

    class function MaxInt: Integer;
    class function MaxLongInt: Integer;
    class function HashCode(AInteger: Integer): Integer;

    class function IntToBin(AInteger: Cardinal): string;
    class function IntToHex(AInteger: Integer): string;

    class function HexToInt(AString: string): Integer;
    class function MakeSerialNo(AInteger: Integer; ADigit: Integer): string;
  end;

  TFloat = class
    class function IsValidFloat(AString: string): Boolean;
    class function MaxDouble: Double;
    class function MinDouble: Double;
    class function MaxExtended: Extended;
    class function MinExtended: Extended;

    class function SameValue(const A, B: Single; Epsilon: Single = 0): Boolean; overload;
    class function SameValue(const A, B: Double; Epsilon: Double = 0): Boolean; overload;
    class function SameValue(const A, B: Extended; Epsilon: Extended = 0): Boolean; overload;

    class function FloatToMoney(const Value: Double; Round: Boolean = True): string;
  end;

  TBoolean = class
    class function BoolToStr(ABoolean: Boolean): string;
    class function StrToBool(AString: string): Boolean;  //如果不为'true'则为false;
  end;
implementation

{ TString }
function GetPYIndexChar(AChar: string): Char;
begin
  case WORD(AChar[1]) shl 8 + WORD(AChar[2]) of
    $B0A1..$B0C4: Result := 'A';
    $B0C5..$B2C0: Result := 'B';
    $B2C1..$B4ED: Result := 'C';
    $B4EE..$B6E9: Result := 'D';
    $B6EA..$B7A1: Result := 'E';
    $B7A2..$B8C0: Result := 'F';
    $B8C1..$B9FD: Result := 'G';
    $B9FE..$BBF6: Result := 'H';
    $BBF7..$BFA5: Result := 'J';
    $BFA6..$C0AB: Result := 'K';
    $C0AC..$C2E7: Result := 'L';
    $C2E8..$C4C2: Result := 'M';
    $C4C3..$C5B5: Result := 'N';
    $C5B6..$C5BD: Result := 'O';
    $C5BE..$C6D9: Result := 'P';
    $C6DA..$C8BA: Result := 'Q';
    $C8BB..$C8F5: Result := 'R';
    $C8F6..$CBF9: Result := 'S';
    $CBFA..$CDD9: Result := 'T';
    $CDDA..$CEF3: Result := 'W';
    $CEF4..$D188: Result := 'X';
    $D1B9..$D4D0: Result := 'Y';
    $D4D1..$D7F9: Result := 'Z';
  else
    Result := Char(0);
  end;
end;

class function TString.GetPy(AStr: string): string;
var
  I: Integer;
begin
  Result := '';
  for I := 1 to Length(AStr) do
  begin

    if ByteType(AStr, i) = mbTrailByte then
      Result := Result + GetPYIndexChar(AStr[i - 1] + AStr[i])
    else
      if ByteType(AStr, i) = mbSingleByte then
        Result := Result + AStr[i];
  end;

end;

function TString.CharAt(APosition: Integer): Char;
begin
  Result := FText[APosition];
end;

class function TString.CharSetToString(AValue: TCharSet): string;
begin

end;

class function TString.CompareString(AValue1, AValue2: string): Boolean;
begin
  Result := UpperCase(AValue1) = UpperCase(AValue2);
end;

class function TString.DeletePrefix(AValue: string;
  FixedString: TCharSet): string;
begin
  while System.Length(AValue) > 0 do
  begin
    if AValue[1] in FixedString then
      Delete(AValue, 1, 1)
    else
      Break;
  end;
  Result := AValue;
end;

class function TString.GetFirstWord(AValue: string; var AWord: string; ASeparator: TCharSet
  ): Integer;
var
  tmpStr: string;
  tmpPos: Integer;
begin
  tmpStr := DeleteSuffix(AValue, ASeparator);
  tmpStr := DeletePrefix(AValue, ASeparator);

  Result := Length(AValue) - Length(tmpStr);
{  if Length(tmpStr) = 0 then Exit;

  if (tmpStr[1] = '''') and (tmpStr[2] = '''')then
  begin
    for tmpPos := 3 to Length(tmpStr) do
    begin
      if tmpStr[tmpPos] in [''''] then
        Break;
    end;
  end;

  if tmpPos > 3 then tmpPos :=tmpPos + 2;
}
  for tmpPos := 1 to Length(tmpStr) do
  begin
    if tmpStr[tmpPos] in ASeparator then
      Break;
  end;

  tmpPos := tmpPos -1;

//  {TODO : -oghs 修复最后一个参数解析不正确}
  if (tmpPos = 0) and (AValue <> '') then
    tmpPos := Length(AValue);

  AWord := Copy(AValue, Result + 1, tmpPos);

  Result := Result + tmpPos;
end;

class function TString.HashCode(AValue: string): Integer;
var
  i: Integer;
  tmpValue: Integer;
begin
  tmpValue := 0;
  for I := 1 to System.Length(AValue) do
  begin
    tmpValue := 3 * tmpValue + Ord(AValue[I]);
  end;
  Result := tmpValue;
end;

class function TString.IsAllChinese(AStr: string): Boolean;
var
  I: Integer;
begin
  Result := True;
  for I := 1 to Length(AStr) do
  begin
    if ByteType(AStr, I) = mbSingleByte then
    begin
      Result := False;
      Break;
    end;
  end;
end;

class function TString.IsAllEnglish(AStr: string): Boolean;
var
  I: Integer;
begin
  Result := True;
  for I := 1 to Length(AStr) do
  begin
    if ByteType(AStr, I) <> mbSingleByte then
    begin
      Result := False;
      Break;
    end;
  end;
end;

class function TString.LastChar(AValue: string): Char;
begin
  Result := AValue[System.Length(AValue)];
end;

class function TString.Left(AValue: string; ALength: Integer): string;
begin
  Result := Copy(AValue, 1, ALength);
end;

class function TString.Right(AValue: string; ALength: Integer): string;
begin
  Result := StrUtils.RightStr(AValue, ALength);
end;

class function TString.SetString(var s: string; buffer: PChar;
  len: Integer): string;
begin
  System.SetString(s, buffer, len);
  Result := s;
end;

class function TString.StringIn(AValue: string;
   AValues: array of string): Boolean;
var
  I: Integer;
begin
  Result := False;
  for I := Low(AValues) to High(AValues) do
  begin
    if UpperCase(AValue) = UpperCase(AValues[I]) then
    begin
      Result := True;
      Break;
    end;
  end;
end;

class function TString.StringOfChar(Ch: Char; Count: Integer): string;
begin
  Result := System.StringOfChar(Ch, Count);
end;

class function TString.StringReplace(const S, OldPattern,
  NewPattern: string; Flags: TReplaceFlags): string;
begin
  Result := Sysutils.StringReplace(S, OldPattern, NewPattern, Flags);
end;

class function TString.StringToCharSet(AValue: string): TCharSet;
var
  I: Integer;
begin
  Result := [];
  for I := 1 to Length(AValue) do
  begin
    Result := Result + [AValue[I]];
  end;   
end;

function TString.toLowerCase: string;
begin
  Result := LowerCase(FText);
end;

function TString.toUpperCase: string;
begin
  Result := Uppercase(FText);
end;

class function TString.ValueOf(AValue: Boolean): string;
begin
  if AValue then
    Result := '是'
  else
    Result := '否';
end;

class function TString.ValueOf(AValue: string): Boolean;
begin
  Result := StringIn(AValue, ['是', 'yes', 'ok']);
end;

class function TString.GetFirstWord(AValue: string; var AWord: string;
  ASeparator: string): Integer;
begin
  Result := GetFirstWord(AValue, AWord, StringToCharSet(ASeparator));
end;

class function TString.GetAllWord(AValue, ASeparator: string): TStringList;
var
  tmpList: TStringList;
  tmpWord: string;
begin
  tmpList := TStringList.Create;
  while Length(AValue) > 0 do
  begin
    tmpWord := '';
    Delete(AValue, 1, GetFirstWord(AValue, tmpWord, ASeparator));
    if tmpWord <> '' then
      tmpList.Add(tmpWord)
    else
      Break;
  end;
  Result := tmpList;
end;

class function TString.UpdateSentence(AOldString, AUpdateSource,
  AUpdateString, ASentenceSeparator, AWordSeparator: string): string;
var
  tmpSentence: string;
  tmpWord: string;
  tmpWord1: string;
  i: Integer;
  tmpResult: string;
begin
  //得到第一个句子
  tmpSentence := AOldString;
  tmpResult := '';
  while Length(tmpSentence) > 0 do
  begin
    i := GetFirstWord(tmpSentence, tmpWord, ASentenceSeparator);
    tmpResult := tmpResult + Left(tmpSentence, i - Length(tmpWord));
    Delete(tmpSentence, 1, I);
    if tmpWord <> '' then
    begin
      i := GetFirstWord(tmpWord, tmpWord1, AWordSeparator);
      tmpResult := tmpResult + Left(tmpWord, i - Length(tmpWord1));
      if CompareString(tmpWord1, AUpdateSource) then
      begin
        tmpResult := tmpResult + AUpdateString;
      end
      else
      begin
        tmpResult := tmpResult + tmpWord;
      end;
    end;
  end;
  tmpResult := DeletePrefix(tmpResult, [' ', ',']);
  tmpResult := DeleteSuffix(tmpResult, [' ', ',']);
  tmpResult := DeleteRepeat(tmpResult, ',', ' ');
  tmpResult := DeleteRepeat(tmpResult, ' ', ' ');
  Result := tmpResult;

end;

class function TString.DeleteRepeat(AOldString: string; ADeleteString,
  ARepeat: Char): string;
var
  I: Integer;
  tmpfind1: Boolean;
begin
  tmpfind1 := False;
  for I := Length(AOldString) downto 1 do
  begin
    if tmpfind1 then
    begin
      if AOldString[I] = ADeleteString then
        Delete(AOldString, I, 1)
      else
      begin
        if AOldString[I] = ARepeat then
        Continue;
        tmpfind1 := AOldString[I] = ADeleteString;
      end;
    end
    else
    begin
      if ADeleteString <> ARepeat then
        if AOldString[I] = ARepeat then
          Continue;
       
      tmpfind1 := AOldString[I] = ADeleteString
    end;
  end;
  Result := AOldString;
end;

class function TString.DeleteSuffix(AValue: string;
  FixedString: TCharSet): string;
begin
  while System.Length(AValue) > 0 do
  begin
    if AValue[System.Length(AValue)] in FixedString then
      Delete(AValue, System.Length(AValue), 1)
    else
      Break;
  end;
  Result := AValue;
end;

class procedure TString.GetAllWord(AValue, ASeparator: string;
  AStringList: TStrings);
var
  tmpWord: string;
begin
  if AStringList = nil then
    AStringList := TStringList.Create;

  while Length(AValue) > 0 do
  begin
    tmpWord := '';
    Delete(AValue, 1, GetFirstWord(AValue, tmpWord, ASeparator));
    if tmpWord <> '' then
      AStringList.Add(tmpWord)
    else
      Break;
  end;

end;

class function TString.IfThen(AExpression: Boolean; ATrue,
  AFalse: string): string;
begin
  if AExpression then
    Result := ATrue
  else
    Result := AFalse;   
end;

class function TString.AbsoluteToRelate(AAbsolute,
  ACurrent: string): string;
var
  tmpSimilarString: string;
  AOldFile: string;
  i: Integer;
  tmpPos: Integer;
begin
  //转换后形成 ..\..\a.ini;
//如果不在同一个驱动器上,则直接返回绝对路径.
  if ExtractFileDrive(AAbsolute) <> ExtractFileDrive(ACurrent) then
    Result := AAbsolute
  else
  begin
    tmpSimilarString := '';
    AOldFile := AAbsolute;
    AAbsolute := ExtractFilePath(AAbsolute);
    tmpPos := SimilarPosition(AAbsolute, ACurrent);
    Delete(AOldFile, 1, tmpPos - 1);
    Delete(ACurrent, 1, tmpPos - 1);
    for i := 0 to GetCharNum(ACurrent, '\') -1 do
    begin
      tmpSimilarString := tmpSimilarString + '..\';
    end;
    Result := tmpSimilarString + AOldFile;
  end;
end;

class function TString.RelateToAbsolute(ARelate, ACurrent: string): string;
var
  tmpSimilarString: string;
  tmpRootCount: Integer;
  i: Integer;
begin
  if Length(ARelate) > 2 then
  begin
    if ARelate[2] = ':' then
    begin
      Result := ARelate;
      Exit;
    end;
  end;

  tmpSimilarString := '';
  tmpRootCount := 0;
  while True do
  begin
    if LeftStr(ARelate, 3) = '..\' then
    begin
      Inc(tmpRootCount);
      Delete(ARelate, 1, 3);
    end
    else
      break;
  end;

  tmpSimilarString := ReverseString(ExtractFilePath(ACurrent));
  for i := 0 to tmpRootCount do
  begin
    Delete(tmpSimilarString, 1, Pos('\', tmpSimilarString));
  end;

  Result := ReverseString(tmpSimilarString) + ARelate;
end;

class function TString.SimilarPosition(AOne, ATwo: string): Integer;
var
  i: Integer;
  Max: Integer;
begin
  if Length(AOne) < Length(ATwo) then
    Max := Length(AOne)
  else
    Max := Length(ATwo);
  for i := 1 to Max do
  begin
    if AOne[i] <> ATwo[i] then
      Break;
  end;
  Result := i;
end;

class function TString.GetCharNum(AString: string; AChar: Char): Integer;
var
  i: Integer;
begin
  Result := 0;
  for i := 1 to Length(AString) do
  begin
    if AString[i] = AChar then
      Inc(Result);
  end;
end;

class procedure TString.GetAllWordWithAll(AValue, ASeparator: string;
  AStringList: TStrings);
var
  tmpI: Integer;
  tmpPos: Integer;
begin
  if AStringList = nil then
    AStringList := TStringList.Create;

  tmpPos := 0;
  while Length(AValue) > 0 do
  begin
    for tmpI := 1 to Length(AValue) do
    begin
      tmpPos := Pos(AValue[tmpPos], ASeparator);
      if tmpPos > 0 then
      begin
        AStringList.Add(Copy(AValue, 1, tmpPos - 1));
        AStringList.Add(Copy(AValue, tmpPos, 1));
        Delete(AValue, 1, tmpPos);
        Break;
      end
    end;

  end;    // while
 
end;

class function TString.toLowerCase(AString: string): string;
begin
  Result := LowerCase(AString);
end;

class function TString.toUpperCase(AString: string): string;
begin
  Result := Uppercase(AString);
end;

class function TString.IndexOf(AString, ASubString: string): Integer;
begin
  Result := Pos(ASubstring, AString);
end;

class function TString.ZeroToInt(AString: string): Integer;
begin
  if Trim(AString) = '' then
    AString := '0';

  Result := StrToInt(AString);
end;

class function TString.ZeroToFloat(AString: string): Double;
begin
  if Trim(AString) = '' then
    AString := '0.0';
  Result := StrToFloat(AString);   
end;

class function TString.SameText(AString, AString1: string): Boolean;
begin
  Result := SysUtils.SameText(AString, AString1);
end;

class function TString.Reverse(AString: string): string;
begin
  Result := ReverseString(AString);
end;

class function TString.IsValidIP(const S: String): Boolean;
var
  j, i: Integer;
  LTmp: String;
begin
  Result := True;
  LTmp := Trim(S);
  for i := 1 to 4 do begin
    j := StrToIntDef(Fetch(LTmp, '.'), -1);   
    Result := Result and (j > -1) and (j < 256);
    if NOT Result then begin
      Break;
    end;
  end;
end;

class function TString.ZeroToStr(AString: string): string;
begin
  if Trim(AString) = '' then
    Result := '0'
  else
    Result := AString;
end;

class function TString.FillString(AChar: Char; ALength: Integer): string;
var
  i: Integer;
begin
  Result := '';
  for I := 1 to ALength do    // Iterate
  begin
    Result := Result + AChar;
  end;    // for
end;

class function TString.StuffString(const AText: string; AStart,
  ALength: Cardinal; const ASubText: string): string;
begin
  Result := StrUtils.StuffString(AText, AStart, ALength, ASubText);
end;

class function TString.GetNextString(var SourceString: string;
  ASplitChar: string): string;
var
  tmpPos: Integer;
begin
  tmpPos := Pos(ASplitChar, SourceString);
  if tmpPos = 0 then
  begin
    Result := SourceString;
    SourceString := ''
  end
  else
  begin
    Result := TString.Left(SourceString, tmpPos -1);
    Delete(SourceString, 1, tmpPos);
  end;
end;

{ TInteger }

class function TInteger.IntToStr(AInteger: Integer): string;
begin
  Result := Sysutils.IntToStr(AInteger);
end;

class function TInteger.HashCode(AInteger: Integer): Integer;
begin
  Result := AInteger;
end;

class function TInteger.IntToStr(AInteger: Int64): string;
begin
  Result := Sysutils.IntToStr(AInteger);
end;

class function TInteger.IsValidInt(AString: string): Boolean;
begin
  Result := True;
  try
    StrToInt(AString);
  except
    Result := False;
  end;
end;

class function TInteger.IsValidInt64(AString: string): Boolean;
begin
  Result := True;
  try
    StrToInt(AString);
  except
    Result := False;
  end;
end;

class function TInteger.MaxInt: Integer;
begin
  Result := System.MaxInt;
end;

class function TInteger.MaxLongInt: Integer;
begin
  Result := System.MaxLongint;
end;

class function TInteger.IntToBin(AInteger: Cardinal): string;
var
  i: Integer;
begin
  SetLength(Result, 32);
  for i := 1 to 32 do
  begin
    if ((AInteger shl (i-1)) shr 31) = 0 then
      Result[i] := '0'
    else
      Result[i] := '1';
  end;

end;

class function TInteger.IntToHex(AInteger: Integer): string;
begin
  Result := SysUtils.IntToHex(AInteger, 0);
end;

class function TInteger.HexToInt(AString: string): Integer;
begin
  if TString.Left(AString, 1) = '$' then
    Result := StrToInt(AString)
  else
    Result := StrToInt('$' + AString);   
end;

class function TInteger.MakeSerialNo(AInteger, ADigit: Integer): string;
var
  tmpStr: string;
  i: Integer;
begin
  tmpStr := '';
  for I := 0 to ADigit - 1 do    // Iterate
  begin
    tmpStr := tmpStr + '0';
  end;    // for

  Result := FormatFloat(tmpStr, AInteger);

end;

{ TFloat }

class function TFloat.FloatToMoney(const Value: Double; Round: Boolean): string;
begin
  //金额默认采用四舍五入
 
end;

class function TFloat.IsValidFloat(AString: string): Boolean;
begin
  Result := True;
  try
    StrToFloat(AString);
  except
    Result := False;
  end;
end;

class function TFloat.MaxDouble: Double;
begin
  Result := 1.7e+308;
end;

class function TFloat.MaxExtended: Extended;
begin
  Result := 1.1e+4932;
end;

class function TFloat.MinDouble: Double;
begin
  Result := 5.0e-324;
end;

class function TFloat.MinExtended: Extended;
begin
  Result := 3.4e-4932;
end;

class function TFloat.SameValue(const A, B: Single;
  Epsilon: Single): Boolean;
begin
  Result := Math.SameValue(A, B, Epsilon);
end;

class function TFloat.SameValue(const A, B: Double;
  Epsilon: Double): Boolean;
begin
  Result := Math.SameValue(A, B, Epsilon);
end;

class function TFloat.SameValue(const A, B: Extended;
  Epsilon: Extended): Boolean;
begin
  Result := Math.SameValue(A, B, Epsilon);
end;

{ TBoolean }

class function TBoolean.BoolToStr(ABoolean: Boolean): string;
begin
  if ABoolean then
    Result := 'True'
  else
    Result := 'False';         
end;

class function TBoolean.StrToBool(AString: string): Boolean;
begin
  if UpperCase(AString) = 'TRUE' then
    Result := True
  else
    Result := False;
   
end;

end.

本文地址:http://com.8s8s.com/it/it4442.htm