mirror of
https://git.proxmox.com/git/perlmod
synced 2025-10-04 13:22:15 +00:00
move substr_from_str_slice from ScalarRef to Scalar
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
67c90f2d75
commit
24ca477715
@ -117,6 +117,34 @@ impl Scalar {
|
||||
pub fn new_pointer<T>(s: *mut T) -> Self {
|
||||
Self::new_bytes(&(s as usize).to_ne_bytes())
|
||||
}
|
||||
|
||||
/// Try to produce a substring from an existing "base" value and a `&str`.
|
||||
///
|
||||
/// Returns `None` if `substr` is not part of `value`.
|
||||
pub fn substr_from_str_slice(value: &ScalarRef, substr: &str) -> Result<Option<Scalar>, Error> {
|
||||
let value_bytes = value.pv_bytes();
|
||||
let value_beg = value_bytes.as_ptr() as usize;
|
||||
let value_end = value_beg + value_bytes.len();
|
||||
let value_range = value_beg..value_end;
|
||||
|
||||
let str_bytes = substr.as_bytes();
|
||||
let str_beg = str_bytes.as_ptr() as usize;
|
||||
let str_end = str_beg + str_bytes.len();
|
||||
if !value_range.contains(&str_beg) || !value_range.contains(&str_end) {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
// we just checked the ranges:
|
||||
let start = unsafe { str_bytes.as_ptr().offset_from(value_bytes.as_ptr()) as usize };
|
||||
Ok(Some(unsafe {
|
||||
Scalar::from_raw_move(ffi::RSPL_substr(
|
||||
ffi::RSPL_SvREFCNT_inc(value.sv()),
|
||||
start,
|
||||
substr.len(),
|
||||
))
|
||||
}))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Clone for Scalar {
|
||||
@ -411,33 +439,6 @@ impl ScalarRef {
|
||||
})
|
||||
}
|
||||
|
||||
/// Try to produce a substring from an existing "base" value and a `&str`.
|
||||
///
|
||||
/// Returns `None` if `substr` is not part of `value`.
|
||||
pub fn substr_from_str_slice(value: &ScalarRef, substr: &str) -> Result<Option<Scalar>, Error> {
|
||||
let value_bytes = value.pv_bytes();
|
||||
let value_beg = value_bytes.as_ptr() as usize;
|
||||
let value_end = value_beg + value_bytes.len();
|
||||
let value_range = value_beg..value_end;
|
||||
|
||||
let str_bytes = substr.as_bytes();
|
||||
let str_beg = str_bytes.as_ptr() as usize;
|
||||
let str_end = str_beg + str_bytes.len();
|
||||
if !value_range.contains(&str_beg) || !value_range.contains(&str_end) {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
// we just checked the ranges:
|
||||
let start = unsafe { str_bytes.as_ptr().offset_from(value_bytes.as_ptr()) as usize };
|
||||
Ok(Some(unsafe {
|
||||
Scalar::from_raw_move(ffi::RSPL_substr(
|
||||
ffi::RSPL_SvREFCNT_inc(value.sv()),
|
||||
start,
|
||||
substr.len(),
|
||||
))
|
||||
}))
|
||||
}
|
||||
|
||||
/// Attach magic to this value.
|
||||
///
|
||||
/// # Safety
|
||||
|
Loading…
Reference in New Issue
Block a user