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( "taptest" )
local isblank = require( "isblank" )

t( isblank( " \t " ), true )

res, pos = isblank( "" )
t( res, false )
t( pos, 0 )

re, pos = isblank( " \n" )
t( res, false )
t( pos, 2 )

t()

Inspired by