Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit cc584d5

Browse files
committed
ascii methods on osstr
1 parent 45416cd commit cc584d5

File tree

4 files changed

+233
-6
lines changed

4 files changed

+233
-6
lines changed

src/libstd/ffi/os_str.rs

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,149 @@ impl OsStr {
698698
fn bytes(&self) -> &[u8] {
699699
unsafe { &*(&self.inner as *const _ as *const [u8]) }
700700
}
701+
702+
/// Converts this string to its ASCII lower case equivalent in-place.
703+
///
704+
/// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
705+
/// but non-ASCII letters are unchanged.
706+
///
707+
/// To return a new lowercased value without modifying the existing one, use
708+
/// [`to_ascii_lowercase`].
709+
///
710+
/// [`to_ascii_lowercase`]: #method.to_ascii_lowercase
711+
///
712+
/// # Examples
713+
///
714+
/// ```
715+
/// #![feature(osstring_ascii)]
716+
/// use std::ffi::OsString;
717+
///
718+
/// let mut s = OsString::from("GRÜßE, JÜRGEN ❤");
719+
///
720+
/// s.make_ascii_lowercase();
721+
///
722+
/// assert_eq!("grÜße, jÜrgen ❤", s);
723+
/// ```
724+
#[unstable(feature = "osstring_ascii", issue = "none")]
725+
pub fn make_ascii_lowercase(&mut self) {
726+
self.inner.make_ascii_lowercase()
727+
}
728+
729+
/// Converts this string to its ASCII upper case equivalent in-place.
730+
///
731+
/// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
732+
/// but non-ASCII letters are unchanged.
733+
///
734+
/// To return a new uppercased value without modifying the existing one, use
735+
/// [`to_ascii_uppercase`].
736+
///
737+
/// [`to_ascii_uppercase`]: #method.to_ascii_uppercase
738+
///
739+
/// # Examples
740+
///
741+
/// ```
742+
/// #![feature(osstring_ascii)]
743+
/// use std::ffi::OsString;
744+
///
745+
/// let mut s = OsString::from("Grüße, Jürgen ❤");
746+
///
747+
/// s.make_ascii_uppercase();
748+
///
749+
/// assert_eq!("GRüßE, JüRGEN ❤", s);
750+
/// ```
751+
#[unstable(feature = "osstring_ascii", issue = "none")]
752+
pub fn make_ascii_uppercase(&mut self) {
753+
self.inner.make_ascii_uppercase()
754+
}
755+
756+
/// Returns a copy of this string where each character is mapped to its
757+
/// ASCII lower case equivalent.
758+
///
759+
/// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
760+
/// but non-ASCII letters are unchanged.
761+
///
762+
/// To lowercase the value in-place, use [`make_ascii_lowercase`].
763+
///
764+
/// # Examples
765+
///
766+
/// ```
767+
/// #![feature(osstring_ascii)]
768+
/// use std::ffi::OsString;
769+
/// let s = OsString::from("Grüße, Jürgen ❤");
770+
///
771+
/// assert_eq!("grüße, jürgen ❤", s.to_ascii_lowercase());
772+
/// ```
773+
///
774+
/// [`make_ascii_lowercase`]: #method.make_ascii_lowercase
775+
#[unstable(feature = "osstring_ascii", issue = "none")]
776+
pub fn to_ascii_lowercase(&self) -> OsString {
777+
// OsString::from_inner(Buf::from_inner(self.inner.inner.to_ascii_lowercase()))
778+
OsString::from_inner(self.inner.to_ascii_lowercase())
779+
}
780+
781+
/// Returns a copy of this string where each character is mapped to its
782+
/// ASCII upper case equivalent.
783+
///
784+
/// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
785+
/// but non-ASCII letters are unchanged.
786+
///
787+
/// To uppercase the value in-place, use [`make_ascii_uppercase`].
788+
///
789+
/// # Examples
790+
///
791+
/// ```
792+
/// #![feature(osstring_ascii)]
793+
/// use std::ffi::OsString;
794+
/// let s = OsString::from("Grüße, Jürgen ❤");
795+
///
796+
/// assert_eq!("GRüßE, JüRGEN ❤", s.to_ascii_uppercase());
797+
/// ```
798+
///
799+
/// [`make_ascii_uppercase`]: #method.make_ascii_uppercase
800+
#[unstable(feature = "osstring_ascii", issue = "none")]
801+
pub fn to_ascii_uppercase(&self) -> OsString {
802+
// OsString::from_inner(Buf::from_inner(self.inner.inner.to_ascii_lowercase()))
803+
OsString::from_inner(self.inner.to_ascii_uppercase())
804+
}
805+
806+
/// Checks if all characters in this string are within the ASCII range.
807+
///
808+
/// # Examples
809+
///
810+
/// ```
811+
/// #![feature(osstring_ascii)]
812+
/// use std::ffi::OsString;
813+
///
814+
/// let ascii = OsString::from("hello!\n");
815+
/// let non_ascii = OsString::from("Grüße, Jürgen ❤");
816+
///
817+
/// assert!(ascii.is_ascii());
818+
/// assert!(!non_ascii.is_ascii());
819+
/// ```
820+
#[unstable(feature = "osstring_ascii", issue = "none")]
821+
pub fn is_ascii(&self) -> bool {
822+
self.inner.is_ascii()
823+
}
824+
825+
/// Checks that two strings are an ASCII case-insensitive match.
826+
///
827+
/// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
828+
/// but without allocating and copying temporaries.
829+
///
830+
/// # Examples
831+
///
832+
/// ```
833+
/// #![feature(osstring_ascii)]
834+
/// use std::ffi::OsString;
835+
///
836+
/// assert!(OsString::from("Ferris").eq_ignore_ascii_case("FERRIS"));
837+
/// assert!(OsString::from("Ferrös").eq_ignore_ascii_case("FERRöS"));
838+
/// assert!(!OsString::from("Ferrös").eq_ignore_ascii_case("FERRÖS"));
839+
/// ```
840+
#[unstable(feature = "osstring_ascii", issue = "none")]
841+
pub fn eq_ignore_ascii_case<S: ?Sized + AsRef<OsStr>>(&self, other: &S) -> bool {
842+
self.inner.eq_ignore_ascii_case(&other.as_ref().inner)
843+
}
701844
}
702845

