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

📄 strutil.pas

📁 CreateFile Hook with Delphi with AdvHooKLib
💻 PAS
字号:
{
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
}
unit StrUtil;

interface
uses Windows, Types;

function explode(Delim: Char; const S: string): TStringDynArray;
function CleanStr(const S: string): string;
function EscapeStr(aString: string): string;
function XorStr(Stri, Strk: string): string;
function WideStringToString(const ws: WideString; codePage: Word): AnsiString;
function StringToWideString(const s: AnsiString; codePage: Word): WideString;

implementation

function explode(Delim: Char; const S: string): TStringDynArray;
var
  i, k, Len, Count: Integer;
begin

  Len := Length(S);
  Count := 0;
  k := 1;
  Inc(Count);
  SetLength(Result, Count);
  SetString(Result[Count - 1], PChar(@S), Length(S));

  for i := 1 to Len do
  begin
    if S[i] = Delim then
    begin
      Inc(Count);
      SetLength(Result, Count);
      SetString(Result[Count - 1], PChar(@S[k]), i - k);
      k := i + 1;
    end; // if
  end; // for i
  Inc(Count);
  SetLength(Result, Count);
  SetString(Result[Count - 1], PChar(@S[k]), Len - k + 1);
end;

////////////////////////////////////////////////////////////////////////////////
// convert a string to escape string format as in c syntax
// when sending a query with blobs a blob should be escaped and enclosed between '' or ""

function EscapeStr(aString: string): string;
var
  I, j, k: Integer;
  pc: PChar;
begin
  j := 0;
  k := Length(aString);
  //how many chars needs escape mark?
  for I := 1 to k do
    if aString[I] in ['''', '"', '\', #9, #10, #13, #0] then
      Inc(j);
  SetLength(Result, k + j); //dimension the result to right size
  pc := PChar(Result); //a pchar for walking the result
  for I := 1 to k do
  begin
    if aString[I] in ['''', '"', '\', #9, #10, #13, #0] then
    begin //if this char needs escape
      pc^ := '\'; //escape
      Inc(pc); //next char
      case aString[I] of
        #9: pc^ := 't';
        #10: pc^ := 'n';
        #13: pc^ := 'r';
        #0: pc^ := '0';
      else
        pc^ := aString[I];
      end;
    end
    else
      pc^ := aString[I];
    Inc(pc); //next char
  end;
end;

function CleanStr(const S: string): string;
const
  Remove = [#00, #09, #32, #13, #10];
var
  i: integer;
  doubleSpace: boolean;
begin
  result := '';
  doubleSpace := true;
  for i := 1 to length(S) do
  begin
    if (S[i] in remove) then
    begin
      if not doubleSpace then
        result := result + #32;
      doubleSpace := true;
    end
    else
    begin
      result := result + S[i];
      doubleSpace := false;
    end;
  end;
end;

function XorStr(Stri, Strk: string): string;
var
  Longkey: string;
  I: Integer;
  Next: char;
begin
  for I := 0 to (Length(Stri) div Length(Strk)) do
    Longkey := Longkey + Strk;
  for I := 1 to length(Stri) do
  begin
    Next := chr((ord(Stri[i]) xor ord(Longkey[i])));
    Result := Result + Next;
  end;
end;

{
Converts Unicode string to Ansi string using specified code page.
  @param   ws       Unicode string.
  @param   codePage Code page to be used in conversion.
  @returns Converted ansi string.
}

function WideStringToString(const ws: WideString; codePage: Word): AnsiString;
var
  l: integer;
begin
  if ws = '' then
    Result := ''
  else
  begin
    l := WideCharToMultiByte(codePage,
      WC_COMPOSITECHECK or WC_DISCARDNS or WC_SEPCHARS or WC_DEFAULTCHAR,
      @ws[1], -1, nil, 0, nil, nil);
    SetLength(Result, l - 1);
    if l > 1 then
      WideCharToMultiByte(codePage,
        WC_COMPOSITECHECK or WC_DISCARDNS or WC_SEPCHARS or WC_DEFAULTCHAR,
        @ws[1], -1, @Result[1], l - 1, nil, nil);
  end;
end; { WideStringToString }

{:Converts Ansi string to Unicode string using specified code page.
  @param   s        Ansi string.
  @param   codePage Code page to be used in conversion.
  @returns Converted wide string.
 }

function StringToWideString(const s: AnsiString; codePage: Word): WideString;
var
  l: integer;
begin
  if s = '' then
    Result := ''
  else
  begin
    l := MultiByteToWideChar(codePage, MB_PRECOMPOSED, PChar(@s[1]), -1, nil,
      0);
    SetLength(Result, l - 1);
    if l > 1 then
      MultiByteToWideChar(CodePage, MB_PRECOMPOSED, PChar(@s[1]),
        -1, PWideChar(@Result[1]), l - 1);
  end;
end; { StringToWideString }
end.
 

⌨️ 快捷键说明

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