--[[
///////////////////////////////////////////////////////////////////////////////
 'String manipulation routine'
///////////////////////////////////////////////////////////////////////////////
]]--

function string.contains(str1,str2)
  local count = string.find(str1,str2,0)
  return count~=nil
end

function string.lastIndexOf(str,char)
  local p=-1
  for i=1,string.len(str) do
    if string.find(string.sub(str,i,-1),char) then
      p=i
    end
  end
  return p
end

function string.indexOf(str,char,start)
  local st = start or 1
  local p=-1
  for i=st,string.len(str) do
    if string.find(string.sub(str,i,i+1),char) then
      p=i+1
      return p
    end
  end
  return p
end

function string.startsWith(str,token)
  return token=='' or string.sub(str,1,string.len(token))==token
  --[[
  local res=false
  for i=1,string.len(str) do
    local idx,count = string.gsub(str,token,token)
    res= idx==1
    if res then
      return res
    end
  end
  return res
  ]]
end

--taken from LuMo (as seen on forums.ps2dev.org) previously named 'str_explode'
function string.split(text, needle)
  local array={}
  _, count = string.gsub(text, needle, needle)
  for index=1,count+1 do
    i, j = string.find(text, needle,1)
    if i == nil or j==nil then
      i=0
      j=0
    end
    table.insert(array,string.sub(text, 1, i-1))
    text=string.sub(text, j+1 )
  end
  return array
end

--[[
  This script handles string manipulation. Some functions written by
  LuMo (string.split/str_explode)
  Elxx (string.endsWith)
]]

function str_implode(array, needle)
  local str = ''
  for i=1,table.getn(array)-1 do
    str = str..array[i]..needle
  end
  str = str..array[table.getn(array)]
  return str
end

--tests if 2 strings are equal
function string.equals(st1, st2)
  return string.sub(st1,-string.len(st2))==st2
end

function string.endsWith(String,End)
  return End=='' or string.sub(String,-string.len(End))==End
end

--trims off any newline chars from 'str' (needs rewriting!!)
function string.trimnewlines(str)
  local tmp = string.sub( str, string.len(str)-1, -1 )
  local trim=""
  for i=1,string.len(str) do
    char=string.sub(str,i,i)
    if not string.equals(char,"\n") then
      if not string.equals(char,"\r") then
        trim = trim .. char
      end
    end
  end
  return trim
end

function string.trim (s)
  return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end

--will return the bit representation of 'char'
function string.tobits(char)
  local tmp = string.byte(char)
  local b0="0"
  local b1="0"
  local b2="0"
  local b3="0"
  local b4="0"
  local b5="0"
  local b6="0"
  local b7="0"

  if tmp >= 128 then
      b0="1"
      tmp=tmp-128
  end
  if tmp >= 64 then
      b1="1"
      tmp=tmp-64
  end
  if tmp >= 32 then
      b2="1"
      tmp=tmp-32
  end
  if tmp >= 16 then
      b3="1"
      tmp=tmp-16
  end
  if tmp >= 8 then
      b4="1"
      tmp=tmp-8
  end
  if tmp >= 4 then
      b5="1"
      tmp=tmp-4
  end
  if tmp >= 2 then
      b6="1"
      tmp=tmp-2
  end
  if tmp >= 1 then
      b7="1"
      tmp=tmp-1
  end
  
  return b0..b1..b2..b3..b4..b5..b6..b7,tmp
end