1 // Copyright 2012-2013 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 (`char` type, Unicode Scalar Value)
13 //! This module provides the `Char` trait, as well as its implementation
14 //! for the primitive `char` type, in order to allow basic character manipulation.
16 //! A `char` actually represents a
17 //! *[Unicode Scalar Value](http://www.unicode.org/glossary/#unicode_scalar_value)*,
18 //! as it can contain any Unicode code point except high-surrogate and
19 //! low-surrogate code points.
21 //! As such, only values in the ranges \[0x0,0xD7FF\] and \[0xE000,0x10FFFF\]
22 //! (inclusive) are allowed. A `char` can always be safely cast to a `u32`;
23 //! however the converse is not always true due to the above range limits
24 //! and, as such, should be performed via the `from_u32` function..
28 use option::{None, Option, Some};
29 use iter::{Iterator, range_step};
31 use unicode::{derived_property, property, general_category, decompose, conversions};
33 #[cfg(test)] use str::Str;
34 #[cfg(test)] use strbuf::StrBuf;
35 #[cfg(test)] use slice::ImmutableVector;
37 #[cfg(not(test))] use cmp::{Eq, Ord};
38 #[cfg(not(test))] use default::Default;
40 // UTF-8 ranges and tags for encoding characters
41 static TAG_CONT: uint = 128u;
42 static MAX_ONE_B: uint = 128u;
43 static TAG_TWO_B: uint = 192u;
44 static MAX_TWO_B: uint = 2048u;
45 static TAG_THREE_B: uint = 224u;
46 static MAX_THREE_B: uint = 65536u;
47 static TAG_FOUR_B: uint = 240u;
50 Lu Uppercase_Letter an uppercase letter
51 Ll Lowercase_Letter a lowercase letter
52 Lt Titlecase_Letter a digraphic character, with first part uppercase
53 Lm Modifier_Letter a modifier letter
54 Lo Other_Letter other letters, including syllables and ideographs
55 Mn Nonspacing_Mark a nonspacing combining mark (zero advance width)
56 Mc Spacing_Mark a spacing combining mark (positive advance width)
57 Me Enclosing_Mark an enclosing combining mark
58 Nd Decimal_Number a decimal digit
59 Nl Letter_Number a letterlike numeric character
60 No Other_Number a numeric character of other type
61 Pc Connector_Punctuation a connecting punctuation mark, like a tie
62 Pd Dash_Punctuation a dash or hyphen punctuation mark
63 Ps Open_Punctuation an opening punctuation mark (of a pair)
64 Pe Close_Punctuation a closing punctuation mark (of a pair)
65 Pi Initial_Punctuation an initial quotation mark
66 Pf Final_Punctuation a final quotation mark
67 Po Other_Punctuation a punctuation mark of other type
68 Sm Math_Symbol a symbol of primarily mathematical use
69 Sc Currency_Symbol a currency sign
70 Sk Modifier_Symbol a non-letterlike modifier symbol
71 So Other_Symbol a symbol of other type
72 Zs Space_Separator a space character (of various non-zero widths)
73 Zl Line_Separator U+2028 LINE SEPARATOR only
74 Zp Paragraph_Separator U+2029 PARAGRAPH SEPARATOR only
75 Cc Control a C0 or C1 control code
76 Cf Format a format control character
77 Cs Surrogate a surrogate code point
78 Co Private_Use a private-use character
79 Cn Unassigned a reserved unassigned code point or a noncharacter
82 /// The highest valid code point
83 pub static MAX: char = '\U0010ffff';
85 /// Converts from `u32` to a `char`
87 pub fn from_u32(i: u32) -> Option<char> {
88 // catch out-of-bounds and surrogates
89 if (i > MAX as u32) || (i >= 0xD800 && i <= 0xDFFF) {
92 Some(unsafe { transmute(i) })
96 /// Returns whether the specified `char` is considered a Unicode alphabetic
98 pub fn is_alphabetic(c: char) -> bool { derived_property::Alphabetic(c) }
100 /// Returns whether the specified `char` satisfies the 'XID_Start' Unicode property
102 /// 'XID_Start' is a Unicode Derived Property specified in
103 /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
104 /// mostly similar to ID_Start but modified for closure under NFKx.
105 pub fn is_XID_start(c: char) -> bool { derived_property::XID_Start(c) }
107 /// Returns whether the specified `char` satisfies the 'XID_Continue' Unicode property
109 /// 'XID_Continue' is a Unicode Derived Property specified in
110 /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
111 /// mostly similar to 'ID_Continue' but modified for closure under NFKx.
112 pub fn is_XID_continue(c: char) -> bool { derived_property::XID_Continue(c) }
115 /// Indicates whether a `char` is in lower case
117 /// This is defined according to the terms of the Unicode Derived Core Property 'Lowercase'.
120 pub fn is_lowercase(c: char) -> bool { derived_property::Lowercase(c) }
123 /// Indicates whether a `char` is in upper case
125 /// This is defined according to the terms of the Unicode Derived Core Property 'Uppercase'.
128 pub fn is_uppercase(c: char) -> bool { derived_property::Uppercase(c) }
131 /// Indicates whether a `char` is whitespace
133 /// Whitespace is defined in terms of the Unicode Property 'White_Space'.
136 pub fn is_whitespace(c: char) -> bool {
137 // As an optimization ASCII whitespace characters are checked separately
139 || ('\x09' <= c && c <= '\x0d')
140 || property::White_Space(c)
144 /// Indicates whether a `char` is alphanumeric
146 /// Alphanumericness is defined in terms of the Unicode General Categories
147 /// 'Nd', 'Nl', 'No' and the Derived Core Property 'Alphabetic'.
150 pub fn is_alphanumeric(c: char) -> bool {
151 derived_property::Alphabetic(c)
152 || general_category::Nd(c)
153 || general_category::Nl(c)
154 || general_category::No(c)
158 /// Indicates whether a `char` is a control code point
160 /// Control code points are defined in terms of the Unicode General Category
164 pub fn is_control(c: char) -> bool { general_category::Cc(c) }
166 /// Indicates whether the `char` is numeric (Nd, Nl, or No)
168 pub fn is_digit(c: char) -> bool {
169 general_category::Nd(c)
170 || general_category::Nl(c)
171 || general_category::No(c)
175 /// Checks if a `char` parses as a numeric digit in the given radix
177 /// Compared to `is_digit()`, this function only recognizes the
178 /// characters `0-9`, `a-z` and `A-Z`.
182 /// Returns `true` if `c` is a valid digit under `radix`, and `false`
187 /// Fails if given a `radix` > 36.
191 /// This just wraps `to_digit()`.
194 pub fn is_digit_radix(c: char, radix: uint) -> bool {
195 match to_digit(c, radix) {
202 /// Converts a `char` to the corresponding digit
206 /// If `c` is between '0' and '9', the corresponding value
207 /// between 0 and 9. If `c` is 'a' or 'A', 10. If `c` is
208 /// 'b' or 'B', 11, etc. Returns none if the `char` does not
209 /// refer to a digit in the given radix.
213 /// Fails if given a `radix` outside the range `[0..36]`.
216 pub fn to_digit(c: char, radix: uint) -> Option<uint> {
218 fail!("to_digit: radix {} is too high (maximum 36)", radix);
221 '0' .. '9' => c as uint - ('0' as uint),
222 'a' .. 'z' => c as uint + 10u - ('a' as uint),
223 'A' .. 'Z' => c as uint + 10u - ('A' as uint),
226 if val < radix { Some(val) }
230 /// Convert a char to its uppercase equivalent
232 /// The case-folding performed is the common or simple mapping:
233 /// it maps one unicode codepoint (one char in Rust) to its uppercase equivalent according
234 /// to the Unicode database at ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt
235 /// The additional SpecialCasing.txt is not considered here, as it expands to multiple
236 /// codepoints in some cases.
238 /// A full reference can be found here
239 /// http://www.unicode.org/versions/Unicode4.0.0/ch03.pdf#G33992
243 /// Returns the char itself if no conversion was made
245 pub fn to_uppercase(c: char) -> char {
246 conversions::to_upper(c)
249 /// Convert a char to its lowercase equivalent
251 /// The case-folding performed is the common or simple mapping
252 /// see `to_uppercase` for references and more information
256 /// Returns the char itself if no conversion if possible
258 pub fn to_lowercase(c: char) -> char {
259 conversions::to_lower(c)
263 /// Converts a number to the character representing it
267 /// Returns `Some(char)` if `num` represents one digit under `radix`,
268 /// using one character of `0-9` or `a-z`, or `None` if it doesn't.
272 /// Fails if given an `radix` > 36.
275 pub fn from_digit(num: uint, radix: uint) -> Option<char> {
277 fail!("from_digit: radix {} is to high (maximum 36)", num);
282 Some(transmute(('0' as uint + num) as u32))
284 Some(transmute(('a' as uint + num - 10u) as u32))
292 // Constants from Unicode 6.2.0 Section 3.12 Conjoining Jamo Behavior
293 static S_BASE: uint = 0xAC00;
294 static L_BASE: uint = 0x1100;
295 static V_BASE: uint = 0x1161;
296 static T_BASE: uint = 0x11A7;
297 static L_COUNT: uint = 19;
298 static V_COUNT: uint = 21;
299 static T_COUNT: uint = 28;
300 static N_COUNT: uint = (V_COUNT * T_COUNT);
301 static S_COUNT: uint = (L_COUNT * N_COUNT);
303 // Decompose a precomposed Hangul syllable
304 fn decompose_hangul(s: char, f: |char|) {
305 let si = s as uint - S_BASE;
307 let li = si / N_COUNT;
309 f(transmute((L_BASE + li) as u32));
311 let vi = (si % N_COUNT) / T_COUNT;
312 f(transmute((V_BASE + vi) as u32));
314 let ti = si % T_COUNT;
316 f(transmute((T_BASE + ti) as u32));
321 /// Returns the canonical decomposition of a character
322 pub fn decompose_canonical(c: char, f: |char|) {
323 if (c as uint) < S_BASE || (c as uint) >= (S_BASE + S_COUNT) {
324 decompose::canonical(c, f);
326 decompose_hangul(c, f);
330 /// Returns the compatibility decomposition of a character
331 pub fn decompose_compatible(c: char, f: |char|) {
332 if (c as uint) < S_BASE || (c as uint) >= (S_BASE + S_COUNT) {
333 decompose::compatibility(c, f);
335 decompose_hangul(c, f);
340 /// Returns the hexadecimal Unicode escape of a `char`
342 /// The rules are as follows:
344 /// - chars in [0,0xff] get 2-digit escapes: `\\xNN`
345 /// - chars in [0x100,0xffff] get 4-digit escapes: `\\uNNNN`
346 /// - chars above 0x10000 get 8-digit escapes: `\\UNNNNNNNN`
348 pub fn escape_unicode(c: char, f: |char|) {
349 // avoid calling str::to_str_radix because we don't really need to allocate
353 _ if c <= '\xff' => { f('x'); 2 }
354 _ if c <= '\uffff' => { f('u'); 4 }
357 for offset in range_step::<i32>(4 * (pad - 1), -1, -4) {
359 match ((c as i32) >> offset) & 0xf {
360 i @ 0 .. 9 => { f(transmute('0' as i32 + i)); }
361 i => { f(transmute('a' as i32 + (i - 10))); }
368 /// Returns a 'default' ASCII and C++11-like literal escape of a `char`
370 /// The default is chosen with a bias toward producing literals that are
371 /// legal in a variety of languages, including C++11 and similar C-family
372 /// languages. The exact rules are:
374 /// - Tab, CR and LF are escaped as '\t', '\r' and '\n' respectively.
375 /// - Single-quote, double-quote and backslash chars are backslash-escaped.
376 /// - Any other chars in the range [0x20,0x7e] are not escaped.
377 /// - Any other chars are given hex unicode escapes; see `escape_unicode`.
379 pub fn escape_default(c: char, f: |char|) {
381 '\t' => { f('\\'); f('t'); }
382 '\r' => { f('\\'); f('r'); }
383 '\n' => { f('\\'); f('n'); }
384 '\\' => { f('\\'); f('\\'); }
385 '\'' => { f('\\'); f('\''); }
386 '"' => { f('\\'); f('"'); }
387 '\x20' .. '\x7e' => { f(c); }
388 _ => c.escape_unicode(f),
392 /// Returns the amount of bytes this `char` would need if encoded in UTF-8
393 pub fn len_utf8_bytes(c: char) -> uint {
394 static MAX_ONE_B: uint = 128u;
395 static MAX_TWO_B: uint = 2048u;
396 static MAX_THREE_B: uint = 65536u;
397 static MAX_FOUR_B: uint = 2097152u;
399 let code = c as uint;
401 _ if code < MAX_ONE_B => 1u,
402 _ if code < MAX_TWO_B => 2u,
403 _ if code < MAX_THREE_B => 3u,
404 _ if code < MAX_FOUR_B => 4u,
405 _ => fail!("invalid character!"),
409 /// Useful functions for Unicode characters.
411 /// Returns whether the specified character is considered a Unicode
412 /// alphabetic code point.
413 fn is_alphabetic(&self) -> bool;
415 /// Returns whether the specified character satisfies the 'XID_Start'
416 /// Unicode property.
418 /// 'XID_Start' is a Unicode Derived Property specified in
419 /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
420 /// mostly similar to ID_Start but modified for closure under NFKx.
421 fn is_XID_start(&self) -> bool;
423 /// Returns whether the specified `char` satisfies the 'XID_Continue'
424 /// Unicode property.
426 /// 'XID_Continue' is a Unicode Derived Property specified in
427 /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
428 /// mostly similar to 'ID_Continue' but modified for closure under NFKx.
429 fn is_XID_continue(&self) -> bool;
432 /// Indicates whether a character is in lowercase.
434 /// This is defined according to the terms of the Unicode Derived Core
435 /// Property `Lowercase`.
436 fn is_lowercase(&self) -> bool;
438 /// Indicates whether a character is in uppercase.
440 /// This is defined according to the terms of the Unicode Derived Core
441 /// Property `Uppercase`.
442 fn is_uppercase(&self) -> bool;
444 /// Indicates whether a character is whitespace.
446 /// Whitespace is defined in terms of the Unicode Property `White_Space`.
447 fn is_whitespace(&self) -> bool;
449 /// Indicates whether a character is alphanumeric.
451 /// Alphanumericness is defined in terms of the Unicode General Categories
452 /// 'Nd', 'Nl', 'No' and the Derived Core Property 'Alphabetic'.
453 fn is_alphanumeric(&self) -> bool;
455 /// Indicates whether a character is a control code point.
457 /// Control code points are defined in terms of the Unicode General
459 fn is_control(&self) -> bool;
461 /// Indicates whether the character is numeric (Nd, Nl, or No).
462 fn is_digit(&self) -> bool;
464 /// Checks if a `char` parses as a numeric digit in the given radix.
466 /// Compared to `is_digit()`, this function only recognizes the characters
467 /// `0-9`, `a-z` and `A-Z`.
471 /// Returns `true` if `c` is a valid digit under `radix`, and `false`
476 /// Fails if given a radix > 36.
477 fn is_digit_radix(&self, radix: uint) -> bool;
479 /// Converts a character to the corresponding digit.
483 /// If `c` is between '0' and '9', the corresponding value between 0 and
484 /// 9. If `c` is 'a' or 'A', 10. If `c` is 'b' or 'B', 11, etc. Returns
485 /// none if the character does not refer to a digit in the given radix.
489 /// Fails if given a radix outside the range [0..36].
490 fn to_digit(&self, radix: uint) -> Option<uint>;
492 /// Converts a character to its lowercase equivalent.
494 /// The case-folding performed is the common or simple mapping. See
495 /// `to_uppercase()` for references and more information.
499 /// Returns the lowercase equivalent of the character, or the character
500 /// itself if no conversion is possible.
501 fn to_lowercase(&self) -> char;
503 /// Converts a character to its uppercase equivalent.
505 /// The case-folding performed is the common or simple mapping: it maps
506 /// one unicode codepoint (one character in Rust) to its uppercase
507 /// equivalent according to the Unicode database [1]. The additional
508 /// `SpecialCasing.txt` is not considered here, as it expands to multiple
509 /// codepoints in some cases.
511 /// A full reference can be found here [2].
515 /// Returns the uppercase equivalent of the character, or the character
516 /// itself if no conversion was made.
518 /// [1]: ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt
520 /// [2]: http://www.unicode.org/versions/Unicode4.0.0/ch03.pdf#G33992
521 fn to_uppercase(&self) -> char;
523 /// Converts a number to the character representing it.
527 /// Returns `Some(char)` if `num` represents one digit under `radix`,
528 /// using one character of `0-9` or `a-z`, or `None` if it doesn't.
532 /// Fails if given a radix > 36.
533 fn from_digit(num: uint, radix: uint) -> Option<char>;
535 /// Returns the hexadecimal Unicode escape of a character.
537 /// The rules are as follows:
539 /// * Characters in [0,0xff] get 2-digit escapes: `\\xNN`
540 /// * Characters in [0x100,0xffff] get 4-digit escapes: `\\uNNNN`.
541 /// * Characters above 0x10000 get 8-digit escapes: `\\UNNNNNNNN`.
542 fn escape_unicode(&self, f: |char|);
544 /// Returns a 'default' ASCII and C++11-like literal escape of a
547 /// The default is chosen with a bias toward producing literals that are
548 /// legal in a variety of languages, including C++11 and similar C-family
549 /// languages. The exact rules are:
551 /// * Tab, CR and LF are escaped as '\t', '\r' and '\n' respectively.
552 /// * Single-quote, double-quote and backslash chars are backslash-
554 /// * Any other chars in the range [0x20,0x7e] are not escaped.
555 /// * Any other chars are given hex unicode escapes; see `escape_unicode`.
556 fn escape_default(&self, f: |char|);
558 /// Returns the amount of bytes this character would need if encoded in
560 fn len_utf8_bytes(&self) -> uint;
562 /// Encodes this character as UTF-8 into the provided byte buffer.
564 /// The buffer must be at least 4 bytes long or a runtime failure may
567 /// This will then return the number of bytes written to the slice.
568 fn encode_utf8(&self, dst: &mut [u8]) -> uint;
570 /// Encodes this character as UTF-16 into the provided `u16` buffer.
572 /// The buffer must be at least 2 elements long or a runtime failure may
575 /// This will then return the number of `u16`s written to the slice.
576 fn encode_utf16(&self, dst: &mut [u16]) -> uint;
580 fn is_alphabetic(&self) -> bool { is_alphabetic(*self) }
582 fn is_XID_start(&self) -> bool { is_XID_start(*self) }
584 fn is_XID_continue(&self) -> bool { is_XID_continue(*self) }
586 fn is_lowercase(&self) -> bool { is_lowercase(*self) }
588 fn is_uppercase(&self) -> bool { is_uppercase(*self) }
590 fn is_whitespace(&self) -> bool { is_whitespace(*self) }
592 fn is_alphanumeric(&self) -> bool { is_alphanumeric(*self) }
594 fn is_control(&self) -> bool { is_control(*self) }
596 fn is_digit(&self) -> bool { is_digit(*self) }
598 fn is_digit_radix(&self, radix: uint) -> bool { is_digit_radix(*self, radix) }
600 fn to_digit(&self, radix: uint) -> Option<uint> { to_digit(*self, radix) }
602 fn to_lowercase(&self) -> char { to_lowercase(*self) }
604 fn to_uppercase(&self) -> char { to_uppercase(*self) }
606 fn from_digit(num: uint, radix: uint) -> Option<char> { from_digit(num, radix) }
608 fn escape_unicode(&self, f: |char|) { escape_unicode(*self, f) }
610 fn escape_default(&self, f: |char|) { escape_default(*self, f) }
612 fn len_utf8_bytes(&self) -> uint { len_utf8_bytes(*self) }
614 fn encode_utf8(&self, dst: &mut [u8]) -> uint {
615 let code = *self as uint;
616 if code < MAX_ONE_B {
619 } else if code < MAX_TWO_B {
620 dst[0] = (code >> 6u & 31u | TAG_TWO_B) as u8;
621 dst[1] = (code & 63u | TAG_CONT) as u8;
623 } else if code < MAX_THREE_B {
624 dst[0] = (code >> 12u & 15u | TAG_THREE_B) as u8;
625 dst[1] = (code >> 6u & 63u | TAG_CONT) as u8;
626 dst[2] = (code & 63u | TAG_CONT) as u8;
629 dst[0] = (code >> 18u & 7u | TAG_FOUR_B) as u8;
630 dst[1] = (code >> 12u & 63u | TAG_CONT) as u8;
631 dst[2] = (code >> 6u & 63u | TAG_CONT) as u8;
632 dst[3] = (code & 63u | TAG_CONT) as u8;
637 fn encode_utf16(&self, dst: &mut [u16]) -> uint {
638 let mut ch = *self as uint;
639 if (ch & 0xFFFF_u) == ch {
640 // The BMP falls through (assuming non-surrogate, as it
642 assert!(ch <= 0xD7FF_u || ch >= 0xE000_u);
646 // Supplementary planes break into surrogates.
647 assert!(ch >= 0x1_0000_u && ch <= 0x10_FFFF_u);
649 dst[0] = 0xD800_u16 | ((ch >> 10) as u16);
650 dst[1] = 0xDC00_u16 | ((ch as u16) & 0x3FF_u16);
659 fn eq(&self, other: &char) -> bool { (*self) == (*other) }
665 fn lt(&self, other: &char) -> bool { *self < *other }
669 impl Default for char {
671 fn default() -> char { '\x00' }
675 fn test_is_lowercase() {
676 assert!('a'.is_lowercase());
677 assert!('ö'.is_lowercase());
678 assert!('ß'.is_lowercase());
679 assert!(!'Ü'.is_lowercase());
680 assert!(!'P'.is_lowercase());
684 fn test_is_uppercase() {
685 assert!(!'h'.is_uppercase());
686 assert!(!'ä'.is_uppercase());
687 assert!(!'ß'.is_uppercase());
688 assert!('Ö'.is_uppercase());
689 assert!('T'.is_uppercase());
693 fn test_is_whitespace() {
694 assert!(' '.is_whitespace());
695 assert!('\u2007'.is_whitespace());
696 assert!('\t'.is_whitespace());
697 assert!('\n'.is_whitespace());
698 assert!(!'a'.is_whitespace());
699 assert!(!'_'.is_whitespace());
700 assert!(!'\u0000'.is_whitespace());
705 assert_eq!('0'.to_digit(10u), Some(0u));
706 assert_eq!('1'.to_digit(2u), Some(1u));
707 assert_eq!('2'.to_digit(3u), Some(2u));
708 assert_eq!('9'.to_digit(10u), Some(9u));
709 assert_eq!('a'.to_digit(16u), Some(10u));
710 assert_eq!('A'.to_digit(16u), Some(10u));
711 assert_eq!('b'.to_digit(16u), Some(11u));
712 assert_eq!('B'.to_digit(16u), Some(11u));
713 assert_eq!('z'.to_digit(36u), Some(35u));
714 assert_eq!('Z'.to_digit(36u), Some(35u));
715 assert_eq!(' '.to_digit(10u), None);
716 assert_eq!('$'.to_digit(36u), None);
720 fn test_to_lowercase() {
721 assert_eq!('A'.to_lowercase(), 'a');
722 assert_eq!('Ö'.to_lowercase(), 'ö');
723 assert_eq!('ß'.to_lowercase(), 'ß');
724 assert_eq!('Ü'.to_lowercase(), 'ü');
725 assert_eq!('💩'.to_lowercase(), '💩');
726 assert_eq!('Σ'.to_lowercase(), 'σ');
727 assert_eq!('Τ'.to_lowercase(), 'τ');
728 assert_eq!('Ι'.to_lowercase(), 'ι');
729 assert_eq!('Γ'.to_lowercase(), 'γ');
730 assert_eq!('Μ'.to_lowercase(), 'μ');
731 assert_eq!('Α'.to_lowercase(), 'α');
732 assert_eq!('Σ'.to_lowercase(), 'σ');
736 fn test_to_uppercase() {
737 assert_eq!('a'.to_uppercase(), 'A');
738 assert_eq!('ö'.to_uppercase(), 'Ö');
739 assert_eq!('ß'.to_uppercase(), 'ß'); // not ẞ: Latin capital letter sharp s
740 assert_eq!('ü'.to_uppercase(), 'Ü');
741 assert_eq!('💩'.to_uppercase(), '💩');
743 assert_eq!('σ'.to_uppercase(), 'Σ');
744 assert_eq!('τ'.to_uppercase(), 'Τ');
745 assert_eq!('ι'.to_uppercase(), 'Ι');
746 assert_eq!('γ'.to_uppercase(), 'Γ');
747 assert_eq!('μ'.to_uppercase(), 'Μ');
748 assert_eq!('α'.to_uppercase(), 'Α');
749 assert_eq!('ς'.to_uppercase(), 'Σ');
753 fn test_is_control() {
754 assert!('\u0000'.is_control());
755 assert!('\u0003'.is_control());
756 assert!('\u0006'.is_control());
757 assert!('\u0009'.is_control());
758 assert!('\u007f'.is_control());
759 assert!('\u0092'.is_control());
760 assert!(!'\u0020'.is_control());
761 assert!(!'\u0055'.is_control());
762 assert!(!'\u0068'.is_control());
767 assert!('2'.is_digit());
768 assert!('7'.is_digit());
769 assert!(!'c'.is_digit());
770 assert!(!'i'.is_digit());
771 assert!(!'z'.is_digit());
772 assert!(!'Q'.is_digit());
776 fn test_escape_default() {
777 fn string(c: char) -> ~str {
778 let mut result = StrBuf::new();
779 escape_default(c, |c| { result.push_char(c); });
780 return result.into_owned();
782 assert_eq!(string('\n'), ~"\\n");
783 assert_eq!(string('\r'), ~"\\r");
784 assert_eq!(string('\''), ~"\\'");
785 assert_eq!(string('"'), ~"\\\"");
786 assert_eq!(string(' '), ~" ");
787 assert_eq!(string('a'), ~"a");
788 assert_eq!(string('~'), ~"~");
789 assert_eq!(string('\x00'), ~"\\x00");
790 assert_eq!(string('\x1f'), ~"\\x1f");
791 assert_eq!(string('\x7f'), ~"\\x7f");
792 assert_eq!(string('\xff'), ~"\\xff");
793 assert_eq!(string('\u011b'), ~"\\u011b");
794 assert_eq!(string('\U0001d4b6'), ~"\\U0001d4b6");
798 fn test_escape_unicode() {
799 fn string(c: char) -> ~str {
800 let mut result = StrBuf::new();
801 escape_unicode(c, |c| { result.push_char(c); });
802 return result.into_owned();
804 assert_eq!(string('\x00'), ~"\\x00");
805 assert_eq!(string('\n'), ~"\\x0a");
806 assert_eq!(string(' '), ~"\\x20");
807 assert_eq!(string('a'), ~"\\x61");
808 assert_eq!(string('\u011b'), ~"\\u011b");
809 assert_eq!(string('\U0001d4b6'), ~"\\U0001d4b6");
815 let s = 't'.to_str();
820 fn test_encode_utf8() {
821 fn check(input: char, expect: &[u8]) {
822 let mut buf = [0u8, ..4];
823 let n = input.encode_utf8(buf /* as mut slice! */);
824 assert_eq!(buf.slice_to(n), expect);
828 check('\u00e9', [0xc3, 0xa9]);
829 check('\ua66e', [0xea, 0x99, 0xae]);
830 check('\U0001f4a9', [0xf0, 0x9f, 0x92, 0xa9]);
834 fn test_encode_utf16() {
835 fn check(input: char, expect: &[u16]) {
836 let mut buf = [0u16, ..2];
837 let n = input.encode_utf16(buf /* as mut slice! */);
838 assert_eq!(buf.slice_to(n), expect);
841 check('x', [0x0078]);
842 check('\u00e9', [0x00e9]);
843 check('\ua66e', [0xa66e]);
844 check('\U0001f4a9', [0xd83d, 0xdca9]);