Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add PyString::chars #2451

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `PyCode` and `PyFrame` high level objects. [#2408](https://github.com/PyO3/pyo3/pull/2408)
- Add FFI definitions `Py_fstring_input`, `sendfunc`, and `_PyErr_StackItem`. [#2423](https://github.com/PyO3/pyo3/pull/2423)
- Add `PyDateTime::new_with_fold`, `PyTime::new_with_fold`, `PyTime::get_fold`, `PyDateTime::get_fold` for PyPy. [#2428](https://github.com/PyO3/pyo3/pull/2428)
- Add `PyString::chars` [#2451](https://github.com/PyO3/pyo3/pull/2451)

### Changed

Expand Down
57 changes: 54 additions & 3 deletions src/types/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
use crate::exceptions::PyUnicodeDecodeError;
use crate::types::PyBytes;
use crate::{
ffi, AsPyPointer, FromPyObject, IntoPy, Py, PyAny, PyObject, PyResult, PyTryFrom, Python,
ToPyObject,
exceptions, ffi, AsPyPointer, FromPyObject, IntoPy, Py, PyAny, PyObject, PyResult, PyTryFrom,
Python, ToPyObject,
};
use std::borrow::Cow;
use std::ffi::CStr;
use std::os::raw::c_char;
use std::str;

Expand Down Expand Up @@ -69,7 +70,6 @@ impl<'a> PyStringData<'a> {
/// C APIs that skip input validation (like `PyUnicode_FromKindAndData`) and should
/// never occur for strings that were created from Python code.
pub fn to_string(self, py: Python<'_>) -> PyResult<Cow<'a, str>> {
use std::ffi::CStr;
match self {
Self::Ucs1(data) => match str::from_utf8(data) {
Ok(s) => Ok(Cow::Borrowed(s)),
Expand Down Expand Up @@ -227,6 +227,29 @@ impl PyString {
}
}

/// Returns an iterator over the PyString.
///
/// Does not allocate anything (python or rust heap).
pub fn chars(&self) -> impl ExactSizeIterator<Item = PyResult<char>> + '_ {
unsafe {
let len = ffi::PyUnicode_GetLength(self.as_ptr());
(0..len).map(move |i| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the implementation is based on indexing, would it make sense to expose this a the interface as well? Something like char_at(&self, index: usize) -> Option<char> so that the iterator can be produced on the outside?

let c = ffi::PyUnicode_ReadChar(self.as_ptr(), i);
match char::from_u32(c) {
Some(c) => Ok(c),
None => Err(exceptions::PyUnicodeDecodeError::new(
self.py(),
CStr::from_bytes_with_nul(b"utf-8\0").unwrap(),
&c.to_ne_bytes(),
i as usize..(i + 1) as usize,
CStr::from_bytes_with_nul(b"invalid codepoint\0").unwrap(),
)?
.into()),
}
})
}
}

/// Obtains the raw data backing the Python string.
///
/// If the Python string object was created through legacy APIs, its internal storage format
Expand Down Expand Up @@ -620,6 +643,34 @@ mod tests {
});
}

#[test]
fn test_string_chars() {
Python::with_gil(|py| {
let s: &PyString = PyString::new(py, "哈哈🐈");
let result = s.chars().collect::<PyResult<String>>();
assert_eq!(result.unwrap(), "哈哈🐈");
});
}

#[test]
fn test_string_chars_ucs4_invalid() {
Python::with_gil(|py| {
// U+20000 (valid) & U+d800 (never valid)
let buffer = b"\x00\x00\x02\x00\x00\xd8\x00\x00\x00\x00\x00\x00";
let ptr = unsafe {
crate::ffi::PyUnicode_FromKindAndData(
crate::ffi::PyUnicode_4BYTE_KIND as _,
buffer.as_ptr() as *const _,
2,
)
};
assert!(!ptr.is_null());
let s: &PyString = unsafe { py.from_owned_ptr(ptr) };
let err = s.chars().collect::<PyResult<String>>().unwrap_err();
assert!(err.get_type(py).is(PyUnicodeDecodeError::type_object(py)));
});
}

#[test]
fn test_intern_string() {
Python::with_gil(|py| {
Expand Down