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 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
/// Replaces all the instances of `$pattern` in `$input`
/// (a `&'static str` constant) with `$replace_with` (a `&'static str` constant).
///
/// # Signature
///
/// This macro acts like a function of this signature:
/// ```rust
/// # trait Pattern {}
/// fn str_replace(
/// input: &'static str,
/// pattern: impl Pattern,
/// replace_with: &'static str,
/// ) -> &'static str
/// # {""}
/// ```
/// and is evaluated at compile-time.
///
/// Where `pattern` can be any of these types:
///
/// - `&'static str`
///
/// - `char`
///
/// - `u8`: required to be ascii (`0` up to `127` inclusive).
///
/// # Example
///
///
/// ```rust
/// use const_format::str_replace;
///
/// // Passing a string pattern
/// assert_eq!(
/// str_replace!("The incredible shrinking man.", "i", "eee"),
/// "The eeencredeeeble shreeenkeeeng man.",
/// );
///
/// // Passing a char pattern
/// assert_eq!(
/// str_replace!("The incredible shrinking man.", ' ', "---"),
/// "The---incredible---shrinking---man.",
/// );
///
/// // Passing an ascii u8 pattern.
/// assert_eq!(
/// str_replace!("The incredible shrinking man.", b'i', "eee"),
/// "The eeencredeeeble shreeenkeeeng man.",
/// );
///
/// // Removing all instances of the pattern
/// assert_eq!(
/// str_replace!("remove haire", "re", ""),
/// "move hai",
/// );
///
/// // This shows that all the arguments can be `const`s, they don't have to be literals.
/// {
/// const IN: &str = "Foo Boo Patoo";
/// const REPLACING: &str = "oo";
/// const REPLACE_WITH: &str = "uh";
/// assert_eq!(str_replace!(IN, REPLACING, REPLACE_WITH), "Fuh Buh Patuh");
/// }
/// ```
///
/// [`str::replace`]: https://doc.rust-lang.org/std/primitive.str.html#method.replace
#[macro_export]
macro_rules! str_replace {
($input:expr, $pattern:expr, $replace_with:expr $(,)*) => {{
const ARGS_OSRCTFL4A: $crate::__str_methods::ReplaceInput =
$crate::__str_methods::ReplaceInputConv($input, $pattern, $replace_with).conv();
{
const OB: &[$crate::pmr::u8; ARGS_OSRCTFL4A.replace_length()] =
&ARGS_OSRCTFL4A.replace();
const OS: &$crate::pmr::str = unsafe { $crate::__priv_transmute_bytes_to_str!(OB) };
OS
}
}};
}
/// Creates a `&'static str` by repeating a `&'static str` constant `times` times
///
/// This is evaluated at compile-time.
///
/// # Example
///
/// ```rust
/// use const_format::str_repeat;
///
/// {
/// const OUT: &str = str_repeat!("hi ", 4);
/// assert_eq!(OUT, "hi hi hi hi ")
/// }
/// {
/// const IN: &str = "bye ";
/// const REPEAT: usize = 5;
/// const OUT: &str = str_repeat!(IN, REPEAT);
/// assert_eq!(OUT, "bye bye bye bye bye ")
/// }
///
/// ```
///
/// ### Failing
///
/// If this macro would produce too large a string,
/// it causes a compile-time error.
///
/// ```compile_fail
/// const_format::str_repeat!("hello", usize::MAX / 4);
/// ```
///
#[cfg_attr(
feature = "__test",
doc = r##"
```rust
const_format::str_repeat!("hello", usize::MAX.wrapping_add(4));
```
"##
)]
#[macro_export]
macro_rules! str_repeat {
($string:expr, $times:expr $(,)*) => {{
const P_OSRCTFL4A: &$crate::__str_methods::StrRepeatArgs =
&$crate::__str_methods::StrRepeatArgs($string, $times);
{
use $crate::__hidden_utils::PtrToRef;
use $crate::pmr::{str, transmute, u8};
const P: &$crate::__str_methods::StrRepeatArgs = P_OSRCTFL4A;
$crate::pmr::respan_to! {
($string)
const _ASSERT_VALID_LEN: () = P.assert_valid();
}
const OUT_B: &[u8; P.out_len] = &unsafe {
let ptr = P.str.as_ptr() as *const [u8; P.str_len];
transmute::<[[u8; P.str_len]; P.repeat], [u8; P.out_len]>(
[*PtrToRef { ptr }.reff; P.repeat],
)
};
const OUT_S: &str = unsafe { $crate::__priv_transmute_bytes_to_str!(OUT_B) };
OUT_S
}
}};
}
/// Replaces a substring in a `&'static str` constant.
/// Returns both the new resulting `&'static str`, and the replaced substring.
///
/// # Signature
///
/// This macro acts like a function of this signature:
/// ```rust
/// # trait SomeIndex {}
/// fn str_splice(
/// input: &'static str,
/// range: impl SomeIndex,
/// replace_with: &'static str,
/// ) -> const_format::SplicedStr
/// # {unimplemented!()}
/// ```
/// and is evaluated at compile-time.
///
/// ### `range` argument
///
/// The `range` parameter determines what part of `input` is replaced,
/// and can be any of these types:
///
/// - `usize`: the starting index of a char, only includes that char.
/// - `Range<usize>`
/// - `RangeTo<usize>`
/// - `RangeFrom<usize>`
/// - `RangeInclusive<usize>`
/// - `RangeToInclusive<usize>`
/// - `RangeFull`
///
/// [`SplicedStr`] contains:
/// - `output`: a `&'static str` with the substring at `range` in `input` replaced with
/// `replace_with`.
/// - `removed`: the substring at `range` in `input`.
///
/// # Example
///
/// ```rust
/// use const_format::{str_splice, SplicedStr};
///
/// const OUT: SplicedStr = str_splice!("foo bar baz", 4..=6, "is");
/// assert_eq!(OUT , SplicedStr{output: "foo is baz", removed: "bar"});
///
/// // You can pass `const`ants to this macro, not just literals
/// {
/// const IN: &str = "this is bad";
/// const INDEX: std::ops::RangeFrom<usize> = 8..;
/// const REPLACE_WITH: &str = "... fine";
/// const OUT: SplicedStr = str_splice!(IN, INDEX, REPLACE_WITH);
/// assert_eq!(OUT , SplicedStr{output: "this is ... fine", removed: "bad"});
/// }
/// {
/// const OUT: SplicedStr = str_splice!("ABC豆-", 3, "DEFGH");
/// assert_eq!(OUT , SplicedStr{output: "ABCDEFGH-", removed: "豆"});
/// }
/// ```
///
/// ### Invalid index
///
/// Invalid indices cause compilation errors.
///
/// ```compile_fail
/// const_format::str_splice!("foo", 0..10, "");
/// ```
#[cfg_attr(
feature = "__test",
doc = r#"
```rust
const_format::str_splice!("foo", 0..3, "");
```
```compile_fail
const_format::str_splice!("foo", 0..usize::MAX, "");
```
```rust
assert_eq!(
const_format::str_splice!("効率的", 3..6, "A"),
const_format::SplicedStr{output: "効A的", removed: "率"} ,
);
```
```compile_fail
assert_eq!(
const_format::str_splice!("効率的", 1..6, "A"),
const_format::SplicedStr{output: "効A的", removed: "率"} ,
);
```
```compile_fail
assert_eq!(
const_format::str_splice!("効率的", 3..5, "A"),
const_format::SplicedStr{output: "効A的", removed: "率"} ,
);
```
"#
)]
///
///
/// [`SplicedStr`]: ./struct.SplicedStr.html
#[macro_export]
macro_rules! str_splice {
($string:expr, $index:expr, $insert:expr $(,)*) => {{
const P_OSRCTFL4A: $crate::__str_methods::StrSpliceArgs =
$crate::__str_methods::StrSplceArgsConv($string, $index, $insert).conv();
{
use $crate::__hidden_utils::PtrToRef;
use $crate::__str_methods::{DecomposedString, SplicedStr, StrSpliceArgs};
use $crate::pmr::{str, u8};
const P: &StrSpliceArgs = &P_OSRCTFL4A;
type DecompIn =
DecomposedString<[u8; P.used_rstart], [u8; P.used_rlen], [u8; P.suffix_len]>;
type DecompOut =
DecomposedString<[u8; P.used_rstart], [u8; P.insert_len], [u8; P.suffix_len]>;
$crate::pmr::respan_to! {
($string)
const _ASSERT_VALID_INDEX: () = P.index_validity.assert_valid();
}
const OUT_A: (&DecompOut, &str) = unsafe {
let input = PtrToRef {
ptr: P.str.as_ptr() as *const DecompIn,
}
.reff;
let insert = PtrToRef {
ptr: P.insert.as_ptr() as *const [u8; P.insert_len],
}
.reff;
(
&DecomposedString {
prefix: input.prefix,
middle: *insert,
suffix: input.suffix,
},
$crate::__priv_transmute_bytes_to_str!(&input.middle),
)
};
const OUT: SplicedStr = unsafe {
let output = OUT_A.0 as *const DecompOut as *const [u8; P.out_len];
SplicedStr {
output: $crate::__priv_transmute_raw_bytes_to_str!(output),
removed: OUT_A.1,
}
};
OUT
}
}};
}
/// Indexes a `&'static str` constant.
///
///
/// # Signature
///
/// This macro acts like a function of this signature:
/// ```rust
/// # trait SomeIndex {}
/// fn str_index(input: &'static str, range: impl SomeIndex) -> &'static str
/// # {unimplemented!()}
/// ```
/// and is evaluated at compile-time.
///
/// This accepts
/// [the same `range` arguments as `str_splice`](macro.str_splice.html#range-argument)
///
/// # Example
///
/// ```
/// use const_format::str_index;
///
/// use std::ops::RangeFrom;
///
/// assert_eq!(str_index!("foo bar baz", ..7), "foo bar");
/// assert_eq!(str_index!("foo bar baz", 4..7), "bar");
/// assert_eq!(str_index!("foo bar baz", 4..), "bar baz");
///
/// {
/// const IN: &str = "hello world";
/// const INDEX: RangeFrom<usize> = 6..;
/// // You can pass `const`ants to this macro, not just literals
/// const OUT_0: &str = str_index!(IN, INDEX);
/// assert_eq!(OUT_0, "world");
/// }
/// {
/// const OUT: &str = str_index!("hello world", 4);
/// assert_eq!(OUT, "o");
/// }
///
/// ```
///
/// ### Invalid index
///
/// Invalid indices cause compilation errors.
///
/// ```compile_fail
/// const_format::str_index!("foo", 0..10);
/// ```
#[cfg_attr(
feature = "__test",
doc = r#"
```rust
assert_eq!(const_format::str_index!("効率的", 3..6), "率");
```
```compile_fail
assert_eq!(const_format::str_index!("効率的", 3..5), "率");
```
```compile_fail
assert_eq!(const_format::str_index!("効率的", 4..6), "率");
```
"#
)]
///
///
#[macro_export]
macro_rules! str_index {
($string:expr, $index:expr $(,)*) => {{
const P_OSRCTFL4A: $crate::__str_methods::StrIndexArgs =
$crate::__str_methods::StrIndexArgsConv($string, $index).conv();
{
$crate::pmr::respan_to! {
($string)
const _ASSERT_VALID_INDEX: () =
P_OSRCTFL4A.index_validity.assert_valid();
}
use $crate::__hidden_utils::PtrToRef;
use $crate::__str_methods::DecomposedString;
type DecompIn = DecomposedString<
[u8; P_OSRCTFL4A.used_rstart],
[u8; P_OSRCTFL4A.used_rlen],
[u8; 0],
>;
const OUT: &'static str = unsafe {
let input = PtrToRef {
ptr: P_OSRCTFL4A.str.as_ptr() as *const DecompIn,
}
.reff;
$crate::__priv_transmute_raw_bytes_to_str!(&input.middle)
};
OUT
}
}};
}
/// Indexes a `&'static str` constant,
/// returning `None` when the index is not on a character boundary.
///
///
/// # Signature
///
/// This macro acts like a function of this signature:
/// ```rust
/// # trait SomeIndex {}
/// fn str_get(input: &'static str, range: impl SomeIndex) -> Option<&'static str>
/// # {unimplemented!()}
/// ```
/// and is evaluated at compile-time.
///
/// This accepts
/// [the same `range` arguments as `str_splice`](macro.str_splice.html#range-argument)
///
/// # Example
///
/// ```
/// use const_format::str_get;
///
/// use std::ops::RangeFrom;
///
/// assert_eq!(str_get!("foo 鉄 baz", ..7), Some("foo 鉄"));
/// assert_eq!(str_get!("foo 鉄 baz", 4..7), Some("鉄"));
/// assert_eq!(str_get!("foo 鉄 baz", 4..100), None);
///
///
/// {
/// const IN: &str = "hello 鉄";
/// const INDEX: RangeFrom<usize> = 6..;
/// // You can pass `const`ants to this macro, not just literals
/// const OUT: Option<&str> = str_get!(IN, INDEX);
/// assert_eq!(OUT, Some("鉄"));
/// }
/// {
/// const OUT: Option<&str> = str_get!("hello 鉄", 4);
/// assert_eq!(OUT, Some("o"));
/// }
/// {
/// // End index not on a character boundary
/// const OUT: Option<&str> = str_get!("hello 鉄", 0..7);
/// assert_eq!(OUT, None);
/// }
/// {
/// // Out of bounds indexing
/// const OUT: Option<&str> = str_get!("hello 鉄", 0..1000 );
/// assert_eq!(OUT, None);
/// }
///
/// ```
#[cfg_attr(
feature = "__test",
doc = r#"
```rust
assert_eq!(const_format::str_get!("効率的", 3..6), Some("率"));
assert_eq!(const_format::str_get!("効率的", 3..5), None);
assert_eq!(const_format::str_get!("効率的", 4..6), None);
```
"#
)]
///
#[macro_export]
macro_rules! str_get {
($string:expr, $index:expr $(,)*) => {{
const P_OSRCTFL4A: $crate::__str_methods::StrIndexArgs =
$crate::__str_methods::StrIndexArgsConv($string, $index).conv();
{
use $crate::__hidden_utils::PtrToRef;
use $crate::__str_methods::DecomposedString;
type DecompIn = DecomposedString<
[u8; P_OSRCTFL4A.used_rstart],
[u8; P_OSRCTFL4A.used_rlen],
[u8; 0],
>;
const OUT: $crate::pmr::Option<&'static $crate::pmr::str> = unsafe {
if P_OSRCTFL4A.index_validity.is_valid() {
let input = PtrToRef {
ptr: P_OSRCTFL4A.str.as_ptr() as *const DecompIn,
}
.reff;
$crate::pmr::Some($crate::__priv_transmute_raw_bytes_to_str!(&input.middle))
} else {
$crate::pmr::None
}
};
OUT
}
}};
}
/// Splits `$string` (a `&'static str` constant) with `$splitter`,
/// returning an array of `&'static str`s.
///
/// # Signature
///
/// This macro acts like a function of this signature:
/// ```rust
/// # const LEN: usize = 0;
/// # trait Splitter {}
/// fn str_split(string: &'static str, splitter: impl Splitter) -> [&'static str; LEN]
/// # { [] }
/// ```
/// and is evaluated at compile-time.
///
/// `impl Splitter` is any of these types:
///
/// - `&'static str`
///
/// - `char`
///
/// - `u8`: only ascii values (0 up to 127 inclusive) are allowed
///
/// The value of `LEN` depends on the `string` and `splitter` arguments.
///
///
/// # Example
///
/// ```rust
/// use const_format::str_split;
///
/// assert_eq!(str_split!("this is nice", ' '), ["this", "is", "nice"]);
///
/// assert_eq!(str_split!("Hello, world!", ", "), ["Hello", "world!"]);
///
/// // A `""` splitter outputs all chars individually (`str::split` does the same)
/// assert_eq!(str_split!("🧡BAR🧠", ""), ["", "🧡", "B", "A", "R", "🧠", ""]);
///
/// // Splitting the string with an ascii byte
/// assert_eq!(str_split!("dash-separated-string", b'-'), ["dash", "separated", "string"]);
///
/// {
/// const STR: &str = "foo bar baz";
/// const SPLITTER: &str = " ";
///
/// // both arguments to the `str_aplit` macro can be non-literal constants
/// const SPLIT: [&str; 3] = str_split!(STR, SPLITTER);
///
/// assert_eq!(SPLIT, ["foo", "bar", "baz"]);
/// }
///
/// ```
#[macro_export]
#[cfg(feature = "rust_1_64")]
#[cfg_attr(feature = "__docsrs", doc(cfg(feature = "rust_1_64")))]
macro_rules! str_split {
($string:expr, $splitter:expr $(,)?) => {{
const ARGS_OSRCTFL4A: $crate::__str_methods::SplitInput =
$crate::__str_methods::SplitInputConv($string, $splitter).conv();
{
const OB: [&$crate::pmr::str; ARGS_OSRCTFL4A.length()] = ARGS_OSRCTFL4A.split_it();
OB
}
}};
}