⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subutils.pas

📁 delphi 中用于 css 样式表的解析的类
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit SubUtils;

// Version 1.0
//
// 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.
//
// Site: http://www.mythcode.org
// Author: Dzianis Koshkin
// E-mail: k5@yandex.ru
//
// (C) 2005 MYTHcode.org

interface

uses Types, SysUtils;

type

  TValueRelationship = -1..1;


  TSysByteSet = set of Byte;
  TStrCompare = function(const A, B: string): TValueRelationship;
  TStrArray = array of string;
  
const
  LessThanValue = Low(TValueRelationship);
  EqualsValue = 0;
  GreaterThanValue = High(TValueRelationship);


//--- Get substring ---//

function Sub(const S: string; After: Integer): string; overload;
function Sub(const S: string; After: Integer; Before: Integer): string; overload;
function Sub(const S: string; After: Integer; Before: string): string; overload;
function Sub(const S: string; After: Integer; Before: TSysCharSet): string; overload;
function Sub(const S: string; After: string): string; overload;
function Sub(const S: string; After: string; Before: Integer): string; overload;
function Sub(const S: string; After: string; Before: string): string; overload;
function Sub(const S: string; After: string; Before: TSysCharSet): string; overload;
function Sub(const S: string; After: TSysCharSet): string; overload;
function Sub(const S: string; After: TSysCharSet; Before: Integer): string; overload;
function Sub(const S: string; After: TSysCharSet; Before: string): string; overload;
function Sub(const S: string; After: TSysCharSet; Before: TSysCharSet): string; overload;
function Sub(const After, Before: PChar): string; overload;

function Get(const From, Before: PChar): string; overload;

//--- Position of value ---//

function PosOf(Value: string; S: string; Offset: Integer = 1): Integer; overload;
function PosOf(Value: TSysCharSet; S: string; Offset: Integer = 1): Integer; overload;

//--- Set of string ---//

function SetOf(const S: string): TSysCharSet; overload;
function SetOf(const S: array of Char): TSysCharSet; overload;
function SetOf(P: PChar): TSysCharSet; overload;

//--- Convert to string ---//

function _(const Value: string): string; overload;
function _(const Value: Boolean): string; overload;
function _(const Value: Integer): string; overload;
function _(const Value: Int64): string; overload;
function _(const Value: Currency): string; overload;
function _(const Value: Extended): string; overload;
function _(const Value: TSysCharSet): string; overload;
function _(const Value: TDateTime): string; overload;
function _(const Value: TPoint): string; overload;
function _(const Value: TRect): string; overload;
function _(const Value: TValueRelationship): string; overload;
function _(const Value: OleVariant): string; overload;
function _(const Value: TClass): string; overload;

function File_(const Value: TFileName): string; overload;

//--- Compare values ---//

function Compare(const A, B: Byte): TValueRelationship; overload;
function Compare(const A, B: ShortInt): TValueRelationship; overload;
function Compare(const A, B: Word): TValueRelationship; overload;
function Compare(const A, B: Smallint): TValueRelationship; overload;
function Compare(const A, B: Cardinal): TValueRelationship; overload;
function Compare(const A, B: Integer): TValueRelationship; overload;
function Compare(const A, B: Int64): TValueRelationship; overload;
function Compare(const A, B: Single): TValueRelationship; overload;
function Compare(const A, B: Real48): TValueRelationship; overload;
function Compare(const A, B: Real): TValueRelationship; overload;
//function Compare(const A, B: Double): TValueRelationship; overload;
function Compare(const A, B: Extended): TValueRelationship; overload;
function Compare(const A, B: Comp): TValueRelationship; overload;
function Compare(const A, B: Currency): TValueRelationship; overload;
function Compare(const A, B: TDateTime): TValueRelationship; overload;
function Compare(const A, B: string): TValueRelationship; overload;
function Compare(const A, B: PChar): TValueRelationship; overload;
function Compare(const A, B: Pointer): TValueRelationship; overload;
function Compare(const A, B: TSysCharSet): TValueRelationship; overload;
function Compare(const A, B: TSysByteSet): TValueRelationship; overload;
function CompareAsText(const A, B: string): TValueRelationship; overload;

function _R(const Value: Integer): TValueRelationship;

//--- Other ---//

function Local(const FileName: TFileName): TFileName;
function UnQuote(const S: string): string;
function Straight(const S: string): string;
function Union(const S1, S2: string; const Separator: string = ' '): string; overload;
function Separate(const Value: string; const Separator: string = ' '): TStrArray; overload;

implementation

type
  TSetAsArray = array[1..SizeOf(TSysCharSet)] of Byte;

function Sub(const S: string; After: Integer): string;
begin
  if After<0 then After:=Succ(Length(S)+After);
  Result:=Copy(S, Succ(After), Length(S)-After);
end;

function Sub(const S: string; After: Integer; Before: Integer): string;
begin
  if After<0 then After:=Succ(Length(S)+After);
  if Before<=0 then Before:=Succ(Length(S)+Before);
  Result:=Copy(S, Succ(After), Pred(Before-After));
end;

function Sub(const S: string; After: Integer; Before: string): string; overload;
var
  B: Integer;
begin
  if After<0 then After:=Succ(Length(S)+After);
  B:=PosOf(Before, S, Succ(After));
  Result:=Copy(S, Succ(After), Pred(B-After));
end;

function Sub(const S: string; After: Integer; Before: TSysCharSet): string; overload;
var
  B: Integer;
