konst/string.rs
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 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
//! `const fn` equivalents of `str` methods.
//!
#[cfg(feature = "iter")]
mod chars_methods;
#[cfg(feature = "iter")]
pub use chars_methods::*;
mod concatenation;
pub use concatenation::*;
#[cfg(test)]
mod priv_string_tests;
mod pattern;
use core::fmt::{self, Debug, Display};
pub use self::pattern::Pattern;
pub(crate) use self::pattern::PatternNorm;
mod split_once;
pub use split_once::*;
#[cfg(feature = "iter")]
mod splitting;
#[cfg(feature = "iter")]
pub use splitting::*;
#[cfg(feature = "iter")]
mod split_terminator_items;
#[cfg(feature = "iter")]
pub use split_terminator_items::*;
use konst_kernel::string::__is_char_boundary_bytes;
__declare_string_cmp_fns! {
import_path = "konst",
equality_fn = eq_str,
ordering_fn = cmp_str,
ordering_fn_inner = cmp_str_inner,
}
#[cfg(feature = "cmp")]
__declare_fns_with_docs! {
(Option<&'a str>, (eq_option_str, cmp_option_str))
docs(default)
macro = __impl_option_cmp_fns!(
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "cmp")))]
for['a,]
params(l, r)
eq_comparison = crate::cmp::CmpWrapper(l).const_eq(r),
cmp_comparison = crate::cmp::CmpWrapper(l).const_cmp(r),
parameter_copyability = copy,
),
}
/// Delegates to [`core::str::from_utf8`],
/// wrapping the error to provide a `panic` method for use in [`unwrap_ctx`]
///
/// # Example
///
/// ### Basic
///
/// ```rust
/// use konst::{
/// result::unwrap_ctx,
/// string,
/// };
///
/// const STR: &str = unwrap_ctx!(string::from_utf8(b"foo bar"));
///
/// assert_eq!(STR, "foo bar")
/// ```
///
/// ### Compile-time error
///
/// ```compile_fail
/// use konst::{
/// result::unwrap_ctx,
/// string,
/// };
///
/// const _: &str = unwrap_ctx!(string::from_utf8(&[255, 255, 255]));
/// ```
///
/// ```text
/// error[E0080]: evaluation of constant value failed
/// --> src/string.rs:88:17
/// |
/// 9 | const _: &str = unwrap_ctx!(string::from_utf8(&[255, 255, 255]));
/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'invalid utf-8 sequence of 1 bytes from index 0', src/string.rs:9:17
/// |
/// = note: this error originates in the macro `unwrap_ctx` (in Nightly builds, run with -Z macro-backtrace for more info)
///
/// ```
///
/// [`unwrap_ctx`]: crate::result::unwrap_ctx
pub const fn from_utf8(slice: &[u8]) -> Result<&str, Utf8Error> {
match core::str::from_utf8(slice) {
Ok(x) => Ok(x),
Err(e) => Err(Utf8Error(e)),
}
}
/// Wrapper around [`core::str::Utf8Error`]
/// to provide a `panic` method for use in [`unwrap_ctx`],
/// returned by [`from_utf8`](crate::string::from_utf8).
///
/// [`unwrap_ctx`]: crate::result::unwrap_ctx
#[derive(Copy, Clone)]
pub struct Utf8Error(pub core::str::Utf8Error);
impl Utf8Error {
/// Panics with a `Display` formatted error message
#[track_caller]
pub const fn panic(self) -> ! {
let pvs = const_panic::StdWrapper(&self.0).to_panicvals(const_panic::FmtArg::DISPLAY);
const_panic::concat_panic(&[&pvs])
}
}
impl Debug for Utf8Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Debug::fmt(&self.0, f)
}
}
impl Display for Utf8Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&self.0, f)
}
}
/// A const equivalent of
/// [`str::starts_with`](https://doc.rust-lang.org/std/primitive.str.html#method.starts_with)
/// , taking a [`Pattern`] parameter.
///
/// # Example
///
/// ```rust
/// use konst::string;
///
/// assert!( string::starts_with("foo,bar,baz", "foo,"));
///
/// assert!(!string::starts_with("foo,bar,baz", "bar"));
/// assert!(!string::starts_with("foo,bar,baz", "baz"));
///
/// ```
///
#[inline(always)]
pub const fn starts_with<'a, P>(left: &str, pat: P) -> bool
where
P: Pattern<'a>,
{
let pat = PatternNorm::new(pat);
crate::slice::__bytes_start_with(left.as_bytes(), pat.as_bytes())
}
/// A const equivalent of
/// [`str::ends_with`](https://doc.rust-lang.org/std/primitive.str.html#method.ends_with)
/// , taking a [`Pattern`] parameter.
///
/// # Example
///
/// ```rust
/// use konst::string;
///
/// assert!( string::ends_with("foo,bar,baz", ",baz"));
/// assert!( string::ends_with("abc...z", 'z'));
///
/// assert!(!string::ends_with("foo,bar,baz", "bar"));
/// assert!(!string::ends_with("foo,bar,baz", "foo"));
/// assert!(!string::ends_with("abc", 'z'));
///
/// ```
///
#[inline(always)]
pub const fn ends_with<'a, P>(left: &str, pat: P) -> bool
where
P: Pattern<'a>,
{
let pat = PatternNorm::new(pat);
crate::slice::__bytes_end_with(left.as_bytes(), pat.as_bytes())
}
/// A const equivalent of
/// [`str::find`](https://doc.rust-lang.org/std/primitive.str.html#method.find)
/// , taking a [`Pattern`] parameter.
///
/// # Example
///
/// ```rust
/// use konst::string;
///
/// assert_eq!(string::find("foo-bar-baz", 'q'), None);
/// assert_eq!(string::find("foo-bar-baz", '-'), Some(3));
///
/// assert_eq!(string::find("foo-bar-baz-foo", "qux"), None);
/// assert_eq!(string::find("foo-bar-baz-foo", "foo"), Some(0));
/// assert_eq!(string::find("foo-bar-baz-foo-bar", "bar"), Some(4));
/// assert_eq!(string::find("foo-the-baz-foo-bar", "bar"), Some(16));
///
/// ```
///
#[inline]
pub const fn find<'a, P>(left: &str, pat: P) -> Option<usize>
where
P: Pattern<'a>,
{
let pat = PatternNorm::new(pat);
crate::slice::__bytes_find(left.as_bytes(), pat.as_bytes())
}
/// A const equivalent of
/// [`str::contains`](https://doc.rust-lang.org/std/primitive.str.html#method.contains)
/// , taking a [`Pattern`] parameter.
///
/// # Example
///
/// ```rust
/// use konst::string;
///
/// assert!(string::contains("foo-bar-baz", '-'));
/// assert!(!string::contains("foo-bar-baz", 'q'));
///
/// assert!(string::contains("foo-bar-baz-foo", "foo"));
///
/// assert!( string::contains("foo-bar-baz-foo-bar", "bar"));
/// assert!(!string::contains("foo-he-baz-foo-he", "bar"));
///
/// ```
///
#[inline]
pub const fn contains<'a, P>(left: &str, pat: P) -> bool
where
P: Pattern<'a>,
{
let pat = PatternNorm::new(pat);
matches!(
crate::slice::__bytes_find(left.as_bytes(), pat.as_bytes()),
Some(_)
)
}
/// A const equivalent of
/// [`str::rfind`](https://doc.rust-lang.org/std/primitive.str.html#method.rfind)
/// , taking a [`Pattern`] parameter.
///
/// # Example
///
/// ```rust
/// use konst::string;
///
/// assert_eq!(string::rfind("bar-baz-baz", 'q'), None);
/// assert_eq!(string::rfind("bar-baz-baz", '-'), Some(7));
///
/// assert_eq!(string::rfind("bar-baz", "foo"), None);
/// assert_eq!(string::rfind("bar-baz-foo", "foo"), Some(8));
/// assert_eq!(string::rfind("foo-bar-baz", "foo"), Some(0));
///
/// ```
///
#[inline]
pub const fn rfind<'a, P>(left: &str, pat: P) -> Option<usize>
where
P: Pattern<'a>,
{
let pat = PatternNorm::new(pat);
crate::slice::__bytes_rfind(left.as_bytes(), pat.as_bytes())
}
/// A const equivalent of
/// [`str::contains`](https://doc.rust-lang.org/std/primitive.str.html#method.contains)
/// , taking a [`Pattern`] parameter.
///
/// # Example
///
/// ```rust
/// use konst::string;
///
/// assert!(string::rcontains("foo-bar-baz", '-'));
/// assert!(!string::rcontains("foo-bar-baz", 'q'));
///
/// assert!(!string::rcontains("bar-baz", "foo"));
/// assert!(string::rcontains("foo-bar", "foo"));
///
/// ```
///
#[inline(always)]
pub const fn rcontains<'a, P>(left: &str, pat: P) -> bool
where
P: Pattern<'a>,
{
let pat = PatternNorm::new(pat);
matches!(
crate::slice::__bytes_rfind(left.as_bytes(), pat.as_bytes()),
Some(_)
)
}
/// A const equivalent of `&string[..len]`.
///
/// If `string.len() < len`, this simply returns `string` back.
///
/// # Panics
///
/// This function panics if `len` is inside the string but doesn't fall on a char boundary.
///
/// # Example
///
/// ```
/// use konst::string::str_up_to;
///
/// const STR: &str = "foo bar baz";
///
/// const SUB0: &str = str_up_to(STR, 3);
/// assert_eq!(SUB0, "foo");
///
/// const SUB1: &str = str_up_to(STR, 7);
/// assert_eq!(SUB1, "foo bar");
///
/// const SUB2: &str = str_up_to(STR, 11);
/// assert_eq!(SUB2, STR);
///
/// const SUB3: &str = str_up_to(STR, 100);
/// assert_eq!(SUB3, STR);
///
///
/// ```
#[doc(inline)]
pub use konst_kernel::string::str_up_to;
/// A const equivalent of `&string[start..]`.
///
/// If `string.len() < start`, this simply returns an empty string` back.
///
/// # Panics
///
/// This function panics if `start` is inside the string but doesn't fall on a char boundary.
///
/// # Example
///
/// ```
/// use konst::string::str_from;
///
/// const STR: &str = "foo bar baz";
///
/// const SUB0: &str = str_from(STR, 0);
/// assert_eq!(SUB0, STR);
///
/// const SUB1: &str = str_from(STR, 4);
/// assert_eq!(SUB1, "bar baz");
///
/// const SUB2: &str = str_from(STR, 8);
/// assert_eq!(SUB2, "baz");
///
/// const SUB3: &str = str_from(STR, 11);
/// assert_eq!(SUB3, "");
///
/// const SUB4: &str = str_from(STR, 1000);
/// assert_eq!(SUB3, "");
///
///
/// ```
#[doc(inline)]
pub use konst_kernel::string::str_from;
/// A const equivalent of `&string[start..end]`.
///
/// If `start >= end ` or `string.len() < start `, this returns an empty string.
///
/// If `string.len() < end`, this returns the string from `start`.
///
/// # Alternatives
///
/// For a const equivalent of `&string[start..]` there's [`str_from`].
///
/// For a const equivalent of `&string[..end]` there's [`str_up_to`].
///
/// [`str_from`]: ./fn.str_from.html
/// [`str_up_to`]: ./fn.str_up_to.html
///
/// # Panics
///
/// This function panics if either `start` or `end` are inside the string and
/// don't fall on a char boundary.
///
/// # Example
///
/// ```
/// use konst::string::str_range;
///
/// const STR: &str = "foo bar baz";
///
/// const SUB0: &str = str_range(STR, 0, 3);
/// assert_eq!(SUB0, "foo");
///
/// const SUB1: &str = str_range(STR, 0, 7);
/// assert_eq!(SUB1, "foo bar");
///
/// const SUB2: &str = str_range(STR, 4, 11);
/// assert_eq!(SUB2, "bar baz");
///
/// const SUB3: &str = str_range(STR, 0, 1000);
/// assert_eq!(SUB3, STR);
///
///
/// ```
#[doc(inline)]
pub use konst_kernel::string::str_range;
/// Const equivalent of [`str::is_char_boundary`].
///
/// # Example
///
/// ```
/// use konst::string::is_char_boundary;
///
/// let string = "锈 is 🧠";
///
/// // Start of "锈"
/// assert!(is_char_boundary(string, 0));
/// assert!(!is_char_boundary(string, 1));
/// assert!(!is_char_boundary(string, 2));
///
/// // start of " "
/// assert!(is_char_boundary(string, 3));
///
/// // start of "🧠"
/// assert!(is_char_boundary(string, 7));
/// assert!(!is_char_boundary(string, 8));
///
/// // end of string
/// assert!(is_char_boundary(string, string.len()));
///
/// // after end of string
/// assert!(!is_char_boundary(string, string.len() + 1));
///
///
/// ```
#[doc(inline)]
pub use konst_kernel::string::is_char_boundary;
/// Checks that the start and end are valid utf8 char boundaries
/// when the `"debug"` feature is enabled.
///
/// When the `"debug"` feature is disabled,
/// this is equivalent to calling `core::str::from_utf8_unchecled`
///
/// # Safety
///
/// The input byte slice must be a subslice of a `&str`,
/// so that only the start and end need to be checked.
#[doc(inline)]
pub use konst_kernel::string::__from_u8_subslice_of_str;
/// A const equivalent of `string.get(..len)`.
///
/// # Example
///
/// ```
/// use konst::string;
///
/// const STR: &str = "foo bar baz";
///
/// const SUB0: Option<&str> = string::get_up_to(STR, 3);
/// assert_eq!(SUB0, Some("foo"));
///
/// const SUB1: Option<&str> = string::get_up_to(STR, 7);
/// assert_eq!(SUB1, Some("foo bar"));
///
/// const SUB2: Option<&str> = string::get_up_to(STR, 11);
/// assert_eq!(SUB2, Some(STR));
///
/// const SUB3: Option<&str> = string::get_up_to(STR, 100);
/// assert_eq!(SUB3, None);
///
///
/// ```
pub const fn get_up_to(string: &str, len: usize) -> Option<&str> {
let bytes = string.as_bytes();
crate::option::and_then!(
crate::slice::get_up_to(bytes, len),
|x| if __is_char_boundary_bytes(bytes, len) {
// Safety: __is_char_boundary_bytes checks that `len` falls on a char boundary.
unsafe { Some(__from_u8_subslice_of_str(x)) }
} else {
None
}
)
}
/// A const equivalent of `string.get(from..)`.
///
/// # Example
///
/// ```
/// use konst::string;
///
/// const STR: &str = "foo bar baz";
///
/// const SUB0: Option<&str> = string::get_from(STR, 0);
/// assert_eq!(SUB0, Some(STR));
///
/// const SUB1: Option<&str> = string::get_from(STR, 4);
/// assert_eq!(SUB1, Some("bar baz"));
///
/// const SUB2: Option<&str> = string::get_from(STR, 8);
/// assert_eq!(SUB2, Some("baz"));
///
/// const SUB3: Option<&str> = string::get_from(STR, 100);
/// assert_eq!(SUB3, None);
///
///
/// ```
pub const fn get_from(string: &str, from: usize) -> Option<&str> {
let bytes = string.as_bytes();
crate::option::and_then!(
crate::slice::get_from(bytes, from),
|x| if __is_char_boundary_bytes(bytes, from) {
// Safety: __is_char_boundary_bytes checks that `from` falls on a char boundary.
unsafe { Some(__from_u8_subslice_of_str(x)) }
} else {
None
}
)
}
/// A const equivalent of [`str::split_at`]
///
/// If `at > string.len()` this returns `(string, "")`.
///
/// # Panics
///
/// This function panics if `at` is inside the string but doesn't fall on a char boundary.
///
/// # Example
///
/// ```rust
/// use konst::string;
///
/// const IN: &str = "foo bar baz";
///
/// {
/// const SPLIT0: (&str, &str) = string::split_at(IN, 0);
/// assert_eq!(SPLIT0, ("", "foo bar baz"));
/// }
/// {
/// const SPLIT1: (&str, &str) = string::split_at(IN, 4);
/// assert_eq!(SPLIT1, ("foo ", "bar baz"));
/// }
/// {
/// const SPLIT2: (&str, &str) = string::split_at(IN, 8);
/// assert_eq!(SPLIT2, ("foo bar ", "baz"));
/// }
/// {
/// const SPLIT3: (&str, &str) = string::split_at(IN, 11);
/// assert_eq!(SPLIT3, ("foo bar baz", ""));
/// }
/// {
/// const SPLIT4: (&str, &str) = string::split_at(IN, 13);
/// assert_eq!(SPLIT4, ("foo bar baz", ""));
/// }
///
/// ```
///
/// [`str::split_at`]: https://doc.rust-lang.org/std/primitive.str.html#method.split_at
pub const fn split_at(string: &str, at: usize) -> (&str, &str) {
(str_up_to(string, at), str_from(string, at))
}
/// A const equivalent of `string.get(start..end)`.
///
/// # Alternatives
///
/// For a const equivalent of `string.get(start..)` there's [`get_from`].
///
/// For a const equivalent of `string.get(..end)` there's [`get_up_to`].
///
/// [`get_from`]: ./fn.get_from.html
/// [`get_up_to`]: ./fn.get_up_to.html
///
/// # Example
///
/// ```
/// use konst::string;
///
/// const STR: &str = "foo bar baz";
///
/// const SUB0: Option<&str> = string::get_range(STR, 0, 3);
/// assert_eq!(SUB0, Some("foo"));
///
/// const SUB1: Option<&str> = string::get_range(STR, 0, 7);
/// assert_eq!(SUB1, Some("foo bar"));
///
/// const SUB2: Option<&str> = string::get_range(STR, 4, 11);
/// assert_eq!(SUB2, Some("bar baz"));
///
/// const SUB3: Option<&str> = string::get_range(STR, 0, 1000);
/// assert_eq!(SUB3, None);
///
///
/// ```
pub const fn get_range(string: &str, start: usize, end: usize) -> Option<&str> {
let bytes = string.as_bytes();
crate::option::and_then!(crate::slice::get_range(bytes, start, end), |x| {
if __is_char_boundary_bytes(bytes, start) && __is_char_boundary_bytes(bytes, end) {
// Safety: __is_char_boundary_bytes checks that `start` and `end` fall on a char boundary.
unsafe { Some(__from_u8_subslice_of_str(x)) }
} else {
None
}
})
}
/// A const subset of [`str::strip_prefix`].
///
/// This takes [`Pattern`] implementors as the pattern.
///
/// # Example
///
/// ```rust
/// use konst::string;
///
/// {
/// const STRIP: Option<&str> = string::strip_prefix("--5 8", '-');
/// assert_eq!(STRIP, Some("-5 8"));
/// }
/// {
/// const STRIP: Option<&str> = string::strip_prefix("--5 8", '_');
/// assert_eq!(STRIP, None);
/// }
///
/// {
/// const STRIP: Option<&str> = string::strip_prefix("33 5 8", "3");
/// assert_eq!(STRIP, Some("3 5 8"));
/// }
/// {
/// const STRIP: Option<&str> = string::strip_prefix("3 5 8", "hello");
/// assert_eq!(STRIP, None);
/// }
///
///
/// ```
///
/// [`str::strip_prefix`]: https://doc.rust-lang.org/std/primitive.str.html#method.strip_prefix
pub const fn strip_prefix<'a, 'p, P>(string: &'a str, pattern: P) -> Option<&'a str>
where
P: Pattern<'p>,
{
let pat = PatternNorm::new(pattern);
// Safety: because `pat` is a `Pattern`, removing it should result in a valid `&str`
unsafe {
crate::option::map!(
crate::slice::__bytes_strip_prefix(string.as_bytes(), pat.as_bytes()),
__from_u8_subslice_of_str,
)
}
}
/// A const subset of [`str::strip_suffix`].
///
/// This takes [`Pattern`] implementors as the pattern.
///
/// # Example
///
/// ```rust
/// use konst::string;
///
/// {
/// const STRIP: Option<&str> = string::strip_suffix("3 5 8--", '-');
/// assert_eq!(STRIP, Some("3 5 8-"));
/// }
/// {
/// const STRIP: Option<&str> = string::strip_suffix("3 5 8", '_');
/// assert_eq!(STRIP, None);
/// }
///
/// {
/// const STRIP: Option<&str> = string::strip_suffix("3 5 6868", "68");
/// assert_eq!(STRIP, Some("3 5 68"));
/// }
/// {
/// const STRIP: Option<&str> = string::strip_suffix("3 5 8", "hello");
/// assert_eq!(STRIP, None);
/// }
///
///
/// ```
///
pub const fn strip_suffix<'a, 'p, P>(string: &'a str, pattern: P) -> Option<&'a str>
where
P: Pattern<'p>,
{
let pat = PatternNorm::new(pattern);
// Safety: because `suffix` is a `&str`, removing it should result in a valid `&str`
unsafe {
crate::option::map!(
crate::slice::__bytes_strip_suffix(string.as_bytes(), pat.as_bytes()),
__from_u8_subslice_of_str,
)
}
}
/// A const subset of [`str::trim`] which only removes ascii whitespace.
///
/// # Const stabilization
///
/// The [equivalent std function](str::trim_ascii) was const-stabilized in Rust 1.80.0.
///
/// # Example
///
/// ```rust
/// use konst::string;
///
/// const TRIMMED: &str = string::trim("\nhello world ");
///
/// assert_eq!(TRIMMED, "hello world");
///
/// ```
pub const fn trim(this: &str) -> &str {
let trimmed = crate::slice::bytes_trim(this.as_bytes());
// safety: bytes_trim only removes ascii bytes
unsafe { __from_u8_subslice_of_str(trimmed) }
}
/// A const subset of [`str::trim_start`] which only removes ascii whitespace.
///
/// # Const stabilization
///
/// The [equivalent std function](str::trim_ascii_start) was const-stabilized in Rust 1.80.0.
///
/// # Example
///
/// ```rust
/// use konst::string;
///
/// const TRIMMED: &str = string::trim_start("\rfoo bar ");
///
/// assert_eq!(TRIMMED, "foo bar ");
///
/// ```
pub const fn trim_start(this: &str) -> &str {
let trimmed = crate::slice::bytes_trim_start(this.as_bytes());
// safety: bytes_trim_start only removes ascii bytes
unsafe { __from_u8_subslice_of_str(trimmed) }
}
/// A const subset of [`str::trim_end`] which only removes ascii whitespace.
///
/// # Const stabilization
///
/// The [equivalent std function](str::trim_ascii_end) was const-stabilized in Rust 1.80.0.
///
/// # Example
///
/// ```rust
/// use konst::string;
///
/// const TRIMMED: &str = string::trim_end("\rfoo bar ");
///
/// assert_eq!(TRIMMED, "\rfoo bar");
///
/// ```
///
pub const fn trim_end(this: &str) -> &str {
let trimmed = crate::slice::bytes_trim_end(this.as_bytes());
// safety: bytes_trim_end only removes ascii bytes
unsafe { __from_u8_subslice_of_str(trimmed) }
}
/// A const subset of [`str::trim_matches`].
///
/// This takes [`Pattern`] implementors as the needle.
///
/// # Example
///
/// ```rust
/// use konst::string;
///
/// const CHAR_TRIMMED: &str = string::trim_matches("---baz qux---", '-');
/// const STR_TRIMMED: &str = string::trim_matches("<>baz qux<><><>", "<>");
///
/// assert_eq!(CHAR_TRIMMED, "baz qux");
/// assert_eq!(STR_TRIMMED, "baz qux");
///
/// ```
pub const fn trim_matches<'a, 'p, P>(this: &'a str, needle: P) -> &'a str
where
P: Pattern<'p>,
{
let needle = PatternNorm::new(needle);
let trimmed = crate::slice::__bytes_trim_matches(this.as_bytes(), needle.as_bytes());
// safety:
// because bytes_trim_matches was passed `&str`s casted to `&[u8]`s,
// it returns a valid utf8 sequence.
unsafe { __from_u8_subslice_of_str(trimmed) }
}
/// A const subset of [`str::trim_start_matches`].
///
/// This takes [`Pattern`] implementors as the needle.
///
/// # Example
///
/// ```rust
/// use konst::string;
///
/// const CHAR_TRIMMED: &str = string::trim_start_matches("#####huh###", '#');
/// const STR_TRIMMED: &str = string::trim_start_matches("#####huh###", "##");
///
/// assert_eq!(CHAR_TRIMMED, "huh###");
/// assert_eq!(STR_TRIMMED, "#huh###");
///
/// ```
pub const fn trim_start_matches<'a, 'p, P>(this: &'a str, needle: P) -> &'a str
where
P: Pattern<'p>,
{
let needle = PatternNorm::new(needle);
let trimmed = crate::slice::__bytes_trim_start_matches(this.as_bytes(), needle.as_bytes());
// safety:
// because bytes_trim_start_matches was passed `&str`s casted to `&[u8]`s,
// it returns a valid utf8 sequence.
unsafe { __from_u8_subslice_of_str(trimmed) }
}
/// A const subset of [`str::trim_end_matches`].
///
/// This takes [`Pattern`] implementors as the needle.
///
/// # Example
///
/// ```rust
/// use konst::string;
///
/// const CHAR_TRIMMED: &str = string::trim_end_matches("oowowooooo", 'o');
/// const STR_TRIMMED: &str = string::trim_end_matches("oowowooooo", "oo");
///
/// assert_eq!(CHAR_TRIMMED, "oowow");
/// assert_eq!(STR_TRIMMED, "oowowo");
///
/// ```
pub const fn trim_end_matches<'a, 'p, P>(this: &'a str, needle: P) -> &'a str
where
P: Pattern<'p>,
{
let needle = PatternNorm::new(needle);
let trimmed = crate::slice::__bytes_trim_end_matches(this.as_bytes(), needle.as_bytes());
// safety:
// because bytes_trim_end_matches was passed `&str`s casted to `&[u8]`s,
// it returns a valid utf8 sequence.
unsafe { __from_u8_subslice_of_str(trimmed) }
}
/// Advances `this` past the first instance of `needle`.
///
/// Returns `None` if no instance of `needle` is found.
///
/// Returns `Some(this)` if `needle` is empty.
///
/// This takes [`Pattern`] implementors as the needle.
///
/// # Example
///
/// ```rust
/// use konst::string;
///
/// {
/// const FOUND: Option<&str> = string::find_skip("foo bar baz", ' ');
/// assert_eq!(FOUND, Some("bar baz"));
/// }
///
/// {
/// const FOUND: Option<&str> = string::find_skip("foo bar baz", "bar");
/// assert_eq!(FOUND, Some(" baz"));
/// }
/// {
/// const NOT_FOUND: Option<&str> = string::find_skip("foo bar baz", "qux");
/// assert_eq!(NOT_FOUND, None);
/// }
/// ```
pub const fn find_skip<'a, 'p, P>(this: &'a str, needle: P) -> Option<&'a str>
where
P: Pattern<'p>,
{
let needle = PatternNorm::new(needle);
unsafe {
crate::option::map!(
crate::slice::__bytes_find_skip(this.as_bytes(), needle.as_bytes()),
// safety:
// because bytes_find_skip was passed `&str`s casted to `&[u8]`s,
// it returns a valid utf8 sequence.
__from_u8_subslice_of_str,
)
}
}
/// Advances `this` up to the first instance of `needle`.
///
/// Returns `None` if no instance of `needle` is found.
///
/// Returns `Some(this)` if `needle` is empty.
///
/// This takes [`Pattern`] implementors as the needle.
///
/// # Example
///
/// ```rust
/// use konst::string;
///
/// {
/// const FOUND: Option<&str> = string::find_keep("foo-bar-baz", '-');
/// assert_eq!(FOUND, Some("-bar-baz"));
/// }
///
/// {
/// const FOUND: Option<&str> = string::find_keep("foo bar baz", "bar");
/// assert_eq!(FOUND, Some("bar baz"));
/// }
/// {
/// const NOT_FOUND: Option<&str> = string::find_keep("foo bar baz", "qux");
/// assert_eq!(NOT_FOUND, None);
/// }
/// ```
pub const fn find_keep<'a, 'p, P>(this: &'a str, needle: P) -> Option<&'a str>
where
P: Pattern<'p>,
{
let needle = PatternNorm::new(needle);
unsafe {
crate::option::map!(
crate::slice::__bytes_find_keep(this.as_bytes(), needle.as_bytes()),
// safety:
// because bytes_find_keep was passed `&str`s casted to `&[u8]`s,
// it returns a valid utf8 sequence.
__from_u8_subslice_of_str,
)
}
}
/// Truncates `this` to before the last instance of `needle`.
///
/// Returns `None` if no instance of `needle` is found.
///
/// Returns `Some(this)` if `needle` is empty.
///
/// This takes [`Pattern`] implementors as the needle.
///
/// # Example
///
/// ```rust
/// use konst::string;
///
/// {
/// const FOUND: Option<&str> = string::rfind_skip("foo bar _ bar baz", '_');
/// assert_eq!(FOUND, Some("foo bar "));
/// }
///
/// {
/// const FOUND: Option<&str> = string::rfind_skip("foo bar _ bar baz", "bar");
/// assert_eq!(FOUND, Some("foo bar _ "));
/// }
/// {
/// const NOT_FOUND: Option<&str> = string::rfind_skip("foo bar baz", "qux");
/// assert_eq!(NOT_FOUND, None);
/// }
/// ```
pub const fn rfind_skip<'a, 'p, P>(this: &'a str, needle: P) -> Option<&'a str>
where
P: Pattern<'p>,
{
let needle = PatternNorm::new(needle);
unsafe {
crate::option::map!(
crate::slice::__bytes_rfind_skip(this.as_bytes(), needle.as_bytes()),
// safety:
// because bytes_rfind_skip was passed `&str`s casted to `&[u8]`s,
// it returns a valid utf8 sequence.
__from_u8_subslice_of_str,
)
}
}
/// Truncates `this` to the last instance of `needle`.
///
/// Returns `None` if no instance of `needle` is found.
///
/// Returns `Some(this)` if `needle` is empty.
///
/// This takes [`Pattern`] implementors as the needle.
///
/// # Example
///
/// ```rust
/// use konst::string;
///
/// {
/// const FOUND: Option<&str> = string::rfind_keep("foo bar _ bar baz", '_');
/// assert_eq!(FOUND, Some("foo bar _"));
/// }
///
/// {
/// const FOUND: Option<&str> = string::rfind_keep("foo bar _ bar baz", "bar");
/// assert_eq!(FOUND, Some("foo bar _ bar"));
/// }
/// {
/// const NOT_FOUND: Option<&str> = string::rfind_keep("foo bar baz", "qux");
/// assert_eq!(NOT_FOUND, None);
/// }
/// ```
pub const fn rfind_keep<'a, 'p, P>(this: &'a str, needle: P) -> Option<&'a str>
where
P: Pattern<'p>,
{
let needle = PatternNorm::new(needle);
unsafe {
crate::option::map!(
crate::slice::__bytes_rfind_keep(this.as_bytes(), needle.as_bytes()),
// safety:
// because bytes_rfind_keep was passed `&str`s casted to `&[u8]`s,
// it returns a valid utf8 sequence.
__from_u8_subslice_of_str,
)
}
}