rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Danilo Krummrich <dakr@redhat.com>
To: Alice Ryhl <aliceryhl@google.com>
Cc: a.hindborg@samsung.com, alex.gaynor@gmail.com,
	benno.lossin@proton.me, bjorn3_gh@protonmail.com,
	boqun.feng@gmail.com, gary@garyguo.net,
	linux-kernel@vger.kernel.org, ojeda@kernel.org,
	rust-for-linux@vger.kernel.org, wedsonaf@gmail.com
Subject: Re: [PATCH v3] rust: str: add {make,to}_{upper,lower}case() to CString
Date: Fri, 16 Feb 2024 18:11:30 +0100	[thread overview]
Message-ID: <18a49662-3640-49c1-9587-db5cc9d62fbc@redhat.com> (raw)
In-Reply-To: <20240216165320.1820346-1-aliceryhl@google.com>

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
> 


  reply	other threads:[~2024-02-16 17:11 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-14 17:24 [PATCH v3] rust: str: add {make,to}_{upper,lower}case() to CString Danilo Krummrich
2024-02-14 19:27 ` Boqun Feng
2024-02-14 19:59   ` Alice Ryhl
2024-02-15  1:18     ` Boqun Feng
2024-02-15  9:38       ` Alice Ryhl
2024-02-15 16:51         ` Boqun Feng
2024-02-16  9:09           ` Alice Ryhl
2024-02-16 16:53 ` Alice Ryhl
2024-02-16 17:11   ` Danilo Krummrich [this message]
2024-02-16 19:25     ` Alice Ryhl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=18a49662-3640-49c1-9587-db5cc9d62fbc@redhat.com \
    --to=dakr@redhat.com \
    --cc=a.hindborg@samsung.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=wedsonaf@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).