Skip to main content

String

Trait String 

Source
pub trait String:
    StringView
    + Debug
    + Display
    + PartialOrd
    + Ord
    + Hash
    + Deref<Target = [u8]>
    + PartialEq
    + Eq {
Show 23 methods // Required methods fn capacity(&self) -> usize; fn len(&self) -> usize; // Provided methods fn as_bytes(&self) -> &[u8] { ... } fn as_bytes_with_nul(&self) -> &[u8] { ... } fn as_c_str(&self) -> *const c_char { ... } fn as_str(&self) -> &str { ... } fn clear(&mut self) { ... } fn find(&self, bytes: &[u8]) -> Option<usize> { ... } fn is_empty(&self) -> bool { ... } fn is_full(&self) -> bool { ... } fn insert( &mut self, idx: usize, byte: u8, ) -> Result<(), StringModificationError> { ... } fn insert_bytes( &mut self, idx: usize, bytes: &[u8], ) -> Result<(), StringModificationError> { ... } unsafe fn insert_bytes_unchecked(&mut self, idx: usize, bytes: &[u8]) { ... } fn pop(&mut self) -> Option<u8> { ... } fn push(&mut self, byte: u8) -> Result<(), StringModificationError> { ... } fn push_bytes( &mut self, bytes: &[u8], ) -> Result<(), StringModificationError> { ... } fn remove(&mut self, idx: usize) -> Option<u8> { ... } fn remove_range(&mut self, idx: usize, len: usize) -> bool { ... } fn retain<F: FnMut(u8) -> bool>(&mut self, f: F) { ... } fn rfind(&self, bytes: &[u8]) -> Option<usize> { ... } fn strip_prefix(&mut self, bytes: &[u8]) -> bool { ... } fn strip_suffix(&mut self, bytes: &[u8]) -> bool { ... } fn truncate(&mut self, new_len: usize) { ... }
}
Expand description

A UTF-8 string trait. The string class uses Unicode (ISO/IEC 10646) terminology throughout its interface. In particular:

  • A code point is the numerical index assigned to a character in the Unicode standard.
  • A code unit is the basic component of a character encoding system. For UTF-8, the code unit has a size of 8-bits

For example, the code point U+0041 represents the letter ‘A’ and can be encoded in a single 8-bit code unit in UTF-8. The code point U+1F4A9 requires four 8-bit code units in the UTF-8 encoding.

The NUL code point (U+0000) is not allowed anywhere in the string.

§Note

Currently only Unicode code points less than 128 (U+0080) are supported. This restricts the valid contents of a string to those UTF8 strings that are also valid 7-bit ASCII strings. Full Unicode support will get added later.

Required Methods§

Source

fn capacity(&self) -> usize

Returns the capacity of the string

Source

fn len(&self) -> usize

Returns the length of the string

Provided Methods§

Source

fn as_bytes(&self) -> &[u8]

Returns a slice to the underlying bytes

Source

fn as_bytes_with_nul(&self) -> &[u8]

Returns a null-terminated slice to the underlying bytes

Source

fn as_c_str(&self) -> *const c_char

Returns a zero terminated slice of the underlying bytes

Source

fn as_str(&self) -> &str

Returns the content as a string slice if the bytes are valid UTF-8

Source

fn clear(&mut self)

Removes all bytes from the string and set the len to zero

Source

fn find(&self, bytes: &[u8]) -> Option<usize>

Finds the first occurrence of a byte string in the given string. If the byte string was found the start position of the byte string is returned, otherwise None.

Source

fn is_empty(&self) -> bool

True if the string is empty, otherwise false

Source

fn is_full(&self) -> bool

True if the string is full, otherwise false.

Source

fn insert( &mut self, idx: usize, byte: u8, ) -> Result<(), StringModificationError>

Inserts a byte at a provided index. If the index is out of bounds it panics. If the string has no more capacity left it fails otherwise it succeeds.


use iceoryx2_bb_container::string::*;

const STRING_CAPACITY: usize = 123;

let mut some_string = StaticString::<STRING_CAPACITY>::from_bytes(b"helo").unwrap();
some_string.insert(3, 'l' as u8).unwrap();
assert!(some_string == b"hello");
Source

fn insert_bytes( &mut self, idx: usize, bytes: &[u8], ) -> Result<(), StringModificationError>

Inserts a byte array at a provided index. If the index is out of bounds it panics. If the string has no more capacity left it fails otherwise it succeeds.


use iceoryx2_bb_container::string::*;

const STRING_CAPACITY: usize = 123;

let mut some_string = StaticString::<STRING_CAPACITY>::from_bytes(b"ho").unwrap();
some_string.insert_bytes(1, b"ell").unwrap();
assert!(some_string == b"hello");
Source

unsafe fn insert_bytes_unchecked(&mut self, idx: usize, bytes: &[u8])

Inserts a byte array at a provided index.

§Safety
Source

fn pop(&mut self) -> Option<u8>

Removes the last character from the string and returns it. If the string is empty it returns none.


use iceoryx2_bb_container::string::*;

const STRING_CAPACITY: usize = 123;

let mut some_string = StaticString::<STRING_CAPACITY>::from_bytes(b"hello!").unwrap();
let char = some_string.pop().unwrap();

assert!(char == '!' as u8);
assert!(some_string == b"hello");
Source

fn push(&mut self, byte: u8) -> Result<(), StringModificationError>

Adds a byte at the end of the string. If there is no more space left it fails, otherwise it succeeds.

Source

fn push_bytes(&mut self, bytes: &[u8]) -> Result<(), StringModificationError>

Adds a byte array at the end of the string. If there is no more space left it fails, otherwise it succeeds.

Source

fn remove(&mut self, idx: usize) -> Option<u8>

Removes a character at the provided index and returns it.

Source

fn remove_range(&mut self, idx: usize, len: usize) -> bool

Removes a range beginning from idx.

Source

fn retain<F: FnMut(u8) -> bool>(&mut self, f: F)

Removes all characters where f(c) returns false.

Source

fn rfind(&self, bytes: &[u8]) -> Option<usize>

Finds the last occurrence of a byte string in the given string. If the byte string was found the start position of the byte string is returned, otherwise None.

Source

fn strip_prefix(&mut self, bytes: &[u8]) -> bool

Removes a given prefix from the string. If the prefix was not found it returns false, otherwise the prefix is removed and the function returns true.

Source

fn strip_suffix(&mut self, bytes: &[u8]) -> bool

Removes a given suffix from the string. If the suffix was not found it returns false, otherwise the suffix is removed and the function returns true.

Source

fn truncate(&mut self, new_len: usize)

Truncates the string to new_len.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl String for RelocatableString

Source§

impl<Allocator: Allocate<NonNull<u8>> + Deallocate<NonNull<u8>>> String for PolymorphicString<'_, Allocator>

Source§

impl<const CAPACITY: usize> String for StaticString<CAPACITY>