wrap

Wrap text into a paragraph.

The input text string s is formed into a paragraph by breaking it up into a sequence of lines, delineated by \n, such that the number of columns is not exceeded on each line. The last line is terminated with a \n.

S
wrap
(
S
)
(
S s
,
in size_t columns = 80
,
S firstindent = null
,
S indent = null
,
in size_t tabsize = 8
)

Parameters

s S

text string to be wrapped

columns size_t

maximum number of columns in the paragraph

firstindent S

string used to indent first line of the paragraph

indent S

string to use to indent following lines of the paragraph

tabsize size_t

column spacing of tabs in firstindent[] and indent[]

Return Value

Type: S

resulting paragraph as an allocated string

Bugs

Columns are counted as the number of code points in the string. This may not correspond with the actual number of columns displayed if the string contains combining characters, modifiers, zero-width characters, or double-width characters.

Examples

assert(wrap("a short string", 7) == "a short\nstring\n");

// wrap will not break inside of a word, but at the next space
assert(wrap("a short string", 4) == "a\nshort\nstring\n");

assert(wrap("a short string", 7, "\t") == "\ta\nshort\nstring\n");
assert(wrap("a short string", 7, "\t", "    ") == "\ta\n    short\n    string\n");

Meta