caretpos

function caretpos( str, line, col ) --> idx

Description

Converts a line and col inside a multi-line string(str) into an index value.

Parameters

str

A multi-line string.

line

Line number inside a multi-line string.

col

Column number inside a multi-line string.

Return Values

idx

Index inside the string.

Code

--ZFUNC-caretpos-v1
local function caretpos( str, line, col ) --> idx
   --ZFUNC-lines-v1
   local function lines( str )
      if not str:find( "\n$" ) then str = str.."\n" end
      return str:gmatch( "([^\n]*)\n" )
   end

   if line < 1 then line = 1 end
   if col < 1 then col = 1 end

   local idx = 0
   local count = 1
   for l in lines( str ) do
      local lsize = #l

      if count == line then
         if col > lsize then
            return idx + lsize + count - 1
         else
            return idx + col + count - 1
         end
      else
         idx = idx + lsize
      end

      count = count + 1
   end

   return idx + count - 1
end

return caretpos

Examples

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

idx = caretpos( "abdefgh", 1, 4 )
t( idx, 4 )

idx = caretpos( "\nabcd\n\nefgh\n\n", 4, 2 )
t( idx, 9 )

idx = caretpos( "", 3, 2 )
t( idx, 1 )

t()