Skip to content

std::String::slice<Char, Traits>

slice<Char, Traits>

Represents a slice of a string with specified character type and traits, similar to std::string_view in C++

Template Parameters

  • Char
  • Traits

Type Aliases

  • view_t = libcxx::basic_string_view<CharT>
  • char_traits = Traits
  • char_t = CharT
  • slice_t = slice<CharT>
  • size_t = usize
  • slice_vec = vec<slice_t>
  • char_vec = vec<CharT>

Enums

Operation

Operation type for string operations

Values

  • Keep: Keep the delimiter in the result
  • Remove: Remove the delimiter from the result

Private Members

len

  • Type: usize
  • Description: Length of the slice

data

  • Type: view_t
  • Description: String view data
  • Default: {}

Constructors

slice

Default constructor

slice() noexcept

slice

Copy constructor

slice(const slice& other) noexcept

slice

Move constructor

slice(slice&& other) noexcept

slice

Constructor from C-style string

slice(const CharT* str) noexcept

slice

Constructor from C-style string with size

slice(const CharT* str, size: size_t) noexcept

slice

Constructor from string view

slice(view: view_t) noexcept

slice

Constructor from character vector

slice(vec: char_vec&) noexcept

slice

Constructor from character vector (move)

slice(vec: char_vec&&) noexcept

Operators

operator view_t

Conversion to string view

op view_t(self) -> view_t

operator==

Equality comparison

op ==(self, const slice& other) -> bool

operator!=

Inequality comparison

op !=(self, const slice& other) -> bool

operator<

Less than comparison

op <(self, const slice& other) -> bool

operator>

Greater than comparison

op >(self, const slice& other) -> bool

operator<=

Less than or equal comparison

op <=(self, const slice& other) -> bool

operator>=

Greater than or equal comparison

op >=(self, const slice& other) -> bool

operator in

Contains operator for slice

op in(self, needle: slice&) -> bool

operator in

Contains operator for character

op in(self, chr: CharT&) -> bool

operator[]

Access character at index

op [``](self, index: usize) -> CharT

Methods

size

Returns the length of the slice’s data, essential for bounds checking and iteration

size(self) -> size_t

exchange

Exchanges content with another slice

exchange(self, other: slice&) -> void

replace

Replaces content with another slice

replace(self, other: slice&) -> void

replace

Replaces content with C-style string

replace(self, str: CharT*, size: usize) -> void

raw

Returns a pointer to the slice’s underlying data, essential for direct access

raw(self) -> const CharT*

is_empty

Indicates whether the slice has no data, crucial for control flow

is_empty(self) -> bool

subslice

Creates a view into a portion of the slice, vital for safe substring operations

subslice(self, pos: usize, len: usize) -> slice

l_strip

Strips whitespace from the left

l_strip(self, delim: char_vec& = {' ', '\t', '\n', '\r'}) -> slice

r_strip

Strips whitespace from the right

r_strip(self, delim: char_vec& = {' ', '\t', '\n', '\r'}) -> slice

strip

Strips whitespace from both ends

strip(self, delim: char_vec& = {' ', '\t', '\n', '\r'}) -> slice

length

Returns the length of the slice

length(self) -> usize

starts_with

Checks if slice starts with needle

starts_with(self, needle: slice&) -> bool

ends_with

Checks if slice ends with needle

ends_with(self, needle: slice&) -> bool

contains

Checks if slice contains needle

contains(self, needle: slice&) -> bool

contains

Checks if slice contains character

contains(self, chr: CharT&) -> bool

compare

Compares two slices lexicographically

compare(self, other: slice&) -> isize

split_lines

Returns a vector of line views, necessary for line-based processing

split_lines(self) -> slice_vec

split

Returns a vector of views, necessary for delimiter-based processing

split(self, delim: slice&, op: Operation = Operation::Remove) -> slice_vec

lfind

Finds first occurrence of needle

lfind(self, needle: slice&) -> usize?

rfind

Finds last occurrence of needle

rfind(self, needle: slice&) -> usize?

find_first_of

Finds first occurrence of any character in needle

find_first_of(self, needle: slice&) -> usize?

find_last_of

Finds last occurrence of any character in needle

find_last_of(self, needle: slice&) -> usize?

find_first_not_of

Finds first character not in needle

find_first_not_of(self, needle: slice&) -> usize?

find_last_not_of

Finds last character not in needle

find_last_not_of(self, needle: slice&) -> usize?