1use crate::error::{Error, ErrorCode, Result};
2use alloc::vec::Vec;
3use core::char;
4use core::cmp;
5use core::ops::Deref;
6use core::str;
7
8#[cfg(feature = "std")]
9use crate::io;
10#[cfg(feature = "std")]
11use crate::iter::LineColIterator;
12
13#[cfg(feature = "raw_value")]
14use crate::raw::BorrowedRawDeserializer;
15#[cfg(all(feature = "raw_value", feature = "std"))]
16use crate::raw::OwnedRawDeserializer;
17#[cfg(all(feature = "raw_value", feature = "std"))]
18use alloc::string::String;
19#[cfg(feature = "raw_value")]
20use serde::de::Visitor;
21
22pub trait Read<'de>: private::Sealed {
29 #[doc(hidden)]
30 fn next(&mut self) -> Result<Option<u8>>;
31 #[doc(hidden)]
32 fn peek(&mut self) -> Result<Option<u8>>;
33
34 #[doc(hidden)]
36 fn discard(&mut self);
37
38 #[doc(hidden)]
46 fn position(&self) -> Position;
47
48 #[doc(hidden)]
56 fn peek_position(&self) -> Position;
57
58 #[doc(hidden)]
61 fn byte_offset(&self) -> usize;
62
63 #[doc(hidden)]
67 fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>>;
68
69 #[doc(hidden)]
76 fn parse_str_raw<'s>(
77 &'s mut self,
78 scratch: &'s mut Vec<u8>,
79 ) -> Result<Reference<'de, 's, [u8]>>;
80
81 #[doc(hidden)]
84 fn ignore_str(&mut self) -> Result<()>;
85
86 #[doc(hidden)]
89 fn decode_hex_escape(&mut self) -> Result<u16>;
90
91 #[cfg(feature = "raw_value")]
95 #[doc(hidden)]
96 fn begin_raw_buffering(&mut self);
97
98 #[cfg(feature = "raw_value")]
101 #[doc(hidden)]
102 fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
103 where
104 V: Visitor<'de>;
105
106 #[doc(hidden)]
111 const should_early_return_if_failed: bool;
112
113 #[doc(hidden)]
116 fn set_failed(&mut self, failed: &mut bool);
117}
118
119pub struct Position {
120 pub line: usize,
121 pub column: usize,
122}
123
124pub enum Reference<'b, 'c, T>
125where
126 T: ?Sized + 'static,
127{
128 Borrowed(&'b T),
129 Copied(&'c T),
130}
131
132impl<'b, 'c, T> Deref for Reference<'b, 'c, T>
133where
134 T: ?Sized + 'static,
135{
136 type Target = T;
137
138 fn deref(&self) -> &Self::Target {
139 match *self {
140 Reference::Borrowed(b) => b,
141 Reference::Copied(c) => c,
142 }
143 }
144}
145
146#[cfg(feature = "std")]
148#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
149pub struct IoRead<R>
150where
151 R: io::Read,
152{
153 iter: LineColIterator<io::Bytes<R>>,
154 ch: Option<u8>,
156 #[cfg(feature = "raw_value")]
157 raw_buffer: Option<Vec<u8>>,
158}
159
160pub struct SliceRead<'a> {
165 slice: &'a [u8],
166 index: usize,
168 #[cfg(feature = "raw_value")]
169 raw_buffering_start_index: usize,
170}
171
172pub struct StrRead<'a> {
176 delegate: SliceRead<'a>,
177 #[cfg(feature = "raw_value")]
178 data: &'a str,
179}
180
181mod private {
183 pub trait Sealed {}
184}
185
186#[cfg(feature = "std")]
189impl<R> IoRead<R>
190where
191 R: io::Read,
192{
193 pub fn new(reader: R) -> Self {
195 IoRead {
196 iter: LineColIterator::new(reader.bytes()),
197 ch: None,
198 #[cfg(feature = "raw_value")]
199 raw_buffer: None,
200 }
201 }
202}
203
204#[cfg(feature = "std")]
205impl<R> private::Sealed for IoRead<R> where R: io::Read {}
206
207#[cfg(feature = "std")]
208impl<R> IoRead<R>
209where
210 R: io::Read,
211{
212 fn parse_str_bytes<'s, T, F>(
213 &'s mut self,
214 scratch: &'s mut Vec<u8>,
215 validate: bool,
216 result: F,
217 ) -> Result<T>
218 where
219 T: 's,
220 F: FnOnce(&'s Self, &'s [u8]) -> Result<T>,
221 {
222 loop {
223 let ch = tri!(next_or_eof(self));
224 if !ESCAPE[ch as usize] {
225 scratch.push(ch);
226 continue;
227 }
228 match ch {
229 b'"' => {
230 return result(self, scratch);
231 }
232 b'\\' => {
233 tri!(parse_escape(self, validate, scratch));
234 }
235 _ => {
236 if validate {
237 return error(self, ErrorCode::ControlCharacterWhileParsingString);
238 }
239 scratch.push(ch);
240 }
241 }
242 }
243 }
244}
245
246#[cfg(feature = "std")]
247impl<'de, R> Read<'de> for IoRead<R>
248where
249 R: io::Read,
250{
251 #[inline]
252 fn next(&mut self) -> Result<Option<u8>> {
253 match self.ch.take() {
254 Some(ch) => {
255 #[cfg(feature = "raw_value")]
256 {
257 if let Some(buf) = &mut self.raw_buffer {
258 buf.push(ch);
259 }
260 }
261 Ok(Some(ch))
262 }
263 None => match self.iter.next() {
264 Some(Err(err)) => Err(Error::io(err)),
265 Some(Ok(ch)) => {
266 #[cfg(feature = "raw_value")]
267 {
268 if let Some(buf) = &mut self.raw_buffer {
269 buf.push(ch);
270 }
271 }
272 Ok(Some(ch))
273 }
274 None => Ok(None),
275 },
276 }
277 }
278
279 #[inline]
280 fn peek(&mut self) -> Result<Option<u8>> {
281 match self.ch {
282 Some(ch) => Ok(Some(ch)),
283 None => match self.iter.next() {
284 Some(Err(err)) => Err(Error::io(err)),
285 Some(Ok(ch)) => {
286 self.ch = Some(ch);
287 Ok(self.ch)
288 }
289 None => Ok(None),
290 },
291 }
292 }
293
294 #[cfg(not(feature = "raw_value"))]
295 #[inline]
296 fn discard(&mut self) {
297 self.ch = None;
298 }
299
300 #[cfg(feature = "raw_value")]
301 fn discard(&mut self) {
302 if let Some(ch) = self.ch.take() {
303 if let Some(buf) = &mut self.raw_buffer {
304 buf.push(ch);
305 }
306 }
307 }
308
309 fn position(&self) -> Position {
310 Position {
311 line: self.iter.line(),
312 column: self.iter.col(),
313 }
314 }
315
316 fn peek_position(&self) -> Position {
317 self.position()
320 }
321
322 fn byte_offset(&self) -> usize {
323 match self.ch {
324 Some(_) => self.iter.byte_offset() - 1,
325 None => self.iter.byte_offset(),
326 }
327 }
328
329 fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>> {
330 self.parse_str_bytes(scratch, true, as_str)
331 .map(Reference::Copied)
332 }
333
334 fn parse_str_raw<'s>(
335 &'s mut self,
336 scratch: &'s mut Vec<u8>,
337 ) -> Result<Reference<'de, 's, [u8]>> {
338 self.parse_str_bytes(scratch, false, |_, bytes| Ok(bytes))
339 .map(Reference::Copied)
340 }
341
342 fn ignore_str(&mut self) -> Result<()> {
343 loop {
344 let ch = tri!(next_or_eof(self));
345 if !ESCAPE[ch as usize] {
346 continue;
347 }
348 match ch {
349 b'"' => {
350 return Ok(());
351 }
352 b'\\' => {
353 tri!(ignore_escape(self));
354 }
355 _ => {
356 return error(self, ErrorCode::ControlCharacterWhileParsingString);
357 }
358 }
359 }
360 }
361
362 fn decode_hex_escape(&mut self) -> Result<u16> {
363 let mut n = 0;
364 for _ in 0..4 {
365 match decode_hex_val(tri!(next_or_eof(self))) {
366 None => return error(self, ErrorCode::InvalidEscape),
367 Some(val) => {
368 n = (n << 4) + val;
369 }
370 }
371 }
372 Ok(n)
373 }
374
375 #[cfg(feature = "raw_value")]
376 fn begin_raw_buffering(&mut self) {
377 self.raw_buffer = Some(Vec::new());
378 }
379
380 #[cfg(feature = "raw_value")]
381 fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
382 where
383 V: Visitor<'de>,
384 {
385 let raw = self.raw_buffer.take().unwrap();
386 let raw = match String::from_utf8(raw) {
387 Ok(raw) => raw,
388 Err(_) => return error(self, ErrorCode::InvalidUnicodeCodePoint),
389 };
390 visitor.visit_map(OwnedRawDeserializer {
391 raw_value: Some(raw),
392 })
393 }
394
395 const should_early_return_if_failed: bool = true;
396
397 #[inline]
398 #[cold]
399 fn set_failed(&mut self, failed: &mut bool) {
400 *failed = true;
401 }
402}
403
404impl<'a> SliceRead<'a> {
407 pub fn new(slice: &'a [u8]) -> Self {
409 SliceRead {
410 slice,
411 index: 0,
412 #[cfg(feature = "raw_value")]
413 raw_buffering_start_index: 0,
414 }
415 }
416
417 fn position_of_index(&self, i: usize) -> Position {
418 let start_of_line = match memchr::memrchr(b'\n', &self.slice[..i]) {
419 Some(position) => position + 1,
420 None => 0,
421 };
422 Position {
423 line: 1 + memchr::memchr_iter(b'\n', &self.slice[..start_of_line]).count(),
424 column: i - start_of_line,
425 }
426 }
427
428 fn parse_str_bytes<'s, T, F>(
432 &'s mut self,
433 scratch: &'s mut Vec<u8>,
434 validate: bool,
435 result: F,
436 ) -> Result<Reference<'a, 's, T>>
437 where
438 T: ?Sized + 's,
439 F: for<'f> FnOnce(&'s Self, &'f [u8]) -> Result<&'f T>,
440 {
441 let mut start = self.index;
443
444 loop {
445 while self.index < self.slice.len() && !ESCAPE[self.slice[self.index] as usize] {
446 self.index += 1;
447 }
448 if self.index == self.slice.len() {
449 return error(self, ErrorCode::EofWhileParsingString);
450 }
451 match self.slice[self.index] {
452 b'"' => {
453 if scratch.is_empty() {
454 let borrowed = &self.slice[start..self.index];
457 self.index += 1;
458 return result(self, borrowed).map(Reference::Borrowed);
459 } else {
460 scratch.extend_from_slice(&self.slice[start..self.index]);
461 self.index += 1;
462 return result(self, scratch).map(Reference::Copied);
463 }
464 }
465 b'\\' => {
466 scratch.extend_from_slice(&self.slice[start..self.index]);
467 self.index += 1;
468 tri!(parse_escape(self, validate, scratch));
469 start = self.index;
470 }
471 _ => {
472 self.index += 1;
473 if validate {
474 return error(self, ErrorCode::ControlCharacterWhileParsingString);
475 }
476 }
477 }
478 }
479 }
480}
481
482impl<'a> private::Sealed for SliceRead<'a> {}
483
484impl<'a> Read<'a> for SliceRead<'a> {
485 #[inline]
486 fn next(&mut self) -> Result<Option<u8>> {
487 Ok(if self.index < self.slice.len() {
490 let ch = self.slice[self.index];
491 self.index += 1;
492 Some(ch)
493 } else {
494 None
495 })
496 }
497
498 #[inline]
499 fn peek(&mut self) -> Result<Option<u8>> {
500 Ok(if self.index < self.slice.len() {
503 Some(self.slice[self.index])
504 } else {
505 None
506 })
507 }
508
509 #[inline]
510 fn discard(&mut self) {
511 self.index += 1;
512 }
513
514 fn position(&self) -> Position {
515 self.position_of_index(self.index)
516 }
517
518 fn peek_position(&self) -> Position {
519 self.position_of_index(cmp::min(self.slice.len(), self.index + 1))
522 }
523
524 fn byte_offset(&self) -> usize {
525 self.index
526 }
527
528 fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'a, 's, str>> {
529 self.parse_str_bytes(scratch, true, as_str)
530 }
531
532 fn parse_str_raw<'s>(
533 &'s mut self,
534 scratch: &'s mut Vec<u8>,
535 ) -> Result<Reference<'a, 's, [u8]>> {
536 self.parse_str_bytes(scratch, false, |_, bytes| Ok(bytes))
537 }
538
539 fn ignore_str(&mut self) -> Result<()> {
540 loop {
541 while self.index < self.slice.len() && !ESCAPE[self.slice[self.index] as usize] {
542 self.index += 1;
543 }
544 if self.index == self.slice.len() {
545 return error(self, ErrorCode::EofWhileParsingString);
546 }
547 match self.slice[self.index] {
548 b'"' => {
549 self.index += 1;
550 return Ok(());
551 }
552 b'\\' => {
553 self.index += 1;
554 tri!(ignore_escape(self));
555 }
556 _ => {
557 return error(self, ErrorCode::ControlCharacterWhileParsingString);
558 }
559 }
560 }
561 }
562
563 fn decode_hex_escape(&mut self) -> Result<u16> {
564 if self.index + 4 > self.slice.len() {
565 self.index = self.slice.len();
566 return error(self, ErrorCode::EofWhileParsingString);
567 }
568
569 let mut n = 0;
570 for _ in 0..4 {
571 let ch = decode_hex_val(self.slice[self.index]);
572 self.index += 1;
573 match ch {
574 None => return error(self, ErrorCode::InvalidEscape),
575 Some(val) => {
576 n = (n << 4) + val;
577 }
578 }
579 }
580 Ok(n)
581 }
582
583 #[cfg(feature = "raw_value")]
584 fn begin_raw_buffering(&mut self) {
585 self.raw_buffering_start_index = self.index;
586 }
587
588 #[cfg(feature = "raw_value")]
589 fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
590 where
591 V: Visitor<'a>,
592 {
593 let raw = &self.slice[self.raw_buffering_start_index..self.index];
594 let raw = match str::from_utf8(raw) {
595 Ok(raw) => raw,
596 Err(_) => return error(self, ErrorCode::InvalidUnicodeCodePoint),
597 };
598 visitor.visit_map(BorrowedRawDeserializer {
599 raw_value: Some(raw),
600 })
601 }
602
603 const should_early_return_if_failed: bool = false;
604
605 #[inline]
606 #[cold]
607 fn set_failed(&mut self, _failed: &mut bool) {
608 self.slice = &self.slice[..self.index];
609 }
610}
611
612impl<'a> StrRead<'a> {
615 pub fn new(s: &'a str) -> Self {
617 StrRead {
618 delegate: SliceRead::new(s.as_bytes()),
619 #[cfg(feature = "raw_value")]
620 data: s,
621 }
622 }
623}
624
625impl<'a> private::Sealed for StrRead<'a> {}
626
627impl<'a> Read<'a> for StrRead<'a> {
628 #[inline]
629 fn next(&mut self) -> Result<Option<u8>> {
630 self.delegate.next()
631 }
632
633 #[inline]
634 fn peek(&mut self) -> Result<Option<u8>> {
635 self.delegate.peek()
636 }
637
638 #[inline]
639 fn discard(&mut self) {
640 self.delegate.discard();
641 }
642
643 fn position(&self) -> Position {
644 self.delegate.position()
645 }
646
647 fn peek_position(&self) -> Position {
648 self.delegate.peek_position()
649 }
650
651 fn byte_offset(&self) -> usize {
652 self.delegate.byte_offset()
653 }
654
655 fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'a, 's, str>> {
656 self.delegate.parse_str_bytes(scratch, true, |_, bytes| {
657 Ok(unsafe { str::from_utf8_unchecked(bytes) })
661 })
662 }
663
664 fn parse_str_raw<'s>(
665 &'s mut self,
666 scratch: &'s mut Vec<u8>,
667 ) -> Result<Reference<'a, 's, [u8]>> {
668 self.delegate.parse_str_raw(scratch)
669 }
670
671 fn ignore_str(&mut self) -> Result<()> {
672 self.delegate.ignore_str()
673 }
674
675 fn decode_hex_escape(&mut self) -> Result<u16> {
676 self.delegate.decode_hex_escape()
677 }
678
679 #[cfg(feature = "raw_value")]
680 fn begin_raw_buffering(&mut self) {
681 self.delegate.begin_raw_buffering();
682 }
683
684 #[cfg(feature = "raw_value")]
685 fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
686 where
687 V: Visitor<'a>,
688 {
689 let raw = &self.data[self.delegate.raw_buffering_start_index..self.delegate.index];
690 visitor.visit_map(BorrowedRawDeserializer {
691 raw_value: Some(raw),
692 })
693 }
694
695 const should_early_return_if_failed: bool = false;
696
697 #[inline]
698 #[cold]
699 fn set_failed(&mut self, failed: &mut bool) {
700 self.delegate.set_failed(failed);
701 }
702}
703
704impl<'a, 'de, R> private::Sealed for &'a mut R where R: Read<'de> {}
707
708impl<'a, 'de, R> Read<'de> for &'a mut R
709where
710 R: Read<'de>,
711{
712 fn next(&mut self) -> Result<Option<u8>> {
713 R::next(self)
714 }
715
716 fn peek(&mut self) -> Result<Option<u8>> {
717 R::peek(self)
718 }
719
720 fn discard(&mut self) {
721 R::discard(self);
722 }
723
724 fn position(&self) -> Position {
725 R::position(self)
726 }
727
728 fn peek_position(&self) -> Position {
729 R::peek_position(self)
730 }
731
732 fn byte_offset(&self) -> usize {
733 R::byte_offset(self)
734 }
735
736 fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>> {
737 R::parse_str(self, scratch)
738 }
739
740 fn parse_str_raw<'s>(
741 &'s mut self,
742 scratch: &'s mut Vec<u8>,
743 ) -> Result<Reference<'de, 's, [u8]>> {
744 R::parse_str_raw(self, scratch)
745 }
746
747 fn ignore_str(&mut self) -> Result<()> {
748 R::ignore_str(self)
749 }
750
751 fn decode_hex_escape(&mut self) -> Result<u16> {
752 R::decode_hex_escape(self)
753 }
754
755 #[cfg(feature = "raw_value")]
756 fn begin_raw_buffering(&mut self) {
757 R::begin_raw_buffering(self);
758 }
759
760 #[cfg(feature = "raw_value")]
761 fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
762 where
763 V: Visitor<'de>,
764 {
765 R::end_raw_buffering(self, visitor)
766 }
767
768 const should_early_return_if_failed: bool = R::should_early_return_if_failed;
769
770 fn set_failed(&mut self, failed: &mut bool) {
771 R::set_failed(self, failed);
772 }
773}
774
775pub trait Fused: private::Sealed {}
779impl<'a> Fused for SliceRead<'a> {}
780impl<'a> Fused for StrRead<'a> {}
781
782static ESCAPE: [bool; 256] = {
785 const CT: bool = true; const QU: bool = true; const BS: bool = true; const __: bool = false; [
790 CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, __, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, ]
808};
809
810fn next_or_eof<'de, R>(read: &mut R) -> Result<u8>
811where
812 R: ?Sized + Read<'de>,
813{
814 match tri!(read.next()) {
815 Some(b) => Ok(b),
816 None => error(read, ErrorCode::EofWhileParsingString),
817 }
818}
819
820fn peek_or_eof<'de, R>(read: &mut R) -> Result<u8>
821where
822 R: ?Sized + Read<'de>,
823{
824 match tri!(read.peek()) {
825 Some(b) => Ok(b),
826 None => error(read, ErrorCode::EofWhileParsingString),
827 }
828}
829
830fn error<'de, R, T>(read: &R, reason: ErrorCode) -> Result<T>
831where
832 R: ?Sized + Read<'de>,
833{
834 let position = read.position();
835 Err(Error::syntax(reason, position.line, position.column))
836}
837
838fn as_str<'de, 's, R: Read<'de>>(read: &R, slice: &'s [u8]) -> Result<&'s str> {
839 str::from_utf8(slice).or_else(|_| error(read, ErrorCode::InvalidUnicodeCodePoint))
840}
841
842fn parse_escape<'de, R: Read<'de>>(
845 read: &mut R,
846 validate: bool,
847 scratch: &mut Vec<u8>,
848) -> Result<()> {
849 let ch = tri!(next_or_eof(read));
850
851 match ch {
852 b'"' => scratch.push(b'"'),
853 b'\\' => scratch.push(b'\\'),
854 b'/' => scratch.push(b'/'),
855 b'b' => scratch.push(b'\x08'),
856 b'f' => scratch.push(b'\x0c'),
857 b'n' => scratch.push(b'\n'),
858 b'r' => scratch.push(b'\r'),
859 b't' => scratch.push(b'\t'),
860 b'u' => {
861 fn encode_surrogate(scratch: &mut Vec<u8>, n: u16) {
862 scratch.extend_from_slice(&[
863 (n >> 12 & 0b0000_1111) as u8 | 0b1110_0000,
864 (n >> 6 & 0b0011_1111) as u8 | 0b1000_0000,
865 (n & 0b0011_1111) as u8 | 0b1000_0000,
866 ]);
867 }
868
869 let c = match tri!(read.decode_hex_escape()) {
870 n @ 0xDC00..=0xDFFF => {
871 return if validate {
872 error(read, ErrorCode::LoneLeadingSurrogateInHexEscape)
873 } else {
874 encode_surrogate(scratch, n);
875 Ok(())
876 };
877 }
878
879 n1 @ 0xD800..=0xDBFF => {
884 if tri!(peek_or_eof(read)) == b'\\' {
885 read.discard();
886 } else {
887 return if validate {
888 read.discard();
889 error(read, ErrorCode::UnexpectedEndOfHexEscape)
890 } else {
891 encode_surrogate(scratch, n1);
892 Ok(())
893 };
894 }
895
896 if tri!(peek_or_eof(read)) == b'u' {
897 read.discard();
898 } else {
899 return if validate {
900 read.discard();
901 error(read, ErrorCode::UnexpectedEndOfHexEscape)
902 } else {
903 encode_surrogate(scratch, n1);
904 parse_escape(read, validate, scratch)
910 };
911 }
912
913 let n2 = tri!(read.decode_hex_escape());
914
915 if n2 < 0xDC00 || n2 > 0xDFFF {
916 return error(read, ErrorCode::LoneLeadingSurrogateInHexEscape);
917 }
918
919 let n = (((n1 - 0xD800) as u32) << 10 | (n2 - 0xDC00) as u32) + 0x1_0000;
920
921 match char::from_u32(n) {
922 Some(c) => c,
923 None => {
924 return error(read, ErrorCode::InvalidUnicodeCodePoint);
925 }
926 }
927 }
928
929 n => char::from_u32(n as u32).unwrap(),
932 };
933
934 scratch.extend_from_slice(c.encode_utf8(&mut [0_u8; 4]).as_bytes());
935 }
936 _ => {
937 return error(read, ErrorCode::InvalidEscape);
938 }
939 }
940
941 Ok(())
942}
943
944fn ignore_escape<'de, R>(read: &mut R) -> Result<()>
947where
948 R: ?Sized + Read<'de>,
949{
950 let ch = tri!(next_or_eof(read));
951
952 match ch {
953 b'"' | b'\\' | b'/' | b'b' | b'f' | b'n' | b'r' | b't' => {}
954 b'u' => {
955 tri!(read.decode_hex_escape());
962 }
963 _ => {
964 return error(read, ErrorCode::InvalidEscape);
965 }
966 }
967
968 Ok(())
969}
970
971static HEX: [u8; 256] = {
972 const __: u8 = 255; [
974 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, __, __, __, __, __, __, __, 10, 11, 12, 13, 14, 15, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, 10, 11, 12, 13, 14, 15, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, ]
992};
993
994fn decode_hex_val(val: u8) -> Option<u16> {
995 let n = HEX[val as usize] as u16;
996 if n == 255 {
997 None
998 } else {
999 Some(n)
1000 }
1001}