2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
//! Utilities for manipulating the char type
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
use option::{None, Option, Some};
|
2012-12-23 16:41:37 -06:00
|
|
|
use str;
|
|
|
|
use u32;
|
|
|
|
use uint;
|
|
|
|
use unicode;
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2013-02-28 10:57:33 -06:00
|
|
|
#[cfg(notest)] use cmp::Eq;
|
|
|
|
|
2011-12-23 20:48:08 -06:00
|
|
|
/*
|
|
|
|
Lu Uppercase_Letter an uppercase letter
|
|
|
|
Ll Lowercase_Letter a lowercase letter
|
|
|
|
Lt Titlecase_Letter a digraphic character, with first part uppercase
|
|
|
|
Lm Modifier_Letter a modifier letter
|
|
|
|
Lo Other_Letter other letters, including syllables and ideographs
|
|
|
|
Mn Nonspacing_Mark a nonspacing combining mark (zero advance width)
|
|
|
|
Mc Spacing_Mark a spacing combining mark (positive advance width)
|
|
|
|
Me Enclosing_Mark an enclosing combining mark
|
|
|
|
Nd Decimal_Number a decimal digit
|
|
|
|
Nl Letter_Number a letterlike numeric character
|
|
|
|
No Other_Number a numeric character of other type
|
|
|
|
Pc Connector_Punctuation a connecting punctuation mark, like a tie
|
|
|
|
Pd Dash_Punctuation a dash or hyphen punctuation mark
|
|
|
|
Ps Open_Punctuation an opening punctuation mark (of a pair)
|
|
|
|
Pe Close_Punctuation a closing punctuation mark (of a pair)
|
|
|
|
Pi Initial_Punctuation an initial quotation mark
|
|
|
|
Pf Final_Punctuation a final quotation mark
|
|
|
|
Po Other_Punctuation a punctuation mark of other type
|
|
|
|
Sm Math_Symbol a symbol of primarily mathematical use
|
|
|
|
Sc Currency_Symbol a currency sign
|
|
|
|
Sk Modifier_Symbol a non-letterlike modifier symbol
|
|
|
|
So Other_Symbol a symbol of other type
|
|
|
|
Zs Space_Separator a space character (of various non-zero widths)
|
|
|
|
Zl Line_Separator U+2028 LINE SEPARATOR only
|
|
|
|
Zp Paragraph_Separator U+2029 PARAGRAPH SEPARATOR only
|
|
|
|
Cc Control a C0 or C1 control code
|
|
|
|
Cf Format a format control character
|
|
|
|
Cs Surrogate a surrogate code point
|
|
|
|
Co Private_Use a private-use character
|
|
|
|
Cn Unassigned a reserved unassigned code point or a noncharacter
|
|
|
|
*/
|
|
|
|
|
2012-09-26 20:16:44 -05:00
|
|
|
pub use is_alphabetic = unicode::derived_property::Alphabetic;
|
|
|
|
pub use is_XID_start = unicode::derived_property::XID_Start;
|
|
|
|
pub use is_XID_continue = unicode::derived_property::XID_Continue;
|
2011-12-23 20:48:08 -06:00
|
|
|
|
2011-12-24 03:41:11 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Indicates whether a character is in lower case, defined
|
|
|
|
* in terms of the Unicode General Category 'Ll'
|
|
|
|
*/
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn is_lowercase(c: char) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return unicode::general_category::Ll(c);
|
2011-12-24 03:41:11 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Indicates whether a character is in upper case, defined
|
|
|
|
* in terms of the Unicode General Category 'Lu'.
|
|
|
|
*/
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn is_uppercase(c: char) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return unicode::general_category::Lu(c);
|
2011-12-24 03:41:11 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
2012-09-20 17:33:52 -05:00
|
|
|
* Indicates whether a character is whitespace. Whitespace is defined in
|
2012-07-04 16:53:12 -05:00
|
|
|
* terms of the Unicode General Categories 'Zs', 'Zl', 'Zp'
|
|
|
|
* additional 'Cc'-category control codes in the range [0x09, 0x0d]
|
|
|
|
*/
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn is_whitespace(c: char) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return ('\x09' <= c && c <= '\x0d')
|
2011-12-23 20:48:08 -06:00
|
|
|
|| unicode::general_category::Zs(c)
|
|
|
|
|| unicode::general_category::Zl(c)
|
|
|
|
|| unicode::general_category::Zp(c);
|
|
|
|
}
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
2012-09-20 17:33:52 -05:00
|
|
|
* Indicates whether a character is alphanumeric. Alphanumericness is
|
|
|
|
* defined in terms of the Unicode General Categories 'Nd', 'Nl', 'No'
|
|
|
|
* and the Derived Core Property 'Alphabetic'.
|
2012-07-04 16:53:12 -05:00
|
|
|
*/
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn is_alphanumeric(c: char) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return unicode::derived_property::Alphabetic(c) ||
|
2011-12-23 20:48:08 -06:00
|
|
|
unicode::general_category::Nd(c) ||
|
|
|
|
unicode::general_category::Nl(c) ||
|
|
|
|
unicode::general_category::No(c);
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Indicates whether the character is an ASCII character
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn is_ascii(c: char) -> bool {
|
2012-02-08 03:28:23 -06:00
|
|
|
c - ('\x7F' & c) == '\x00'
|
|
|
|
}
|
2011-12-23 20:48:08 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Indicates whether the character is numeric (Nd, Nl, or No)
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn is_digit(c: char) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return unicode::general_category::Nd(c) ||
|
2012-02-08 03:52:09 -06:00
|
|
|
unicode::general_category::Nl(c) ||
|
|
|
|
unicode::general_category::No(c);
|
|
|
|
}
|
|
|
|
|
2013-01-20 14:28:12 -06:00
|
|
|
/**
|
|
|
|
* Checks if a character parses as a numeric digit in the given radix.
|
|
|
|
* Compared to `is_digit()`, this function only recognizes the ascii
|
|
|
|
* characters `0-9`, `a-z` and `A-Z`.
|
|
|
|
*
|
|
|
|
* Returns `true` if `c` is a valid digit under `radix`, and `false`
|
|
|
|
* otherwise.
|
|
|
|
*
|
|
|
|
* Fails if given a `radix` > 36.
|
|
|
|
*
|
|
|
|
* Note: This just wraps `to_digit()`.
|
|
|
|
*/
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn is_digit_radix(c: char, radix: uint) -> bool {
|
2013-01-20 14:28:12 -06:00
|
|
|
match to_digit(c, radix) {
|
|
|
|
Some(_) => true,
|
|
|
|
None => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Convert a char to the corresponding digit.
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* If `c` is between '0' and '9', the corresponding value
|
|
|
|
* between 0 and 9. If `c` is 'a' or 'A', 10. If `c` is
|
|
|
|
* 'b' or 'B', 11, etc. Returns none if the char does not
|
|
|
|
* refer to a digit in the given radix.
|
2013-01-20 14:28:12 -06:00
|
|
|
*
|
|
|
|
* # Failure
|
|
|
|
* Fails if given a `radix` outside the range `[0..36]`.
|
2012-07-04 16:53:12 -05:00
|
|
|
*/
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn to_digit(c: char, radix: uint) -> Option<uint> {
|
2013-01-20 14:28:12 -06:00
|
|
|
if radix > 36 {
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!(fmt!("to_digit: radix %? is to high (maximum 36)", radix));
|
2013-01-20 14:28:12 -06:00
|
|
|
}
|
2012-08-06 14:34:08 -05:00
|
|
|
let val = match c {
|
2012-09-01 20:38:05 -05:00
|
|
|
'0' .. '9' => c as uint - ('0' as uint),
|
|
|
|
'a' .. 'z' => c as uint + 10u - ('a' as uint),
|
|
|
|
'A' .. 'Z' => c as uint + 10u - ('A' as uint),
|
2012-08-20 14:23:37 -05:00
|
|
|
_ => return None
|
2012-02-22 06:18:15 -06:00
|
|
|
};
|
2012-08-20 14:23:37 -05:00
|
|
|
if val < radix { Some(val) }
|
|
|
|
else { None }
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
|
2013-01-20 14:28:12 -06:00
|
|
|
/**
|
|
|
|
* Converts a number to the ascii character representing it.
|
|
|
|
*
|
|
|
|
* Returns `Some(char)` if `num` represents one digit under `radix`,
|
|
|
|
* using one character of `0-9` or `a-z`, or `None` if it doesn't.
|
|
|
|
*
|
|
|
|
* Fails if given an `radix` > 36.
|
|
|
|
*/
|
|
|
|
#[inline]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn from_digit(num: uint, radix: uint) -> Option<char> {
|
2013-01-20 14:28:12 -06:00
|
|
|
if radix > 36 {
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!(fmt!("from_digit: radix %? is to high (maximum 36)", num));
|
2013-01-20 14:28:12 -06:00
|
|
|
}
|
|
|
|
if num < radix {
|
|
|
|
if num < 10 {
|
|
|
|
Some(('0' as uint + num) as char)
|
|
|
|
} else {
|
|
|
|
Some(('a' as uint + num - 10u) as char)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Return the hexadecimal unicode escape of a char.
|
|
|
|
*
|
|
|
|
* The rules are as follows:
|
|
|
|
*
|
|
|
|
* - chars in [0,0xff] get 2-digit escapes: `\\xNN`
|
|
|
|
* - chars in [0x100,0xffff] get 4-digit escapes: `\\uNNNN`
|
|
|
|
* - chars above 0x10000 get 8-digit escapes: `\\UNNNNNNNN`
|
|
|
|
*/
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn escape_unicode(c: char) -> ~str {
|
2013-01-24 14:47:57 -06:00
|
|
|
let s = u32::to_str_radix(c as u32, 16u);
|
2012-05-31 17:31:13 -05:00
|
|
|
let (c, pad) = (if c <= '\xff' { ('x', 2u) }
|
|
|
|
else if c <= '\uffff' { ('u', 4u) }
|
|
|
|
else { ('U', 8u) });
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(str::len(s) <= pad);
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut out = ~"\\";
|
2012-11-17 12:13:11 -06:00
|
|
|
unsafe {
|
|
|
|
str::push_str(&mut out, str::from_char(c));
|
2012-11-17 13:00:38 -06:00
|
|
|
for uint::range(str::len(s), pad) |_i|
|
|
|
|
{ str::push_str(&mut out, ~"0"); }
|
2012-11-17 12:13:11 -06:00
|
|
|
str::push_str(&mut out, s);
|
|
|
|
}
|
2012-12-12 17:38:50 -06:00
|
|
|
out
|
2012-05-31 17:31:13 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Return a 'default' ASCII and C++11-like char-literal escape of a char.
|
|
|
|
*
|
|
|
|
* The default is chosen with a bias toward producing literals that are
|
|
|
|
* legal in a variety of languages, including C++11 and similar C-family
|
|
|
|
* languages. The exact rules are:
|
|
|
|
*
|
|
|
|
* - Tab, CR and LF are escaped as '\t', '\r' and '\n' respectively.
|
|
|
|
* - Single-quote, double-quote and backslash chars are backslash-escaped.
|
|
|
|
* - Any other chars in the range [0x20,0x7e] are not escaped.
|
|
|
|
* - Any other chars are given hex unicode escapes; see `escape_unicode`.
|
|
|
|
*/
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn escape_default(c: char) -> ~str {
|
2012-08-06 14:34:08 -05:00
|
|
|
match c {
|
2012-08-03 21:59:04 -05:00
|
|
|
'\t' => ~"\\t",
|
|
|
|
'\r' => ~"\\r",
|
|
|
|
'\n' => ~"\\n",
|
|
|
|
'\\' => ~"\\\\",
|
|
|
|
'\'' => ~"\\'",
|
|
|
|
'"' => ~"\\\"",
|
2012-09-01 20:38:05 -05:00
|
|
|
'\x20' .. '\x7e' => str::from_char(c),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => escape_unicode(c)
|
2012-05-31 17:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Compare two chars
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* -1 if a < b, 0 if a == b, +1 if a > b
|
|
|
|
*/
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn cmp(a: char, b: char) -> int {
|
2012-08-01 19:30:05 -05:00
|
|
|
return if b > a { -1 }
|
2011-12-13 18:25:51 -06:00
|
|
|
else if b < a { 1 }
|
|
|
|
else { 0 }
|
2011-12-24 03:41:11 -06:00
|
|
|
}
|
2012-01-17 19:28:21 -06:00
|
|
|
|
2012-11-30 02:47:45 -06:00
|
|
|
#[cfg(notest)]
|
2013-02-14 13:47:00 -06:00
|
|
|
impl Eq for char {
|
2013-03-21 23:20:48 -05:00
|
|
|
fn eq(&self, other: &char) -> bool { (*self) == (*other) }
|
|
|
|
fn ne(&self, other: &char) -> bool { (*self) != (*other) }
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2012-01-17 19:28:21 -06:00
|
|
|
#[test]
|
|
|
|
fn test_is_lowercase() {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(is_lowercase('a'));
|
|
|
|
fail_unless!(is_lowercase('ö'));
|
|
|
|
fail_unless!(is_lowercase('ß'));
|
|
|
|
fail_unless!(!is_lowercase('Ü'));
|
|
|
|
fail_unless!(!is_lowercase('P'));
|
2012-01-17 19:28:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_is_uppercase() {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(!is_uppercase('h'));
|
|
|
|
fail_unless!(!is_uppercase('ä'));
|
|
|
|
fail_unless!(!is_uppercase('ß'));
|
|
|
|
fail_unless!(is_uppercase('Ö'));
|
|
|
|
fail_unless!(is_uppercase('T'));
|
2012-01-17 19:28:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_is_whitespace() {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(is_whitespace(' '));
|
|
|
|
fail_unless!(is_whitespace('\u2007'));
|
|
|
|
fail_unless!(is_whitespace('\t'));
|
|
|
|
fail_unless!(is_whitespace('\n'));
|
2012-01-17 19:28:21 -06:00
|
|
|
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(!is_whitespace('a'));
|
|
|
|
fail_unless!(!is_whitespace('_'));
|
|
|
|
fail_unless!(!is_whitespace('\u0000'));
|
2012-01-17 19:28:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_to_digit() {
|
2013-03-13 17:30:37 -05:00
|
|
|
assert_eq!(to_digit('0', 10u), Some(0u));
|
|
|
|
assert_eq!(to_digit('1', 2u), Some(1u));
|
|
|
|
assert_eq!(to_digit('2', 3u), Some(2u));
|
|
|
|
assert_eq!(to_digit('9', 10u), Some(9u));
|
|
|
|
assert_eq!(to_digit('a', 16u), Some(10u));
|
|
|
|
assert_eq!(to_digit('A', 16u), Some(10u));
|
|
|
|
assert_eq!(to_digit('b', 16u), Some(11u));
|
|
|
|
assert_eq!(to_digit('B', 16u), Some(11u));
|
|
|
|
assert_eq!(to_digit('z', 36u), Some(35u));
|
|
|
|
assert_eq!(to_digit('Z', 36u), Some(35u));
|
2012-08-20 14:23:37 -05:00
|
|
|
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(to_digit(' ', 10u).is_none());
|
|
|
|
fail_unless!(to_digit('$', 36u).is_none());
|
2012-01-17 19:28:21 -06:00
|
|
|
}
|
|
|
|
|
2012-02-08 03:28:23 -06:00
|
|
|
#[test]
|
2012-06-24 22:18:18 -05:00
|
|
|
fn test_is_ascii() {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(str::all(~"banana", is_ascii));
|
|
|
|
fail_unless!(! str::all(~"ประเทศไทย中华Việt Nam", is_ascii));
|
2012-02-08 03:28:23 -06:00
|
|
|
}
|
|
|
|
|
2012-02-08 03:52:09 -06:00
|
|
|
#[test]
|
|
|
|
fn test_is_digit() {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(is_digit('2'));
|
|
|
|
fail_unless!(is_digit('7'));
|
|
|
|
fail_unless!(! is_digit('c'));
|
|
|
|
fail_unless!(! is_digit('i'));
|
|
|
|
fail_unless!(! is_digit('z'));
|
|
|
|
fail_unless!(! is_digit('Q'));
|
2012-02-08 03:52:09 -06:00
|
|
|
}
|
|
|
|
|
2012-05-31 17:31:13 -05:00
|
|
|
#[test]
|
|
|
|
fn test_escape_default() {
|
2013-03-13 17:30:37 -05:00
|
|
|
assert_eq!(escape_default('\n'), ~"\\n");
|
|
|
|
assert_eq!(escape_default('\r'), ~"\\r");
|
|
|
|
assert_eq!(escape_default('\''), ~"\\'");
|
|
|
|
assert_eq!(escape_default('"'), ~"\\\"");
|
|
|
|
assert_eq!(escape_default(' '), ~" ");
|
|
|
|
assert_eq!(escape_default('a'), ~"a");
|
|
|
|
assert_eq!(escape_default('~'), ~"~");
|
|
|
|
assert_eq!(escape_default('\x00'), ~"\\x00");
|
|
|
|
assert_eq!(escape_default('\x1f'), ~"\\x1f");
|
|
|
|
assert_eq!(escape_default('\x7f'), ~"\\x7f");
|
|
|
|
assert_eq!(escape_default('\xff'), ~"\\xff");
|
|
|
|
assert_eq!(escape_default('\u011b'), ~"\\u011b");
|
|
|
|
assert_eq!(escape_default('\U0001d4b6'), ~"\\U0001d4b6");
|
2012-05-31 17:31:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_escape_unicode() {
|
2013-03-13 17:30:37 -05:00
|
|
|
assert_eq!(escape_unicode('\x00'), ~"\\x00");
|
|
|
|
assert_eq!(escape_unicode('\n'), ~"\\x0a");
|
|
|
|
assert_eq!(escape_unicode(' '), ~"\\x20");
|
|
|
|
assert_eq!(escape_unicode('a'), ~"\\x61");
|
|
|
|
assert_eq!(escape_unicode('\u011b'), ~"\\u011b");
|
|
|
|
assert_eq!(escape_unicode('\U0001d4b6'), ~"\\U0001d4b6");
|
2012-05-31 17:31:13 -05:00
|
|
|
}
|