findbyte

function findbyte( str, byte [, init] ) --> idx

Description

Returns the index of the first appearance byte in str. The optional argument init specifies wher to start the search.

Parameters

str

The string that should be checked.

byte

The byte to search for.

init

The position where the search should be started.

Return Values

idx

The first appearance of byte in str, otherwise nil.

Code

--ZFUNC-findbyte-v1
local function findbyte( str, byte, init ) --> idx
   if not init then init = 1 end

   for idx = init, #str do
      if str:byte(idx) == byte then
         return idx
      end
   end

   return nil
end

return findbyte

Examples

local t = require( "taptest" )
local findbyte = require( "findbyte" )

local b = string.byte

t( findbyte( "image.png", b'.' ), 6 )
t( findbyte( "foo-bar-tar", b'-', 5 ), 8 )
t( findbyte( "abcdefgh", b'x' ), nil )

t()