rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] rust: str: add {make,to}_{upper,lower}case() to CString
@ 2024-01-30  2:35 Danilo Krummrich
  2024-02-13 14:12 ` Danilo Krummrich
  2024-02-13 14:19 ` Alice Ryhl
  0 siblings, 2 replies; 4+ messages in thread
From: Danilo Krummrich @ 2024-01-30  2:35 UTC (permalink / raw)
  To: ojeda, alex.gaynor, wedsonaf, boqun.feng, gary, bjorn3_gh,
	benno.lossin, a.hindborg, aliceryhl
  Cc: rust-for-linux, linux-kernel, Danilo Krummrich

Add functions to convert a CString to upper- / lowercase, either
in-place or by creating a copy of the original CString.

Naming followes the one from the Rust stdlib, where functions starting
with 'to' create a copy and functions starting with 'make' perform an
in-place conversion.

This is required by the Nova project (GSP only Rust successor of
Nouveau) to convert stringified enum values (representing different GPU
chipsets) to strings in order to generate the corresponding firmware
paths. See also [1].

[1] https://rust-for-linux.zulipchat.com/#narrow/stream/288089-General/topic/String.20manipulation.20in.20kernel.20Rust

Signed-off-by: Danilo Krummrich <dakr@redhat.com>
---
Changes in V2:
  - expand commit message mentioning the use case
  - expand function doc comments to match the ones from Rust's stdlib
  - rename to_* to make_* and add the actual to_* implementations
---
 rust/kernel/str.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)

diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index 7d848b83add4..758bb70d98e9 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -581,6 +581,66 @@ pub fn try_from_fmt(args: fmt::Arguments<'_>) -> Result<Self, Error> {
         // exist in the buffer.
         Ok(Self { buf })
     }
+
+    /// Converts this CString to its ASCII lower case equivalent in-place.
+    ///
+    /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
+    /// but non-ASCII letters are unchanged.
+    ///
+    /// To return a new lowercased value without modifying the existing one, use
+    /// [`to_ascii_lowercase()`].
+    ///
+    /// [`to_ascii_lowercase()`]: #method.to_ascii_lowercase
+    pub fn make_ascii_lowercase(&mut self) {
+        self.buf.make_ascii_lowercase();
+    }
+
+    /// Converts this CString to its ASCII upper case equivalent in-place.
+    ///
+    /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
+    /// but non-ASCII letters are unchanged.
+    ///
+    /// To return a new uppercased value without modifying the existing one, use
+    /// [`to_ascii_uppercase()`].
+    ///
+    /// [`to_ascii_uppercase()`]: #method.to_ascii_uppercase
+    pub fn make_ascii_uppercase(&mut self) {
+        self.buf.make_ascii_uppercase();
+    }
+
+    /// 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);
+    }
 }
 
 impl Deref for CString {

base-commit: 41bccc98fb7931d63d03f326a746ac4d429c1dd3
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] rust: str: add {make,to}_{upper,lower}case() to CString
  2024-01-30  2:35 [PATCH v2] rust: str: add {make,to}_{upper,lower}case() to CString Danilo Krummrich
@ 2024-02-13 14:12 ` Danilo Krummrich
  2024-02-13 14:19 ` Alice Ryhl
  1 sibling, 0 replies; 4+ messages in thread
From: Danilo Krummrich @ 2024-02-13 14:12 UTC (permalink / raw)
  To: ojeda, alex.gaynor, wedsonaf, boqun.feng, gary, bjorn3_gh,
	benno.lossin, a.hindborg, aliceryhl
  Cc: rust-for-linux, linux-kernel

On 1/30/24 03:35, Danilo Krummrich wrote:
> Add functions to convert a CString to upper- / lowercase, either
> in-place or by creating a copy of the original CString.
> 
> Naming followes the one from the Rust stdlib, where functions starting
> with 'to' create a copy and functions starting with 'make' perform an
> in-place conversion.
> 
> This is required by the Nova project (GSP only Rust successor of
> Nouveau) to convert stringified enum values (representing different GPU
> chipsets) to strings in order to generate the corresponding firmware
> paths. See also [1].
> 
> [1] https://rust-for-linux.zulipchat.com/#narrow/stream/288089-General/topic/String.20manipulation.20in.20kernel.20Rust
> 
> Signed-off-by: Danilo Krummrich <dakr@redhat.com>

Ping.

