Skip to content

Commit 0302d37

Browse files
committed
Merge UnicodeChar and CharExt.
This "reexports" all the functionality of `core::char::CharExt` as methods on `unicode::u_char::UnicodeChar` (renamed to `CharExt`). Imports may need to be updated (one now just imports `unicode::CharExt`, or `std::char::CharExt` rather than two traits from either), so this is a [breaking-change]
1 parent 1912020 commit 0302d37

File tree

9 files changed

+116
-19
lines changed

9 files changed

+116
-19
lines changed

src/libcollections/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ mod std {
103103
mod prelude {
104104
// from core.
105105
pub use core::borrow::IntoCow;
106-
pub use core::char::CharExt;
107106
pub use core::clone::Clone;
108107
pub use core::cmp::{PartialEq, Eq, PartialOrd, Ord};
109108
pub use core::cmp::Ordering::{Less, Equal, Greater};
@@ -127,7 +126,7 @@ mod prelude {
127126

128127
// from other crates.
129128
pub use alloc::boxed::Box;
130-
pub use unicode::char::UnicodeChar;
129+
pub use unicode::char::CharExt;
131130

132131
// from collections.
133132
pub use slice::SliceConcatExt;

src/libstd/io/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ use str;
248248
use string::String;
249249
use uint;
250250
use unicode;
251-
use unicode::char::UnicodeChar;
252251
use vec::Vec;
253252

254253
// Reexports

src/libstd/num/strconv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use self::ExponentFormat::*;
1616
use self::SignificantDigits::*;
1717
use self::SignFormat::*;
1818

19-
use char::{self, Char};
19+
use char::{self, CharExt};
2020
use num::{self, Int, Float, ToPrimitive};
2121
use num::FpCategory as Fp;
2222
use ops::FnMut;

src/libstd/path/windows.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use self::PathPrefix::*;
1616

1717
use ascii::AsciiExt;
1818
use c_str::{CString, ToCStr};
19+
use char::CharExt;
1920
use clone::Clone;
2021
use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
2122
use hash;
@@ -28,7 +29,6 @@ use option::Option::{Some, None};
2829
use slice::{SliceExt, SliceConcatExt};
2930
use str::{SplitTerminator, FromStr, StrExt};
3031
use string::{String, ToString};
31-
use unicode::char::UnicodeChar;
3232
use vec::Vec;
3333

3434
use super::{contains_nul, BytesContainer, GenericPath, GenericPathUnsafe};

src/libstd/prelude/v1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
// Reexported types and traits
2323

2424
#[stable] #[doc(no_inline)] pub use boxed::Box;
25-
#[stable] #[doc(no_inline)] pub use char::{CharExt, UnicodeChar};
25+
#[stable] #[doc(no_inline)] pub use char::CharExt;
2626
#[stable] #[doc(no_inline)] pub use clone::Clone;
2727
#[stable] #[doc(no_inline)] pub use cmp::{PartialEq, PartialOrd, Eq, Ord};
2828
#[stable] #[doc(no_inline)] pub use iter::CloneIteratorExt;

src/libunicode/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ mod u_str;
4444
// re-export char so that std et al see it correctly
4545
/// Character manipulation (`char` type, Unicode Scalar Value)
4646
///
47-
/// This module provides the `Char` and `UnicodeChar` traits, as well as their
48-
/// implementation for the primitive `char` type, in order to allow basic character
49-
/// manipulation.
47+
/// This module provides the `CharExt` trait, as well as its
48+
/// implementation for the primitive `char` type, in order to allow
49+
/// basic character manipulation.
5050
///
5151
/// A `char` actually represents a
5252
/// *[Unicode Scalar Value](http://www.unicode.org/glossary/#unicode_scalar_value)*,
@@ -58,14 +58,14 @@ mod u_str;
5858
/// however the converse is not always true due to the above range limits
5959
/// and, as such, should be performed via the `from_u32` function..
6060
pub mod char {
61-
pub use core::char::{MAX, from_u32, from_digit, CharExt};
61+
pub use core::char::{MAX, from_u32, from_digit};
6262

6363
pub use normalize::{decompose_canonical, decompose_compatible, compose};
6464

6565
pub use tables::normalization::canonical_combining_class;
6666
pub use tables::UNICODE_VERSION;
6767

68-
pub use u_char::UnicodeChar;
68+
pub use u_char::CharExt;
6969
}
7070

7171
pub mod str {

src/libunicode/tables.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#![allow(missing_docs, non_upper_case_globals, non_snake_case)]
1414

1515
/// The version of [Unicode](http://www.unicode.org/)
16-
/// that the `UnicodeChar` and `UnicodeStrPrelude` traits are based on.
16+
/// that the unicode parts of `CharExt` and `UnicodeStrPrelude` traits are based on.
1717
pub const UNICODE_VERSION: (uint, uint, uint) = (7, 0, 0);
1818

1919
fn bsearch_range_table(c: char, r: &'static [(char,char)]) -> bool {

src/libunicode/u_char.rs

Lines changed: 103 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,99 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! Unicode-intensive `char` methods.
11+
//! Unicode-intensive `char` methods along with the `core` methods.
1212
//!
1313
//! These methods implement functionality for `char` that requires knowledge of
1414
//! Unicode definitions, including normalization, categorization, and display information.
1515
16+
use core::char;
17+
use core::char::CharExt as C;
1618
use core::option::Option;
1719
use tables::{derived_property, property, general_category, conversions, charwidth};
1820

19-
/// Useful functions for Unicode characters.
21+
/// Functionality for manipulating `char`.
2022
#[experimental = "pending prelude organization"]
21-
pub trait UnicodeChar {
23+
pub trait CharExt {
24+
/// Checks if a `char` parses as a numeric digit in the given radix.
25+
///
26+
/// Compared to `is_numeric()`, this function only recognizes the characters
27+
/// `0-9`, `a-z` and `A-Z`.
28+
///
29+
/// # Return value
30+
///
31+
/// Returns `true` if `c` is a valid digit under `radix`, and `false`
32+
/// otherwise.
33+
///
34+
/// # Panics
35+
///
36+
/// Panics if given a radix > 36.
37+
#[unstable = "pending integer conventions"]
38+
fn is_digit(self, radix: uint) -> bool;
39+
40+
/// Converts a character to the corresponding digit.
41+
///
42+
/// # Return value
43+
///
44+
/// If `c` is between '0' and '9', the corresponding value between 0 and
45+
/// 9. If `c` is 'a' or 'A', 10. If `c` is 'b' or 'B', 11, etc. Returns
46+
/// none if the character does not refer to a digit in the given radix.
47+
///
48+
/// # Panics
49+
///
50+
/// Panics if given a radix outside the range [0..36].
51+
#[unstable = "pending integer conventions"]
52+
fn to_digit(self, radix: uint) -> Option<uint>;
53+
54+
/// Returns an iterator that yields the hexadecimal Unicode escape
55+
/// of a character, as `char`s.
56+
///
57+
/// All characters are escaped with Rust syntax of the form `\\u{NNNN}`
58+
/// where `NNNN` is the shortest hexadecimal representation of the code
59+
/// point.
60+
#[stable]
61+
fn escape_unicode(self) -> char::EscapeUnicode;
62+
63+
/// Returns an iterator that yields the 'default' ASCII and
64+
/// C++11-like literal escape of a character, as `char`s.
65+
///
66+
/// The default is chosen with a bias toward producing literals that are
67+
/// legal in a variety of languages, including C++11 and similar C-family
68+
/// languages. The exact rules are:
69+
///
70+
/// * Tab, CR and LF are escaped as '\t', '\r' and '\n' respectively.
71+
/// * Single-quote, double-quote and backslash chars are backslash-
72+
/// escaped.
73+
/// * Any other chars in the range [0x20,0x7e] are not escaped.
74+
/// * Any other chars are given hex Unicode escapes; see `escape_unicode`.
75+
#[stable]
76+
fn escape_default(self) -> char::EscapeDefault;
77+
78+
/// Returns the amount of bytes this character would need if encoded in
79+
/// UTF-8.
80+
#[stable]
81+
fn len_utf8(self) -> uint;
82+
83+
/// Returns the amount of bytes this character would need if encoded in
84+
/// UTF-16.
85+
#[stable]
86+
fn len_utf16(self) -> uint;
87+
88+
/// Encodes this character as UTF-8 into the provided byte buffer,
89+
/// and then returns the number of bytes written.
90+
///
91+
/// If the buffer is not large enough, nothing will be written into it
92+
/// and a `None` will be returned.
93+
#[unstable = "pending decision about Iterator/Writer/Reader"]
94+
fn encode_utf8(self, dst: &mut [u8]) -> Option<uint>;
95+
96+
/// Encodes this character as UTF-16 into the provided `u16` buffer,
97+
/// and then returns the number of `u16`s written.
98+
///
99+
/// If the buffer is not large enough, nothing will be written into it
100+
/// and a `None` will be returned.
101+
#[unstable = "pending decision about Iterator/Writer/Reader"]
102+
fn encode_utf16(self, dst: &mut [u16]) -> Option<uint>;
103+
22104
/// Returns whether the specified character is considered a Unicode
23105
/// alphabetic code point.
24106
fn is_alphabetic(self) -> bool;
@@ -118,7 +200,24 @@ pub trait UnicodeChar {
118200
}
119201

120202
#[experimental = "pending prelude organization"]
121-
impl UnicodeChar for char {
203+
impl CharExt for char {
204+
#[unstable = "pending integer conventions"]
205+
fn is_digit(self, radix: uint) -> bool { C::is_digit(self, radix) }
206+
#[unstable = "pending integer conventions"]
207+
fn to_digit(self, radix: uint) -> Option<uint> { C::to_digit(self, radix) }
208+
#[stable]
209+
fn escape_unicode(self) -> char::EscapeUnicode { C::escape_unicode(self) }
210+
#[stable]
211+
fn escape_default(self) -> char::EscapeDefault { C::escape_default(self) }
212+
#[stable]
213+
fn len_utf8(self) -> uint { C::len_utf8(self) }
214+
#[stable]
215+
fn len_utf16(self) -> uint { C::len_utf16(self) }
216+
#[unstable = "pending decision about Iterator/Writer/Reader"]
217+
fn encode_utf8(self, dst: &mut [u8]) -> Option<uint> { C::encode_utf8(self, dst) }
218+
#[unstable = "pending decision about Iterator/Writer/Reader"]
219+
fn encode_utf16(self, dst: &mut [u16]) -> Option<uint> { C::encode_utf16(self, dst) }
220+
122221
fn is_alphabetic(self) -> bool {
123222
match self {
124223
'a' ... 'z' | 'A' ... 'Z' => true,

src/libunicode/u_str.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! Unicode-intensive string manipulations.
1414
//!
1515
//! This module provides functionality to `str` that requires the Unicode methods provided by the
16-
//! UnicodeChar trait.
16+
//! unicode parts of the CharExt trait.
1717
1818
use self::GraphemeState::*;
1919
use core::prelude::*;
@@ -26,7 +26,7 @@ use core::num::Int;
2626
use core::slice;
2727
use core::str::Split;
2828

29-
use u_char::UnicodeChar;
29+
use u_char::CharExt as UCharExt; // conflicts with core::prelude::CharExt
3030
use tables::grapheme::GraphemeCat;
3131

3232
/// An iterator over the words of a string, separated by a sequence of whitespace
@@ -529,7 +529,7 @@ impl<I> Iterator for Utf16Encoder<I> where I: Iterator<Item=char> {
529529

530530
let mut buf = [0u16; 2];
531531
self.chars.next().map(|ch| {
532-
let n = ch.encode_utf16(buf.as_mut_slice()).unwrap_or(0);
532+
let n = CharExt::encode_utf16(ch, buf.as_mut_slice()).unwrap_or(0);
533533
if n == 2 { self.extra = buf[1]; }
534534
buf[0]
535535
})

0 commit comments

Comments
 (0)