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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
//! `Fmt` and related types.
use crate::argument::Ascii;
use core::fmt::Alignment;
use crate::utils::{assert_is_ascii, count_chars};
/// Length of a string measured in bytes and chars.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct StrLength {
/// Number of bytes the string occupies.
pub bytes: usize,
/// Number of chars in the string.
pub chars: usize,
}
impl StrLength {
pub(crate) const fn for_str(s: &str) -> Self {
Self {
bytes: s.len(),
chars: count_chars(s),
}
}
pub(crate) const fn for_char(c: char) -> Self {
Self {
bytes: c.len_utf8(),
chars: 1,
}
}
/// Creates a length in which both `bytes` and `chars` fields are set to the specified `value`.
pub const fn both(value: usize) -> Self {
Self {
bytes: value,
chars: value,
}
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct Pad {
pub align: Alignment,
pub width: usize,
pub using: char,
}
impl Pad {
pub const fn compute_padding(&self, char_count: usize) -> (usize, usize) {
if char_count >= self.width {
return (0, 0);
}
match self.align {
Alignment::Left => (0, self.width - char_count),
Alignment::Right => (self.width - char_count, 0),
Alignment::Center => {
let total_padding = self.width - char_count;
(total_padding / 2, total_padding - total_padding / 2)
}
}
}
}
/// Formatting specification for an [`Argument`](crate::Argument).
///
/// A format is necessary to specify for *dynamic* arguments of [`compile_args!`](crate::compile_args)
/// and related macros (i.e., for arguments that are not constants). For now, the only meaningful
/// format customization is provided for strings (`&str`). All other arguments have the only
/// available format that can be created using [`fmt()`].
///
/// # Examples
///
/// ## Clipping string to certain width
///
/// ```
/// use compile_fmt::{compile_args, clip, fmt};
///
/// const fn format_clipped_str(s: &str) -> impl AsRef<str> {
/// compile_args!(
/// "Clipped string: '", s => clip(8, "…"),
/// "', original length: ", s.len() => fmt::<usize>()
/// )
/// }
///
/// let s = format_clipped_str("very long string indeed");
/// assert_eq!(
/// s.as_ref(),
/// "Clipped string: 'very lon…', original length: 23"
/// );
/// ```
///
/// ## Padding
///
/// ```
/// # use compile_fmt::{compile_args, fmt};
/// const fn format_with_padding(value: u32) -> impl AsRef<str> {
/// compile_args!(
/// "Number: ", value => fmt::<u32>().pad_right(4, '0')
/// )
/// }
///
/// let s = format_with_padding(42);
/// assert_eq!(s.as_ref(), "Number: 0042");
/// let s = format_with_padding(19_999);
/// assert_eq!(s.as_ref(), "Number: 19999");
/// // ^ If the string before padding contains more chars than in the padding spec,
/// // padding is not applied at all.
/// ```
///
/// Any Unicode char can be used as padding:
///
/// ```
/// # use compile_fmt::{compile_args, fmt};
/// let s = compile_args!(
/// "Number: ", 42 => fmt::<u32>().pad_left(4, '💣')
/// );
/// assert_eq!(s.as_str(), "Number: 42💣💣");
/// ```
///
/// Strings can be padded as well:
///
/// ```
/// # use compile_fmt::{compile_args, clip};
/// const fn pad_str(s: &str) -> impl AsRef<str> {
/// compile_args!("[", s => clip(8, "").pad_center(8, ' '), "]")
/// }
///
/// assert_eq!(pad_str("test").as_ref(), "[ test ]");
/// assert_eq!(pad_str("test!").as_ref(), "[ test! ]");
/// ```
#[derive(Debug, Clone, Copy)]
pub struct Fmt<T: FormatArgument> {
/// Byte capacity of the format without taking padding into account. This is a field
/// rather than a method in `FormatArgument` because we wouldn't be able to call this method
/// in `const fn`s.
capacity: StrLength,
pub(crate) details: T::Details,
pub(crate) pad: Option<Pad>,
}
/// Creates a default format for a type that has known bounded formatting width.
pub const fn fmt<T>() -> Fmt<T>
where
T: FormatArgument<Details = ()> + MaxLength,
{
Fmt {
capacity: T::MAX_LENGTH,
details: (),
pad: None,
}
}
/// Creates a format that will clip the value to the specified max **char** width (not byte width!).
/// If clipped, the end of the string will be replaced with the specified replacer, which can be empty.
///
/// # Panics
///
/// Panics if `clip_at` is zero.
pub const fn clip<'a>(clip_at: usize, using: &'static str) -> Fmt<&'a str> {
assert!(clip_at > 0, "Clip width must be positive");
Fmt {
capacity: StrLength {
bytes: clip_at * char::MAX_LENGTH.bytes + using.len(),
chars: clip_at + count_chars(using),
},
details: StrFormat { clip_at, using },
pad: None,
}
}
/// Same as [`clip()`], but for [`Ascii`] strings.
///
/// # Panics
///
/// Panics if `clip_at` is zero or `using` contains non-ASCII chars.
pub const fn clip_ascii<'a>(clip_at: usize, using: &'static str) -> Fmt<Ascii<'a>> {
assert!(clip_at > 0, "Clip width must be positive");
assert_is_ascii(using);
Fmt {
capacity: StrLength::both(clip_at + using.len()),
details: StrFormat { clip_at, using },
pad: None,
}
}
impl<T: FormatArgument> Fmt<T> {
const fn pad(mut self, align: Alignment, width: usize, using: char) -> Self {
let pad = Pad {
align,
width,
using,
};
self.pad = Some(pad);
self
}
/// Specifies left-aligned padding. `width` is measured in chars, rather than bytes.
#[must_use]
pub const fn pad_left(self, width: usize, using: char) -> Self {
self.pad(Alignment::Left, width, using)
}
/// Specifies right-aligned padding. `width` is measured in chars, rather than bytes.
#[must_use]
pub const fn pad_right(self, width: usize, using: char) -> Self {
self.pad(Alignment::Right, width, using)
}
/// Specifies center-aligned padding. `width` is measured in chars, rather than bytes.
#[must_use]
pub const fn pad_center(self, width: usize, using: char) -> Self {
self.pad(Alignment::Center, width, using)
}
/// Returns the byte capacity of this format in bytes.
#[doc(hidden)] // only used by macros
pub const fn capacity(&self) -> usize {
if let Some(pad) = &self.pad {
// Capacity necessary for an empty non-padded string (which we assume is always possible).
let full_pad_capacity = pad.using.len_utf8() * pad.width;
let max_width = if self.capacity.chars > pad.width {
pad.width
} else {
self.capacity.chars
};
// Capacity necessary for the maximum-length string that still has padding.
let min_pad_capacity =
pad.using.len_utf8() * (pad.width - max_width) + max_width * T::MAX_BYTES_PER_CHAR;
// Select maximum of `max_pad_capacity`, `min_pad_capacity` and the original capacity.
let pad_capacity = if full_pad_capacity > min_pad_capacity {
full_pad_capacity
} else {
min_pad_capacity
};
if pad_capacity > self.capacity.bytes {
return pad_capacity;
}
}
self.capacity.bytes
}
}
/// Type that can be formatted. Implemented for standard integer types, `&str` and `char`.
pub trait FormatArgument {
/// Formatting specification for the type.
type Details: 'static + Copy;
/// Maximum number of bytes a single char from this format can occupy.
#[doc(hidden)] // implementation detail
const MAX_BYTES_PER_CHAR: usize;
}
impl FormatArgument for &str {
type Details = StrFormat;
const MAX_BYTES_PER_CHAR: usize = 4;
}
impl FormatArgument for Ascii<'_> {
type Details = StrFormat;
const MAX_BYTES_PER_CHAR: usize = 1;
}
/// Formatting details for strings.
#[doc(hidden)] // implementation detail
#[derive(Debug, Clone, Copy)]
pub struct StrFormat {
pub(crate) clip_at: usize,
pub(crate) using: &'static str,
}
/// Type that has a known upper boundary for the formatted length.
pub trait MaxLength {
/// Upper boundary for the formatted length in bytes and chars.
const MAX_LENGTH: StrLength;
}
macro_rules! impl_max_width_for_uint {
($($uint:ty),+) => {
$(
impl MaxLength for $uint {
const MAX_LENGTH: StrLength = StrLength::both(
crate::ArgumentWrapper::new(Self::MAX).into_argument().formatted_len(),
);
}
impl FormatArgument for $uint {
type Details = ();
const MAX_BYTES_PER_CHAR: usize = 1;
}
)+
};
}
impl_max_width_for_uint!(u8, u16, u32, u64, u128, usize);
macro_rules! impl_max_width_for_int {
($($int:ty),+) => {
$(
impl MaxLength for $int {
const MAX_LENGTH: StrLength = StrLength::both(
crate::ArgumentWrapper::new(Self::MIN).into_argument().formatted_len(),
);
}
impl FormatArgument for $int {
type Details = ();
const MAX_BYTES_PER_CHAR: usize = 1;
}
)+
};
}
impl_max_width_for_int!(i8, i16, i32, i64, i128, isize);
impl MaxLength for char {
const MAX_LENGTH: StrLength = StrLength { bytes: 4, chars: 1 };
}
impl FormatArgument for char {
type Details = ();
const MAX_BYTES_PER_CHAR: usize = 4;
}
#[cfg(test)]
mod tests {
use std::string::ToString;
use super::*;
#[test]
fn max_length_bound_is_correct() {
assert_eq!(u8::MAX_LENGTH.bytes, u8::MAX.to_string().len());
assert_eq!(u16::MAX_LENGTH.bytes, u16::MAX.to_string().len());
assert_eq!(u32::MAX_LENGTH.bytes, u32::MAX.to_string().len());
assert_eq!(u64::MAX_LENGTH.bytes, u64::MAX.to_string().len());
assert_eq!(u128::MAX_LENGTH.bytes, u128::MAX.to_string().len());
assert_eq!(usize::MAX_LENGTH.bytes, usize::MAX.to_string().len());
assert_eq!(i8::MAX_LENGTH.bytes, i8::MIN.to_string().len());
assert_eq!(i16::MAX_LENGTH.bytes, i16::MIN.to_string().len());
assert_eq!(i32::MAX_LENGTH.bytes, i32::MIN.to_string().len());
assert_eq!(i64::MAX_LENGTH.bytes, i64::MIN.to_string().len());
assert_eq!(i128::MAX_LENGTH.bytes, i128::MIN.to_string().len());
assert_eq!(isize::MAX_LENGTH.bytes, isize::MIN.to_string().len());
}
#[test]
fn capacity_for_padded_format() {
let format = fmt::<u8>().pad(Alignment::Right, 8, ' ');
assert_eq!(format.capacity(), 8);
let format = fmt::<u8>().pad(Alignment::Right, 8, 'ℝ');
assert_eq!(format.capacity(), 24); // each padding char is 3 bytes
let format = fmt::<u64>().pad(Alignment::Right, 8, ' ');
assert_eq!(format.capacity(), u64::MAX.to_string().len()); // original capacity
let format = clip(8, "").pad(Alignment::Left, 8, ' ');
assert_eq!(format.capacity.chars, 8);
assert_eq!(format.capacity.bytes, 32);
assert_eq!(format.capacity(), 32);
let format = clip(4, "").pad(Alignment::Left, 8, ' ');
assert_eq!(format.capacity.chars, 4);
assert_eq!(format.capacity.bytes, 16);
assert_eq!(format.capacity(), 20); // 16 + 4 padding chars
let format = clip(4, "").pad(Alignment::Left, 8, 'ß');
assert_eq!(format.capacity.chars, 4);
assert_eq!(format.capacity.bytes, 16);
assert_eq!(format.capacity(), 24); // 16 + 4 padding chars * 2 bytes each
let format = clip(4, "…").pad(Alignment::Left, 8, ' ');
assert_eq!(format.capacity.chars, 5);
assert_eq!(format.capacity.bytes, 16 + "…".len());
assert_eq!(format.capacity(), 23); // 20 (5 chars * 4 bytes) + 3 padding chars * 4 bytes each
}
}