-- Unit Tests unter [[Modul:Str/testcases]]

  local Str = {} 

    function Str.len(frame)
      return mw.ustring.len(frame.args[1])
    end

    function Str.left(frame)
      local idx = tonumber(frame.args[2])
      if (not idx) or idx < 1 then
        return ""
      end
      return mw.ustring.sub(frame.args[1],1,idx)
    end

    function Str.right(frame)
      local laenge = tonumber(frame.args[2])
      if (not laenge) or laenge < 1 then
        return ""
      else
      laenge = - laenge
      end
      return mw.ustring.sub(frame.args[1],laenge,-1)
    end

    function Str.index(frame)
      local idx = tonumber(frame.args[2])
      if (not idx) or idx < 1 then
        return ""
      end
      return mw.ustring.sub(frame.args[1],idx,idx)
    end
    
    function Str.reverse(frame)
      local s = ""
      for i = mw.ustring.len( frame.args[1] ), 1, -1 do
        s = s .. mw.ustring.char(mw.ustring.codepoint(frame.args[1],i))
      end
      return s
    end

    function Str.sub(frame)
      local von = tonumber(frame.args[2])
      local laenge = tonumber(frame.args[3])
      if (not von) or (not laenge) then
        return ""
      end
      if (von < 1) then
        von = 1
      end      
      local bis = von + laenge - 1
      if (bis < von) then
        return ""
      end
      return mw.ustring.sub(frame.args[1],von,bis)
    end

    function Str.subrev(frame)
      local zlang = mw.ustring.len(frame.args[1])
      if (not zlang) then
        return ""
      end
      local von = tonumber(frame.args[2])
      if (not von) or von < 1 then
        von = 1
      end
      if von > zlang then
        return ""
      else
        von = zlang - von + 1
      end
      local laenge = tonumber(frame.args[3])
      if (not laenge) or laenge < 1 then
        laenge = 1
      end
      local bis = von + laenge - 1
      if (bis > zlang) then
        return ""
      end
      return mw.ustring.sub(frame.args[1],von,bis)
    end

    function Str.crop(frame)
      local s = frame.args[1]
      local cut = tonumber(frame.args[2])
      local laenge =  mw.ustring.len(s)
      if (not cut) or (cut < 1) then
        return s
      end
      return mw.ustring.sub(s,1,laenge - cut)
    end

    function Str.cropleft(frame)
      local s = frame.args[1]
      local cut = tonumber(frame.args[2])
      local laenge =  mw.ustring.len(s)
      if (not cut) or (cut < 1) then
        return s
      end
      return mw.ustring.sub(s,cut+1,-1)
    end

    function Str.find(frame)
      if not frame.args[2] or frame.args[2] == "" then
        return 1
      end
      local  idx = mw.ustring.find(frame.args[1], frame.args[2],1, true)
      if idx then
          return idx
      else
          return -1
      end
    end

    function Str.hex2dez(frame)
      a = tonumber(frame.args[1],16)
      if a then
        return a
      else
        return 0
      end
    end
    
    function Str.match(frame)
        local text = frame.args[1] or ""
        local pattern = frame.args[2] or ""
        local index = tonumber(frame.args[3]) or 0
        if (text == "" or pattern == "") then return "" end
 
        -- return all captures (denoted by brackets in the pattern) if index is zero, otherwise return only the index-th capture
        if index <= 0 then
            return mw.ustring.match(text, pattern)
        else
            return ({mw.ustring.match(text, pattern)})[index]
        end
    end

    function Str.split(frame)
      local str = frame.args[1]
      local sep = frame.args[2] or '%s'
      if mw.ustring.len( sep ) < 1 then
        sep = '%s'
      end
      local idx = frame.args[3]
      local t = {}

      for k,s in string.gmatch(str, "([^"..sep.."]*)("..sep.."?)") do
        table.insert(t,k)
      end

      return t[tonumber(idx)]
    end

 return Str