@@ -67,7 +67,25 @@ static MAX_THREE_B: u32 = 0x10000u32;
67
67
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
68
68
pub const MAX : char = '\u{10ffff}' ;
69
69
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
+ /// ```
71
89
#[ inline]
72
90
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
73
91
pub fn from_u32 ( i : u32 ) -> Option < char > {
@@ -79,8 +97,7 @@ pub fn from_u32(i: u32) -> Option<char> {
79
97
}
80
98
}
81
99
82
- ///
83
- /// Converts a number to the character representing it
100
+ /// Converts a number to the character representing it.
84
101
///
85
102
/// # Return value
86
103
///
@@ -91,6 +108,15 @@ pub fn from_u32(i: u32) -> Option<char> {
91
108
///
92
109
/// Panics if given an `radix` > 36.
93
110
///
111
+ /// # Examples
112
+ ///
113
+ /// ```
114
+ /// use std::char;
115
+ ///
116
+ /// let c = char::from_digit(4, 10);
117
+ ///
118
+ /// assert_eq!(c, '4')
119
+ /// ```
94
120
#[ inline]
95
121
#[ unstable( feature = "core" , reason = "pending integer conventions" ) ]
96
122
pub fn from_digit ( num : uint , radix : uint ) -> Option < char > {
@@ -126,6 +152,16 @@ pub trait CharExt {
126
152
/// # Panics
127
153
///
128
154
/// 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
+ /// ```
129
165
#[ unstable( feature = "core" ,
130
166
reason = "pending integer conventions" ) ]
131
167
fn is_digit ( self , radix : uint ) -> bool ;
@@ -141,16 +177,53 @@ pub trait CharExt {
141
177
/// # Panics
142
178
///
143
179
/// 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
+ /// ```
144
190
#[ unstable( feature = "core" ,
145
191
reason = "pending integer conventions" ) ]
146
192
fn to_digit ( self , radix : uint ) -> Option < uint > ;
147
193
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
+ /// ```
150
206
///
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
+ /// ```
154
227
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
155
228
fn escape_unicode ( self ) -> EscapeUnicode ;
156
229
@@ -166,32 +239,113 @@ pub trait CharExt {
166
239
/// escaped.
167
240
/// * Any other chars in the range [0x20,0x7e] are not escaped.
168
241
/// * 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
+ /// ```
169
265
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
170
266
fn escape_default ( self ) -> EscapeDefault ;
171
267
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
+ /// ```
174
277
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
175
278
fn len_utf8 ( self ) -> uint ;
176
279
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
+ /// ```
179
289
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
180
290
fn len_utf16 ( self ) -> uint ;
181
291
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];
184
304
///
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
+ /// ```
187
319
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
188
320
fn encode_utf8 ( self , dst : & mut [ u8 ] ) -> Option < uint > ;
189
321
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);
192
346
///
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
+ /// ```
195
349
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
196
350
fn encode_utf16 ( self , dst : & mut [ u16 ] ) -> Option < uint > ;
197
351
}
0 commit comments