[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <18a49662-3640-49c1-9587-db5cc9d62fbc@redhat.com>
Date: Fri, 16 Feb 2024 18:11:30 +0100
From: Danilo Krummrich <dakr@...hat.com>
To: Alice Ryhl <aliceryhl@...gle.com>
Cc: a.hindborg@...sung.com, alex.gaynor@...il.com, benno.lossin@...ton.me,
bjorn3_gh@...tonmail.com, boqun.feng@...il.com, gary@...yguo.net,
linux-kernel@...r.kernel.org, ojeda@...nel.org,
rust-for-linux@...r.kernel.org, wedsonaf@...il.com
Subject: Re: [PATCH v3] rust: str: add {make,to}_{upper,lower}case() to
CString
On 2/16/24 17:53, Alice Ryhl wrote:
>> + pub fn make_ascii_lowercase(&mut self) {
>> + self.0.make_ascii_lowercase();
>> + }
>
> It's important to note here that this doesn't remove or introduce NUL
> bytes.
>
> pub fn make_ascii_lowercase(&mut self) {
> // INVARIANT: This doesn't introduce or remove NUL bytes in the c
> // string.
> self.0.make_ascii_lowercase();
> }
>
> Ditto for make_ascii_uppercase. (But not the to_* methods.)
>
>> + /// Returns a copy of this [`CString`] where each character is mapped to its
>> + /// ASCII lower case equivalent.
>> + ///
>> + /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
>> + /// but non-ASCII letters are unchanged.
>> + ///
>> + /// To lowercase the value in-place, use [`make_ascii_lowercase`].
>> + ///
>> + /// [`make_ascii_lowercase`]: str::make_ascii_lowercase
>> + pub fn to_ascii_lowercase(&self) -> Result<CString, AllocError> {
>> + let mut s = (*self).to_cstring()?;
>> +
>> + s.make_ascii_lowercase();
>> +
>> + return Ok(s);
>> + }
>> +
>> + /// Returns a copy of this [`CString`] where each character is mapped to its
>> + /// ASCII upper case equivalent.
>> + ///
>> + /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
>> + /// but non-ASCII letters are unchanged.
>> + ///
>> + /// To uppercase the value in-place, use [`make_ascii_uppercase`].
>> + ///
>> + /// [`make_ascii_uppercase`]: str::make_ascii_uppercase
>> + pub fn to_ascii_uppercase(&self) -> Result<CString, AllocError> {
>> + let mut s = (*self).to_cstring()?;
>> +
>> + s.make_ascii_uppercase();
>> +
>> + return Ok(s);
>> + }
>
> Please move these to `CStr` as well.
That would result into two copies if I actually want a CString, wouldn't it?
Also, what would be the use case? And even if someone wants to have a CStr
again, couldn't we just deref the resulting CString?
- Danilo
>
>> +impl DerefMut for CString {
>> + fn deref_mut(&mut self) -> &mut Self::Target {
>> + unsafe { CStr::from_bytes_with_nul_unchecked_mut(&mut *self.buf) }
>> + }
>> +}
>
> Needs a safety comment.
>
> impl DerefMut for CString {
> fn deref_mut(&mut self) -> &mut Self::Target {
> // SAFETY: A `CString` is always NUL-terminated and contains no
> // other NUL bytes.
> unsafe { CStr::from_bytes_with_nul_unchecked_mut(&mut *self.buf) }
> }
> }
>
> Alice
>
Powered by blists - more mailing lists