1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//!
//! A highly efficient Rust crate for padding data during runtime.
//!
//! padder is a lightweight Rust crate for padding and formatting data structures at runtime
//! efficiently. It provides fine-grained control over alignment, truncating strategies, padding,
//! and memory allocation - making it ideal for performance-critical applications.
//!
//! Unlike the builtin `format!` macro, padder avoids unneccessary repeated heap allocations and
//! lets you pad and format directly into preallocated buffers, or modify buffers in-place.
//!
//! **Fully UTF-8 compatible** - padder works seamlessly with any Unicode characters like emojis (π),
//! Japanese kana/kanji (γγγ«γ‘γ―), or other multibyte symbols, when operating on String types.
//!
//! # Features
//! - Pad strings, slices, and vectors with custom alignment and width.
//! - Zero-cost abstractions via the `Source` and `MutableSource` traits.
//! - Pad directly into buffers for fine-grained heap allocation control.
//! - Highly extensible to custom types through the provided traits.
//!
//! # Usage
//! ```
//! use padder::*;
//!
//! let mut string = String::from("kratos");
//! (&mut string).pad(10, Alignment::Right, '$'); // "$$$$kratos"
//! pad_mut(&mut string, 14, Alignment::Center, '-'); // "--$$$$kratos--"
//!
//! let s = "dragon";
//! let padded = pad(s, 11, Alignment::Left, 'π');
//! assert_eq!("dragonπππππ", padded);
//!
//! let vec: Vec<u8> = Vec::from(&[0u8, 2, 5]);
//! let mut buffer: Vec<u8> = Vec::with_capacity(5);
//! pad_to_buffer(vec, 5, Alignment::Right, 128u8, &mut buffer);
//! assert_eq!(Vec::from(&[128u8, 128, 0, 2, 5]), buffer);
//! ```
//!
pub use ;
pub use MutableSource;
pub use Source;
/// Pads the given source buffer to the specified `width` using the provided `symbol` and alignment `mode`.
///
/// This is a convenience wrapper around the [`Source::pad`] method. It consumes the source buffer
/// and produces a new, owned, and padded buffer.
///
/// # Examples
/// ```
/// use padder::*;
///
/// let string = String::from("this is a string");
/// let padded = pad(string, 20, Alignment::Right, '_');
/// assert_eq!("____this is a string", padded);
///
/// let string = String::from("sackboy");
/// let padded = pad(string, 4, Alignment::Left, ' ');
/// assert_eq!("sack", padded); // 4 < string.chars.count() so it will be truncated
///
/// let vec: Vec<usize> = Vec::from(&[200, 10, 23]);
/// let padded = pad(vec, 6, Alignment::Center, 0usize);
/// assert_eq!(Vec::from(&[0usize, 200, 10, 23, 0, 0]), padded);
/// ```
/// Pad the given mutable source buffer in-place to the specified `width` using the provided `symbol` and `alignment` mode.
///
/// This is a convenience wrapper around the [`MutableSource::pad`] method. It modifies the source
/// buffer directly and does not consume it.
///
/// # Examples
/// ```
/// use padder::*;
///
/// let mut string = String::from("elden ring");
/// pad_mut(&mut string, 14, Alignment::Left, 'π');
/// assert_eq!("elden ringππππ", string);
///
/// let mut string = String::from("dark souls");
/// pad_mut(&mut string, 14, Alignment::Center, 'π');
/// assert_eq!("ππdark soulsππ", string);
/// ```
/// Pad the given source to match the specified `width` according to the specified alignment
/// `mode` by writing into the provided `buffer`.
///
/// This is a convenience wrapper around the [`Source::pad_to_buffer`] method. It does not modify
/// the source buffer and instead directly writes in the the `buffer`.
///
/// # Examples
/// ```
/// use padder::*;
///
/// let slice: &[&str] = &[
/// "liurnia",
/// "of",
/// "the",
/// "lakes",
/// ];
/// let width = 6;
/// let mut buf: Vec<&str> = Vec::with_capacity(width);
/// pad_to_buffer(slice, width, Alignment::Right, "tarnished", &mut buf);
/// let expected: Vec<&str> = Vec::from(&[
/// "tarnished",
/// "tarnished",
/// "liurnia",
/// "of",
/// "the",
/// "lakes",
/// ]);
/// assert_eq!(expected, buf);
/// ```