Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions phper-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,6 @@ mod utils;

use proc_macro::TokenStream;

/// C style string end with '\0'.
///
/// # Examples
///
/// ```no_test
/// use std::ffi::CStr;
///
/// assert_eq!(c_str!("foo"), unsafe {
/// CStr::from_ptr("foo\0".as_ptr().cast())
/// });
/// ```
#[proc_macro]
pub fn c_str(input: TokenStream) -> TokenStream {
utils::c_str(input)
}

/// C style string end with '\0'.
///
/// # Examples
///
/// ```no_test
/// assert_eq!(c_str_ptr!("foo"), "foo\0".as_ptr().cast());
/// ```
#[proc_macro]
pub fn c_str_ptr(input: TokenStream) -> TokenStream {
utils::c_str_ptr(input)
}

/// PHP module entry, wrap the `phper::modules::Module` write operation.
///
/// # Examples
Expand Down
20 changes: 0 additions & 20 deletions phper-macros/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,3 @@
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Expr};

pub(crate) fn c_str(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as Expr);
let result = quote! {
unsafe { ::std::ffi::CStr::from_ptr(::core::concat!(#input, "\0").as_ptr().cast()) }
};
result.into()
}

pub(crate) fn c_str_ptr(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as Expr);
let result = quote! {
::core::concat!(#input, "\0").as_ptr() as *const ::std::os::raw::c_char
};
result.into()
}
25 changes: 0 additions & 25 deletions phper-macros/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,3 @@
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.

use phper_macros::*;
use std::ffi::CStr;

#[test]
fn test_c_str() {
assert_eq!(c_str!("foo"), unsafe {
CStr::from_ptr("foo\0".as_ptr().cast())
});

assert_eq!(
{
#[allow(unused_unsafe)]
unsafe {
c_str!("bar")
}
},
unsafe { CStr::from_ptr("bar\0".as_ptr().cast()) }
);
}

#[test]
fn test_c_str_ptr() {
assert_eq!(c_str_ptr!("foo"), "foo\0".as_ptr().cast());
}
10 changes: 2 additions & 8 deletions phper/src/ini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

//! Apis relate to [zend_ini_entry_def].

use crate::{c_str, sys::*};
use crate::sys::*;
use std::{
ffi::{c_int, CStr},
mem::zeroed,
Expand Down Expand Up @@ -100,13 +100,7 @@ impl FromIniValue for bool {
#[allow(clippy::useless_conversion)]
fn from_ini_value(name: &str) -> Self {
let s = <Option<&CStr>>::from_ini_value(name);
[
Some(c_str!("1")),
Some(c_str!("true")),
Some(c_str!("on")),
Some(c_str!("On")),
]
.contains(&s)
[Some(c"1"), Some(c"true"), Some(c"on"), Some(c"On")].contains(&s)
}
}

Expand Down
5 changes: 2 additions & 3 deletions phper/src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
//! Apis relate to [zend_module_entry].

use crate::{
c_str_ptr,
classes::{ClassEntity, InterfaceEntity},
constants::Constant,
errors::Throwable,
Expand Down Expand Up @@ -105,10 +104,10 @@ unsafe extern "C" fn module_info(zend_module: *mut zend_module_entry) {

php_info_print_table_start();
if !module.version.as_bytes().is_empty() {
php_info_print_table_row(2, c_str_ptr!("version"), module.version.as_ptr());
php_info_print_table_row(2, c"version", module.version.as_ptr());
}
if !module.author.as_bytes().is_empty() {
php_info_print_table_row(2, c_str_ptr!("authors"), module.author.as_ptr());
php_info_print_table_row(2, c"authors", module.author.as_ptr());
}
for (key, value) in &module.infos {
php_info_print_table_row(2, key.as_ptr(), value.as_ptr());
Expand Down
10 changes: 5 additions & 5 deletions phper/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

//! Apis relate to PHP types.

use crate::{c_str, sys::*};
use crate::sys::*;
use derive_more::From;
use std::{
ffi::CStr,
Expand Down Expand Up @@ -133,10 +133,10 @@ impl TypeInfo {
let t = get_base_type_by_raw(self.t);

if t == IS_UNDEF {
return c_str!("undef");
return c"undef";
}
if t == IS_REFERENCE {
return c_str!("reference");
return c"reference";
}

let s = zend_get_type_by_const(t as c_int);
Expand All @@ -145,10 +145,10 @@ impl TypeInfo {
// Compact with PHP7.
let bs = s.to_bytes();
if bs == b"boolean" {
return c_str!("bool");
return c"bool";
}
if bs == b"integer" {
return c_str!("int");
return c"int";
}

s
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/src/ini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// See the Mulan PSL v2 for more details.

use phper::{
c_str,
ini::{ini_get, Policy},
modules::Module,
};
Expand All @@ -33,7 +32,7 @@ pub fn integrate(module: &mut Module) {
assert_eq!(ini_get::<f64>("INTEGRATE_INI_DOUBLE"), 200.);
assert_eq!(
ini_get::<Option<&CStr>>("INTEGRATE_INI_STRING"),
Some(c_str!("something"))
Some(c"something")
);
Ok::<_, Infallible>(())
});
Expand Down
Loading