1 // Copyright 2012-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 //! Character manipulation.
13 //! For more details, see ::unicode::char (a.k.a. std::char)
15 #![allow(non_snake_case)]
16 #![doc(primitive = "char")]
21 use option::Option::{None, Some};
22 use iter::{range_step, Iterator, RangeStep};
23 use slice::SlicePrelude;
25 // UTF-8 ranges and tags for encoding characters
26 static TAG_CONT: u8 = 0b1000_0000u8;
27 static TAG_TWO_B: u8 = 0b1100_0000u8;
28 static TAG_THREE_B: u8 = 0b1110_0000u8;
29 static TAG_FOUR_B: u8 = 0b1111_0000u8;
30 static MAX_ONE_B: u32 = 0x80u32;
31 static MAX_TWO_B: u32 = 0x800u32;
32 static MAX_THREE_B: u32 = 0x10000u32;
35 Lu Uppercase_Letter an uppercase letter
36 Ll Lowercase_Letter a lowercase letter
37 Lt Titlecase_Letter a digraphic character, with first part uppercase
38 Lm Modifier_Letter a modifier letter
39 Lo Other_Letter other letters, including syllables and ideographs
40 Mn Nonspacing_Mark a nonspacing combining mark (zero advance width)
41 Mc Spacing_Mark a spacing combining mark (positive advance width)
42 Me Enclosing_Mark an enclosing combining mark
43 Nd Decimal_Number a decimal digit
44 Nl Letter_Number a letterlike numeric character
45 No Other_Number a numeric character of other type
46 Pc Connector_Punctuation a connecting punctuation mark, like a tie
47 Pd Dash_Punctuation a dash or hyphen punctuation mark
48 Ps Open_Punctuation an opening punctuation mark (of a pair)
49 Pe Close_Punctuation a closing punctuation mark (of a pair)
50 Pi Initial_Punctuation an initial quotation mark
51 Pf Final_Punctuation a final quotation mark
52 Po Other_Punctuation a punctuation mark of other type
53 Sm Math_Symbol a symbol of primarily mathematical use
54 Sc Currency_Symbol a currency sign
55 Sk Modifier_Symbol a non-letterlike modifier symbol
56 So Other_Symbol a symbol of other type
57 Zs Space_Separator a space character (of various non-zero widths)
58 Zl Line_Separator U+2028 LINE SEPARATOR only
59 Zp Paragraph_Separator U+2029 PARAGRAPH SEPARATOR only
60 Cc Control a C0 or C1 control code
61 Cf Format a format control character
62 Cs Surrogate a surrogate code point
63 Co Private_Use a private-use character
64 Cn Unassigned a reserved unassigned code point or a noncharacter
67 /// The highest valid code point
69 pub const MAX: char = '\u{10ffff}';
71 /// Converts from `u32` to a `char`
73 #[unstable = "pending decisions about costructors for primitives"]
74 pub fn from_u32(i: u32) -> Option<char> {
75 // catch out-of-bounds and surrogates
76 if (i > MAX as u32) || (i >= 0xD800 && i <= 0xDFFF) {
79 Some(unsafe { transmute(i) })
84 /// Checks if a `char` parses as a numeric digit in the given radix
86 /// Compared to `is_numeric()`, this function only recognizes the
87 /// characters `0-9`, `a-z` and `A-Z`.
91 /// Returns `true` if `c` is a valid digit under `radix`, and `false`
96 /// Panics if given a `radix` > 36.
100 /// This just wraps `to_digit()`.
103 #[deprecated = "use the Char::is_digit method"]
104 pub fn is_digit_radix(c: char, radix: uint) -> bool {
109 /// Converts a `char` to the corresponding digit
113 /// If `c` is between '0' and '9', the corresponding value
114 /// between 0 and 9. If `c` is 'a' or 'A', 10. If `c` is
115 /// 'b' or 'B', 11, etc. Returns none if the `char` does not
116 /// refer to a digit in the given radix.
120 /// Panics if given a `radix` outside the range `[0..36]`.
123 #[deprecated = "use the Char::to_digit method"]
124 pub fn to_digit(c: char, radix: uint) -> Option<uint> {
129 /// Converts a number to the character representing it
133 /// Returns `Some(char)` if `num` represents one digit under `radix`,
134 /// using one character of `0-9` or `a-z`, or `None` if it doesn't.
138 /// Panics if given an `radix` > 36.
141 #[unstable = "pending decisions about costructors for primitives"]
142 pub fn from_digit(num: uint, radix: uint) -> Option<char> {
144 panic!("from_digit: radix is to high (maximum 36)");
149 Some(transmute(('0' as uint + num) as u32))
151 Some(transmute(('a' as uint + num - 10u) as u32))
160 /// Returns the hexadecimal Unicode escape of a `char`
162 /// The rules are as follows:
164 /// - chars in [0,0xff] get 2-digit escapes: `\\xNN`
165 /// - chars in [0x100,0xffff] get 4-digit escapes: `\\u{NNNN}`
166 /// - chars above 0x10000 get 8-digit escapes: `\\u{{NNN}NNNNN}`
168 #[deprecated = "use the Char::escape_unicode method"]
169 pub fn escape_unicode<F>(c: char, mut f: F) where F: FnMut(char) {
170 for char in c.escape_unicode() {
176 /// Returns a 'default' ASCII and C++11-like literal escape of a `char`
178 /// The default is chosen with a bias toward producing literals that are
179 /// legal in a variety of languages, including C++11 and similar C-family
180 /// languages. The exact rules are:
182 /// - Tab, CR and LF are escaped as '\t', '\r' and '\n' respectively.
183 /// - Single-quote, double-quote and backslash chars are backslash-escaped.
184 /// - Any other chars in the range [0x20,0x7e] are not escaped.
185 /// - Any other chars are given hex Unicode escapes; see `escape_unicode`.
187 #[deprecated = "use the Char::escape_default method"]
188 pub fn escape_default<F>(c: char, mut f: F) where F: FnMut(char) {
189 for c in c.escape_default() {
194 /// Returns the amount of bytes this `char` would need if encoded in UTF-8
196 #[deprecated = "use the Char::len_utf8 method"]
197 pub fn len_utf8_bytes(c: char) -> uint {
201 /// Basic `char` manipulations.
202 #[experimental = "trait organization may change"]
204 /// Checks if a `char` parses as a numeric digit in the given radix.
206 /// Compared to `is_numeric()`, this function only recognizes the characters
207 /// `0-9`, `a-z` and `A-Z`.
211 /// Returns `true` if `c` is a valid digit under `radix`, and `false`
216 /// Panics if given a radix > 36.
217 #[deprecated = "use is_digit"]
218 fn is_digit_radix(self, radix: uint) -> bool;
220 /// Checks if a `char` parses as a numeric digit in the given radix.
222 /// Compared to `is_numeric()`, this function only recognizes the characters
223 /// `0-9`, `a-z` and `A-Z`.
227 /// Returns `true` if `c` is a valid digit under `radix`, and `false`
232 /// Panics if given a radix > 36.
233 #[unstable = "pending error conventions"]
234 fn is_digit(self, radix: uint) -> bool;
236 /// Converts a character to the corresponding digit.
240 /// If `c` is between '0' and '9', the corresponding value between 0 and
241 /// 9. If `c` is 'a' or 'A', 10. If `c` is 'b' or 'B', 11, etc. Returns
242 /// none if the character does not refer to a digit in the given radix.
246 /// Panics if given a radix outside the range [0..36].
247 #[unstable = "pending error conventions, trait organization"]
248 fn to_digit(self, radix: uint) -> Option<uint>;
250 /// Converts a number to the character representing it.
254 /// Returns `Some(char)` if `num` represents one digit under `radix`,
255 /// using one character of `0-9` or `a-z`, or `None` if it doesn't.
259 /// Panics if given a radix > 36.
260 #[deprecated = "use the char::from_digit free function"]
261 fn from_digit(num: uint, radix: uint) -> Option<Self>;
263 /// Converts from `u32` to a `char`
264 #[deprecated = "use the char::from_u32 free function"]
265 fn from_u32(i: u32) -> Option<char>;
267 /// Returns an iterator that yields the hexadecimal Unicode escape
268 /// of a character, as `char`s.
270 /// The rules are as follows:
272 /// * Characters in [0,0xff] get 2-digit escapes: `\\xNN`
273 /// * Characters in [0x100,0xffff] get 4-digit escapes: `\\u{NNNN}`.
274 /// * Characters above 0x10000 get 8-digit escapes: `\\u{{NNN}NNNNN}`.
275 #[unstable = "pending error conventions, trait organization"]
276 fn escape_unicode(self) -> UnicodeEscapedChars;
278 /// Returns an iterator that yields the 'default' ASCII and
279 /// C++11-like literal escape of a character, as `char`s.
281 /// The default is chosen with a bias toward producing literals that are
282 /// legal in a variety of languages, including C++11 and similar C-family
283 /// languages. The exact rules are:
285 /// * Tab, CR and LF are escaped as '\t', '\r' and '\n' respectively.
286 /// * Single-quote, double-quote and backslash chars are backslash-
288 /// * Any other chars in the range [0x20,0x7e] are not escaped.
289 /// * Any other chars are given hex Unicode escapes; see `escape_unicode`.
290 #[unstable = "pending error conventions, trait organization"]
291 fn escape_default(self) -> DefaultEscapedChars;
293 /// Returns the amount of bytes this character would need if encoded in
295 #[deprecated = "use len_utf8"]
296 fn len_utf8_bytes(self) -> uint;
298 /// Returns the amount of bytes this character would need if encoded in
300 #[unstable = "pending trait organization"]
301 fn len_utf8(self) -> uint;
303 /// Returns the amount of bytes this character would need if encoded in
305 #[unstable = "pending trait organization"]
306 fn len_utf16(self) -> uint;
308 /// Encodes this character as UTF-8 into the provided byte buffer,
309 /// and then returns the number of bytes written.
311 /// If the buffer is not large enough, nothing will be written into it
312 /// and a `None` will be returned.
313 #[unstable = "pending trait organization"]
314 fn encode_utf8(&self, dst: &mut [u8]) -> Option<uint>;
316 /// Encodes this character as UTF-16 into the provided `u16` buffer,
317 /// and then returns the number of `u16`s written.
319 /// If the buffer is not large enough, nothing will be written into it
320 /// and a `None` will be returned.
321 #[unstable = "pending trait organization"]
322 fn encode_utf16(&self, dst: &mut [u16]) -> Option<uint>;
325 #[experimental = "trait is experimental"]
327 #[deprecated = "use is_digit"]
328 fn is_digit_radix(self, radix: uint) -> bool { self.is_digit(radix) }
330 #[unstable = "pending trait organization"]
331 fn is_digit(self, radix: uint) -> bool {
332 match self.to_digit(radix) {
338 #[unstable = "pending trait organization"]
339 fn to_digit(self, radix: uint) -> Option<uint> {
341 panic!("to_digit: radix is too high (maximum 36)");
343 let val = match self {
344 '0' ... '9' => self as uint - ('0' as uint),
345 'a' ... 'z' => self as uint + 10u - ('a' as uint),
346 'A' ... 'Z' => self as uint + 10u - ('A' as uint),
349 if val < radix { Some(val) }
353 #[deprecated = "use the char::from_digit free function"]
354 fn from_digit(num: uint, radix: uint) -> Option<char> { from_digit(num, radix) }
357 #[deprecated = "use the char::from_u32 free function"]
358 fn from_u32(i: u32) -> Option<char> { from_u32(i) }
360 #[unstable = "pending error conventions, trait organization"]
361 fn escape_unicode(self) -> UnicodeEscapedChars {
362 UnicodeEscapedChars { c: self, state: UnicodeEscapedCharsState::Backslash }
365 #[unstable = "pending error conventions, trait organization"]
366 fn escape_default(self) -> DefaultEscapedChars {
367 let init_state = match self {
368 '\t' => DefaultEscapedCharsState::Backslash('t'),
369 '\r' => DefaultEscapedCharsState::Backslash('r'),
370 '\n' => DefaultEscapedCharsState::Backslash('n'),
371 '\\' => DefaultEscapedCharsState::Backslash('\\'),
372 '\'' => DefaultEscapedCharsState::Backslash('\''),
373 '"' => DefaultEscapedCharsState::Backslash('"'),
374 '\x20' ... '\x7e' => DefaultEscapedCharsState::Char(self),
375 _ => DefaultEscapedCharsState::Unicode(self.escape_unicode())
377 DefaultEscapedChars { state: init_state }
381 #[deprecated = "use len_utf8"]
382 fn len_utf8_bytes(self) -> uint { self.len_utf8() }
385 #[unstable = "pending trait organization"]
386 fn len_utf8(self) -> uint {
387 let code = self as u32;
389 _ if code < MAX_ONE_B => 1u,
390 _ if code < MAX_TWO_B => 2u,
391 _ if code < MAX_THREE_B => 3u,
397 #[unstable = "pending trait organization"]
398 fn len_utf16(self) -> uint {
399 let ch = self as u32;
400 if (ch & 0xFFFF_u32) == ch { 1 } else { 2 }
404 #[unstable = "pending error conventions, trait organization"]
405 fn encode_utf8<'a>(&self, dst: &'a mut [u8]) -> Option<uint> {
406 // Marked #[inline] to allow llvm optimizing it away
407 let code = *self as u32;
408 if code < MAX_ONE_B && dst.len() >= 1 {
411 } else if code < MAX_TWO_B && dst.len() >= 2 {
412 dst[0] = (code >> 6u & 0x1F_u32) as u8 | TAG_TWO_B;
413 dst[1] = (code & 0x3F_u32) as u8 | TAG_CONT;
415 } else if code < MAX_THREE_B && dst.len() >= 3 {
416 dst[0] = (code >> 12u & 0x0F_u32) as u8 | TAG_THREE_B;
417 dst[1] = (code >> 6u & 0x3F_u32) as u8 | TAG_CONT;
418 dst[2] = (code & 0x3F_u32) as u8 | TAG_CONT;
420 } else if dst.len() >= 4 {
421 dst[0] = (code >> 18u & 0x07_u32) as u8 | TAG_FOUR_B;
422 dst[1] = (code >> 12u & 0x3F_u32) as u8 | TAG_CONT;
423 dst[2] = (code >> 6u & 0x3F_u32) as u8 | TAG_CONT;
424 dst[3] = (code & 0x3F_u32) as u8 | TAG_CONT;
432 #[unstable = "pending error conventions, trait organization"]
433 fn encode_utf16(&self, dst: &mut [u16]) -> Option<uint> {
434 // Marked #[inline] to allow llvm optimizing it away
435 let mut ch = *self as u32;
436 if (ch & 0xFFFF_u32) == ch && dst.len() >= 1 {
437 // The BMP falls through (assuming non-surrogate, as it should)
440 } else if dst.len() >= 2 {
441 // Supplementary planes break into surrogates.
443 dst[0] = 0xD800_u16 | ((ch >> 10) as u16);
444 dst[1] = 0xDC00_u16 | ((ch as u16) & 0x3FF_u16);
452 /// An iterator over the characters that represent a `char`, as escaped by
453 /// Rust's unicode escaping rules.
454 pub struct UnicodeEscapedChars {
456 state: UnicodeEscapedCharsState
459 enum UnicodeEscapedCharsState {
462 Value(RangeStep<i32>),
465 impl Iterator<char> for UnicodeEscapedChars {
466 fn next(&mut self) -> Option<char> {
468 UnicodeEscapedCharsState::Backslash => {
469 self.state = UnicodeEscapedCharsState::Type;
472 UnicodeEscapedCharsState::Type => {
473 let (typechar, pad) = if self.c <= '\x7f' { ('x', 2) }
474 else if self.c <= '\u{ffff}' { ('u', 4) }
476 self.state = UnicodeEscapedCharsState::Value(range_step(4 * (pad - 1), -1, -4i32));
479 UnicodeEscapedCharsState::Value(ref mut range_step) => match range_step.next() {
481 let offset = offset as uint;
482 let v = match ((self.c as i32) >> offset) & 0xf {
483 i @ 0 ... 9 => '0' as i32 + i,
484 i => 'a' as i32 + (i - 10)
486 Some(unsafe { transmute(v) })
494 /// An iterator over the characters that represent a `char`, escaped
495 /// for maximum portability.
496 pub struct DefaultEscapedChars {
497 state: DefaultEscapedCharsState
500 enum DefaultEscapedCharsState {
504 Unicode(UnicodeEscapedChars),
507 impl Iterator<char> for DefaultEscapedChars {
508 fn next(&mut self) -> Option<char> {
510 DefaultEscapedCharsState::Backslash(c) => {
511 self.state = DefaultEscapedCharsState::Char(c);
514 DefaultEscapedCharsState::Char(c) => {
515 self.state = DefaultEscapedCharsState::Done;
518 DefaultEscapedCharsState::Done => None,
519 DefaultEscapedCharsState::Unicode(ref mut iter) => iter.next()