Skip to content

Commit

Permalink
unicode: Add stability attributes to u_char
Browse files Browse the repository at this point in the history
Free functions deprecated. UnicodeChar experimental pending
final decisions about prelude.
  • Loading branch information
brson committed Nov 21, 2014
1 parent d6ee804 commit 76ddd2b
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 59 deletions.
9 changes: 4 additions & 5 deletions src/libfmt_macros/lib.rs
Expand Up @@ -26,7 +26,6 @@ pub use self::Alignment::*;
pub use self::Flag::*;
pub use self::Count::*;

use std::char;
use std::str;
use std::string;

Expand Down Expand Up @@ -221,7 +220,7 @@ impl<'a> Parser<'a> {
fn ws(&mut self) {
loop {
match self.cur.clone().next() {
Some((_, c)) if char::is_whitespace(c) => { self.cur.next(); }
Some((_, c)) if c.is_whitespace() => { self.cur.next(); }
Some(..) | None => { return }
}
}
Expand Down Expand Up @@ -261,7 +260,7 @@ impl<'a> Parser<'a> {
Some(i) => { ArgumentIs(i) }
None => {
match self.cur.clone().next() {
Some((_, c)) if char::is_alphabetic(c) => {
Some((_, c)) if c.is_alphabetic() => {
ArgumentNamed(self.word())
}
_ => ArgumentNext
Expand Down Expand Up @@ -384,7 +383,7 @@ impl<'a> Parser<'a> {
/// characters.
fn word(&mut self) -> &'a str {
let start = match self.cur.clone().next() {
Some((pos, c)) if char::is_XID_start(c) => {
Some((pos, c)) if c.is_XID_start() => {
self.cur.next();
pos
}
Expand All @@ -393,7 +392,7 @@ impl<'a> Parser<'a> {
let mut end;
loop {
match self.cur.clone().next() {
Some((_, c)) if char::is_XID_continue(c) => {
Some((_, c)) if c.is_XID_continue() => {
self.cur.next();
}
Some((pos, _)) => { end = pos; break }
Expand Down
2 changes: 1 addition & 1 deletion src/libgetopts/lib.rs
Expand Up @@ -886,7 +886,7 @@ fn each_split_within<'a>(ss: &'a str, lim: uint, it: |&'a str| -> bool)
}

let machine: |&mut bool, (uint, char)| -> bool = |cont, (i, c)| {
let whitespace = if ::std::char::is_whitespace(c) { Ws } else { Cr };
let whitespace = if c.is_whitespace() { Ws } else { Cr };
let limit = if (i - slice_start + 1) <= lim { UnderLim } else { OverLim };

state = match (state, whitespace, limit) {
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_trans/back/link.rs
Expand Up @@ -27,7 +27,6 @@ use util::common::time;
use util::ppaux;
use util::sha2::{Digest, Sha256};

use std::char;
use std::io::fs::PathExtensions;
use std::io::{fs, TempDir, Command};
use std::io;
Expand Down Expand Up @@ -272,7 +271,7 @@ pub fn sanitize(s: &str) -> String {
// Underscore-qualify anything that didn't start as an ident.
if result.len() > 0u &&
result.as_bytes()[0] != '_' as u8 &&
! char::is_XID_start(result.as_bytes()[0] as char) {
! (result.as_bytes()[0] as char).is_XID_start() {
return format!("_{}", result.as_slice());
}

Expand Down
5 changes: 2 additions & 3 deletions src/librustdoc/test.rs
Expand Up @@ -9,7 +9,6 @@
// except according to those terms.

use std::cell::RefCell;
use std::char;
use std::dynamic_lib::DynamicLibrary;
use std::io::{Command, TempDir};
use std::io;
Expand Down Expand Up @@ -300,8 +299,8 @@ impl Collector {
// we use these headings as test names, so it's good if
// they're valid identifiers.
let name = name.chars().enumerate().map(|(i, c)| {
if (i == 0 && char::is_XID_start(c)) ||
(i != 0 && char::is_XID_continue(c)) {
if (i == 0 && c.is_XID_start()) ||
(i != 0 && c.is_XID_continue()) {
c
} else {
'_'
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/parse/lexer/mod.rs
Expand Up @@ -1385,7 +1385,7 @@ fn ident_start(c: Option<char>) -> bool {
(c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| c == '_'
|| (c > '\x7f' && char::is_XID_start(c))
|| (c > '\x7f' && c.is_XID_start())
}

fn ident_continue(c: Option<char>) -> bool {
Expand All @@ -1395,7 +1395,7 @@ fn ident_continue(c: Option<char>) -> bool {
|| (c >= 'A' && c <= 'Z')
|| (c >= '0' && c <= '9')
|| c == '_'
|| (c > '\x7f' && char::is_XID_continue(c))
|| (c > '\x7f' && c.is_XID_continue())
}

#[cfg(test)]
Expand Down
103 changes: 64 additions & 39 deletions src/libunicode/u_char.rs
Expand Up @@ -20,12 +20,9 @@ use tables::{derived_property, property, general_category, conversions, charwidt

/// Returns whether the specified `char` is considered a Unicode alphabetic
/// code point
#[deprecated = "use UnicodeChar::is_alphabetic"]
pub fn is_alphabetic(c: char) -> bool {
match c {
'a' ... 'z' | 'A' ... 'Z' => true,
c if c > '\x7f' => derived_property::Alphabetic(c),
_ => false
}
c.is_alphabetic()
}

/// Returns whether the specified `char` satisfies the 'XID_Start' Unicode property
Expand All @@ -34,6 +31,7 @@ pub fn is_alphabetic(c: char) -> bool {
/// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
/// mostly similar to ID_Start but modified for closure under NFKx.
#[allow(non_snake_case)]
#[deprecated = "use UnicodeChar::is_XID_start"]
pub fn is_XID_start(c: char) -> bool { derived_property::XID_Start(c) }

/// Returns whether the specified `char` satisfies the 'XID_Continue' Unicode property
Expand All @@ -42,6 +40,7 @@ pub fn is_XID_start(c: char) -> bool { derived_property::XID_Start(c) }
/// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
/// mostly similar to 'ID_Continue' but modified for closure under NFKx.
#[allow(non_snake_case)]
#[deprecated = "use UnicodeChar::is_XID_continue"]
pub fn is_XID_continue(c: char) -> bool { derived_property::XID_Continue(c) }

///
Expand All @@ -50,12 +49,9 @@ pub fn is_XID_continue(c: char) -> bool { derived_property::XID_Continue(c) }
/// This is defined according to the terms of the Unicode Derived Core Property 'Lowercase'.
///
#[inline]
#[deprecated = "use UnicodeChar::is_lowercase"]
pub fn is_lowercase(c: char) -> bool {
match c {
'a' ... 'z' => true,
c if c > '\x7f' => derived_property::Lowercase(c),
_ => false
}
c.is_lowercase()
}

///
Expand All @@ -64,12 +60,9 @@ pub fn is_lowercase(c: char) -> bool {
/// This is defined according to the terms of the Unicode Derived Core Property 'Uppercase'.
///
#[inline]
#[deprecated = "use UnicodeChar::is_uppercase"]
pub fn is_uppercase(c: char) -> bool {
match c {
'A' ... 'Z' => true,
c if c > '\x7f' => derived_property::Uppercase(c),
_ => false
}
c.is_uppercase()
}

///
Expand All @@ -78,12 +71,9 @@ pub fn is_uppercase(c: char) -> bool {
/// Whitespace is defined in terms of the Unicode Property 'White_Space'.
///
#[inline]
#[deprecated = "use UnicodeChar::is_whitespace"]
pub fn is_whitespace(c: char) -> bool {
match c {
' ' | '\x09' ... '\x0d' => true,
c if c > '\x7f' => property::White_Space(c),
_ => false
}
c.is_whitespace()
}

///
Expand All @@ -93,9 +83,9 @@ pub fn is_whitespace(c: char) -> bool {
/// 'Nd', 'Nl', 'No' and the Derived Core Property 'Alphabetic'.
///
#[inline]
#[deprecated = "use UnicodeChar::is_alphanumeric"]
pub fn is_alphanumeric(c: char) -> bool {
is_alphabetic(c)
|| is_digit(c)
c.is_alphanumeric()
}

///
Expand All @@ -105,16 +95,14 @@ pub fn is_alphanumeric(c: char) -> bool {
/// 'Cc'.
///
#[inline]
#[deprecated = "use UnicodeChar::is_control"]
pub fn is_control(c: char) -> bool { general_category::Cc(c) }

/// Indicates whether the `char` is numeric (Nd, Nl, or No)
#[inline]
#[deprecated = "use UnicodeChar::is_numeric"]
pub fn is_digit(c: char) -> bool {
match c {
'0' ... '9' => true,
c if c > '\x7f' => general_category::N(c),
_ => false
}
c.is_numeric()
}

/// Convert a char to its uppercase equivalent
Expand All @@ -132,6 +120,7 @@ pub fn is_digit(c: char) -> bool {
///
/// Returns the char itself if no conversion was made
#[inline]
#[deprecated = "use UnicodeChar::to_uppercase"]
pub fn to_uppercase(c: char) -> char {
conversions::to_upper(c)
}
Expand All @@ -145,6 +134,7 @@ pub fn to_uppercase(c: char) -> char {
///
/// Returns the char itself if no conversion if possible
#[inline]
#[deprecated = "use UnicodeChar::to_lowercase"]
pub fn to_lowercase(c: char) -> char {
conversions::to_lower(c)
}
Expand All @@ -158,11 +148,13 @@ pub fn to_lowercase(c: char) -> char {
/// [Unicode Standard Annex #11](http://www.unicode.org/reports/tr11/)
/// recommends that these characters be treated as 1 column (i.e.,
/// `is_cjk` = `false`) if the context cannot be reliably determined.
#[deprecated = "use UnicodeChar::width"]
pub fn width(c: char, is_cjk: bool) -> Option<uint> {
charwidth::width(c, is_cjk)
}

/// Useful functions for Unicode characters.
#[experimental = "pending prelude organization"]
pub trait UnicodeChar {
/// Returns whether the specified character is considered a Unicode
/// alphabetic code point.
Expand Down Expand Up @@ -265,29 +257,62 @@ pub trait UnicodeChar {
fn width(self, is_cjk: bool) -> Option<uint>;
}

#[experimental = "pending prelude organization"]
impl UnicodeChar for char {
fn is_alphabetic(self) -> bool { is_alphabetic(self) }
fn is_alphabetic(self) -> bool {
match self {
'a' ... 'z' | 'A' ... 'Z' => true,
c if c > '\x7f' => derived_property::Alphabetic(c),
_ => false
}
}

fn is_XID_start(self) -> bool { is_XID_start(self) }
fn is_XID_start(self) -> bool { derived_property::XID_Start(self) }

fn is_XID_continue(self) -> bool { is_XID_continue(self) }
fn is_XID_continue(self) -> bool { derived_property::XID_Continue(self) }

fn is_lowercase(self) -> bool { is_lowercase(self) }
fn is_lowercase(self) -> bool {
match self {
'a' ... 'z' => true,
c if c > '\x7f' => derived_property::Lowercase(c),
_ => false
}
}

fn is_uppercase(self) -> bool { is_uppercase(self) }
fn is_uppercase(self) -> bool {
match self {
'A' ... 'Z' => true,
c if c > '\x7f' => derived_property::Uppercase(c),
_ => false
}
}

fn is_whitespace(self) -> bool { is_whitespace(self) }
fn is_whitespace(self) -> bool {
match self {
' ' | '\x09' ... '\x0d' => true,
c if c > '\x7f' => property::White_Space(c),
_ => false
}
}

fn is_alphanumeric(self) -> bool { is_alphanumeric(self) }
fn is_alphanumeric(self) -> bool {
self.is_alphabetic() || self.is_numeric()
}

fn is_control(self) -> bool { is_control(self) }
fn is_control(self) -> bool { general_category::Cc(self) }

fn is_numeric(self) -> bool { is_digit(self) }
fn is_numeric(self) -> bool {
match self {
'0' ... '9' => true,
c if c > '\x7f' => general_category::N(c),
_ => false
}
}

fn to_lowercase(self) -> char { to_lowercase(self) }
fn to_lowercase(self) -> char { conversions::to_lower(self) }

fn to_uppercase(self) -> char { to_uppercase(self) }
fn to_uppercase(self) -> char { conversions::to_upper(self) }

#[experimental = "needs expert opinion. is_cjk flag stands out as ugly"]
fn width(self, is_cjk: bool) -> Option<uint> { width(self, is_cjk) }
fn width(self, is_cjk: bool) -> Option<uint> { charwidth::width(self, is_cjk) }
}
15 changes: 8 additions & 7 deletions src/libunicode/u_str.rs
Expand Up @@ -24,13 +24,13 @@ use core::iter::{Filter, AdditiveIterator, Iterator, DoubleEndedIterator};
use core::kinds::Sized;
use core::option::{Option, None, Some};
use core::str::{CharSplits, StrPrelude};
use u_char;
use u_char::UnicodeChar;
use tables::grapheme::GraphemeCat;

/// An iterator over the words of a string, separated by a sequence of whitespace
/// FIXME: This should be opaque
pub type Words<'a> =
Filter<'a, &'a str, CharSplits<'a, extern "Rust" fn(char) -> bool>>;
Filter<'a, &'a str, CharSplits<'a, |char|:'a -> bool>>;

/// Methods for Unicode string slices
pub trait UnicodeStrPrelude for Sized? {
Expand Down Expand Up @@ -143,14 +143,15 @@ impl UnicodeStrPrelude for str {

#[inline]
fn words(&self) -> Words {
self.split(u_char::is_whitespace).filter(|s| !s.is_empty())
let f = |c: char| c.is_whitespace();
self.split(f).filter(|s| !s.is_empty())
}

#[inline]
fn is_whitespace(&self) -> bool { self.chars().all(u_char::is_whitespace) }
fn is_whitespace(&self) -> bool { self.chars().all(|c| c.is_whitespace()) }

#[inline]
fn is_alphanumeric(&self) -> bool { self.chars().all(u_char::is_alphanumeric) }
fn is_alphanumeric(&self) -> bool { self.chars().all(|c| c.is_alphanumeric()) }

#[inline]
fn width(&self, is_cjk: bool) -> uint {
Expand All @@ -164,12 +165,12 @@ impl UnicodeStrPrelude for str {

#[inline]
fn trim_left(&self) -> &str {
self.trim_left_chars(u_char::is_whitespace)
self.trim_left_chars(|c: char| c.is_whitespace())
}

#[inline]
fn trim_right(&self) -> &str {
self.trim_right_chars(u_char::is_whitespace)
self.trim_right_chars(|c: char| c.is_whitespace())
}
}

Expand Down

0 comments on commit 76ddd2b

Please sign in to comment.