{ $Id: PDOSysUtils.pas 32 2007-01-22 01:35:20Z jmarino $

############################################################################################

	Pascal Data Objects Library
	Copyright © 2006 John Marino, http://www.synsport.com
	Project site: http://pdo.sourceforge.net

	Note: This file contains code fragments from Zeoslib
	Copyright © 1999-2004 Sergey Seroukhov

############################################################################################

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This library 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
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

############################################################################################}

unit PDOSysUtils;

interface

{$I directives.inc}

uses
  Variants,Classes, SysUtils,
  PDOMessages, PDOCompatibility;


{**
  Determines a position of a first delimiter.
  @param Delimiters a string with possible delimiters.
  @param Str a string to be checked.
  @return a position of the first found delimiter or 0 if no delimiters was found.
}
function FirstDelimiter(Delimiters: string; Str: string): Integer;

{**
  Checks is the string starts with substring.
  @param Str a string to be checked.
  @param SubStr a string to test at the start of the Str.
  @return <code>True</code> if Str started with SubStr;
}
function StartsWith(Str: string; SubStr: string): Boolean;

{**
  Checks is the string ends with substring.
  @param Str a string to be checked.
  @param SubStr a string to test at the end of the Str.
  @return <code>True</code> if Str ended with SubStr;
}
function EndsWith(Str: string; SubStr: string): Boolean;


{**
  Converts string into an array of bytes.
  @param Value a string to be converted.
  @return a converted array of bytes.
}
function StrToBytes(Value: string): TByteDynArray;

{
    Joins arrays of ansistrings similar to the PHP function, using the comma as the default conjunction
}
function implode (const strArray: Array of Ansistring; const conjunction: ansistring = ', '): ansistring;


implementation


{**
  Determines a position of a first delimiter.
  @param Delimiters a string with possible delimiters.
  @param Str a string to be checked.
  @return a position of the first found delimiter or 0 if no delimiters was found.
}

function FirstDelimiter(Delimiters: string; Str: string): Integer;
var
  I, Index: Integer;
begin
  Result := 0;
  for I := 1 to Length(Delimiters) do
  begin
    Index := Pos(Delimiters[I], Str);
    if (Index > 0) and ((Index < Result) or (Result = 0)) then
      Result := Index;
  end;
end;


{**
  Checks is the string starts with substring.
  @param Str a string to be checked.
  @param SubStr a string to test at the start of the Str.
  @return <code>True</code> if Str started with SubStr;
}
function StartsWith(Str: string; SubStr: string): Boolean;
begin
  Result := Copy(Str, 1, Length(SubStr)) = SubStr;
end;

{**
  Checks is the string ends with substring.
  @param Str a string to be checked.
  @param SubStr a string to test at the end of the Str.
  @return <code>True</code> if Str ended with SubStr;
}
function EndsWith(Str: string; SubStr: string): Boolean;
begin
  if Length(SubStr) <= Length(Str) then
  begin
    Result := Copy(Str, Length(Str) - Length(SubStr) + 1,
    Length(SubStr)) = SubStr;
  end else
    Result := False;
end;





{**
  Converts string into an array of bytes.
  @param Value a string to be converted.
  @return a converted array of bytes.
}
function StrToBytes(Value: string): TByteDynArray;
var
  I: Integer;
begin
  SetLength(Result, Length(Value));
  for I := 1 to Length(Value) do
    Result[I - 1] := Ord(Value[I]);
end;


{
    Joins arrays of ansistrings together with commas,
    similar to the PHP function except you can't choose the joining text.
}
function implode (const strArray: Array of AnsiString; const conjunction: ansistring = ', '): ansistring;
var
    numElements: word;
    loop: integer;
begin
    
    numElements := length(strArray);
    case numElements of
        0: Result := '';
        1: Result := strArray[0];
    else
        begin
            Result := strArray[0];
            for loop := 1 to numElements - 1 do
                Result := Result + conjunction + strArray[loop];
        end;
    end;
end;






end.
