Programming-Idioms

New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating resource.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
repeat(x,max(n-len(s),0)) // s
import "strings"
import "utf8"
if n := utf8.RuneCountInString(s); n < m {
	s = strings.Repeat(c, m-n) + s
}
const padded = s.padStart(m, c);
LazUtf8
s := UTF8PadLeft(s,m,c);
s = s.rjust(m, c)
s.rjust(m, c)
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
if let Some(columns_short) = m.checked_sub(s.width()) {
    let padding_width = c
        .width()
        .filter(|n| *n > 0)
        .expect("padding character should be visible");
    // Saturate the columns_short
    let padding_needed = columns_short + padding_width - 1 / padding_width;
    let mut t = String::with_capacity(s.len() + padding_needed);
    t.extend((0..padding_needed).map(|_| c)
    t.push_str(&s);
    s = t;
}