703846
#[stable(feature = "box_from_os_str", since = "1.17.0")]

src/libstd/sys/windows/os_str.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,34 @@ impl Slice {
179179
let rc = self.inner.into_rc();
180180
unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Slice) }
181181
}
182+
183+
#[inline]
184+
pub fn make_ascii_lowercase(&mut self) {
185+
self.inner.make_ascii_lowercase()
186+
}
187+
188+
#[inline]
189+
pub fn make_ascii_uppercase(&mut self) {
190+
self.inner.make_ascii_uppercase()
191+
}
192+
193+
#[inline]
194+
pub fn to_ascii_lowercase(&self) -> Buf {
195+
Buf { inner: self.inner.to_ascii_lowercase() }
196+
}
197+
198+
#[inline]
199+
pub fn to_ascii_uppercase(&self) -> Buf {
200+
Buf { inner: self.inner.to_ascii_uppercase() }
201+
}
202+
203+
#[inline]
204+
pub fn is_ascii(&self) -> bool {
205+
self.inner.is_ascii()
206+
}
207+
208+
#[inline]
209+
pub fn eq_ignore_ascii_case(&self, other: &Self) -> bool {
210+
self.inner.eq_ignore_ascii_case(&other.inner)
211+
}
182212
}

src/libstd/sys_common/os_str_bytes.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,36 @@ impl Slice {
195195
let rc: Rc<[u8]> = Rc::from(&self.inner);
196196
unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Slice) }
197197
}
198+
199+
#[inline]
200+
pub fn make_ascii_lowercase(&mut self) {
201+
self.inner.make_ascii_lowercase()
202+
}
203+
204+
#[inline]
205+
pub fn make_ascii_uppercase(&mut self) {
206+
self.inner.make_ascii_uppercase()
207+
}
208+
209+
#[inline]
210+
pub fn to_ascii_lowercase(&self) -> Buf {
211+
Buf { inner: self.inner.to_ascii_lowercase() }
212+
}
213+
214+
#[inline]
215+
pub fn to_ascii_uppercase(&self) -> Buf {
216+
Buf { inner: self.inner.to_ascii_uppercase() }
217+
}
218+
219+
#[inline]
220+
pub fn is_ascii(&self) -> bool {
221+
self.inner.is_ascii()
222+
}
223+
224+
#[inline]
225+
pub fn eq_ignore_ascii_case(&self, other: &Self) -> bool {
226+
self.inner.eq_ignore_ascii_case(&other.inner)
227+
}
198228
}
199229

200230
/// Platform-specific extensions to [`OsString`].

src/libstd/sys_common/wtf8.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,36 @@ impl Wtf8 {
645645
let rc: Rc<[u8]> = Rc::from(&self.bytes);
646646
unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Wtf8) }
647647
}
648+
649+
#[inline]
650+
pub fn make_ascii_lowercase(&mut self) {
651+
self.bytes.make_ascii_lowercase()
652+
}
653+
654+
#[inline]
655+
pub fn make_ascii_uppercase(&mut self) {
656+
self.bytes.make_ascii_uppercase()
657+
}
658+
659+
#[inline]
660+
pub fn to_ascii_lowercase(&self) -> Wtf8Buf {
661+
Wtf8Buf { bytes: self.bytes.to_ascii_lowercase() }
662+
}
663+
664+
#[inline]
665+
pub fn to_ascii_uppercase(&self) -> Wtf8Buf {
666+
Wtf8Buf { bytes: self.bytes.to_ascii_uppercase() }
667+
}
668+
669+
#[inline]
670+
pub fn is_ascii(&self) -> bool {
671+
self.bytes.is_ascii()
672+
}
673+
674+
#[inline]
675+
pub fn eq_ignore_ascii_case(&self, other: &Self) -> bool {
676+
self.bytes.eq_ignore_ascii_case(&other.bytes)
677+
}
648678
}
649679

650680
/// Returns a slice of the given string for the byte range [`begin`..`end`).
@@ -845,12 +875,6 @@ impl Hash for Wtf8 {
845875
}
846876
}
847877

848-
impl Wtf8 {
849-
pub fn make_ascii_uppercase(&mut self) {
850-
self.bytes.make_ascii_uppercase()
851-
}
852-
}
853-
854878
#[cfg(test)]
855879
mod tests {
856880
use super::*;

0 commit comments

Comments
 (0)