Struct re2::string::StringWrapper
source · pub struct StringWrapper(/* private fields */);Expand description
Handle to a heap-allocated C++ std::string.
This is used as an output for some string search methods such as
RE2::extract() which need to allocate memory dynamically. The same
instance can be provided to multiple calls in order to avoid allocating a
new string upon each call, and Self::as_mut_view() and
Self::resize() can be used to modify the contents in between calls.
This object “owns” its string data, and upon destruction, this object will
deallocate the underlying std::string data as well. Use
Self::as_view() to extract a view of the underlying data in order to
copy it elsewhere.
Implementations§
source§impl StringWrapper
impl StringWrapper
sourcepub const fn blank() -> Self
pub const fn blank() -> Self
Generate an instance without allocating any memory dynamically.
let s = re2::string::StringWrapper::blank();
assert_eq!(unsafe { s.as_view().as_str() }, "");sourcepub fn from_view(s: StringView<'_>) -> Self
pub fn from_view(s: StringView<'_>) -> Self
Generate an instance which copies the bytes from the argument s.
let s = re2::string::StringWrapper::from_view("asdf".into());
assert_eq!(unsafe { s.as_view().as_str() }, "asdf");sourcepub fn as_view(&self) -> StringView<'_>
pub fn as_view(&self) -> StringView<'_>
Generate a read-only view of the dynamically allocated memory.
let s = re2::string::StringWrapper::from_view("asdf".into());
assert_eq!(unsafe { s.as_view().as_str() }, "asdf");sourcepub fn as_mut_view(&mut self) -> StringMut<'_>
pub fn as_mut_view(&mut self) -> StringMut<'_>
Generate a mutable view of the dynamically allocated memory.
let mut s = re2::string::StringWrapper::from_view("asdf".into());
s.as_mut_view().as_mut_slice()[2] = b'e';
assert_eq!(unsafe { s.as_view().as_str() }, "asef");sourcepub fn resize(&mut self, len: usize)
pub fn resize(&mut self, len: usize)
Resize the underlying std::string storage.
If the new length is larger than the old, this method will pad the result with null bytes.
let mut s = re2::string::StringWrapper::from_view("asdf".into());
assert_eq!(unsafe { s.as_view().as_str() }, "asdf");
s.resize(2);
assert_eq!(unsafe { s.as_view().as_str() }, "as");
s.resize(6);
assert_eq!(unsafe { s.as_view().as_str() }, "as\0\0\0\0");
s.resize(0);
assert_eq!(unsafe { s.as_view().as_str() }, "");sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Deallocate the underlying string storage.
This method is idempotent and can be called multiple times in a row.
let mut s = re2::string::StringWrapper::from_view("asdf".into());
assert_eq!(unsafe { s.as_view().as_str() }, "asdf");
s.clear();
assert_eq!(unsafe { s.as_view().as_str() }, "");