Skip to content

Commit 700ac0e

Browse files
committed
Add AsciiExt::into_ascii_{upper,lower}case
The default implementations (with where Self: Sized) are so that methods that take `self` by value can exist in a trait that’s implemented for dynamically-sized types (`str` and `[u8]`). CC #27809 (comment)
1 parent 9041b93 commit 700ac0e

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

src/libstd/ascii.rs

+106
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,112 @@ pub trait AsciiExt {
139139
/// ```
140140
#[unstable(feature = "ascii", issue = "27809")]
141141
fn make_ascii_lowercase(&mut self);
142+
143+
/// Converts this type to its ASCII upper case,
144+
/// consuming the value to avoid allocating memory where `to_ascii_uppercase` would.
145+
///
146+
/// See `to_ascii_uppercase` for more information.
147+
///
148+
/// # Examples
149+
///
150+
/// ```
151+
/// #![feature(ascii)]
152+
///
153+
/// use std::ascii::AsciiExt;
154+
///
155+
/// let ascii: String = "a".to_owned();
156+
///
157+
/// let upper = ascii.into_ascii_uppercase();
158+
///
159+
/// assert_eq!(upper, "A");
160+
/// ```
161+
#[unstable(feature = "ascii", issue = "27809")]
162+
fn into_ascii_uppercase(self) -> Self::Owned where Self: Sized {
163+
self.to_ascii_uppercase()
164+
}
165+
166+
/// Converts this type to its ASCII lower case,
167+
/// consuming the value to avoid allocating memory where `to_ascii_lowercase` would.
168+
///
169+
/// See `to_ascii_lowercase` for more information.
170+
///
171+
/// # Examples
172+
///
173+
/// ```
174+
/// #![feature(ascii)]
175+
///
176+
/// use std::ascii::AsciiExt;
177+
///
178+
/// let ascii: String = "A".to_owned();
179+
///
180+
/// let lower = ascii.into_ascii_lowercase();
181+
///
182+
/// assert_eq!(lower, "a");
183+
/// ```
184+
#[unstable(feature = "ascii", issue = "27809")]
185+
fn into_ascii_lowercase(self) -> Self::Owned where Self: Sized {
186+
self.to_ascii_lowercase()
187+
}
188+
}
189+
190+
/// Implement `into_ascii_lowercase` and `into_ascii_uppercase` without memory allocation,
191+
/// defer other methods to `str`.
192+
#[unstable(feature = "ascii", issue = "27809")]
193+
impl AsciiExt for String {
194+
type Owned = Self;
195+
196+
#[inline] fn is_ascii(&self) -> bool { (**self).is_ascii() }
197+
#[inline] fn to_ascii_uppercase(&self) -> Self { (**self).to_ascii_uppercase() }
198+
#[inline] fn to_ascii_lowercase(&self) -> Self { (**self).to_ascii_lowercase() }
199+
#[inline] fn eq_ignore_ascii_case(&self, o: &Self) -> bool { (**self).eq_ignore_ascii_case(o) }
200+
#[inline] fn make_ascii_uppercase(&mut self) { (**self).make_ascii_uppercase() }
201+
#[inline] fn make_ascii_lowercase(&mut self) { (**self).make_ascii_lowercase() }
202+
203+
fn into_ascii_lowercase(mut self) -> Self {
204+
unsafe {
205+
for byte in self.as_mut_vec() {
206+
*byte = byte.to_ascii_lowercase()
207+
}
208+
}
209+
self
210+
}
211+
212+
fn into_ascii_uppercase(mut self) -> Self {
213+
unsafe {
214+
for byte in self.as_mut_vec() {
215+
*byte = byte.to_ascii_uppercase()
216+
}
217+
}
218+
self
219+
}
220+
}
221+
222+
/// Implement `into_ascii_lowercase` and `into_ascii_uppercase` without memory allocation,
223+
/// defer other methods to `[u8]`.
224+
#[unstable(feature = "ascii", issue = "27809")]
225+
impl AsciiExt for Vec<u8> {
226+
type Owned = Self;
227+
228+
#[inline] fn is_ascii(&self) -> bool { (**self).is_ascii() }
229+
#[inline] fn to_ascii_uppercase(&self) -> Self { (**self).to_ascii_uppercase() }
230+
#[inline] fn to_ascii_lowercase(&self) -> Self { (**self).to_ascii_lowercase() }
231+
#[inline] fn eq_ignore_ascii_case(&self, o: &Self) -> bool { (**self).eq_ignore_ascii_case(o) }
232+
#[inline] fn make_ascii_uppercase(&mut self) { (**self).make_ascii_uppercase() }
233+
#[inline] fn make_ascii_lowercase(&mut self) { (**self).make_ascii_lowercase() }
234+
235+
fn into_ascii_lowercase(mut self) -> Self {
236+
for byte in &mut self {
237+
*byte = byte.to_ascii_lowercase()
238+
}
239+
self
240+
}
241+
242+
fn into_ascii_uppercase(mut self) -> Self {
243+
for byte in &mut self {
244+
*byte = byte.to_ascii_uppercase()
245+
}
246+
self
247+
}
142248
}
143249

144250
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)