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

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

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

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

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

t()

Inspired by