expandtabs

function expandtabs( str [, tabsize] ) --> nstr

Description

Returns a new string where all tab characters(0+0009) are replaced with spaces(U+0020). The optional parameter tabsize defines how much spaces will be set.

Parameters

str

Basic string where tab should be replaced.

tabsize

The number of spaces that should be set for a tab character. The default value is 8.

Return Values

nstr

A string without tabs.

Code

--ZFUNC-expandtabs-v1
local function expandtabs( str, tabsize ) --> nstr
   tabsize = tabsize or 8

   local spaces = string.rep( " ", tabsize )
   return string.gsub( str, "\t", spaces )
end

return expandtabs

Examples

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

nstr = expandtabs( "a\tab\tabc\tabcd" )
t.is( "a        ab        abc        abcd", nstr )

nstr = expandtabs( "a\tab\tabc\tabcd", 3 )
t.is( "a   ab   abc   abcd", nstr )

nstr = expandtabs( "a\tb\t\tb\ta", 2 )
t.is( "a  b    b  a", nstr )

nstr = expandtabs( "a\tb\t\tb\ta", 0 )
t.is( "abba", nstr )

t.done()

Inspired by