Skip to content

Commit fcf1a57

Browse files
committed
Documenting libcore/char.rs
1 parent ca4b967 commit fcf1a57

File tree

1 file changed

+174
-20
lines changed

1 file changed

+174
-20
lines changed

src/libcore/char.rs

Lines changed: 174 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,25 @@ static MAX_THREE_B: u32 = 0x10000u32;
6767
#[stable(feature = "rust1", since = "1.0.0")]
6868
pub const MAX: char = '\u{10ffff}';
6969

70-
/// Converts from `u32` to a `char`
70+
/// Converts a `u32` to an `Option<char>`.
71+
///
72+
/// # Examples
73+
///
74+
/// ```
75+
/// use std::char;
76+
///
77+
/// let c = char::from_u32(10084); // produces `Some(❤)`
78+
/// assert_eq!(c, Some('❤'));
79+
/// ```
80+
///
81+
/// An invalid character:
82+
///
83+
/// ```
84+
/// use std::char;
85+
///
86+
/// let none = char::from_u32(1114112);
87+
/// assert_eq!(none, None);
88+
/// ```
7189
#[inline]
7290
#[stable(feature = "rust1", since = "1.0.0")]
7391
pub fn from_u32(i: u32) -> Option<char> {
@@ -79,8 +97,7 @@ pub fn from_u32(i: u32) -> Option<char> {
7997
}
8098
}
8199

82-
///
83-
/// Converts a number to the character representing it
100+
/// Converts a number to the character representing it.
84101
///
85102
/// # Return value
86103
///
@@ -91,6 +108,15 @@ pub fn from_u32(i: u32) -> Option<char> {
91108
///
92109
/// Panics if given an `radix` > 36.
93110
///
111+
/// # Examples
112+
///
113+
/// ```
114+
/// use std::char;
115+
///
116+
/// let c = char::from_digit(4, 10);
117+
///
118+
/// assert_eq!(c, '4')
119+
/// ```
94120
#[inline]
95121
#[unstable(feature = "core", reason = "pending integer conventions")]
96122
pub fn from_digit(num: uint, radix: uint) -> Option<char> {
@@ -126,6 +152,16 @@ pub trait CharExt {
126152
/// # Panics
127153
///
128154
/// Panics if given a radix > 36.
155+
///
156+
/// # Examples
157+
///
158+
/// ```
159+
/// let c = '1';
160+
///
161+
/// assert!(c.is_digit(10));
162+
///
163+
/// assert!('f'.is_digit(16));
164+
/// ```
129165
#[unstable(feature = "core",
130166
reason = "pending integer conventions")]
131167
fn is_digit(self, radix: uint) -> bool;
@@ -141,16 +177,53 @@ pub trait CharExt {
141177
/// # Panics
142178
///
143179
/// Panics if given a radix outside the range [0..36].
180+
///
181+
/// # Examples
182+
///
183+
/// ```
184+
/// let c = '1';
185+
///
186+
/// assert_eq!(1, c.to_digit(10));
187+
///
188+
/// assert_eq!(15, 'f'.to_digit(16));
189+
/// ```
144190
#[unstable(feature = "core",
145191
reason = "pending integer conventions")]
146192
fn to_digit(self, radix: uint) -> Option<uint>;
147193

148-
/// Returns an iterator that yields the hexadecimal Unicode escape
149-
/// of a character, as `char`s.
194+
/// Returns an iterator that yields the hexadecimal Unicode escape of a character, as `char`s.
195+
///
196+
/// All characters are escaped with Rust syntax of the form `\\u{NNNN}` where `NNNN` is the
197+
/// shortest hexadecimal representation of the code point.
198+
///
199+
/// # Examples
200+
///
201+
/// ```
202+
/// for i in '❤'.escape_unicode() {
203+
/// println!("{}", i);
204+
/// }
205+
/// ```
150206
///
151-
/// All characters are escaped with Rust syntax of the form `\\u{NNNN}`
152-
/// where `NNNN` is the shortest hexadecimal representation of the code
153-
/// point.
207+
/// This prints:
208+
///
209+
/// ```text
210+
/// \
211+
/// u
212+
/// {
213+
/// 2
214+
/// 7
215+
/// 6
216+
/// 4
217+
/// }
218+
/// ```
219+
///
220+
/// Collecting into a `String`:
221+
///
222+
/// ```
223+
/// let heart: String = '❤'.escape_unicode().collect();
224+
///
225+
/// assert_eq!(heart, r"\u{2764}");
226+
/// ```
154227
#[stable(feature = "rust1", since = "1.0.0")]
155228
fn escape_unicode(self) -> EscapeUnicode;
156229

