nstd_core_str_substr

Function nstd_core_str_substr 

Source
#[unsafe(no_mangle)]
pub const unsafe extern "C" fn nstd_core_str_substr( str: &NSTDStr, range: NSTDURange, ) -> NSTDOptionalStr
Available on crate feature core only.
Expand description

Creates a substring of an existing string slice.

§Parameters:

  • const NSTDStr *str - The string slice to create the new substring from.

  • NSTDURange range - The bounds of the new substring (indexed by bytes).

§Returns

NSTDOptionalStr substr - The new substring on success, or a “none” variant if the result is not valid UTF-8.

§Panics

This operation can panic under the following circumstances:

  • range.start is greater than range.end.

  • range.end is greater than str.len.

§Safety

str’s data must be valid for reads of at least str.len consecutive bytes.

§Example

use nstd_sys::core::{
    range::NSTDURange,
    str::{nstd_core_str_byte_len, nstd_core_str_from_raw_cstr, nstd_core_str_substr},
};

let s_str = "33marrow\0";
unsafe {
    let str = nstd_core_str_from_raw_cstr(s_str.as_ptr().cast()).unwrap();
    let range = NSTDURange {
        start: 2,
        end: nstd_core_str_byte_len(&str),
    };
    let marrow = nstd_core_str_substr(&str, range).unwrap();
    assert!(nstd_core_str_byte_len(&marrow) == 6);
}