1#![allow(non_snake_case)]
21#![stable(feature = "rust1", since = "1.0.0")]
22
23mod convert;
24mod decode;
25mod methods;
26
27#[rustfmt::skip]
29#[stable(feature = "try_from", since = "1.34.0")]
30pub use self::convert::CharTryFromError;
31#[stable(feature = "char_from_str", since = "1.20.0")]
32pub use self::convert::ParseCharError;
33#[stable(feature = "decode_utf16", since = "1.9.0")]
34pub use self::decode::{DecodeUtf16, DecodeUtf16Error};
35
36#[rustfmt::skip]
38#[unstable(feature = "char_internals", reason = "exposed only for libstd", issue = "none")]
39pub use self::methods::encode_utf16_raw; #[unstable(feature = "char_internals", reason = "exposed only for libstd", issue = "none")]
41pub use self::methods::{encode_utf8_raw, encode_utf8_raw_unchecked}; #[rustfmt::skip]
44use crate::ascii;
45pub(crate) use self::methods::EscapeDebugExtArgs;
46use crate::error::Error;
47use crate::escape::{AlwaysEscaped, EscapeIterInner, MaybeEscaped};
48use crate::fmt::{self, Write};
49use crate::iter::{FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce};
50use crate::num::NonZero;
51
52const TAG_CONT: u8 = 0b1000_0000;
54const TAG_TWO_B: u8 = 0b1100_0000;
55const TAG_THREE_B: u8 = 0b1110_0000;
56const TAG_FOUR_B: u8 = 0b1111_0000;
57const MAX_ONE_B: u32 = 0x80;
58const MAX_TWO_B: u32 = 0x800;
59const MAX_THREE_B: u32 = 0x10000;
60
61#[stable(feature = "rust1", since = "1.0.0")]
96#[deprecated(since = "TBD", note = "replaced by the `MAX` associated constant on `char`")]
97pub const MAX: char = char::MAX;
98
99#[stable(feature = "decode_utf16", since = "1.9.0")]
102#[deprecated(
103 since = "TBD",
104 note = "replaced by the `REPLACEMENT_CHARACTER` associated constant on `char`"
105)]
106pub const REPLACEMENT_CHARACTER: char = char::REPLACEMENT_CHARACTER;
107
108#[stable(feature = "unicode_version", since = "1.45.0")]
111#[deprecated(
112 since = "TBD",
113 note = "replaced by the `UNICODE_VERSION` associated constant on `char`"
114)]
115pub const UNICODE_VERSION: (u8, u8, u8) = char::UNICODE_VERSION;
116
117#[stable(feature = "decode_utf16", since = "1.9.0")]
120#[deprecated(since = "TBD", note = "replaced by the `decode_utf16` method on `char`")]
121#[inline]
122pub fn decode_utf16<I: IntoIterator<Item = u16>>(iter: I) -> DecodeUtf16<I::IntoIter> {
123 self::decode::decode_utf16(iter)
124}
125
126#[stable(feature = "rust1", since = "1.0.0")]
128#[rustc_const_stable(feature = "const_char_convert", since = "1.67.0")]
129#[deprecated(since = "TBD", note = "replaced by the `from_u32` method on `char`")]
130#[must_use]
131#[inline]
132pub const fn from_u32(i: u32) -> Option<char> {
133 self::convert::from_u32(i)
134}
135
136#[stable(feature = "char_from_unchecked", since = "1.5.0")]
139#[rustc_const_stable(feature = "const_char_from_u32_unchecked", since = "1.81.0")]
140#[deprecated(since = "TBD", note = "replaced by the `from_u32_unchecked` method on `char`")]
141#[must_use]
142#[inline]
143pub const unsafe fn from_u32_unchecked(i: u32) -> char {
144 unsafe { self::convert::from_u32_unchecked(i) }
146}
147
148#[stable(feature = "rust1", since = "1.0.0")]
150#[rustc_const_stable(feature = "const_char_convert", since = "1.67.0")]
151#[deprecated(since = "TBD", note = "replaced by the `from_digit` method on `char`")]
152#[must_use]
153#[inline]
154pub const fn from_digit(num: u32, radix: u32) -> Option<char> {
155 self::convert::from_digit(num, radix)
156}
157
158#[derive(Clone, Debug)]
166#[stable(feature = "rust1", since = "1.0.0")]
167pub struct EscapeUnicode(EscapeIterInner<10, AlwaysEscaped>);
168
169impl EscapeUnicode {
170 #[inline]
171 const fn new(c: char) -> Self {
172 Self(EscapeIterInner::unicode(c))
173 }
174}
175
176#[stable(feature = "rust1", since = "1.0.0")]
177impl Iterator for EscapeUnicode {
178 type Item = char;
179
180 #[inline]
181 fn next(&mut self) -> Option<char> {
182 self.0.next().map(char::from)
183 }
184
185 #[inline]
186 fn size_hint(&self) -> (usize, Option<usize>) {
187 let n = self.0.len();
188 (n, Some(n))
189 }
190
191 #[inline]
192 fn count(self) -> usize {
193 self.0.len()
194 }
195
196 #[inline]
197 fn last(mut self) -> Option<char> {
198 self.0.next_back().map(char::from)
199 }
200
201 #[inline]
202 fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
203 self.0.advance_by(n)
204 }
205}
206
207#[stable(feature = "exact_size_escape", since = "1.11.0")]
208impl ExactSizeIterator for EscapeUnicode {
209 #[inline]
210 fn len(&self) -> usize {
211 self.0.len()
212 }
213}
214
215#[stable(feature = "fused", since = "1.26.0")]
216impl FusedIterator for EscapeUnicode {}
217
218#[stable(feature = "char_struct_display", since = "1.16.0")]
219impl fmt::Display for EscapeUnicode {
220 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
221 fmt::Display::fmt(&self.0, f)
222 }
223}
224
225#[derive(Clone, Debug)]
232#[stable(feature = "rust1", since = "1.0.0")]
233pub struct EscapeDefault(EscapeIterInner<10, AlwaysEscaped>);
234
235impl EscapeDefault {
236 #[inline]
237 const fn printable(c: ascii::Char) -> Self {
238 Self(EscapeIterInner::ascii(c.to_u8()))
239 }
240
241 #[inline]
242 const fn backslash(c: ascii::Char) -> Self {
243 Self(EscapeIterInner::backslash(c))
244 }
245
246 #[inline]
247 const fn unicode(c: char) -> Self {
248 Self(EscapeIterInner::unicode(c))
249 }
250}
251
252#[stable(feature = "rust1", since = "1.0.0")]
253impl Iterator for EscapeDefault {
254 type Item = char;
255
256 #[inline]
257 fn next(&mut self) -> Option<char> {
258 self.0.next().map(char::from)
259 }
260
261 #[inline]
262 fn size_hint(&self) -> (usize, Option<usize>) {
263 let n = self.0.len();
264 (n, Some(n))
265 }
266
267 #[inline]
268 fn count(self) -> usize {
269 self.0.len()
270 }
271
272 #[inline]
273 fn last(mut self) -> Option<char> {
274 self.0.next_back().map(char::from)
275 }
276
277 #[inline]
278 fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
279 self.0.advance_by(n)
280 }
281}
282
283#[stable(feature = "exact_size_escape", since = "1.11.0")]
284impl ExactSizeIterator for EscapeDefault {
285 #[inline]
286 fn len(&self) -> usize {
287 self.0.len()
288 }
289}
290
291#[stable(feature = "fused", since = "1.26.0")]
292impl FusedIterator for EscapeDefault {}
293
294#[stable(feature = "char_struct_display", since = "1.16.0")]
295impl fmt::Display for EscapeDefault {
296 #[inline]
297 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
298 fmt::Display::fmt(&self.0, f)
299 }
300}
301
302#[stable(feature = "char_escape_debug", since = "1.20.0")]
309#[derive(Clone, Debug)]
310pub struct EscapeDebug(EscapeIterInner<10, MaybeEscaped>);
311
312impl EscapeDebug {
313 #[inline]
314 const fn printable(chr: char) -> Self {
315 Self(EscapeIterInner::printable(chr))
316 }
317
318 #[inline]
319 const fn backslash(c: ascii::Char) -> Self {
320 Self(EscapeIterInner::backslash(c))
321 }
322
323 #[inline]
324 const fn unicode(c: char) -> Self {
325 Self(EscapeIterInner::unicode(c))
326 }
327}
328
329#[stable(feature = "char_escape_debug", since = "1.20.0")]
330impl Iterator for EscapeDebug {
331 type Item = char;
332
333 #[inline]
334 fn next(&mut self) -> Option<char> {
335 self.0.next()
336 }
337
338 #[inline]
339 fn size_hint(&self) -> (usize, Option<usize>) {
340 let n = self.len();
341 (n, Some(n))
342 }
343
344 #[inline]
345 fn count(self) -> usize {
346 self.len()
347 }
348}
349
350#[stable(feature = "char_escape_debug", since = "1.20.0")]
351impl ExactSizeIterator for EscapeDebug {
352 fn len(&self) -> usize {
353 self.0.len()
354 }
355}
356
357#[stable(feature = "fused", since = "1.26.0")]
358impl FusedIterator for EscapeDebug {}
359
360#[stable(feature = "char_escape_debug", since = "1.20.0")]
361impl fmt::Display for EscapeDebug {
362 #[inline]
363 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
364 fmt::Display::fmt(&self.0, f)
365 }
366}
367
368macro_rules! casemappingiter_impls {
369 (
370 #[$stab:meta]
371 #[$dendstab:meta]
372 #[$fusedstab:meta]
373 #[$exactstab:meta]
374 #[$displaystab:meta]
375 $(#[$attr:meta])*
376 $ITER_NAME:ident
377 ) => {
378 $(#[$attr])*
379 #[$stab]
380 #[derive(Debug, Clone)]
381 pub struct $ITER_NAME(CaseMappingIter);
382
383 #[$stab]
384 impl Iterator for $ITER_NAME {
385 type Item = char;
386 fn next(&mut self) -> Option<char> {
387 self.0.next()
388 }
389
390 fn size_hint(&self) -> (usize, Option<usize>) {
391 self.0.size_hint()
392 }
393
394 fn fold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
395 where
396 Fold: FnMut(Acc, Self::Item) -> Acc,
397 {
398 self.0.fold(init, fold)
399 }
400
401 fn count(self) -> usize {
402 self.0.count()
403 }
404
405 fn last(self) -> Option<Self::Item> {
406 self.0.last()
407 }
408
409 fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
410 self.0.advance_by(n)
411 }
412
413 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
414 unsafe { self.0.__iterator_get_unchecked(idx) }
416 }
417 }
418
419 #[$dendstab]
420 impl DoubleEndedIterator for $ITER_NAME {
421 fn next_back(&mut self) -> Option<char> {
422 self.0.next_back()
423 }
424
425 fn rfold<Acc, Fold>(self, init: Acc, rfold: Fold) -> Acc
426 where
427 Fold: FnMut(Acc, Self::Item) -> Acc,
428 {
429 self.0.rfold(init, rfold)
430 }
431
432 fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
433 self.0.advance_back_by(n)
434 }
435 }
436
437 #[$fusedstab]
438 impl FusedIterator for $ITER_NAME {}
439
440 #[$exactstab]
441 impl ExactSizeIterator for $ITER_NAME {
442 fn len(&self) -> usize {
443 self.0.len()
444 }
445
446 fn is_empty(&self) -> bool {
447 self.0.is_empty()
448 }
449 }
450
451 #[unstable(feature = "trusted_len", issue = "37572")]
453 unsafe impl TrustedLen for $ITER_NAME {}
454
455 #[doc(hidden)]
457 #[unstable(feature = "std_internals", issue = "none")]
458 unsafe impl TrustedRandomAccessNoCoerce for $ITER_NAME {
459 const MAY_HAVE_SIDE_EFFECT: bool = false;
460 }
461
462 #[doc(hidden)]
464 #[unstable(feature = "std_internals", issue = "none")]
465 unsafe impl TrustedRandomAccess for $ITER_NAME {}
466
467 #[$displaystab]
468 impl fmt::Display for $ITER_NAME {
469 #[inline]
470 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
471 fmt::Display::fmt(&self.0, f)
472 }
473 }
474 }
475}
476
477casemappingiter_impls! {
478 #[stable(feature = "rust1", since = "1.0.0")]
479 #[stable(feature = "case_mapping_double_ended", since = "1.59.0")]
480 #[stable(feature = "fused", since = "1.26.0")]
481 #[stable(feature = "exact_size_case_mapping_iter", since = "1.35.0")]
482 #[stable(feature = "char_struct_display", since = "1.16.0")]
483 ToUppercase
490}
491
492casemappingiter_impls! {
493 #[unstable(feature = "titlecase", issue = "153892")]
494 #[unstable(feature = "titlecase", issue = "153892")]
495 #[unstable(feature = "titlecase", issue = "153892")]
496 #[unstable(feature = "titlecase", issue = "153892")]
497 #[unstable(feature = "titlecase", issue = "153892")]
498 ToTitlecase
505}
506
507casemappingiter_impls! {
508 #[stable(feature = "rust1", since = "1.0.0")]
509 #[stable(feature = "case_mapping_double_ended", since = "1.59.0")]
510 #[stable(feature = "fused", since = "1.26.0")]
511 #[stable(feature = "exact_size_case_mapping_iter", since = "1.35.0")]
512 #[stable(feature = "char_struct_display", since = "1.16.0")]
513 ToLowercase
520}
521
522#[derive(Debug, Clone)]
523struct CaseMappingIter(core::array::IntoIter<char, 3>);
524
525impl CaseMappingIter {
526 #[inline]
527 fn new(chars: [char; 3]) -> CaseMappingIter {
528 let mut iter = chars.into_iter();
529 if chars[2] == '\0' {
530 iter.next_back();
531 if chars[1] == '\0' {
532 iter.next_back();
533
534 }
537 }
538 CaseMappingIter(iter)
539 }
540}
541
542impl Iterator for CaseMappingIter {
543 type Item = char;
544
545 fn next(&mut self) -> Option<char> {
546 self.0.next()
547 }
548
549 fn size_hint(&self) -> (usize, Option<usize>) {
550 self.0.size_hint()
551 }
552
553 fn fold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
554 where
555 Fold: FnMut(Acc, Self::Item) -> Acc,
556 {
557 self.0.fold(init, fold)
558 }
559
560 fn count(self) -> usize {
561 self.0.count()
562 }
563
564 fn last(self) -> Option<Self::Item> {
565 self.0.last()
566 }
567
568 fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
569 self.0.advance_by(n)
570 }
571
572 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
573 unsafe { self.0.__iterator_get_unchecked(idx) }
575 }
576}
577
578impl DoubleEndedIterator for CaseMappingIter {
579 fn next_back(&mut self) -> Option<char> {
580 self.0.next_back()
581 }
582
583 fn rfold<Acc, Fold>(self, init: Acc, rfold: Fold) -> Acc
584 where
585 Fold: FnMut(Acc, Self::Item) -> Acc,
586 {
587 self.0.rfold(init, rfold)
588 }
589
590 fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
591 self.0.advance_back_by(n)
592 }
593}
594
595impl ExactSizeIterator for CaseMappingIter {
596 fn len(&self) -> usize {
597 self.0.len()
598 }
599
600 fn is_empty(&self) -> bool {
601 self.0.is_empty()
602 }
603}
604
605impl FusedIterator for CaseMappingIter {}
606
607unsafe impl TrustedLen for CaseMappingIter {}
609
610unsafe impl TrustedRandomAccessNoCoerce for CaseMappingIter {
612 const MAY_HAVE_SIDE_EFFECT: bool = false;
613}
614
615unsafe impl TrustedRandomAccess for CaseMappingIter {}
617
618impl fmt::Display for CaseMappingIter {
619 #[inline]
620 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
621 for c in self.0.clone() {
622 f.write_char(c)?;
623 }
624 Ok(())
625 }
626}
627
628#[stable(feature = "u8_from_char", since = "1.59.0")]
630#[derive(Debug, Copy, Clone, PartialEq, Eq)]
631pub struct TryFromCharError(pub(crate) ());
632
633#[stable(feature = "u8_from_char", since = "1.59.0")]
634impl fmt::Display for TryFromCharError {
635 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
636 "unicode code point out of range".fmt(fmt)
637 }
638}
639
640#[stable(feature = "u8_from_char", since = "1.59.0")]
641impl Error for TryFromCharError {}
642
643#[unstable(feature = "titlecase", issue = "153892")]
653#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
654pub enum CharCase {
655 Lower = 0b00,
657 Title = 0b10,
659 Upper = 0b11,
661}