RSS Feed

Lisp Project of the Day

trivial-left-pad

You can support this project by donating at:

Donate using PatreonDonate using Liberapay

Or see the list of project sponsors.

trivial-left-padutilstrivial

Documentation๐Ÿ˜€
Docstrings๐Ÿ˜€
Tests ๐Ÿ˜€
Examples๐Ÿคจ
RepositoryActivity๐Ÿฅบ
CI ๐Ÿฅบ

Did you hear the story of how one developer broke thousand JavaScript libraries by removing 11 lines of code from NPM?

Now we can to repeat this feat because we have our own left-pad for Common Lisp! :)

This library brings only one function, which adds some spaces, to make a string of given length:

POFTHEDAY> (trivial-left-pad:left-pad "Foo" 16)
"             Foo"

POFTHEDAY> (trivial-left-pad:left-pad "Bar" 16)
"             Bar"

POFTHEDAY> (trivial-left-pad:left-pad "Blah" 16)
"            Blah"

POFTHEDAY> (trivial-left-pad:left-pad "Minor" 16)
"           Minor"

POFTHEDAY> (trivial-left-pad:left-pad "Hello world!" 16)
"    Hello world!"

You also can specify a custom padding as a character or a string:

POFTHEDAY> (trivial-left-pad:left-pad "Hello world!" 16 ".")
"....Hello world!"

POFTHEDAY> (trivial-left-pad:left-pad "Hello world!" 16 #\.)
"....Hello world!"

POFTHEDAY> (trivial-left-pad:left-pad "Hello world!" 16 ".!")
".!.!Hello world!"

POFTHEDAY> (trivial-left-pad:left-pad "Hello world!" 16 "->")
"->->Hello world!"

Of cause, this library is useful only if you need padding from more than one character. In other cases it is much easier to use standard format function:

POFTHEDAY> (format nil "~16@A" "Hello world!")
"    Hello world!"

POFTHEDAY> (format nil "~16,,,'_@A" "Hello world!")
"____Hello world!"

POFTHEDAY> (format nil "~16,,,'+@A" "Hello world!")
"++++Hello world!"

;; If you want to pass padding in runtime:

POFTHEDAY> (format nil "~v,,,v@A" 16 #\+ "Hello world!")
"++++Hello world!"

Brought to you by 40Ants under Creative Commons License