use std;
use std::borrow::Cow;
use std::os::raw::c_char;
use std::{mem, str};
use super::PyStringData;
use err::{PyErr, PyResult};
use ffi;
use instance::{Py, PyObjectWithToken};
use object::PyObject;
use objects::PyObjectRef;
use python::{Python, ToPyPointer};
#[repr(transparent)]
pub struct PyString(PyObject);
pyobject_native_type!(PyString, ffi::PyUnicode_Type, ffi::PyUnicode_Check);
pub use PyString as PyUnicode;
#[repr(transparent)]
pub struct PyBytes(PyObject);
pyobject_native_type!(PyBytes, ffi::PyBytes_Type, ffi::PyBytes_Check);
impl PyString {
pub fn new(_py: Python, s: &str) -> Py<PyString> {
let ptr = s.as_ptr() as *const c_char;
let len = s.len() as ffi::Py_ssize_t;
unsafe { Py::from_owned_ptr_or_panic(ffi::PyUnicode_FromStringAndSize(ptr, len)) }
}
pub fn from_object<'p>(
src: &'p PyObjectRef,
encoding: &str,
errors: &str,
) -> PyResult<&'p PyString> {
unsafe {
src.py()
.from_owned_ptr_or_err::<PyString>(ffi::PyUnicode_FromEncodedObject(
src.as_ptr(),
encoding.as_ptr() as *const c_char,
errors.as_ptr() as *const c_char,
))
}
}
pub fn data(&self) -> PyStringData {
unsafe {
let mut size: ffi::Py_ssize_t = mem::uninitialized();
let data = ffi::PyUnicode_AsUTF8AndSize(self.0.as_ptr(), &mut size) as *const u8;
if data.is_null() {
PyErr::fetch(self.py()).print(self.py());
panic!("PyUnicode_AsUTF8AndSize failed");
}
PyStringData::Utf8(std::slice::from_raw_parts(data, size as usize))
}
}
pub fn to_string(&self) -> PyResult<Cow<str>> {
self.data().to_string(self.py())
}
pub fn to_string_lossy(&self) -> Cow<str> {
self.data().to_string_lossy()
}
}
impl PyBytes {
pub fn new(_py: Python, s: &[u8]) -> Py<PyBytes> {
let ptr = s.as_ptr() as *const c_char;
let len = s.len() as ffi::Py_ssize_t;
unsafe { Py::from_owned_ptr_or_panic(ffi::PyBytes_FromStringAndSize(ptr, len)) }
}
pub unsafe fn from_ptr(_py: Python, ptr: *const u8, len: usize) -> Py<PyBytes> {
Py::from_owned_ptr_or_panic(ffi::PyBytes_FromStringAndSize(
ptr as *const _,
len as isize,
))
}
pub fn data(&self) -> &[u8] {
unsafe {
let buffer = ffi::PyBytes_AsString(self.as_ptr()) as *const u8;
let length = ffi::PyBytes_Size(self.as_ptr()) as usize;
std::slice::from_raw_parts(buffer, length)
}
}
}
#[cfg(test)]
mod test {
use conversion::{FromPyObject, ToPyObject};
use instance::AsPyRef;
use python::Python;
#[test]
fn test_non_bmp() {
let gil = Python::acquire_gil();
let py = gil.python();
let s = "\u{1F30F}";
let py_string = s.to_object(py);
assert_eq!(s, py_string.extract::<String>(py).unwrap());
}
#[test]
fn test_extract_str() {
let gil = Python::acquire_gil();
let py = gil.python();
let s = "Hello Python";
let py_string = s.to_object(py);
let s2: &str = FromPyObject::extract(py_string.as_ref(py)).unwrap();
assert_eq!(s, s2);
}
}