diff --git a/helix-stdx/src/lib.rs b/helix-stdx/src/lib.rs index eeaba0d81..7c0f25cf0 100644 --- a/helix-stdx/src/lib.rs +++ b/helix-stdx/src/lib.rs @@ -3,6 +3,7 @@ pub mod faccess; pub mod path; pub mod range; pub mod rope; +pub mod str; pub mod time; pub use range::Range; diff --git a/helix-stdx/src/str.rs b/helix-stdx/src/str.rs new file mode 100644 index 000000000..e79cbcccc --- /dev/null +++ b/helix-stdx/src/str.rs @@ -0,0 +1,18 @@ +/// Concatenates strings together. +/// +/// `concat!(a, " ", b, " ", c)` is: +/// - more performant than `format!("{a} {b} {c}")` +/// - more ergonomic than using `String::with_capacity` followed by a series of `String::push_str` +#[macro_export] +macro_rules! concat { + ($($value:expr),*) => {{ + // Rust does not allow using `+` as separator between value + // so we must add that at the end of everything. The `0` is necessary + // at the end so it does not end with "+ " (which would be invalid syntax) + let mut buf = String::with_capacity($($value.len() + )* 0); + $( + buf.push_str(&$value); + )* + buf + }} +} diff --git a/helix-stdx/src/time.rs b/helix-stdx/src/time.rs index cb72a9386..4df88c45c 100644 --- a/helix-stdx/src/time.rs +++ b/helix-stdx/src/time.rs @@ -71,5 +71,5 @@ pub fn format_relative_time(timestamp: i64, timezone_offset: i32) -> String { "from now" }; - format!("{value} {unit} {label}") + crate::concat!(value, " ", unit, " ", label) }