@@ -166,32 +239,113 @@ pub trait CharExt {
166239
/// escaped.
167240
/// * Any other chars in the range [0x20,0x7e] are not escaped.
168241
/// * Any other chars are given hex Unicode escapes; see `escape_unicode`.
242+
///
243+
/// # Examples
244+
///
245+
/// ```
246+
/// for i in '"'.escape_default() {
247+
/// println!("{}", i);
248+
/// }
249+
/// ```
250+
///
251+
/// This prints:
252+
///
253+
/// ```text
254+
/// \
255+
/// "
256+
/// ```
257+
///
258+
/// Collecting into a `String`:
259+
///
260+
/// ```
261+
/// let quote: String = '"'.escape_default().collect();
262+
///
263+
/// assert_eq!(quote, r"\"");
264+
/// ```
169265
#[stable(feature = "rust1", since = "1.0.0")]
170266
fn escape_default(self) -> EscapeDefault;
171267

172-
/// Returns the amount of bytes this character would need if encoded in
173-
/// UTF-8.
268+
/// Returns the number of bytes this character would need if encoded in UTF-8.
269+
///
270+
/// # Examples
271+
///
272+
/// ```
273+
/// let n = 'ß'.len_utf8();
274+
///
275+
/// assert_eq!(n, 2);
276+
/// ```
174277
#[stable(feature = "rust1", since = "1.0.0")]
175278
fn len_utf8(self) -> uint;
176279

177-
/// Returns the amount of bytes this character would need if encoded in
178-
/// UTF-16.
280+
/// Returns the number of bytes this character would need if encoded in UTF-16.
281+
///
282+
/// # Examples
283+
///
284+
/// ```
285+
/// let n = 'ß'.len_utf16();
286+
///
287+
/// assert_eq!(n, 1);
288+
/// ```
179289
#[stable(feature = "rust1", since = "1.0.0")]
180290
fn len_utf16(self) -> uint;
181291

182-
/// Encodes this character as UTF-8 into the provided byte buffer,
183-
/// and then returns the number of bytes written.
292+
/// Encodes this character as UTF-8 into the provided byte buffer, and then returns the number
293+
/// of bytes written.
294+
///
295+
/// If the buffer is not large enough, nothing will be written into it and a `None` will be
296+
/// returned.
297+
///
298+
/// # Examples
299+
///
300+
/// In both of these examples, 'ß' takes two bytes to encode.
301+
///
302+
/// ```
303+
/// let mut b = [0; 2];
184304
///
185-
/// If the buffer is not large enough, nothing will be written into it
186-
/// and a `None` will be returned.
305+
/// let result = 'ß'.encode_utf8(&mut b);
306+
///
307+
/// assert_eq!(result, Some(2));
308+
/// ```
309+
///
310+
/// A buffer that's too small:
311+
///
312+
/// ```
313+
/// let mut b = [0; 1];
314+
///
315+
/// let result = 'ß'.encode_utf8(&mut b);
316+
///
317+
/// assert_eq!(result, None);
318+
/// ```
187319
#[stable(feature = "rust1", since = "1.0.0")]
188320
fn encode_utf8(self, dst: &mut [u8]) -> Option<uint>;
189321

190-
/// Encodes this character as UTF-16 into the provided `u16` buffer,
191-
/// and then returns the number of `u16`s written.
322+
/// Encodes this character as UTF-16 into the provided `u16` buffer, and then returns the
323+
/// number of `u16`s written.
324+
///
325+
/// If the buffer is not large enough, nothing will be written into it and a `None` will be
326+
/// returned.
327+
///
328+
/// # Examples
329+
///
330+
/// In both of these examples, 'ß' takes one byte to encode.
331+
///
332+
/// ```
333+
/// let mut b = [0; 1];
334+
///
335+
/// let result = 'ß'.encode_utf16(&mut b);
336+
///
337+
/// assert_eq!(result, Some(1));
338+
/// ```
339+
///
340+
/// A buffer that's too small:
341+
///
342+
/// ```
343+
/// let mut b = [0; 0];
344+
///
345+
/// let result = 'ß'.encode_utf8(&mut b);
192346
///
193-
/// If the buffer is not large enough, nothing will be written into it
194-
/// and a `None` will be returned.
347+
/// assert_eq!(result, None);
348+
/// ```
195349
#[stable(feature = "rust1", since = "1.0.0")]
196350
fn encode_utf16(self, dst: &mut [u16]) -> Option<uint>;
197351
}

0 commit comments

Comments
 (0)