pad-start

text pad-length padding


Summary

Pad a string to a certain length by prepending another string.

Description

This function will, if necessary, prepend padding on to the beginning of text repeatedly until the desired pad-length is reached.

Because of this the string in padding can be truncated if the amount of characters that need to be added can't be evenly divided through the length of padding.

This means that you have to be careful not to use unicode characters as a padding, or make sure that it can be used without truncation.

Arguments

text
The input string
pad-length
The minimum length of the result
padding
A string that will be used to pad text, defaults to a single space

Return value

Returns the padded string

Examples

(pad-start '123 6 ".-")
"-.-123"

(pad-start 123 6 "0")
"000123"

(pad-start "123" 6)
"   123"

Bytecode

Probably only interesting to you if you want to understand more about the Nujel VM or care very much about performance.

(    0 ($get/val 'padding))
(    2 ($jf* 7))
(    5 ($push/nil))
(    6 ($jmp* 7))
(    9 ($push/val  ))
(   11 ($set/val 'padding))
(   13 ($drop))
(   14 ($push/val :string))
(   16 ($get/val 'type-of))
(   18 ($get/val 'text))
(   20 ($apply 1))
(   22 ($=))
(   23 ($jf* 7))
(   26 ($push/nil))
(   27 ($jmp* 11))
(   30 ($get/val 'string))
(   32 ($get/val 'text))
(   34 ($apply 1))
(   36 ($set/val 'text))
(   38 ($drop))
(   39 ($push/val :string))
(   41 ($get/val 'type-of))
(   43 ($get/val 'padding))
(   45 ($apply 1))
(   47 ($=))
(   48 ($jf* 7))
(   51 ($push/nil))
(   52 ($jmp* 21))
(   55 ($get/val 'throw))
(   57 ($get/val 'list))
(   59 ($push/val :type-error))
(   61 ($push/val pad-start needs char as a string, so that one can pad with multiple characters))
(   63 ($get/val 'padding))
(   65 ($get/val 'current-lambda))
(   67 ($apply 0))
(   69 ($apply 4))
(   71 ($apply 1))
(   73 ($drop))
(   74 ($push/nil))
(   75 ($jmp* 14))
(   78 ($drop))
(   79 ($get/val 'cat))
(   81 ($get/val 'padding))
(   83 ($get/val 'text))
(   85 ($apply 2))
(   87 ($set/val 'text))
(   89 ($get/val 'buffer/length))
(   91 ($get/val 'text))
(   93 ($apply 1))
(   95 ($get/val 'pad-length))
(   97 ($<))
(   98 ($jt* -20))
(  101 ($drop))
(  102 ($get/val 'buffer/length))
(  104 ($get/val 'text))
(  106 ($apply 1))
(  108 ($get/val 'pad-length))
(  110 ($>))
(  111 ($jf* 27))
(  114 ($get/val 'string/cut))
(  116 ($get/val 'text))
(  118 ($get/val 'buffer/length))
(  120 ($get/val 'text))
(  122 ($apply 1))
(  124 ($get/val 'pad-length))
(  126 ($sub))
(  127 ($get/val 'buffer/length))
(  129 ($get/val 'text))
(  131 ($apply 1))
(  133 ($apply 3))
(  135 ($jmp* 5))
(  138 ($get/val 'text))
(  140 ($ret))

Related