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")]
96pub const MAX: char = char::MAX;
97
98#[unstable(feature = "char_max_len", issue = "121714")]
101pub const MAX_LEN_UTF8: usize = char::MAX_LEN_UTF8;
102
103#[unstable(feature = "char_max_len", issue = "121714")]
106pub const MAX_LEN_UTF16: usize = char::MAX_LEN_UTF16;
107
108#[stable(feature = "decode_utf16", since = "1.9.0")]
111pub const REPLACEMENT_CHARACTER: char = char::REPLACEMENT_CHARACTER;
112
113#[stable(feature = "unicode_version", since = "1.45.0")]
116pub const UNICODE_VERSION: (u8, u8, u8) = char::UNICODE_VERSION;
117
118#[stable(feature = "decode_utf16", since = "1.9.0")]
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#[must_use]
130#[inline]
131pub const fn from_u32(i: u32) -> Option<char> {
132 self::convert::from_u32(i)
133}
134
135#[stable(feature = "char_from_unchecked", since = "1.5.0")]
138#[rustc_const_stable(feature = "const_char_from_u32_unchecked", since = "1.81.0")]
139#[must_use]
140#[inline]
141pub const unsafe fn from_u32_unchecked(i: u32) -> char {
142 unsafe { self::convert::from_u32_unchecked(i) }
144}
145
146#[stable(feature = "rust1", since = "1.0.0")]
148#[rustc_const_stable(feature = "const_char_convert", since = "1.67.0")]
149#[must_use]
150#[inline]
151pub const fn from_digit(num: u32, radix: u32) -> Option<char> {
152 self::convert::from_digit(num, radix)
153}
154
155#[derive(Clone, Debug)]
163#[stable(feature = "rust1", since = "1.0.0")]
164pub struct EscapeUnicode(EscapeIterInner<10, AlwaysEscaped>);
165
166impl EscapeUnicode {
167 #[inline]
168 const fn new(c: char) -> Self {
169 Self(EscapeIterInner::unicode(c))
170 }
171}
172
173#[stable(feature = "rust1", since = "1.0.0")]
174impl Iterator for EscapeUnicode {
175 type Item = char;
176
177 #[inline]
178 fn next(&mut self) -> Option<char> {
179 self.0.next().map(char::from)
180 }
181
182 #[inline]
183 fn size_hint(&self) -> (usize, Option<usize>) {
184 let n = self.0.len();
185 (n, Some(n))
186 }
187
188 #[inline]
189 fn count(self) -> usize {
190 self.0.len()
191 }
192
193 #[inline]
194 fn last(mut self) -> Option<char> {
195 self.0.next_back().map(char::from)
196 }
197
198 #[inline]
199 fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
200 self.0.advance_by(n)
201 }
202}
203
204#[stable(feature = "exact_size_escape", since = "1.11.0")]
205impl ExactSizeIterator for EscapeUnicode {
206 #[inline]
207 fn len(&self) -> usize {
208 self.0.len()
209 }
210}
211
212#[stable(feature = "fused", since = "1.26.0")]
213impl FusedIterator for EscapeUnicode {}
214
215#[stable(feature = "char_struct_display", since = "1.16.0")]
216impl fmt::Display for EscapeUnicode {
217 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
218 fmt::Display::fmt(&self.0, f)
219 }
220}
221
222#[derive(Clone, Debug)]
229#[stable(feature = "rust1", since = "1.0.0")]
230pub struct EscapeDefault(EscapeIterInner<10, AlwaysEscaped>);
231
232impl EscapeDefault {
233 #[inline]
234 const fn printable(c: ascii::Char) -> Self {
235 Self(EscapeIterInner::ascii(c.to_u8()))
236 }
237
238 #[inline]
239 const fn backslash(c: ascii::Char) -> Self {
240 Self(EscapeIterInner::backslash(c))
241 }
242
243 #[inline]
244 const fn unicode(c: char) -> Self {
245 Self(EscapeIterInner::unicode(c))
246 }
247}
248
249#[stable(feature = "rust1", since = "1.0.0")]
250impl Iterator for EscapeDefault {
251 type Item = char;
252
253 #[inline]
254 fn next(&mut self) -> Option<char> {
255 self.0.next().map(char::from)
256 }
257
258 #[inline]
259 fn size_hint(&self) -> (usize, Option<usize>) {
260 let n = self.0.len();
261 (n, Some(n))
262 }
263
264 #[inline]
265 fn count(self) -> usize {
266 self.0.len()
267 }
268
269 #[inline]
270 fn last(mut self) -> Option<char> {
271 self.0.next_back().map(char::from)
272 }
273
274 #[inline]
275 fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
276 self.0.advance_by(n)
277 }
278}
279
280#[stable(feature = "exact_size_escape", since = "1.11.0")]
281impl ExactSizeIterator for EscapeDefault {
282 #[inline]
283 fn len(&self) -> usize {
284 self.0.len()
285 }
286}
287
288#[stable(feature = "fused", since = "1.26.0")]
289impl FusedIterator for EscapeDefault {}
290
291#[stable(feature = "char_struct_display", since = "1.16.0")]
292impl fmt::Display for EscapeDefault {
293 #[inline]
294 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
295 fmt::Display::fmt(&self.0, f)
296 }
297}
298
299#[stable(feature = "char_escape_debug", since = "1.20.0")]
306#[derive(Clone, Debug)]
307#[ferrocene::prevalidated]
308pub struct EscapeDebug(EscapeIterInner<10, MaybeEscaped>);
309
310impl EscapeDebug {
311 #[inline]
312 #[ferrocene::prevalidated]
313 const fn printable(chr: char) -> Self {
314 Self(EscapeIterInner::printable(chr))
315 }
316
317 #[inline]
318 #[ferrocene::prevalidated]
319 const fn backslash(c: ascii::Char) -> Self {
320 Self(EscapeIterInner::backslash(c))
321 }
322
323 #[inline]
324 #[ferrocene::prevalidated]
325 const fn unicode(c: char) -> Self {
326 Self(EscapeIterInner::unicode(c))
327 }
328}
329
330#[stable(feature = "char_escape_debug", since = "1.20.0")]
331impl Iterator for EscapeDebug {
332 type Item = char;
333
334 #[inline]
335 #[ferrocene::prevalidated]
336 fn next(&mut self) -> Option<char> {
337 self.0.next()
338 }
339
340 #[inline]
341 #[ferrocene::prevalidated]
342 fn size_hint(&self) -> (usize, Option<usize>) {
343 let n = self.len();
344 (n, Some(n))
345 }
346
347 #[inline]
348 #[ferrocene::prevalidated]
349 fn count(self) -> usize {
350 self.len()
351 }
352}
353
354#[stable(feature = "char_escape_debug", since = "1.20.0")]
355impl ExactSizeIterator for EscapeDebug {
356 #[ferrocene::prevalidated]
357 fn len(&self) -> usize {
358 self.0.len()
359 }
360}
361
362#[stable(feature = "fused", since = "1.26.0")]
363impl FusedIterator for EscapeDebug {}
364
365#[stable(feature = "char_escape_debug", since = "1.20.0")]
366impl fmt::Display for EscapeDebug {
367 #[inline]
368 #[ferrocene::prevalidated]
369 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
370 fmt::Display::fmt(&self.0, f)
371 }
372}
373
374macro_rules! casemappingiter_impls {
375 ($(#[$attr:meta])* $ITER_NAME:ident) => {
376 $(#[$attr])*
377 #[stable(feature = "rust1", since = "1.0.0")]
378 #[derive(Debug, Clone)]
379 pub struct $ITER_NAME(CaseMappingIter);
380
381 #[stable(feature = "rust1", since = "1.0.0")]
382 impl Iterator for $ITER_NAME {
383 type Item = char;
384 fn next(&mut self) -> Option<char> {
385 self.0.next()
386 }
387
388 fn size_hint(&self) -> (usize, Option<usize>) {
389 self.0.size_hint()
390 }
391
392 fn fold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
393 where
394 Fold: FnMut(Acc, Self::Item) -> Acc,
395 {
396 self.0.fold(init, fold)
397 }
398
399 fn count(self) -> usize {
400 self.0.count()
401 }
402
403 fn last(self) -> Option<Self::Item> {
404 self.0.last()
405 }
406
407 fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
408 self.0.advance_by(n)
409 }
410
411 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
412 unsafe { self.0.__iterator_get_unchecked(idx) }
414 }
415 }
416
417 #[stable(feature = "case_mapping_double_ended", since = "1.59.0")]
418 impl DoubleEndedIterator for $ITER_NAME {
419 fn next_back(&mut self) -> Option<char> {
420 self.0.next_back()
421 }
422
423 fn rfold<Acc, Fold>(self, init: Acc, rfold: Fold) -> Acc
424 where
425 Fold: FnMut(Acc, Self::Item) -> Acc,
426 {
427 self.0.rfold(init, rfold)
428 }
429
430 fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
431 self.0.advance_back_by(n)
432 }
433 }
434
435 #[stable(feature = "fused", since = "1.26.0")]
436 impl FusedIterator for $ITER_NAME {}
437
438 #[stable(feature = "exact_size_case_mapping_iter", since = "1.35.0")]
439 impl ExactSizeIterator for $ITER_NAME {
440 fn len(&self) -> usize {
441 self.0.len()
442 }
443
444 fn is_empty(&self) -> bool {
445 self.0.is_empty()
446 }
447 }
448
449 #[unstable(feature = "trusted_len", issue = "37572")]
451 unsafe impl TrustedLen for $ITER_NAME {}
452
453 #[doc(hidden)]
455 #[unstable(feature = "std_internals", issue = "none")]
456 unsafe impl TrustedRandomAccessNoCoerce for $ITER_NAME {
457 const MAY_HAVE_SIDE_EFFECT: bool = false;
458 }
459
460 #[doc(hidden)]
462 #[unstable(feature = "std_internals", issue = "none")]
463 unsafe impl TrustedRandomAccess for $ITER_NAME {}
464
465 #[stable(feature = "char_struct_display", since = "1.16.0")]
466 impl fmt::Display for $ITER_NAME {
467 #[inline]
468 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
469 fmt::Display::fmt(&self.0, f)
470 }
471 }
472 }
473}
474
475casemappingiter_impls! {
476 ToLowercase
483}
484
485casemappingiter_impls! {
486 ToUppercase
493}
494
495#[derive(Debug, Clone)]
496struct CaseMappingIter(core::array::IntoIter<char, 3>);
497
498impl CaseMappingIter {
499 #[inline]
500 fn new(chars: [char; 3]) -> CaseMappingIter {
501 let mut iter = chars.into_iter();
502 if chars[2] == '\0' {
503 iter.next_back();
504 if chars[1] == '\0' {
505 iter.next_back();
506
507 }
510 }
511 CaseMappingIter(iter)
512 }
513}
514
515impl Iterator for CaseMappingIter {
516 type Item = char;
517
518 fn next(&mut self) -> Option<char> {
519 self.0.next()
520 }
521
522 fn size_hint(&self) -> (usize, Option<usize>) {
523 self.0.size_hint()
524 }
525
526 fn fold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
527 where
528 Fold: FnMut(Acc, Self::Item) -> Acc,
529 {
530 self.0.fold(init, fold)
531 }
532
533 fn count(self) -> usize {
534 self.0.count()
535 }
536
537 fn last(self) -> Option<Self::Item> {
538 self.0.last()
539 }
540
541 fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
542 self.0.advance_by(n)
543 }
544
545 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
546 unsafe { self.0.__iterator_get_unchecked(idx) }
548 }
549}
550
551impl DoubleEndedIterator for CaseMappingIter {
552 fn next_back(&mut self) -> Option<char> {
553 self.0.next_back()
554 }
555
556 fn rfold<Acc, Fold>(self, init: Acc, rfold: Fold) -> Acc
557 where
558 Fold: FnMut(Acc, Self::Item) -> Acc,
559 {
560 self.0.rfold(init, rfold)
561 }
562
563 fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
564 self.0.advance_back_by(n)
565 }
566}
567
568impl ExactSizeIterator for CaseMappingIter {
569 fn len(&self) -> usize {
570 self.0.len()
571 }
572
573 fn is_empty(&self) -> bool {
574 self.0.is_empty()
575 }
576}
577
578impl FusedIterator for CaseMappingIter {}
579
580unsafe impl TrustedLen for CaseMappingIter {}
582
583unsafe impl TrustedRandomAccessNoCoerce for CaseMappingIter {
585 const MAY_HAVE_SIDE_EFFECT: bool = false;
586}
587
588unsafe impl TrustedRandomAccess for CaseMappingIter {}
590
591impl fmt::Display for CaseMappingIter {
592 #[inline]
593 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
594 for c in self.0.clone() {
595 f.write_char(c)?;
596 }
597 Ok(())
598 }
599}
600
601#[stable(feature = "u8_from_char", since = "1.59.0")]
603#[derive(Debug, Copy, Clone, PartialEq, Eq)]
604pub struct TryFromCharError(pub(crate) ());
605
606#[stable(feature = "u8_from_char", since = "1.59.0")]
607impl fmt::Display for TryFromCharError {
608 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
609 "unicode code point out of range".fmt(fmt)
610 }
611}
612
613#[stable(feature = "u8_from_char", since = "1.59.0")]
614impl Error for TryFromCharError {}