isblank

function isblank( str ) --> res, idx

Description

Checks if all characters in the string are blank characters and there is at least one character. Blank characters are the tab character(U+0009) and the space character(U+0020).

Parameters

str

The string that should be checked.

Return Values

res

true if all characters in the string are blank characters and there is at least one character, otherwise false.

idx

If res is false shows idx the first character that is not a blank character.

Code

--ZFUNC-isblank-v1
local function isblank( str ) --> res, idx
   local n = #str
   if n == 0 then return false, 0 end

   for idx = 1, n do
      local c = str:sub( idx, idx )
      if c ~= "\t" and c ~= " " then
         return false, idx
      end
   end

   return true
end

return isblank

Examples

local t = require( "tapered" )
local isblank = require( "isblank" )

t.ok( isblank( " \t " ) )

res, pos = isblank( "" )
t.nok( res )
t.is( 0, pos )

re, pos = isblank( " \n" )
t.nok( res )
t.is( 2, pos )

t.done()

Inspired by