begin
  if After<0 then After:=Succ(Length(S)+After);
  B:=PosOf(Before, S, Succ(After));
  Result:=Copy(S, Succ(After), Pred(B-After));
end;

function Sub(const S: string; After: string): string; overload;
var
  A: Integer;
begin
  A:=Pos(After, S);
  if A=0 then Result:='' else
  begin
    A:=A+Pred(Length(After));
    Result:=Copy(S, Succ(A), Length(S)-A);
  end;
end;

function Sub(const S: string; After: string; Before: Integer): string; overload;
var
  A: Integer;
begin
  A:=Pos(After, S);
  if A=0 then Result:='' else
  begin
    A:=A+Pred(Length(After));
    if Before<=0 then Before:=Succ(Length(S)+Before);
    Result:=Copy(S, Succ(A), Pred(Before-A));
  end;
end;

function Sub(const S: string; After: string; Before: string): string; overload;
var
  A: Integer;
  B: Integer;
begin
  A:=Pos(After, S);
  if A=0 then Result:='' else
  begin
    A:=A+Pred(Length(After));
    B:=PosOf(Before,S,Succ(A));
    Result:=Copy(S,Succ(A),Pred(B-A));
  end;
end;

function Sub(const S: string; After: string; Before: TSysCharSet): string; overload;
var
  A,B: Integer;
begin
  A:=Pos(After, S);
  if A=0 then Result:='' else
  begin
    A:=A+Pred(Length(After));
    B:=PosOf(Before, S, Succ(A));
    Result:=Copy(S, Succ(A), Pred(B-A));
  end;
end;

function Sub(const S: string; After: TSysCharSet): string; overload;
var
  A: Integer;
begin
  A:=PosOf(After, S);
  if A=0 then Result:='' else
  begin
    Result:=Copy(S, Succ(A), Length(S)-A);
  end;
end;

function Sub(const S: string; After: TSysCharSet; Before: Integer): string; overload;
var
  A: Integer;
begin
  A:=PosOf(After, S);
  if A=0 then Result:='' else
  begin
    if Before<=0 then Before:=Succ(Length(S)+Before);
    Result:=Copy(S, Succ(A), Pred(Before-A));
  end;
end;

function Sub(const S: string; After: TSysCharSet; Before: string): string; overload;
var
  A,B: Integer;
begin
  A:=PosOf(After,S);
  if A=0 then Result:='' else
  begin
    B:=PosOf(Before,S,Succ(A));
    Result:=Copy(S,Succ(A),Pred(B-A));
  end;
end;

function Sub(const S: string; After: TSysCharSet; Before: TSysCharSet): string; overload;
var
  A,B: Integer;
begin
  A:=PosOf(After,S);
  if A=0 then Result:='' else
  begin
    B:=PosOf(Before,S,Succ(A));
    Result:=Copy(S,Succ(A),Pred(B-A));
  end;
end;

function Sub(const After, Before: PChar): string;
begin
  Assert(Before<>nil);
  if After=nil then Result:='' else
  begin
    SetLength(Result, Pred(Before-After));
    Move(After[1], PChar(Result)^, Length(Result));
  end;
end;

function Get(const From, Before: PChar): string;
begin
  Assert(Before<>nil);
  if From=nil then Result:='' else
  begin
    SetLength(Result, Before-From);
    Move(From^, Pointer(Result)^, Length(Result));
  end;
end;

function _R(const Value: Integer): TValueRelationship;
begin
  if Value>0 then Result:=GreaterThanValue else
  if Value<0 then Result:=LessThanValue else
  Result:=EqualsValue;
end;

function Compare(const A, B: string): TValueRelationship; overload;
begin
  Result:=_R(CompareStr(A,B));
end;

function Compare(const A, B: PChar): TValueRelationship; overload;
begin
  Result:=_R(StrComp(A,B));
end;

function Compare(const A, B: Pointer): TValueRelationship;
begin
  if Cardinal(A)>Cardinal(B) then Result:=GreaterThanValue else
  if Cardinal(A)<Cardinal(B) then Result:=LessThanValue else
  Result:=EqualsValue;
end;

function Compare(const A, B: Byte): TValueRelationship;
begin
  if A>B then Result:=GreaterThanValue else
  if A<B then Result:=LessThanValue else
  Result:=EqualsValue;
end;

function Compare(const A, B: ShortInt): TValueRelationship;
begin
  if A>B then Result:=GreaterThanValue else
  if A<B then Result:=LessThanValue else
  Result:=EqualsValue;
end;

function Compare(const A, B: Word): TValueRelationship;
begin
  if A>B then Result:=GreaterThanValue else
  if A<B then Result:=LessThanValue else
  Result:=EqualsValue;
end;

function Compare(const A, B: Smallint): TValueRelationship;
begin
  if A>B then Result:=GreaterThanValue else
  if A<B then Result:=LessThanValue else
  Result:=EqualsValue;
end;

function Compare(const A, B: Cardinal): TValueRelationship;
begin
  if A>B then Result:=GreaterThanValue else
  if A<B then Result:=LessThanValue else
  Result:=EqualsValue;
end;

function Compare(const A, B: Integer): TValueRelationship;
begin
  if A>B then Result:=GreaterThanValue else
  if A<B then Result:=LessThanValue else
  Result:=EqualsValue;
end;

function Compare(const A, B: Int64): TValueRelationship;
begin
  if A>B then Result:=GreaterThanValue else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -