1 // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Operations on ASCII strings and characters.
13 //! Most string operations in Rust act on UTF-8 strings. However, at times it
14 //! makes more sense to only consider the ASCII character set for a specific
17 //! The [`AsciiExt`] trait provides methods that allow for character
18 //! operations that only act on the ASCII subset and leave non-ASCII characters
21 //! The [`escape_default`] function provides an iterator over the bytes of an
22 //! escaped version of the character given.
24 //! [`AsciiExt`]: trait.AsciiExt.html
25 //! [`escape_default`]: fn.escape_default.html
27 #![stable(feature = "rust1", since = "1.0.0")]
29 #[stable(feature = "rust1", since = "1.0.0")]
30 pub use core::ascii::{EscapeDefault, escape_default};
32 /// Extension methods for ASCII-subset only operations.
34 /// Be aware that operations on seemingly non-ASCII characters can sometimes
35 /// have unexpected results. Consider this example:
38 /// use std::ascii::AsciiExt;
40 /// assert_eq!(AsciiExt::to_ascii_uppercase("café"), "CAFÉ");
41 /// assert_eq!(AsciiExt::to_ascii_uppercase("café"), "CAFé");
44 /// In the first example, the lowercased string is represented `"cafe\u{301}"`
45 /// (the last character is an acute accent [combining character]). Unlike the
46 /// other characters in the string, the combining character will not get mapped
47 /// to an uppercase variant, resulting in `"CAFE\u{301}"`. In the second
48 /// example, the lowercased string is represented `"caf\u{e9}"` (the last
49 /// character is a single Unicode character representing an 'e' with an acute
50 /// accent). Since the last character is defined outside the scope of ASCII,
51 /// it will not get mapped to an uppercase variant, resulting in `"CAF\u{e9}"`.
53 /// [combining character]: https://en.wikipedia.org/wiki/Combining_character
54 #[stable(feature = "rust1", since = "1.0.0")]
55 #[rustc_deprecated(since = "1.26.0", reason = "use inherent methods instead")]
57 /// Container type for copied ASCII characters.
58 #[stable(feature = "rust1", since = "1.0.0")]
61 /// Checks if the value is within the ASCII range.
65 /// This method will be deprecated in favor of the identically-named
66 /// inherent methods on `u8`, `char`, `[u8]` and `str`.
67 #[stable(feature = "rust1", since = "1.0.0")]
68 fn is_ascii(&self) -> bool;
70 /// Makes a copy of the value in its ASCII upper case equivalent.
72 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
73 /// but non-ASCII letters are unchanged.
75 /// To uppercase the value in-place, use [`make_ascii_uppercase`].
77 /// To uppercase ASCII characters in addition to non-ASCII characters, use
78 /// [`str::to_uppercase`].
82 /// This method will be deprecated in favor of the identically-named
83 /// inherent methods on `u8`, `char`, `[u8]` and `str`.
85 /// [`make_ascii_uppercase`]: #tymethod.make_ascii_uppercase
86 /// [`str::to_uppercase`]: ../primitive.str.html#method.to_uppercase
87 #[stable(feature = "rust1", since = "1.0.0")]
89 fn to_ascii_uppercase(&self) -> Self::Owned;
91 /// Makes a copy of the value in its ASCII lower case equivalent.
93 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
94 /// but non-ASCII letters are unchanged.
96 /// To lowercase the value in-place, use [`make_ascii_lowercase`].
98 /// To lowercase ASCII characters in addition to non-ASCII characters, use
99 /// [`str::to_lowercase`].
103 /// This method will be deprecated in favor of the identically-named
104 /// inherent methods on `u8`, `char`, `[u8]` and `str`.
106 /// [`make_ascii_lowercase`]: #tymethod.make_ascii_lowercase
107 /// [`str::to_lowercase`]: ../primitive.str.html#method.to_lowercase
108 #[stable(feature = "rust1", since = "1.0.0")]
110 fn to_ascii_lowercase(&self) -> Self::Owned;
112 /// Checks that two values are an ASCII case-insensitive match.
114 /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
115 /// but without allocating and copying temporaries.
119 /// This method will be deprecated in favor of the identically-named
120 /// inherent methods on `u8`, `char`, `[u8]` and `str`.
121 #[stable(feature = "rust1", since = "1.0.0")]
122 fn eq_ignore_ascii_case(&self, other: &Self) -> bool;
124 /// Converts this type to its ASCII upper case equivalent in-place.
126 /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
127 /// but non-ASCII letters are unchanged.
129 /// To return a new uppercased value without modifying the existing one, use
130 /// [`to_ascii_uppercase`].
134 /// This method will be deprecated in favor of the identically-named
135 /// inherent methods on `u8`, `char`, `[u8]` and `str`.
137 /// [`to_ascii_uppercase`]: #tymethod.to_ascii_uppercase
138 #[stable(feature = "ascii", since = "1.9.0")]
139 fn make_ascii_uppercase(&mut self);
141 /// Converts this type to its ASCII lower case equivalent in-place.
143 /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
144 /// but non-ASCII letters are unchanged.
146 /// To return a new lowercased value without modifying the existing one, use
147 /// [`to_ascii_lowercase`].
151 /// This method will be deprecated in favor of the identically-named
152 /// inherent methods on `u8`, `char`, `[u8]` and `str`.
154 /// [`to_ascii_lowercase`]: #tymethod.to_ascii_lowercase
155 #[stable(feature = "ascii", since = "1.9.0")]
156 fn make_ascii_lowercase(&mut self);
158 /// Checks if the value is an ASCII alphabetic character:
159 /// U+0041 'A' ... U+005A 'Z' or U+0061 'a' ... U+007A 'z'.
160 /// For strings, true if all characters in the string are
161 /// ASCII alphabetic.
165 /// This method will be deprecated in favor of the identically-named
166 /// inherent methods on `u8`, `char`, `[u8]` and `str`.
167 #[unstable(feature = "ascii_ctype", issue = "39658")]
168 #[rustc_deprecated(since = "1.26.0", reason = "use inherent methods instead")]
169 fn is_ascii_alphabetic(&self) -> bool { unimplemented!(); }
171 /// Checks if the value is an ASCII uppercase character:
172 /// U+0041 'A' ... U+005A 'Z'.
173 /// For strings, true if all characters in the string are
178 /// This method will be deprecated in favor of the identically-named
179 /// inherent methods on `u8`, `char`, `[u8]` and `str`.
180 #[unstable(feature = "ascii_ctype", issue = "39658")]
181 #[rustc_deprecated(since = "1.26.0", reason = "use inherent methods instead")]
182 fn is_ascii_uppercase(&self) -> bool { unimplemented!(); }
184 /// Checks if the value is an ASCII lowercase character:
185 /// U+0061 'a' ... U+007A 'z'.
186 /// For strings, true if all characters in the string are
191 /// This method will be deprecated in favor of the identically-named
192 /// inherent methods on `u8`, `char`, `[u8]` and `str`.
193 #[unstable(feature = "ascii_ctype", issue = "39658")]
194 #[rustc_deprecated(since = "1.26.0", reason = "use inherent methods instead")]
195 fn is_ascii_lowercase(&self) -> bool { unimplemented!(); }
197 /// Checks if the value is an ASCII alphanumeric character:
198 /// U+0041 'A' ... U+005A 'Z', U+0061 'a' ... U+007A 'z', or
199 /// U+0030 '0' ... U+0039 '9'.
200 /// For strings, true if all characters in the string are
201 /// ASCII alphanumeric.
205 /// This method will be deprecated in favor of the identically-named
206 /// inherent methods on `u8`, `char`, `[u8]` and `str`.
207 #[unstable(feature = "ascii_ctype", issue = "39658")]
208 #[rustc_deprecated(since = "1.26.0", reason = "use inherent methods instead")]
209 fn is_ascii_alphanumeric(&self) -> bool { unimplemented!(); }
211 /// Checks if the value is an ASCII decimal digit:
212 /// U+0030 '0' ... U+0039 '9'.
213 /// For strings, true if all characters in the string are
218 /// This method will be deprecated in favor of the identically-named
219 /// inherent methods on `u8`, `char`, `[u8]` and `str`.
220 #[unstable(feature = "ascii_ctype", issue = "39658")]
221 #[rustc_deprecated(since = "1.26.0", reason = "use inherent methods instead")]
222 fn is_ascii_digit(&self) -> bool { unimplemented!(); }
224 /// Checks if the value is an ASCII hexadecimal digit:
225 /// U+0030 '0' ... U+0039 '9', U+0041 'A' ... U+0046 'F', or
226 /// U+0061 'a' ... U+0066 'f'.
227 /// For strings, true if all characters in the string are
228 /// ASCII hex digits.
232 /// This method will be deprecated in favor of the identically-named
233 /// inherent methods on `u8`, `char`, `[u8]` and `str`.
234 #[unstable(feature = "ascii_ctype", issue = "39658")]
235 #[rustc_deprecated(since = "1.26.0", reason = "use inherent methods instead")]
236 fn is_ascii_hexdigit(&self) -> bool { unimplemented!(); }
238 /// Checks if the value is an ASCII punctuation character:
240 /// U+0021 ... U+002F `! " # $ % & ' ( ) * + , - . /`
241 /// U+003A ... U+0040 `: ; < = > ? @`
242 /// U+005B ... U+0060 ``[ \\ ] ^ _ ` ``
243 /// U+007B ... U+007E `{ | } ~`
245 /// For strings, true if all characters in the string are
246 /// ASCII punctuation.
250 /// This method will be deprecated in favor of the identically-named
251 /// inherent methods on `u8`, `char`, `[u8]` and `str`.
252 #[unstable(feature = "ascii_ctype", issue = "39658")]
253 #[rustc_deprecated(since = "1.26.0", reason = "use inherent methods instead")]
254 fn is_ascii_punctuation(&self) -> bool { unimplemented!(); }
256 /// Checks if the value is an ASCII graphic character:
257 /// U+0021 '!' ... U+007E '~'.
258 /// For strings, true if all characters in the string are
259 /// ASCII graphic characters.
263 /// This method will be deprecated in favor of the identically-named
264 /// inherent methods on `u8`, `char`, `[u8]` and `str`.
265 #[unstable(feature = "ascii_ctype", issue = "39658")]
266 #[rustc_deprecated(since = "1.26.0", reason = "use inherent methods instead")]
267 fn is_ascii_graphic(&self) -> bool { unimplemented!(); }
269 /// Checks if the value is an ASCII whitespace character:
270 /// U+0020 SPACE, U+0009 HORIZONTAL TAB, U+000A LINE FEED,
271 /// U+000C FORM FEED, or U+000D CARRIAGE RETURN.
272 /// For strings, true if all characters in the string are
273 /// ASCII whitespace.
275 /// Rust uses the WhatWG Infra Standard's [definition of ASCII
276 /// whitespace][infra-aw]. There are several other definitions in
277 /// wide use. For instance, [the POSIX locale][pct] includes
278 /// U+000B VERTICAL TAB as well as all the above characters,
279 /// but—from the very same specification—[the default rule for
280 /// "field splitting" in the Bourne shell][bfs] considers *only*
281 /// SPACE, HORIZONTAL TAB, and LINE FEED as whitespace.
283 /// If you are writing a program that will process an existing
284 /// file format, check what that format's definition of whitespace is
285 /// before using this function.
287 /// [infra-aw]: https://infra.spec.whatwg.org/#ascii-whitespace
288 /// [pct]: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html#tag_07_03_01
289 /// [bfs]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05
293 /// This method will be deprecated in favor of the identically-named
294 /// inherent methods on `u8`, `char`, `[u8]` and `str`.
295 #[unstable(feature = "ascii_ctype", issue = "39658")]
296 #[rustc_deprecated(since = "1.26.0", reason = "use inherent methods instead")]
297 fn is_ascii_whitespace(&self) -> bool { unimplemented!(); }
299 /// Checks if the value is an ASCII control character:
300 /// U+0000 NUL ... U+001F UNIT SEPARATOR, or U+007F DELETE.
301 /// Note that most ASCII whitespace characters are control
302 /// characters, but SPACE is not.
306 /// This method will be deprecated in favor of the identically-named
307 /// inherent methods on `u8`, `char`, `[u8]` and `str`.
308 #[unstable(feature = "ascii_ctype", issue = "39658")]
309 #[rustc_deprecated(since = "1.26.0", reason = "use inherent methods instead")]
310 fn is_ascii_control(&self) -> bool { unimplemented!(); }
313 macro_rules! delegating_ascii_methods {
316 fn is_ascii(&self) -> bool { self.is_ascii() }
319 fn to_ascii_uppercase(&self) -> Self::Owned { self.to_ascii_uppercase() }
322 fn to_ascii_lowercase(&self) -> Self::Owned { self.to_ascii_lowercase() }
325 fn eq_ignore_ascii_case(&self, o: &Self) -> bool { self.eq_ignore_ascii_case(o) }
328 fn make_ascii_uppercase(&mut self) { self.make_ascii_uppercase(); }
331 fn make_ascii_lowercase(&mut self) { self.make_ascii_lowercase(); }
335 macro_rules! delegating_ascii_ctype_methods {
338 fn is_ascii_alphabetic(&self) -> bool { self.is_ascii_alphabetic() }
341 fn is_ascii_uppercase(&self) -> bool { self.is_ascii_uppercase() }
344 fn is_ascii_lowercase(&self) -> bool { self.is_ascii_lowercase() }
347 fn is_ascii_alphanumeric(&self) -> bool { self.is_ascii_alphanumeric() }
350 fn is_ascii_digit(&self) -> bool { self.is_ascii_digit() }
353 fn is_ascii_hexdigit(&self) -> bool { self.is_ascii_hexdigit() }
356 fn is_ascii_punctuation(&self) -> bool { self.is_ascii_punctuation() }
359 fn is_ascii_graphic(&self) -> bool { self.is_ascii_graphic() }
362 fn is_ascii_whitespace(&self) -> bool { self.is_ascii_whitespace() }
365 fn is_ascii_control(&self) -> bool { self.is_ascii_control() }
369 #[stable(feature = "rust1", since = "1.0.0")]
371 impl AsciiExt for u8 {
374 delegating_ascii_methods!();
375 delegating_ascii_ctype_methods!();
378 #[stable(feature = "rust1", since = "1.0.0")]
380 impl AsciiExt for char {
383 delegating_ascii_methods!();
384 delegating_ascii_ctype_methods!();
387 #[stable(feature = "rust1", since = "1.0.0")]
389 impl AsciiExt for [u8] {
390 type Owned = Vec<u8>;
392 delegating_ascii_methods!();
395 fn is_ascii_alphabetic(&self) -> bool {
396 self.iter().all(|b| b.is_ascii_alphabetic())
400 fn is_ascii_uppercase(&self) -> bool {
401 self.iter().all(|b| b.is_ascii_uppercase())
405 fn is_ascii_lowercase(&self) -> bool {
406 self.iter().all(|b| b.is_ascii_lowercase())
410 fn is_ascii_alphanumeric(&self) -> bool {
411 self.iter().all(|b| b.is_ascii_alphanumeric())
415 fn is_ascii_digit(&self) -> bool {
416 self.iter().all(|b| b.is_ascii_digit())
420 fn is_ascii_hexdigit(&self) -> bool {
421 self.iter().all(|b| b.is_ascii_hexdigit())
425 fn is_ascii_punctuation(&self) -> bool {
426 self.iter().all(|b| b.is_ascii_punctuation())
430 fn is_ascii_graphic(&self) -> bool {
431 self.iter().all(|b| b.is_ascii_graphic())
435 fn is_ascii_whitespace(&self) -> bool {
436 self.iter().all(|b| b.is_ascii_whitespace())
440 fn is_ascii_control(&self) -> bool {
441 self.iter().all(|b| b.is_ascii_control())
445 #[stable(feature = "rust1", since = "1.0.0")]
447 impl AsciiExt for str {
450 delegating_ascii_methods!();
453 fn is_ascii_alphabetic(&self) -> bool {
454 self.bytes().all(|b| b.is_ascii_alphabetic())
458 fn is_ascii_uppercase(&self) -> bool {
459 self.bytes().all(|b| b.is_ascii_uppercase())
463 fn is_ascii_lowercase(&self) -> bool {
464 self.bytes().all(|b| b.is_ascii_lowercase())
468 fn is_ascii_alphanumeric(&self) -> bool {
469 self.bytes().all(|b| b.is_ascii_alphanumeric())
473 fn is_ascii_digit(&self) -> bool {
474 self.bytes().all(|b| b.is_ascii_digit())
478 fn is_ascii_hexdigit(&self) -> bool {
479 self.bytes().all(|b| b.is_ascii_hexdigit())
483 fn is_ascii_punctuation(&self) -> bool {
484 self.bytes().all(|b| b.is_ascii_punctuation())
488 fn is_ascii_graphic(&self) -> bool {
489 self.bytes().all(|b| b.is_ascii_graphic())
493 fn is_ascii_whitespace(&self) -> bool {
494 self.bytes().all(|b| b.is_ascii_whitespace())
498 fn is_ascii_control(&self) -> bool {
499 self.bytes().all(|b| b.is_ascii_control())