pub fn printed_string_size(s: &str) -> usize
Expand description

Returns the byte length of string literal according to RFC8785.

Examples found in repository?
src/print.rs (line 643)
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
		match self {
			crate::Value::Null => Size::Width(4),
			crate::Value::Boolean(b) => b.pre_compute_size(options, sizes),
			crate::Value::Number(n) => Size::Width(n.as_str().len()),
			crate::Value::String(s) => Size::Width(printed_string_size(s)),
			crate::Value::Array(a) => pre_compute_array_size(a, options, sizes),
			crate::Value::Object(o) => pre_compute_object_size(
				o.iter().map(|e| (e.key.as_str(), &e.value)),
				options,
				sizes,
			),
		}
	}
}

impl<'a, T: PrecomputeSize + ?Sized> PrecomputeSize for &'a T {
	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
		(**self).pre_compute_size(options, sizes)
	}
}

impl<T: PrecomputeSize> PrecomputeSize for locspan::Stripped<T> {
	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
		self.0.pre_compute_size(options, sizes)
	}
}

impl<T: PrecomputeSize, M> PrecomputeSize for locspan::Meta<T, M> {
	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
		self.value().pre_compute_size(options, sizes)
	}
}

pub fn pre_compute_array_size<I: IntoIterator>(
	items: I,
	options: &Options,
	sizes: &mut Vec<Size>,
) -> Size
where
	I::Item: PrecomputeSize,
{
	let index = sizes.len();
	sizes.push(Size::Width(0));

	let mut size = Size::Width(2 + options.object_begin + options.object_end);

	let mut len = 0;
	for (i, item) in items.into_iter().enumerate() {
		if i > 0 {
			size.add(Size::Width(
				1 + options.array_before_comma + options.array_after_comma,
			));
		}

		size.add(item.pre_compute_size(options, sizes));
		len += 1
	}

	let size = match size {
		Size::Expanded => Size::Expanded,
		Size::Width(width) => match options.array_limit {
			None => Size::Width(width),
			Some(Limit::Always) => Size::Expanded,
			Some(Limit::Item(i)) => {
				if len > i {
					Size::Expanded
				} else {
					Size::Width(width)
				}
			}
			Some(Limit::ItemOrWidth(i, w)) => {
				if len > i || width > w {
					Size::Expanded
				} else {
					Size::Width(width)
				}
			}
			Some(Limit::Width(w)) => {
				if width > w {
					Size::Expanded
				} else {
					Size::Width(width)
				}
			}
		},
	};

	sizes[index] = size;
	size
}

pub fn pre_compute_object_size<'a, V, I: IntoIterator<Item = (&'a str, V)>>(
	entries: I,
	options: &Options,
	sizes: &mut Vec<Size>,
) -> Size
where
	V: PrecomputeSize,
{
	let index = sizes.len();
	sizes.push(Size::Width(0));

	let mut size = Size::Width(2 + options.object_begin + options.object_end);

	let mut len = 0;
	for (i, (key, value)) in entries.into_iter().enumerate() {
		if i > 0 {
			size.add(Size::Width(
				1 + options.object_before_comma + options.object_after_comma,
			));
		}

		size.add(Size::Width(
			printed_string_size(key) + 1 + options.object_before_colon + options.object_after_colon,
		));
		size.add(value.pre_compute_size(options, sizes));
		len += 1;
	}

	let size = match size {
		Size::Expanded => Size::Expanded,
		Size::Width(width) => match options.object_limit {
			None => Size::Width(width),
			Some(Limit::Always) => Size::Expanded,
			Some(Limit::Item(i)) => {
				if len > i {
					Size::Expanded
				} else {
					Size::Width(width)
				}
			}
			Some(Limit::ItemOrWidth(i, w)) => {
				if len > i || width > w {
					Size::Expanded
				} else {
					Size::Width(width)
				}
			}
			Some(Limit::Width(w)) => {
				if width > w {
					Size::Expanded
				} else {
					Size::Width(width)
				}
			}
		},
	};

	sizes[index] = size;
	size
}