Message ID | 20250109-module-params-v3-v4-2-c208bcfbe11f@kernel.org (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | rust: extend `module!` macro with integer parameter support | expand |
On Thu, Jan 9, 2025 at 11:56 AM Andreas Hindborg <a.hindborg@kernel.org> wrote: > > Implement `strip_prefix` for `BStr` by deferring to `slice::strip_prefix` > on the underlying `&[u8]`. > > Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
On Thu, 09 Jan 2025 11:54:57 +0100 Andreas Hindborg <a.hindborg@kernel.org> wrote: > Implement `strip_prefix` for `BStr` by deferring to `slice::strip_prefix` > on the underlying `&[u8]`. > > Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> Reviewed-by: Gary Guo <gary@garyguo.net> > > --- > > It is also possible to get this method by implementing > `core::slice::SlicePattern` for `BStr`. `SlicePattern` is unstable, so this > seems more reasonable. > --- > rust/kernel/str.rs | 16 ++++++++++++++++ > 1 file changed, 16 insertions(+) > > diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs > index c441acf76ebd1f14919b6d233edffbbbbf944619..9c446ff1ad7adba7ca09a5ae9df00fd369a32899 100644 > --- a/rust/kernel/str.rs > +++ b/rust/kernel/str.rs > @@ -31,6 +31,22 @@ pub const fn from_bytes(bytes: &[u8]) -> &Self { > // SAFETY: `BStr` is transparent to `[u8]`. > unsafe { &*(bytes as *const [u8] as *const BStr) } > } > + > + /// Strip a prefix from `self`. Delegates to [`slice::strip_prefix`]. > + /// > + /// # Example > + /// ``` > + /// use kernel::b_str; > + /// assert_eq!(Some(b_str!("bar")), b_str!("foobar").strip_prefix(b_str!("foo"))); > + /// assert_eq!(None, b_str!("foobar").strip_prefix(b_str!("bar"))); > + /// assert_eq!(Some(b_str!("foobar")), b_str!("foobar").strip_prefix(b_str!(""))); > + /// assert_eq!(Some(b_str!("")), b_str!("foobar").strip_prefix(b_str!("foobar"))); > + /// ``` > + pub fn strip_prefix(&self, pattern: &Self) -> Option<&BStr> { > + self.deref() > + .strip_prefix(pattern.deref()) > + .map(Self::from_bytes) > + } > } > > impl fmt::Display for BStr { >
diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index c441acf76ebd1f14919b6d233edffbbbbf944619..9c446ff1ad7adba7ca09a5ae9df00fd369a32899 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -31,6 +31,22 @@ pub const fn from_bytes(bytes: &[u8]) -> &Self { // SAFETY: `BStr` is transparent to `[u8]`. unsafe { &*(bytes as *const [u8] as *const BStr) } } + + /// Strip a prefix from `self`. Delegates to [`slice::strip_prefix`]. + /// + /// # Example + /// ``` + /// use kernel::b_str; + /// assert_eq!(Some(b_str!("bar")), b_str!("foobar").strip_prefix(b_str!("foo"))); + /// assert_eq!(None, b_str!("foobar").strip_prefix(b_str!("bar"))); + /// assert_eq!(Some(b_str!("foobar")), b_str!("foobar").strip_prefix(b_str!(""))); + /// assert_eq!(Some(b_str!("")), b_str!("foobar").strip_prefix(b_str!("foobar"))); + /// ``` + pub fn strip_prefix(&self, pattern: &Self) -> Option<&BStr> { + self.deref() + .strip_prefix(pattern.deref()) + .map(Self::from_bytes) + } } impl fmt::Display for BStr {
Implement `strip_prefix` for `BStr` by deferring to `slice::strip_prefix` on the underlying `&[u8]`. Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> --- It is also possible to get this method by implementing `core::slice::SlicePattern` for `BStr`. `SlicePattern` is unstable, so this seems more reasonable. --- rust/kernel/str.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)