Skip to content

Commit

Permalink
allows ZendStr to contain null bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
ju1ius committed Nov 26, 2022
1 parent a331213 commit ee38c99
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/types/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::{
borrow::Cow,
convert::TryFrom,
convert::{TryFrom, TryInto},
ffi::{CStr, CString},
fmt::Debug,
slice,
Expand Down Expand Up @@ -80,8 +80,14 @@ impl ZendStr {
///
/// let s = ZendStr::new("Hello, world!", false).unwrap();
/// ```
pub fn new(str: &str, persistent: bool) -> Result<ZBox<Self>> {
Ok(Self::from_c_str(&CString::new(str)?, persistent))
pub fn new(str: impl AsRef<[u8]>, persistent: bool) -> Result<ZBox<Self>> {
let s = str.as_ref();
unsafe {
let ptr = ext_php_rs_zend_string_init(s.as_ptr().cast(), s.len(), persistent)
.as_mut()
.expect("Failed to allocate memory for new Zend string");
Ok(ZBox::from_raw(ptr))
}
}

/// Creates a new Zend string from a [`CStr`].
Expand Down Expand Up @@ -171,8 +177,18 @@ impl ZendStr {
///
/// let s = ZendStr::new_interned("PHP", true);
/// ```
pub fn new_interned(str: &str, persistent: bool) -> Result<ZBox<Self>> {
Ok(Self::interned_from_c_str(&CString::new(str)?, persistent))
pub fn new_interned(str: impl AsRef<[u8]>, persistent: bool) -> Result<ZBox<Self>> {
let _lock = INTERNED_LOCK.lock();
let s = str.as_ref();
let len: u64 = s.len().try_into().map_err(|_| Error::IntegerOverflow)?;
unsafe {
let init_interned =
zend_string_init_interned.expect("`zend_string_init_interned` not ready");
let ptr = init_interned(s.as_ptr().cast(), len, persistent)
.as_mut()
.expect("Failed to allocate memory for new Zend string");
Ok(ZBox::from_raw(ptr))
}
}

/// Creates a new interned Zend string from a [`CStr`].
Expand Down

0 comments on commit ee38c99

Please sign in to comment.