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
//! Split a string without another allocation //! //! Helpfull for some types that need to be parsed from a string //! and get split into smaller parts like an `Url` or a `Vec` containing lines //! which need to be owned by the parent type. //! //! ## Note //! //! First try to store references, for example `&str` which is more efficient. //! //! ## Example //! //! ``` //! use shared_string::SharedString; //! // or SharedSyncString if `Sync` is required //! //! struct Name { //! firstname: SharedString, //! lastname: SharedString //! } //! //! impl Name { //! pub fn new( fullname: impl Into<SharedString> ) -> Option<Self> { //! let mut split = fullname.into().split(b' '); //! Some(Self { //! firstname: split.next()?, //! lastname: split.next()? //! }) //! } //! } //! //! let name = Name::new("Albert Einstein").unwrap(); //! assert_eq!( name.firstname, "Albert" ); //! assert_eq!( name.lastname, "Einstein" ); //! ``` #[doc(hidden)] pub mod as_range_inclusive; use as_range_inclusive::AsRangeInclusive; pub mod iter; use iter::{ Split, Lines }; use std::{ ops, str, cmp, fmt, hash, borrow }; use std::rc::Rc; use std::sync::Arc; use std::string::FromUtf8Error; /// A `SharedString`, generic over its reference counter. /// /// Most likely you will only need to interact with the type definitions /// `SharedString` and `SharedSyncString`. /// /// This struct is useful for parsers or other struct that hold a lot of strings /// which could all reference. For example a `Uri` Struct or /// if you have a string with many lines and need every line `independently`. /// /// ## Lines example /// /// ``` /// use shared_string::SharedString; /// // or SharedSyncString if `Sync` is required /// /// let lines: Vec<_> = SharedString::from("many\nlines\nmany").lines().collect(); /// assert_eq!( lines[0], SharedString::from("many") ); /// assert_eq!( lines[1], "lines" ); /// assert_eq!( lines.len(), 3 ); /// ``` #[derive(Clone)] pub struct SharedGenString<R> where R: RefCounter { // start & end | start..=end pos: Option<(usize, usize)>, // can only be generated from valid utf8 bytes: R } /// Use `SharedString` if you only need this type in one thread pub type SharedString = SharedGenString< Rc<Box<[u8]>> >; /// Use `SharedSyncString` if you need to pass it between threads pub type SharedSyncString = SharedGenString< Arc<Box<[u8]>> >; /// A trait to allow `SharedString` to be generic over any reference counter. /// /// Implemented for `Rc` and `Arc`. /// /// Requires the traits `Clone` + `Sized` + /// `Deref<Box<[u8]>>` + `From<Box<[u8]>>` pub trait RefCounter: Clone + Sized + ops::Deref<Target = Box<[u8]>> + From<Box<[u8]>> { fn try_unwrap( self ) -> Result<Box<[u8]>, Self>; } impl RefCounter for Rc<Box<[u8]>> { #[inline] fn try_unwrap( self ) -> Result<Box<[u8]>, Self> { Rc::try_unwrap( self ) } } impl RefCounter for Arc<Box<[u8]>> { #[inline] fn try_unwrap( self ) -> Result<Box<[u8]>, Self> { Arc::try_unwrap( self ) } } impl<R> SharedGenString<R> where R: RefCounter { /// Creates a new `SharedString` with the content of `String`. /// /// This will convert the String into a Boxed [u8] slice. #[inline] pub fn new( string: String ) -> Self { string.into() } #[inline] pub(crate) fn new_raw( pos: (usize, usize), bytes: R ) -> Self { Self { pos: Some(pos), bytes } } /// Convert a vector of bytes to a `SharedString`. /// /// Behaves the same way as [String::from_utf8](https://doc.rust-lang.org/std/string/struct.String.html#method.from_utf8). /// /// If you are sure that the bytes are valid UTF-8, there is an unsafe method /// [from_utf8_unchecked](#method.from_utf8_unchecked) which behaves the same way /// but skips the checks. /// /// ## Errors /// /// Returns an `FromUtf8Error` if the bytes are not valid UTF-8 with a description /// were an invalid byte was found. #[inline] pub fn from_utf8( vec: Vec<u8> ) -> Result<Self, FromUtf8Error> { String::from_utf8( vec ).map( |s| s.into() ) } /// Converts a vector of bytes to a `SharedString` with out checking that every /// bytes is valid UTF-8. /// /// Safe version [from_utf8](#method.from_utf8) #[inline] pub unsafe fn from_utf8_unchecked( vec: Vec<u8> ) -> Self { Self { pos: None, bytes: vec.into_boxed_slice().into() } } /// Returns a byte slice of the underlying bytes. /// /// To get the full bytes from which this `SharedString` was created from use /// [as_bytes_full](#method.as_bytes_full). #[inline] pub fn as_bytes( &self ) -> &[u8] { match self.pos { // Safe because we control start and end // and know that it is not out-of-bounds Some(( start, end )) => unsafe { self.bytes.get_unchecked(start..=end) }, None => &self.bytes } } /// Return a byte slice of the bytes from which this `SharedString` was created. #[inline] pub fn as_full_bytes( &self ) -> &[u8] { &self.bytes } /// Returns a string slice of the `SharedString`. /// /// ## Example /// /// ``` /// # use shared_string::SharedString; /// let s = SharedString::from("foo"); /// /// assert_eq!( "foo", s.as_str() ); /// ``` #[inline] pub fn as_str( &self ) -> &str { &self } /// Return a string slice of the bytes from which this `SharedString` was created. /// /// ## Example /// /// ``` /// # use shared_string::SharedString; /// let mut foo = SharedString::from("foobar"); /// let bar = foo.split_off(3); /// /// assert_eq!( "foo", foo.as_str() ); /// assert_eq!( "foobar", foo.as_full_str() ); /// ``` #[inline] pub fn as_full_str( &self ) -> &str { unsafe { str::from_utf8_unchecked( &self.bytes ) } } /// Returns the len of `SharedString`. /// /// ## Example /// /// ``` /// # use shared_string::SharedString; /// let mut foo = SharedString::from("foobar"); /// let bar = foo.split_off(3); /// /// assert_eq!( 3, foo.len() ); /// assert_eq!( 3, bar.len() ); /// ``` #[inline] pub fn len( &self ) -> usize { match self.pos { Some(( start, end )) => end - start + 1, None => self.bytes.len() } } /// Returns `true` if the length is zero, and `false` otherwise. #[inline] pub fn is_empty( &self ) -> bool { self.len() == 0 } #[inline] fn calc_range<I>( &self, range: I ) -> Option<(usize, usize)> where I: AsRangeInclusive { let (start, end) = self.pos(); let mut range = range.as_range_inclusive( end ); if range.1 <= range.0 { return None } range.0 += start; range.1 += start; if range.1 > end { return None } Some( range ) } /// Returns a substring of `SharedString`. /// /// This is the non-panicking alternative to [idx](#method.idx) and returns `None` /// if the range is out-of-bounds or if the start or the end are not at a char_boundary. /// /// No allocation is performed. /// /// ## Example /// /// ``` /// # use shared_string::SharedString; /// # fn inner() -> Option<()> { /// let foobar = SharedString::from("foobar"); /// /// assert_eq!( "foo", foobar.get(..3)? ); /// assert_eq!( "foob", foobar.get(..=3)? ); /// assert_eq!( "foobar", foobar.get(..)? ); /// assert_eq!( "bar", foobar.get(3..)? ); /// # None /// # } /// # inner(); /// ``` #[inline] pub fn get<I>( &self, range: I ) -> Option<Self> where I: AsRangeInclusive { let range = self.calc_range( range )?; // should validate if is char boundary let s = self.as_full_str(); if !s.is_char_boundary( range.0 ) || !s.is_char_boundary( range.1 ) { return None } Some(Self { pos: Some(range), bytes: self.bytes.clone() }) } /// Returns a substring of `SharedString` for which no allocation is performed. /// /// ## Panics /// /// Panics if the range is out-of-bounds. /// /// ## Warning /// /// This method can lead to invalid utf8 if not "split" at a char boundary. /// /// ## Note /// /// The [Index](https://doc.rust-lang.org/std/ops/trait.Index.html) Trait /// is not implemented because `index` always returns a reference /// and here you always received an owned type. /// /// ## Example /// /// ``` /// # use shared_string::SharedString; /// let foobar = SharedString::from("foobar"); /// /// assert_eq!( "foo", foobar.idx(..3) ); /// assert_eq!( "foob", foobar.idx(..=3) ); /// assert_eq!( "foobar", foobar.idx(..) ); /// assert_eq!( "bar", foobar.idx(3..) ); /// ``` #[inline] pub fn idx<I>( &self, range: I ) -> Self where I: AsRangeInclusive { let range = self.calc_range( range ) .expect("range out-of-bounds"); Self { pos: Some(range), bytes: self.bytes.clone() } } /// Convert `SharedString` to a `Vec<u8>`. /// /// Tries to avoid a call to `clone` if the underlying data is not used /// by another instance of `SharedString` and start is at zero. #[inline] pub fn into_bytes( self ) -> Vec<u8> { match ( self.bytes.try_unwrap().map( |b| b.into_vec() ), self.pos ) { // don't copy (allocate) (Ok( mut bytes ), Some(( 0, end ))) => { bytes.truncate( end + 1 ); bytes }, // don't copy (allocate) (Ok( bytes ), _ ) => bytes, // copy (allocate) (Err( slice ), Some(( start, end ))) => unsafe { slice.get_unchecked( start..=end ).to_vec() }, // copy (allocate) (Err( slice ), None ) => slice.to_vec() } } /// Returns the underlying Bytes from which this `SharedString` was created. /// /// Tries to avoid a call to `clone` if the underlying data is not used /// by another instance. #[inline] pub fn into_full_bytes( self ) -> Vec<u8> { match self.bytes.try_unwrap() { Ok( bytes ) => bytes.into(), Err( slice ) => slice.to_vec() } } /// Convert `SharedString` to a `String`. /// /// Tries to avoid a call to `clone` if the underlying data is not used /// by another instance of `SharedString` and start is at zero. #[inline] pub fn into_string( self ) -> String { let vec = self.into_bytes(); unsafe { String::from_utf8_unchecked( vec ) } } /// Returns the underlying Bytes as a `String` from which this `SharedString` /// was created. /// /// Tries to avoid a call to `clone` if the underlying data is not used /// by another instance. #[inline] pub fn into_full_string( self ) -> String { let vec = self.into_full_bytes(); unsafe { String::from_utf8_unchecked( vec ) } } /// Pushes a char to the `String` returned by [into_string](#method.into_string). /// /// If the conditions in `into_string` are met no `clone` is perfomed. /// /// ## Example /// /// ``` /// # use shared_string::SharedString; /// let fooba = SharedString::from("fooba"); /// let foobar = fooba.push('r'); /// /// assert_eq!( foobar, "foobar" ); /// ``` #[inline] pub fn push( self, ch: char ) -> String { let mut s = self.into_string(); s.push( ch ); s } /// Pushes a string slice to the `String` returned by [into_string](#method.into_string). /// /// If the conditions in `into_string` are met no `clone` is perfomed. /// /// ## Example /// /// ``` /// # use shared_string::SharedString; /// let foo = SharedString::from("foo"); /// let foobar = foo.push_str("bar"); /// /// assert_eq!( foobar, "foobar" ); /// ``` #[inline] pub fn push_str( self, string: &str ) -> String { let mut s = self.into_string(); s.push_str( string ); s } #[inline] fn pos( &self ) -> (usize, usize) { match self.pos { Some(p) => p, None => ( 0, self.bytes.len().saturating_sub(1) ) } } /// Splits the `SharedString` into two at the given index. /// /// No allocation is needed. /// /// This is `O(1)` because only the reference counter is increased. /// /// ## Panics /// /// Panics if at is not at a char boundary. #[inline] pub fn split_off( &mut self, mut at: usize ) -> Self { if at == 0 { return self.clone(); } // maybe can improve this // but maybe everything here gets inlined and // some stuff can be optimized away let s = self.as_str(); assert!( s.is_char_boundary(at) ); let (start, end) = self.pos(); at += start; // active string self.pos = Some(( start, at - 1 )); Self { pos: Some(( at, end )), bytes: self.bytes.clone() } } /// Returns an iterator which returns for every "segment" a `SharedString`. /// /// At the moment only u8 as "splitter" is supported. /// /// u8 will be replaced when [Pattern](https://doc.rust-lang.org/std/str/pattern/trait.Pattern.html) gets stabilized. /// /// ## Example /// /// ``` /// # use shared_string::SharedString; /// let mut foobar = SharedString::from("foo bar").split(b' '); /// let foo = foobar.next().unwrap(); /// let bar = foobar.next().unwrap(); /// /// assert_eq!( foo, "foo" ); /// assert_eq!( bar, "bar" ); /// ``` #[inline] pub fn split( self, byte: u8 ) -> Split<R> { Split::new( self.pos(), self.bytes, byte ) } /// Returns an iterator which returns for every line a `SharedString`. /// /// Be aware that this doens't behave exactly like [lines](#method.lines). /// /// This implementation returns an empty line at the if there is a `\n` byte. /// /// ## Example /// /// ``` /// # use shared_string::SharedString; /// let mut lines = SharedString::from("foo\r\nbar\n\nbaz\n").lines(); /// /// assert_eq!( "foo", lines.next().unwrap() ); /// assert_eq!( "bar", lines.next().unwrap() ); /// assert_eq!( "", lines.next().unwrap() ); /// assert_eq!( "baz", lines.next().unwrap() ); /// /// assert_eq!( None, lines.next() ); /// ``` #[inline] pub fn lines( self ) -> Lines<R> { Lines::new( self.pos(), self.bytes ) } } impl<R> fmt::Display for SharedGenString<R> where R: RefCounter { #[inline] fn fmt( &self, f: &mut fmt::Formatter<'_> ) -> fmt::Result { fmt::Display::fmt( self.as_str(), f ) } } impl<R> fmt::Debug for SharedGenString<R> where R: RefCounter { #[inline] fn fmt( &self, f: &mut fmt::Formatter<'_> ) -> fmt::Result { fmt::Debug::fmt( self.as_str(), f ) } } impl<R> hash::Hash for SharedGenString<R> where R: RefCounter { #[inline] fn hash<H: hash::Hasher>( &self, hasher: &mut H ) { self.as_str().hash( hasher ) } } impl<R> ops::Deref for SharedGenString<R> where R: RefCounter { type Target = str; #[inline] fn deref( &self ) -> &str { // Safe because we know that Self contains valid utf8 unsafe { str::from_utf8_unchecked( self.as_bytes() ) } } } impl<R> AsRef<str> for SharedGenString<R> where R: RefCounter { #[inline] fn as_ref( &self ) -> &str { self } } impl<R> borrow::Borrow<str> for SharedGenString<R> where R: RefCounter { #[inline] fn borrow( &self ) -> &str { self } } // need a custom eq impl<R, O> cmp::PartialEq<SharedGenString<O>> for SharedGenString<R> where R: RefCounter, O: RefCounter { #[inline] fn eq( &self, other: &SharedGenString<O> ) -> bool { self.as_bytes() == other.as_bytes() } } impl<R> cmp::Eq for SharedGenString<R> where R: RefCounter {} impl<R> cmp::PartialEq<str> for SharedGenString<R> where R: RefCounter { #[inline] fn eq( &self, other: &str ) -> bool { self.as_str() == other } } impl<R> cmp::PartialEq<&str> for SharedGenString<R> where R: RefCounter { #[inline] fn eq( &self, other: &&str ) -> bool { self.as_str() == *other } } impl cmp::PartialEq<SharedString> for str { #[inline] fn eq( &self, other: &SharedString ) -> bool { self == other.as_str() } } impl cmp::PartialEq<SharedString> for &str { #[inline] fn eq( &self, other: &SharedString ) -> bool { *self == other.as_str() } } impl cmp::PartialEq<SharedSyncString> for str { #[inline] fn eq( &self, other: &SharedSyncString ) -> bool { self == other.as_str() } } impl cmp::PartialEq<SharedSyncString> for &str { #[inline] fn eq( &self, other: &SharedSyncString ) -> bool { *self == other.as_str() } } // need a custom ord impl<R> From<String> for SharedGenString<R> where R: RefCounter { #[inline] fn from( s: String ) -> Self { Self { pos: None, bytes: s.into_bytes() .into_boxed_slice() .into() } } } impl<R> From<&str> for SharedGenString<R> where R: RefCounter { #[inline] fn from( s: &str ) -> Self { s.to_string().into() } } // Tests #[cfg(test)] mod tests { use super::{ SharedString, SharedSyncString }; #[test] fn rc() { let mut hello: SharedString = "Hello, World!".into(); assert_eq!( hello.len(), 13 ); let world = hello.split_off( 7 ); assert_eq!( hello, "Hello, " ); assert_eq!( hello.len(), 7 ); assert_eq!( world, "World!" ); assert_eq!( world.len(), 6 ); } #[test] fn arc() { let mut hello: SharedSyncString = "Hello, World!".into(); let world = hello.split_off(7); std::thread::spawn( move || { assert_eq!( world, "World!" ); assert_eq!( world.as_full_str(), "Hello, World!" ); } ); assert_eq!( hello, "Hello, " ); } #[test] fn into() { let hello = SharedString::from("Hello, World!"); let s = hello.into_string(); assert_eq!( s, "Hello, World!" ); let mut hello: SharedString = s.into(); let world = hello.split_off( 7 ); let s = world.into_string(); assert_eq!( s, "World!" ); assert!( hello != s.as_str() ); let n_hello = SharedString::from("Hello, "); assert_eq!( hello, n_hello ); } #[test] #[should_panic] fn panic_char_boundary() { let mut s = SharedString::from("abc 好 def"); let _ = s.split_off( 5 ); } #[test] #[should_panic] fn panic_length() { let mut s = SharedString::from("abc"); let _ = s.split_off( 5 ); } #[test] fn range_as_str() { let raw = SharedString::from("Hello, World!"); let hello = &raw[..5]; let world = &raw[7..]; assert_eq!( hello, "Hello" ); assert_eq!( world, "World!" ); } #[test] fn range_with_get() { let raw = SharedString::from("Hello, World!"); let hello = raw.get(..5).unwrap(); let world = raw.get(7..).unwrap(); assert_eq!( hello, "Hello" ); assert_eq!( world, "World!" ); } #[test] fn range_with_idx() { let raw = SharedString::from("Hello, World!"); let hello = raw.idx(..5); let world = raw.idx(7..); assert_eq!( hello, "Hello" ); assert_eq!( world, "World!" ); } #[test] fn empty() { let s = SharedString::from(""); assert_eq!( s.len(), 0 ); assert!( s.is_empty() ); assert!( s.get(..).is_none() ); assert!( s.get(1..).is_none() ); } #[test] fn equal() { let rc: SharedString = "Hello, World!".into(); let arc: SharedSyncString = "Hello, World!".into(); assert_eq!( rc, arc ); } #[test] fn split() { let fullname = SharedString::from("Albert Einstein"); let mut split = fullname.split(b' '); assert_eq!( split.next().unwrap(), "Albert" ); assert_eq!( split.next().unwrap(), "Einstein" ); assert_eq!( split.next(), None ); } #[test] fn lines() { let quote = SharedString::from("Wenn die Menschen nur über das sprächen,\nwas sie begreifen,\r\ndann würde es sehr still auf der Welt sein.\n\r\n"); let mut lines = quote.lines(); assert_eq!( lines.next().unwrap(), "Wenn die Menschen nur über das sprächen," ); assert_eq!( lines.next().unwrap(), "was sie begreifen," ); assert_eq!( lines.next().unwrap(), "dann würde es sehr still auf der Welt sein." ); assert_eq!( lines.next().unwrap(), "" ); assert_eq!( lines.next(), None ); let empty = SharedString::from(" "); let mut lines = empty.lines(); assert_eq!( " ", lines.next().unwrap() ); assert_eq!( lines.next(), None ); } }