trim

function trim( str ) --> nstr

Description

Returns the string stripped of whitespace from both ends.

Parameters

str

The string that should be trimmed.

Return Values

nstr

A trimmed version of the assigned parameter string.

Code

--ZFUNC-trim-v1
local function trim( str ) --> nstr
   local n = str:find( "%S" )
   return n and str:match( ".*%S", n ) or ""
end

return trim

Examples

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

t.is( "", trim( "" ) )
t.is( "", trim( " " ) )
t.is( "", trim( "  " ) )
t.is( "a", trim( "a" ) )
t.is( "a", trim( " a" ) )
t.is( "a", trim( "a " ) )
t.is( "a", trim( " a " ) )
t.is( "a", trim( "  a  " ) )
t.is( "ab cd", trim( " ab cd " ) )
t.is( "a\000b", trim( " \t\r\n\f\va\000b \r\t\n\f\v" ) )

t.done()