pad-middle

text pad-length padding


Summary

Pad a string to a certain length by surrounding it with another string.

Description

This function will, if necessary, append and prepend padding to the beginning and end 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-middle '123 6 ".-")
"-123.-"

(pad-middle 123 6 "0")
"012300"

(pad-middle "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 string/pad-middle 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* 16))
(   78 ($drop))
(   79 ($get/val 'cat))
(   81 ($get/val 'padding))
(   83 ($get/val 'text))
(   85 ($get/val 'padding))
(   87 ($apply 3))
(   89 ($set/val 'text))
(   91 ($get/val 'buffer/length))
(   93 ($get/val 'text))
(   95 ($apply 1))
(   97 ($get/val 'pad-length))
(   99 ($<))
(  100 ($jt* -22))
(  103 ($drop))
(  104 ($get/val 'buffer/length))
(  106 ($get/val 'text))
(  108 ($apply 1))
(  110 ($get/val 'pad-length))
(  112 ($>))
(  113 ($jf* 54))
(  116 ($let))
(  117 ($get/val 'div/int))
(  119 ($get/val 'buffer/length))
(  121 ($get/val 'text))
(  123 ($apply 1))
(  125 ($get/val 'pad-length))
(  127 ($sub))
(  128 ($push/int/byte 2))
(  130 ($apply 2))
(  132 ($det/val 'end-overflow))
(  134 ($drop))
(  135 ($get/val 'buffer/length))
(  137 ($get/val 'text))
(  139 ($apply 1))
(  141 ($get/val 'pad-length))
(  143 ($sub))
(  144 ($get/val 'end-overflow))
(  146 ($sub))
(  147 ($det/val 'start-overflow))
(  149 ($drop))
(  150 ($get/val 'string/cut))
(  152 ($get/val 'text))
(  154 ($get/val 'start-overflow))
(  156 ($get/val 'start-overflow))
(  158 ($get/val 'pad-length))
(  160 ($add))
(  161 ($apply 3))
(  163 ($closure/pop))
(  164 ($jmp* 5))
(  167 ($get/val 'text))
(  169 ($ret))

Related