> ---
> Changes in V2:
>    - expand commit message mentioning the use case
>    - expand function doc comments to match the ones from Rust's stdlib
>    - rename to_* to make_* and add the actual to_* implementations
> ---
>   rust/kernel/str.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 60 insertions(+)
> 
> diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
> index 7d848b83add4..758bb70d98e9 100644
> --- a/rust/kernel/str.rs
> +++ b/rust/kernel/str.rs
> @@ -581,6 +581,66 @@ pub fn try_from_fmt(args: fmt::Arguments<'_>) -> Result<Self, Error> {
>           // exist in the buffer.
>           Ok(Self { buf })
>       }
> +
> +    /// Converts this CString to its ASCII lower case equivalent in-place.
> +    ///
> +    /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
> +    /// but non-ASCII letters are unchanged.
> +    ///
> +    /// To return a new lowercased value without modifying the existing one, use
> +    /// [`to_ascii_lowercase()`].
> +    ///
> +    /// [`to_ascii_lowercase()`]: #method.to_ascii_lowercase
> +    pub fn make_ascii_lowercase(&mut self) {
> +        self.buf.make_ascii_lowercase();
> +    }
> +
> +    /// Converts this CString to its ASCII upper case equivalent in-place.
> +    ///
> +    /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
> +    /// but non-ASCII letters are unchanged.
> +    ///
> +    /// To return a new uppercased value without modifying the existing one, use
> +    /// [`to_ascii_uppercase()`].
> +    ///
> +    /// [`to_ascii_uppercase()`]: #method.to_ascii_uppercase
> +    pub fn make_ascii_uppercase(&mut self) {
> +        self.buf.make_ascii_uppercase();
> +    }
> +
> +    /// 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);
> +    }
>   }
>   
>   impl Deref for CString {
> 
> base-commit: 41bccc98fb7931d63d03f326a746ac4d429c1dd3


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] rust: str: add {make,to}_{upper,lower}case() to CString
  2024-01-30  2:35 [PATCH v2] rust: str: add {make,to}_{upper,lower}case() to CString Danilo Krummrich
  2024-02-13 14:12 ` Danilo Krummrich
@ 2024-02-13 14:19 ` Alice Ryhl
  2024-02-14 17:20   ` Danilo Krummrich
  1 sibling, 1 reply; 4+ messages in thread
From: Alice Ryhl @ 2024-02-13 14:19 UTC (permalink / raw)
  To: Danilo Krummrich
  Cc: ojeda, alex.gaynor, wedsonaf, boqun.feng, gary, bjorn3_gh,
	benno.lossin, a.hindborg, rust-for-linux, linux-kernel

On Tue, Jan 30, 2024 at 3:38 AM Danilo Krummrich <dakr@redhat.com> wrote:
>
> Add functions to convert a CString to upper- / lowercase, either
> in-place or by creating a copy of the original CString.
>
> Naming followes the one from the Rust stdlib, where functions starting
> with 'to' create a copy and functions starting with 'make' perform an
> in-place conversion.
>
> This is required by the Nova project (GSP only Rust successor of
> Nouveau) to convert stringified enum values (representing different GPU
> chipsets) to strings in order to generate the corresponding firmware
> paths. See also [1].
>
> [1] https://rust-for-linux.zulipchat.com/#narrow/stream/288089-General/topic/String.20manipulation.20in.20kernel.20Rust
>
> Signed-off-by: Danilo Krummrich <dakr@redhat.com>

Not a deal-breaker, but this comment of mine still applies:

https://lore.kernel.org/rust-for-linux/CAH5fLgiwRDcyaxbcUNY8M1c_w11vkCWyRfqVVrN9Sgc7XYT0xw@mail.gmail.com/

Alice

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] rust: str: add {make,to}_{upper,lower}case() to CString
  2024-02-13 14:19 ` Alice Ryhl
@ 2024-02-14 17:20   ` Danilo Krummrich
  0 siblings, 0 replies; 4+ messages in thread
From: Danilo Krummrich @ 2024-02-14 17:20 UTC (permalink / raw)
  To: Alice Ryhl
  Cc: ojeda, alex.gaynor, wedsonaf, boqun.feng, gary, bjorn3_gh,
	benno.lossin, a.hindborg, rust-for-linux, linux-kernel

On 2/13/24 15:19, Alice Ryhl wrote:
> On Tue, Jan 30, 2024 at 3:38 AM Danilo Krummrich <dakr@redhat.com> wrote:
>>
>> Add functions to convert a CString to upper- / lowercase, either
>> in-place or by creating a copy of the original CString.
>>
>> Naming followes the one from the Rust stdlib, where functions starting
>> with 'to' create a copy and functions starting with 'make' perform an
>> in-place conversion.
>>
>> This is required by the Nova project (GSP only Rust successor of
>> Nouveau) to convert stringified enum values (representing different GPU
>> chipsets) to strings in order to generate the corresponding firmware
>> paths. See also [1].
>>
>> [1] https://rust-for-linux.zulipchat.com/#narrow/stream/288089-General/topic/String.20manipulation.20in.20kernel.20Rust
>>
>> Signed-off-by: Danilo Krummrich <dakr@redhat.com>
> 
> Not a deal-breaker, but this comment of mine still applies:
> 
> https://lore.kernel.org/rust-for-linux/CAH5fLgiwRDcyaxbcUNY8M1c_w11vkCWyRfqVVrN9Sgc7XYT0xw@mail.gmail.com/

I missed your mail, gonna send a v3.

- Danilo

> 
> Alice
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-02-14 17:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-30  2:35 [PATCH v2] rust: str: add {make,to}_{upper,lower}case() to CString Danilo Krummrich
2024-02-13 14:12 ` Danilo Krummrich
2024-02-13 14:19 ` Alice Ryhl
2024-02-14 17:20   ` Danilo Krummrich

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).