6 use alloc
::{borrow::Cow, string::String, vec, vec::Vec}
;
8 #[cfg(feature = "std")]
11 ffi
::{OsStr, OsString}
,
12 path
::{Path, PathBuf}
,
17 utf8
::{self, Utf8Error}
,
20 /// Concatenate the elements given by the iterator together into a single
23 /// The elements may be any type that can be cheaply converted into an `&[u8]`.
24 /// This includes, but is not limited to, `&str`, `&BStr` and `&[u8]` itself.
33 /// let s = bstr::concat(&["foo", "bar", "baz"]);
34 /// assert_eq!(s, "foobarbaz".as_bytes());
37 pub fn concat
<T
, I
>(elements
: I
) -> Vec
<u8>
40 I
: IntoIterator
<Item
= T
>,
42 let mut dest
= vec
![];
43 for element
in elements
{
44 dest
.push_str(element
);
49 /// Join the elements given by the iterator with the given separator into a
52 /// Both the separator and the elements may be any type that can be cheaply
53 /// converted into an `&[u8]`. This includes, but is not limited to,
54 /// `&str`, `&BStr` and `&[u8]` itself.
63 /// let s = bstr::join(",", &["foo", "bar", "baz"]);
64 /// assert_eq!(s, "foo,bar,baz".as_bytes());
67 pub fn join
<B
, T
, I
>(separator
: B
, elements
: I
) -> Vec
<u8>
71 I
: IntoIterator
<Item
= T
>,
73 let mut it
= elements
.into_iter();
74 let mut dest
= vec
![];
82 dest
.push_str(&separator
);
83 dest
.push_str(element
);
88 impl ByteVec
for Vec
<u8> {
90 fn as_vec(&self) -> &Vec
<u8> {
95 fn as_vec_mut(&mut self) -> &mut Vec
<u8> {
100 fn into_vec(self) -> Vec
<u8> {
105 /// Ensure that callers cannot implement `ByteSlice` by making an
106 /// umplementable trait its super trait.
110 impl private
::Sealed
for Vec
<u8> {}
112 /// A trait that extends `Vec<u8>` with string oriented methods.
114 /// Note that when using the constructor methods, such as
115 /// `ByteVec::from_slice`, one should actually call them using the concrete
116 /// type. For example:
119 /// use bstr::{B, ByteVec};
121 /// let s = Vec::from_slice(b"abc"); // NOT ByteVec::from_slice("...")
122 /// assert_eq!(s, B("abc"));
125 /// This trait is sealed and cannot be implemented outside of `bstr`.
126 pub trait ByteVec
: private
::Sealed
{
127 /// A method for accessing the raw vector bytes of this type. This is
128 /// always a no-op and callers shouldn't care about it. This only exists
129 /// for making the extension trait work.
131 fn as_vec(&self) -> &Vec
<u8>;
133 /// A method for accessing the raw vector bytes of this type, mutably. This
134 /// is always a no-op and callers shouldn't care about it. This only exists
135 /// for making the extension trait work.
137 fn as_vec_mut(&mut self) -> &mut Vec
<u8>;
139 /// A method for consuming ownership of this vector. This is always a no-op
140 /// and callers shouldn't care about it. This only exists for making the
141 /// extension trait work.
143 fn into_vec(self) -> Vec
<u8>
147 /// Create a new owned byte string from the given byte slice.
154 /// use bstr::{B, ByteVec};
156 /// let s = Vec::from_slice(b"abc");
157 /// assert_eq!(s, B("abc"));
160 fn from_slice
<B
: AsRef
<[u8]>>(bytes
: B
) -> Vec
<u8> {
161 bytes
.as_ref().to_vec()
164 /// Create a new byte string from an owned OS string.
166 /// When the underlying bytes of OS strings are accessible, then this
167 /// always succeeds and is zero cost. Otherwise, this returns the given
168 /// `OsString` if it is not valid UTF-8.
175 /// use std::ffi::OsString;
177 /// use bstr::{B, ByteVec};
179 /// let os_str = OsString::from("foo");
180 /// let bs = Vec::from_os_string(os_str).expect("valid UTF-8");
181 /// assert_eq!(bs, B("foo"));
184 #[cfg(feature = "std")]
185 fn from_os_string(os_str
: OsString
) -> Result
<Vec
<u8>, OsString
> {
188 fn imp(os_str
: OsString
) -> Result
<Vec
<u8>, OsString
> {
189 use std
::os
::unix
::ffi
::OsStringExt
;
191 Ok(Vec
::from(os_str
.into_vec()))
196 fn imp(os_str
: OsString
) -> Result
<Vec
<u8>, OsString
> {
197 os_str
.into_string().map(Vec
::from
)
203 /// Lossily create a new byte string from an OS string slice.
205 /// When the underlying bytes of OS strings are accessible, then this is
206 /// zero cost and always returns a slice. Otherwise, a UTF-8 check is
207 /// performed and if the given OS string is not valid UTF-8, then it is
208 /// lossily decoded into valid UTF-8 (with invalid bytes replaced by the
209 /// Unicode replacement codepoint).
216 /// use std::ffi::OsStr;
218 /// use bstr::{B, ByteVec};
220 /// let os_str = OsStr::new("foo");
221 /// let bs = Vec::from_os_str_lossy(os_str);
222 /// assert_eq!(bs, B("foo"));
225 #[cfg(feature = "std")]
226 fn from_os_str_lossy
<'a
>(os_str
: &'a OsStr
) -> Cow
<'a
, [u8]> {
229 fn imp
<'a
>(os_str
: &'a OsStr
) -> Cow
<'a
, [u8]> {
230 use std
::os
::unix
::ffi
::OsStrExt
;
232 Cow
::Borrowed(os_str
.as_bytes())
237 fn imp
<'a
>(os_str
: &'a OsStr
) -> Cow
<'a
, [u8]> {
238 match os_str
.to_string_lossy() {
239 Cow
::Borrowed(x
) => Cow
::Borrowed(x
.as_bytes()),
240 Cow
::Owned(x
) => Cow
::Owned(Vec
::from(x
)),
247 /// Create a new byte string from an owned file path.
249 /// When the underlying bytes of paths are accessible, then this always
250 /// succeeds and is zero cost. Otherwise, this returns the given `PathBuf`
251 /// if it is not valid UTF-8.
258 /// use std::path::PathBuf;
260 /// use bstr::{B, ByteVec};
262 /// let path = PathBuf::from("foo");
263 /// let bs = Vec::from_path_buf(path).expect("must be valid UTF-8");
264 /// assert_eq!(bs, B("foo"));
267 #[cfg(feature = "std")]
268 fn from_path_buf(path
: PathBuf
) -> Result
<Vec
<u8>, PathBuf
> {
269 Vec
::from_os_string(path
.into_os_string()).map_err(PathBuf
::from
)
272 /// Lossily create a new byte string from a file path.
274 /// When the underlying bytes of paths are accessible, then this is
275 /// zero cost and always returns a slice. Otherwise, a UTF-8 check is
276 /// performed and if the given path is not valid UTF-8, then it is lossily
277 /// decoded into valid UTF-8 (with invalid bytes replaced by the Unicode
278 /// replacement codepoint).
285 /// use std::path::Path;
287 /// use bstr::{B, ByteVec};
289 /// let path = Path::new("foo");
290 /// let bs = Vec::from_path_lossy(path);
291 /// assert_eq!(bs, B("foo"));
294 #[cfg(feature = "std")]
295 fn from_path_lossy
<'a
>(path
: &'a Path
) -> Cow
<'a
, [u8]> {
296 Vec
::from_os_str_lossy(path
.as_os_str())
299 /// Appends the given byte to the end of this byte string.
301 /// Note that this is equivalent to the generic `Vec::push` method. This
302 /// method is provided to permit callers to explicitly differentiate
303 /// between pushing bytes, codepoints and strings.
310 /// use bstr::ByteVec;
312 /// let mut s = <Vec<u8>>::from("abc");
313 /// s.push_byte(b'\xE2');
314 /// s.push_byte(b'\x98');
315 /// s.push_byte(b'\x83');
316 /// assert_eq!(s, "abc☃".as_bytes());
319 fn push_byte(&mut self, byte
: u8) {
320 self.as_vec_mut().push(byte
);
323 /// Appends the given `char` to the end of this byte string.
330 /// use bstr::ByteVec;
332 /// let mut s = <Vec<u8>>::from("abc");
333 /// s.push_char('1');
334 /// s.push_char('2');
335 /// s.push_char('3');
336 /// assert_eq!(s, "abc123".as_bytes());
339 fn push_char(&mut self, ch
: char) {
340 if ch
.len_utf8() == 1 {
341 self.push_byte(ch
as u8);
345 .extend_from_slice(ch
.encode_utf8(&mut [0; 4]).as_bytes());
348 /// Appends the given slice to the end of this byte string. This accepts
349 /// any type that be converted to a `&[u8]`. This includes, but is not
350 /// limited to, `&str`, `&BStr`, and of course, `&[u8]` itself.
357 /// use bstr::ByteVec;
359 /// let mut s = <Vec<u8>>::from("abc");
360 /// s.push_str(b"123");
361 /// assert_eq!(s, "abc123".as_bytes());
364 fn push_str
<B
: AsRef
<[u8]>>(&mut self, bytes
: B
) {
365 self.as_vec_mut().extend_from_slice(bytes
.as_ref());
368 /// Converts a `Vec<u8>` into a `String` if and only if this byte string is
371 /// If it is not valid UTF-8, then a
372 /// [`FromUtf8Error`](struct.FromUtf8Error.html)
373 /// is returned. (This error can be used to examine why UTF-8 validation
374 /// failed, or to regain the original byte string.)
381 /// use bstr::ByteVec;
383 /// let bytes = Vec::from("hello");
384 /// let string = bytes.into_string().unwrap();
386 /// assert_eq!("hello", string);
389 /// If this byte string is not valid UTF-8, then an error will be returned.
390 /// That error can then be used to inspect the location at which invalid
391 /// UTF-8 was found, or to regain the original byte string:
394 /// use bstr::{B, ByteVec};
396 /// let bytes = Vec::from_slice(b"foo\xFFbar");
397 /// let err = bytes.into_string().unwrap_err();
399 /// assert_eq!(err.utf8_error().valid_up_to(), 3);
400 /// assert_eq!(err.utf8_error().error_len(), Some(1));
402 /// // At no point in this example is an allocation performed.
403 /// let bytes = Vec::from(err.into_vec());
404 /// assert_eq!(bytes, B(b"foo\xFFbar"));
407 fn into_string(self) -> Result
<String
, FromUtf8Error
>
411 match utf8
::validate(self.as_vec()) {
412 Err(err
) => Err(FromUtf8Error { original: self.into_vec(), err }
),
414 // SAFETY: This is safe because of the guarantees provided by
416 unsafe { Ok(self.into_string_unchecked()) }
421 /// Lossily converts a `Vec<u8>` into a `String`. If this byte string
422 /// contains invalid UTF-8, then the invalid bytes are replaced with the
423 /// Unicode replacement codepoint.
430 /// use bstr::ByteVec;
432 /// let bytes = Vec::from_slice(b"foo\xFFbar");
433 /// let string = bytes.into_string_lossy();
434 /// assert_eq!(string, "foo\u{FFFD}bar");
437 fn into_string_lossy(self) -> String
441 match self.as_vec().to_str_lossy() {
442 Cow
::Borrowed(_
) => {
443 // SAFETY: to_str_lossy() returning a Cow::Borrowed guarantees
444 // the entire string is valid utf8.
445 unsafe { self.into_string_unchecked() }
451 /// Unsafely convert this byte string into a `String`, without checking for
456 /// Callers *must* ensure that this byte string is valid UTF-8 before
457 /// calling this method. Converting a byte string into a `String` that is
458 /// not valid UTF-8 is considered undefined behavior.
460 /// This routine is useful in performance sensitive contexts where the
461 /// UTF-8 validity of the byte string is already known and it is
462 /// undesirable to pay the cost of an additional UTF-8 validation check
463 /// that [`into_string`](#method.into_string) performs.
470 /// use bstr::ByteVec;
472 /// // SAFETY: This is safe because string literals are guaranteed to be
473 /// // valid UTF-8 by the Rust compiler.
474 /// let s = unsafe { Vec::from("☃βツ").into_string_unchecked() };
475 /// assert_eq!("☃βツ", s);
478 unsafe fn into_string_unchecked(self) -> String
482 String
::from_utf8_unchecked(self.into_vec())
485 /// Converts this byte string into an OS string, in place.
487 /// When OS strings can be constructed from arbitrary byte sequences, this
488 /// always succeeds and is zero cost. Otherwise, if this byte string is not
489 /// valid UTF-8, then an error (with the original byte string) is returned.
496 /// use std::ffi::OsStr;
498 /// use bstr::ByteVec;
500 /// let bs = Vec::from("foo");
501 /// let os_str = bs.into_os_string().expect("should be valid UTF-8");
502 /// assert_eq!(os_str, OsStr::new("foo"));
504 #[cfg(feature = "std")]
506 fn into_os_string(self) -> Result
<OsString
, FromUtf8Error
>
512 fn imp(v
: Vec
<u8>) -> Result
<OsString
, FromUtf8Error
> {
513 use std
::os
::unix
::ffi
::OsStringExt
;
515 Ok(OsString
::from_vec(v
))
520 fn imp(v
: Vec
<u8>) -> Result
<OsString
, FromUtf8Error
> {
521 v
.into_string().map(OsString
::from
)
527 /// Lossily converts this byte string into an OS string, in place.
529 /// When OS strings can be constructed from arbitrary byte sequences, this
530 /// is zero cost and always returns a slice. Otherwise, this will perform a
531 /// UTF-8 check and lossily convert this byte string into valid UTF-8 using
532 /// the Unicode replacement codepoint.
534 /// Note that this can prevent the correct roundtripping of file paths when
535 /// the representation of `OsString` is opaque.
542 /// use bstr::ByteVec;
544 /// let bs = Vec::from_slice(b"foo\xFFbar");
545 /// let os_str = bs.into_os_string_lossy();
546 /// assert_eq!(os_str.to_string_lossy(), "foo\u{FFFD}bar");
549 #[cfg(feature = "std")]
550 fn into_os_string_lossy(self) -> OsString
556 fn imp(v
: Vec
<u8>) -> OsString
{
557 use std
::os
::unix
::ffi
::OsStringExt
;
559 OsString
::from_vec(v
)
564 fn imp(v
: Vec
<u8>) -> OsString
{
565 OsString
::from(v
.into_string_lossy())
571 /// Converts this byte string into an owned file path, in place.
573 /// When paths can be constructed from arbitrary byte sequences, this
574 /// always succeeds and is zero cost. Otherwise, if this byte string is not
575 /// valid UTF-8, then an error (with the original byte string) is returned.
582 /// use bstr::ByteVec;
584 /// let bs = Vec::from("foo");
585 /// let path = bs.into_path_buf().expect("should be valid UTF-8");
586 /// assert_eq!(path.as_os_str(), "foo");
588 #[cfg(feature = "std")]
590 fn into_path_buf(self) -> Result
<PathBuf
, FromUtf8Error
>
594 self.into_os_string().map(PathBuf
::from
)
597 /// Lossily converts this byte string into an owned file path, in place.
599 /// When paths can be constructed from arbitrary byte sequences, this is
600 /// zero cost and always returns a slice. Otherwise, this will perform a
601 /// UTF-8 check and lossily convert this byte string into valid UTF-8 using
602 /// the Unicode replacement codepoint.
604 /// Note that this can prevent the correct roundtripping of file paths when
605 /// the representation of `PathBuf` is opaque.
612 /// use bstr::ByteVec;
614 /// let bs = Vec::from_slice(b"foo\xFFbar");
615 /// let path = bs.into_path_buf_lossy();
616 /// assert_eq!(path.to_string_lossy(), "foo\u{FFFD}bar");
619 #[cfg(feature = "std")]
620 fn into_path_buf_lossy(self) -> PathBuf
624 PathBuf
::from(self.into_os_string_lossy())
627 /// Removes the last byte from this `Vec<u8>` and returns it.
629 /// If this byte string is empty, then `None` is returned.
631 /// If the last codepoint in this byte string is not ASCII, then removing
632 /// the last byte could make this byte string contain invalid UTF-8.
634 /// Note that this is equivalent to the generic `Vec::pop` method. This
635 /// method is provided to permit callers to explicitly differentiate
636 /// between popping bytes and codepoints.
643 /// use bstr::ByteVec;
645 /// let mut s = Vec::from("foo");
646 /// assert_eq!(s.pop_byte(), Some(b'o'));
647 /// assert_eq!(s.pop_byte(), Some(b'o'));
648 /// assert_eq!(s.pop_byte(), Some(b'f'));
649 /// assert_eq!(s.pop_byte(), None);
652 fn pop_byte(&mut self) -> Option
<u8> {
653 self.as_vec_mut().pop()
656 /// Removes the last codepoint from this `Vec<u8>` and returns it.
658 /// If this byte string is empty, then `None` is returned. If the last
659 /// bytes of this byte string do not correspond to a valid UTF-8 code unit
660 /// sequence, then the Unicode replacement codepoint is yielded instead in
661 /// accordance with the
662 /// [replacement codepoint substitution policy](index.html#handling-of-invalid-utf8-8).
669 /// use bstr::ByteVec;
671 /// let mut s = Vec::from("foo");
672 /// assert_eq!(s.pop_char(), Some('o'));
673 /// assert_eq!(s.pop_char(), Some('o'));
674 /// assert_eq!(s.pop_char(), Some('f'));
675 /// assert_eq!(s.pop_char(), None);
678 /// This shows the replacement codepoint substitution policy. Note that
679 /// the first pop yields a replacement codepoint but actually removes two
680 /// bytes. This is in contrast with subsequent pops when encountering
681 /// `\xFF` since `\xFF` is never a valid prefix for any valid UTF-8
682 /// code unit sequence.
685 /// use bstr::ByteVec;
687 /// let mut s = Vec::from_slice(b"f\xFF\xFF\xFFoo\xE2\x98");
688 /// assert_eq!(s.pop_char(), Some('\u{FFFD}'));
689 /// assert_eq!(s.pop_char(), Some('o'));
690 /// assert_eq!(s.pop_char(), Some('o'));
691 /// assert_eq!(s.pop_char(), Some('\u{FFFD}'));
692 /// assert_eq!(s.pop_char(), Some('\u{FFFD}'));
693 /// assert_eq!(s.pop_char(), Some('\u{FFFD}'));
694 /// assert_eq!(s.pop_char(), Some('f'));
695 /// assert_eq!(s.pop_char(), None);
698 fn pop_char(&mut self) -> Option
<char> {
699 let (ch
, size
) = utf8
::decode_last_lossy(self.as_vec());
703 let new_len
= self.as_vec().len() - size
;
704 self.as_vec_mut().truncate(new_len
);
708 /// Removes a `char` from this `Vec<u8>` at the given byte position and
711 /// If the bytes at the given position do not lead to a valid UTF-8 code
712 /// unit sequence, then a
713 /// [replacement codepoint is returned instead](index.html#handling-of-invalid-utf8-8).
717 /// Panics if `at` is larger than or equal to this byte string's length.
724 /// use bstr::ByteVec;
726 /// let mut s = Vec::from("foo☃bar");
727 /// assert_eq!(s.remove_char(3), '☃');
728 /// assert_eq!(s, b"foobar");
731 /// This example shows how the Unicode replacement codepoint policy is
735 /// use bstr::ByteVec;
737 /// let mut s = Vec::from_slice(b"foo\xFFbar");
738 /// assert_eq!(s.remove_char(3), '\u{FFFD}');
739 /// assert_eq!(s, b"foobar");
742 fn remove_char(&mut self, at
: usize) -> char {
743 let (ch
, size
) = utf8
::decode_lossy(&self.as_vec()[at
..]);
746 "expected {} to be less than {}",
750 self.as_vec_mut().drain(at
..at
+ size
);
754 /// Inserts the given codepoint into this `Vec<u8>` at a particular byte
757 /// This is an `O(n)` operation as it may copy a number of elements in this
758 /// byte string proportional to its length.
762 /// Panics if `at` is larger than the byte string's length.
769 /// use bstr::ByteVec;
771 /// let mut s = Vec::from("foobar");
772 /// s.insert_char(3, '☃');
773 /// assert_eq!(s, "foo☃bar".as_bytes());
776 fn insert_char(&mut self, at
: usize, ch
: char) {
777 self.insert_str(at
, ch
.encode_utf8(&mut [0; 4]).as_bytes());
780 /// Inserts the given byte string into this byte string at a particular
783 /// This is an `O(n)` operation as it may copy a number of elements in this
784 /// byte string proportional to its length.
786 /// The given byte string may be any type that can be cheaply converted
787 /// into a `&[u8]`. This includes, but is not limited to, `&str` and
792 /// Panics if `at` is larger than the byte string's length.
799 /// use bstr::ByteVec;
801 /// let mut s = Vec::from("foobar");
802 /// s.insert_str(3, "☃☃☃");
803 /// assert_eq!(s, "foo☃☃☃bar".as_bytes());
806 fn insert_str
<B
: AsRef
<[u8]>>(&mut self, at
: usize, bytes
: B
) {
807 let bytes
= bytes
.as_ref();
808 let len
= self.as_vec().len();
809 assert
!(at
<= len
, "expected {} to be <= {}", at
, len
);
811 // SAFETY: We'd like to efficiently splice in the given bytes into
812 // this byte string. Since we are only working with `u8` elements here,
813 // we only need to consider whether our bounds are correct and whether
814 // our byte string has enough space.
815 self.as_vec_mut().reserve(bytes
.len());
817 // Shift bytes after `at` over by the length of `bytes` to make
818 // room for it. This requires referencing two regions of memory
819 // that may overlap, so we use ptr::copy.
821 self.as_vec().as_ptr().add(at
),
822 self.as_vec_mut().as_mut_ptr().add(at
+ bytes
.len()),
825 // Now copy the bytes given into the room we made above. In this
826 // case, we know that the given bytes cannot possibly overlap
827 // with this byte string since we have a mutable borrow of the
828 // latter. Thus, we can use a nonoverlapping copy.
829 ptr
::copy_nonoverlapping(
831 self.as_vec_mut().as_mut_ptr().add(at
),
834 self.as_vec_mut().set_len(len
+ bytes
.len());
838 /// Removes the specified range in this byte string and replaces it with
839 /// the given bytes. The given bytes do not need to have the same length
840 /// as the range provided.
844 /// Panics if the given range is invalid.
851 /// use bstr::ByteVec;
853 /// let mut s = Vec::from("foobar");
854 /// s.replace_range(2..4, "xxxxx");
855 /// assert_eq!(s, "foxxxxxar".as_bytes());
858 fn replace_range
<R
, B
>(&mut self, range
: R
, replace_with
: B
)
860 R
: ops
::RangeBounds
<usize>,
863 self.as_vec_mut().splice(range
, replace_with
.as_ref().iter().cloned());
866 /// Creates a draining iterator that removes the specified range in this
867 /// `Vec<u8>` and yields each of the removed bytes.
869 /// Note that the elements specified by the given range are removed
870 /// regardless of whether the returned iterator is fully exhausted.
872 /// Also note that is is unspecified how many bytes are removed from the
873 /// `Vec<u8>` if the `DrainBytes` iterator is leaked.
877 /// Panics if the given range is not valid.
884 /// use bstr::ByteVec;
886 /// let mut s = Vec::from("foobar");
888 /// let mut drainer = s.drain_bytes(2..4);
889 /// assert_eq!(drainer.next(), Some(b'o'));
890 /// assert_eq!(drainer.next(), Some(b'b'));
891 /// assert_eq!(drainer.next(), None);
893 /// assert_eq!(s, "foar".as_bytes());
896 fn drain_bytes
<R
>(&mut self, range
: R
) -> DrainBytes
<'_
>
898 R
: ops
::RangeBounds
<usize>,
900 DrainBytes { it: self.as_vec_mut().drain(range) }
904 /// A draining byte oriented iterator for `Vec<u8>`.
906 /// This iterator is created by
907 /// [`ByteVec::drain_bytes`](trait.ByteVec.html#method.drain_bytes).
914 /// use bstr::ByteVec;
916 /// let mut s = Vec::from("foobar");
918 /// let mut drainer = s.drain_bytes(2..4);
919 /// assert_eq!(drainer.next(), Some(b'o'));
920 /// assert_eq!(drainer.next(), Some(b'b'));
921 /// assert_eq!(drainer.next(), None);
923 /// assert_eq!(s, "foar".as_bytes());
926 pub struct DrainBytes
<'a
> {
927 it
: vec
::Drain
<'a
, u8>,
930 impl<'a
> iter
::FusedIterator
for DrainBytes
<'a
> {}
932 impl<'a
> Iterator
for DrainBytes
<'a
> {
936 fn next(&mut self) -> Option
<u8> {
941 impl<'a
> DoubleEndedIterator
for DrainBytes
<'a
> {
943 fn next_back(&mut self) -> Option
<u8> {
948 impl<'a
> ExactSizeIterator
for DrainBytes
<'a
> {
950 fn len(&self) -> usize {
955 /// An error that may occur when converting a `Vec<u8>` to a `String`.
957 /// This error includes the original `Vec<u8>` that failed to convert to a
958 /// `String`. This permits callers to recover the allocation used even if it
959 /// it not valid UTF-8.
966 /// use bstr::{B, ByteVec};
968 /// let bytes = Vec::from_slice(b"foo\xFFbar");
969 /// let err = bytes.into_string().unwrap_err();
971 /// assert_eq!(err.utf8_error().valid_up_to(), 3);
972 /// assert_eq!(err.utf8_error().error_len(), Some(1));
974 /// // At no point in this example is an allocation performed.
975 /// let bytes = Vec::from(err.into_vec());
976 /// assert_eq!(bytes, B(b"foo\xFFbar"));
978 #[derive(Debug, Eq, PartialEq)]
979 pub struct FromUtf8Error
{
985 /// Return the original bytes as a slice that failed to convert to a
993 /// use bstr::{B, ByteVec};
995 /// let bytes = Vec::from_slice(b"foo\xFFbar");
996 /// let err = bytes.into_string().unwrap_err();
998 /// // At no point in this example is an allocation performed.
999 /// assert_eq!(err.as_bytes(), B(b"foo\xFFbar"));
1002 pub fn as_bytes(&self) -> &[u8] {
1006 /// Consume this error and return the original byte string that failed to
1007 /// convert to a `String`.
1014 /// use bstr::{B, ByteVec};
1016 /// let bytes = Vec::from_slice(b"foo\xFFbar");
1017 /// let err = bytes.into_string().unwrap_err();
1018 /// let original = err.into_vec();
1020 /// // At no point in this example is an allocation performed.
1021 /// assert_eq!(original, B(b"foo\xFFbar"));
1024 pub fn into_vec(self) -> Vec
<u8> {
1028 /// Return the underlying UTF-8 error that occurred. This error provides
1029 /// information on the nature and location of the invalid UTF-8 detected.
1036 /// use bstr::{B, ByteVec};
1038 /// let bytes = Vec::from_slice(b"foo\xFFbar");
1039 /// let err = bytes.into_string().unwrap_err();
1041 /// assert_eq!(err.utf8_error().valid_up_to(), 3);
1042 /// assert_eq!(err.utf8_error().error_len(), Some(1));
1045 pub fn utf8_error(&self) -> &Utf8Error
{
1050 #[cfg(feature = "std")]
1051 impl error
::Error
for FromUtf8Error
{
1053 fn description(&self) -> &str {
1054 "invalid UTF-8 vector"
1058 impl fmt
::Display
for FromUtf8Error
{
1060 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
1061 write
!(f
, "{}", self.err
)
1065 #[cfg(all(test, feature = "std"))]
1067 use crate::ext_vec
::ByteVec
;
1072 s
.insert_str(0, "foo");
1073 assert_eq
!(s
, "foo".as_bytes());
1075 let mut s
= Vec
::from("a");
1076 s
.insert_str(0, "foo");
1077 assert_eq
!(s
, "fooa".as_bytes());
1079 let mut s
= Vec
::from("a");
1080 s
.insert_str(1, "foo");
1081 assert_eq
!(s
, "afoo".as_bytes());
1083 let mut s
= Vec
::from("foobar");
1084 s
.insert_str(3, "quux");
1085 assert_eq
!(s
, "fooquuxbar".as_bytes());
1087 let mut s
= Vec
::from("foobar");
1088 s
.insert_str(3, "x");
1089 assert_eq
!(s
, "fooxbar".as_bytes());
1091 let mut s
= Vec
::from("foobar");
1092 s
.insert_str(0, "x");
1093 assert_eq
!(s
, "xfoobar".as_bytes());
1095 let mut s
= Vec
::from("foobar");
1096 s
.insert_str(6, "x");
1097 assert_eq
!(s
, "foobarx".as_bytes());
1099 let mut s
= Vec
::from("foobar");
1100 s
.insert_str(3, "quuxbazquux");
1101 assert_eq
!(s
, "fooquuxbazquuxbar".as_bytes());
1108 s
.insert_str(1, "foo");
1114 let mut s
= Vec
::from("a");
1115 s
.insert_str(2, "foo");
1121 let mut s
= Vec
::from("foobar");
1122 s
.insert_str(7, "foo");