git.delta.rocks / jrsonnet / refs/commits / 81c4597d19ef

difftreelog

feat optional bigint preservation

Yaroslav Bolyukin2023-08-12parent: #7986fee.patch.diff
in: master

1 file changed

modifiedcrates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/manifest.rs
1use std::{borrow::Cow, fmt::Write};23use crate::{4	error::{ErrorKind::*, Result},5	throw, State, Val,6};78pub trait ManifestFormat {9	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()>;10	fn manifest(&self, val: Val) -> Result<String> {11		let mut out = String::new();12		self.manifest_buf(val, &mut out)?;13		Ok(out)14	}15	/// When outputing to file, is it safe to append a trailing newline (I.e newline won't change16	/// the meaning).17	///18	/// Default implementation returns `true`19	fn file_trailing_newline(&self) -> bool {20		true21	}22}23impl<T> ManifestFormat for Box<T>24where25	T: ManifestFormat + ?Sized,26{27	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {28		let inner = &**self;29		inner.manifest_buf(val, buf)30	}31	fn file_trailing_newline(&self) -> bool {32		let inner = &**self;33		inner.file_trailing_newline()34	}35}36impl<T> ManifestFormat for &'_ T37where38	T: ManifestFormat + ?Sized,39{40	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {41		let inner = &**self;42		inner.manifest_buf(val, buf)43	}44	fn file_trailing_newline(&self) -> bool {45		let inner = &**self;46		inner.file_trailing_newline()47	}48}4950#[derive(PartialEq, Eq, Clone, Copy)]51enum JsonFormatting {52	// Applied in manifestification53	Manifest,54	/// Used for std.manifestJson55	/// Empty array/objects extends to "[\n\n]" instead of "[ ]" as in manifest56	Std,57	/// No line breaks, used in `obj+''`58	ToString,59	/// Minified json60	Minify,61}6263pub struct JsonFormat<'s> {64	padding: Cow<'s, str>,65	mtype: JsonFormatting,66	newline: &'s str,67	key_val_sep: &'s str,68	#[cfg(feature = "exp-preserve-order")]69	preserve_order: bool,70}7172impl<'s> JsonFormat<'s> {73	// Minifying format74	pub fn minify(#[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Self {75		Self {76			padding: Cow::Borrowed(""),77			mtype: JsonFormatting::Minify,78			newline: "\n",79			key_val_sep: ":",80			#[cfg(feature = "exp-preserve-order")]81			preserve_order,82		}83	}84	// Same format as std.toString85	pub fn std_to_string() -> Self {86		Self {87			padding: Cow::Borrowed(""),88			mtype: JsonFormatting::ToString,89			newline: "\n",90			key_val_sep: ": ",91			#[cfg(feature = "exp-preserve-order")]92			preserve_order: false,93		}94	}95	pub fn std_to_json(96		padding: String,97		newline: &'s str,98		key_val_sep: &'s str,99		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,100	) -> Self {101		Self {102			padding: Cow::Owned(padding),103			mtype: JsonFormatting::Std,104			newline,105			key_val_sep,106			#[cfg(feature = "exp-preserve-order")]107			preserve_order,108		}109	}110	// Same format as CLI manifestification111	pub fn cli(112		padding: usize,113		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,114	) -> Self {115		if padding == 0 {116			return Self::minify(117				#[cfg(feature = "exp-preserve-order")]118				preserve_order,119			);120		}121		Self {122			padding: Cow::Owned(" ".repeat(padding)),123			mtype: JsonFormatting::Manifest,124			newline: "\n",125			key_val_sep: ": ",126			#[cfg(feature = "exp-preserve-order")]127			preserve_order,128		}129	}130}131impl Default for JsonFormat<'static> {132	fn default() -> Self {133		Self {134			padding: Cow::Borrowed("    "),135			mtype: JsonFormatting::Manifest,136			newline: "\n",137			key_val_sep: ": ",138			#[cfg(feature = "exp-preserve-order")]139			preserve_order: false,140		}141	}142}143144pub fn manifest_json_ex(val: &Val, options: &JsonFormat<'_>) -> Result<String> {145	let mut out = String::new();146	manifest_json_ex_buf(val, &mut out, &mut String::new(), options)?;147	Ok(out)148}149fn manifest_json_ex_buf(150	val: &Val,151	buf: &mut String,152	cur_padding: &mut String,153	options: &JsonFormat<'_>,154) -> Result<()> {155	let mtype = options.mtype;156	match val {157		Val::Bool(v) => {158			if *v {159				buf.push_str("true");160			} else {161				buf.push_str("false");162			}163		}164		Val::Null => buf.push_str("null"),165		Val::Str(s) => escape_string_json_buf(&s.clone().into_flat(), buf),166		Val::Num(n) => write!(buf, "{n}").unwrap(),167		#[cfg(feature = "exp-bigint")]168		Val::BigInt(n) => write!(buf, "{n}").unwrap(),169		Val::Arr(items) => {170			buf.push('[');171			if !items.is_empty() {172				if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {173					buf.push_str(options.newline);174				}175176				let old_len = cur_padding.len();177				cur_padding.push_str(&options.padding);178				for (i, item) in items.iter().enumerate() {179					if i != 0 {180						buf.push(',');181						if mtype == JsonFormatting::ToString {182							buf.push(' ');183						} else if mtype != JsonFormatting::Minify {184							buf.push_str(options.newline);185						}186					}187					buf.push_str(cur_padding);188					manifest_json_ex_buf(&item?, buf, cur_padding, options)?;189				}190				cur_padding.truncate(old_len);191192				if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {193					buf.push_str(options.newline);194					buf.push_str(cur_padding);195				}196			} else if mtype == JsonFormatting::Std {197				buf.push_str(options.newline);198				buf.push_str(options.newline);199				buf.push_str(cur_padding);200			} else if mtype == JsonFormatting::ToString || mtype == JsonFormatting::Manifest {201				buf.push(' ');202			}203			buf.push(']');204		}205		Val::Obj(obj) => {206			obj.run_assertions()?;207			buf.push('{');208			let fields = obj.fields(209				#[cfg(feature = "exp-preserve-order")]210				options.preserve_order,211			);212			if !fields.is_empty() {213				if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {214					buf.push_str(options.newline);215				}216217				let old_len = cur_padding.len();218				cur_padding.push_str(&options.padding);219				for (i, field) in fields.into_iter().enumerate() {220					if i != 0 {221						buf.push(',');222						if mtype == JsonFormatting::ToString {223							buf.push(' ');224						} else if mtype != JsonFormatting::Minify {225							buf.push_str(options.newline);226						}227					}228					buf.push_str(cur_padding);229					escape_string_json_buf(&field, buf);230					buf.push_str(options.key_val_sep);231					State::push_description(232						|| format!("field <{}> manifestification", field.clone()),233						|| {234							let value = obj.get(field.clone())?.unwrap();235							manifest_json_ex_buf(&value, buf, cur_padding, options)?;236							Ok(())237						},238					)?;239				}240				cur_padding.truncate(old_len);241242				if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {243					buf.push_str(options.newline);244					buf.push_str(cur_padding);245				}246			} else if mtype == JsonFormatting::Std {247				buf.push_str(options.newline);248				buf.push_str(options.newline);249				buf.push_str(cur_padding);250			} else if mtype == JsonFormatting::ToString || mtype == JsonFormatting::Manifest {251				buf.push(' ');252			}253			buf.push('}');254		}255		Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),256	};257	Ok(())258}259260impl ManifestFormat for JsonFormat<'_> {261	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {262		manifest_json_ex_buf(&val, buf, &mut String::new(), self)263	}264}265266pub struct ToStringFormat;267impl ManifestFormat for ToStringFormat {268	fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {269		JsonFormat::std_to_string().manifest_buf(val, out)270	}271	fn file_trailing_newline(&self) -> bool {272		false273	}274}275pub struct StringFormat;276impl ManifestFormat for StringFormat {277	fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {278		let Val::Str(s) = val else {279			throw!(280				"output should be string for string manifest format, got {}",281				val.value_type()282			)283		};284		write!(out, "{s}").unwrap();285		Ok(())286	}287	fn file_trailing_newline(&self) -> bool {288		false289	}290}291292pub struct YamlStreamFormat<I>(pub I);293impl<I: ManifestFormat> ManifestFormat for YamlStreamFormat<I> {294	fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {295		let Val::Arr(arr) = val else {296			throw!(297				"output should be array for yaml stream format, got {}",298				val.value_type()299			)300		};301		if !arr.is_empty() {302			for v in arr.iter() {303				let v = v?;304				out.push_str("---\n");305				self.0.manifest_buf(v, out)?;306				out.push('\n');307			}308			out.push_str("...");309		}310		Ok(())311	}312}313314pub fn escape_string_json(s: &str) -> String {315	let mut buf = String::new();316	escape_string_json_buf(s, &mut buf);317	buf318}319320// Json string encoding was borrowed from https://github.com/serde-rs/json321322const BB: u8 = b'b'; // \x08323const TT: u8 = b't'; // \x09324const NN: u8 = b'n'; // \x0A325const FF: u8 = b'f'; // \x0C326const RR: u8 = b'r'; // \x0D327const QU: u8 = b'"'; // \x22328const BS: u8 = b'\\'; // \x5C329const UU: u8 = b'u'; // \x00...\x1F except the ones above330const __: u8 = 0;331332// Lookup table of escape sequences. A value of b'x' at index i means that byte333// i is escaped as "\x" in JSON. A value of 0 means that byte i is not escaped.334static ESCAPE: [u8; 256] = [335	//   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F336	UU, UU, UU, UU, UU, UU, UU, UU, BB, TT, NN, UU, FF, RR, UU, UU, // 0337	UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, // 1338	__, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2339	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 3340	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 4341	__, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, // 5342	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 6343	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7344	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8345	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9346	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A347	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B348	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C349	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D350	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E351	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F352];353354pub fn escape_string_json_buf(value: &str, buf: &mut String) {355	// Safety: we only write correct utf-8 in this function356	let buf: &mut Vec<u8> = unsafe { &mut *(buf as *mut String).cast::<Vec<u8>>() };357	let bytes = value.as_bytes();358359	// Perfect for ascii strings, removes any reallocations360	buf.reserve(value.len() + 2);361362	buf.push(b'"');363364	let mut start = 0;365366	for (i, &byte) in bytes.iter().enumerate() {367		let escape = ESCAPE[byte as usize];368		if escape == __ {369			continue;370		}371372		if start < i {373			buf.extend_from_slice(&bytes[start..i]);374		}375		start = i + 1;376377		match escape {378			self::BB | self::TT | self::NN | self::FF | self::RR | self::QU | self::BS => {379				buf.extend_from_slice(&[b'\\', escape]);380			}381			self::UU => {382				static HEX_DIGITS: [u8; 16] = *b"0123456789abcdef";383				let bytes = &[384					b'\\',385					b'u',386					b'0',387					b'0',388					HEX_DIGITS[(byte >> 4) as usize],389					HEX_DIGITS[(byte & 0xF) as usize],390				];391				buf.extend_from_slice(bytes);392			}393			_ => unreachable!(),394		}395	}396397	if start == bytes.len() {398		buf.push(b'"');399		return;400	}401402	buf.extend_from_slice(&bytes[start..]);403	buf.push(b'"');404}
after · crates/jrsonnet-evaluator/src/manifest.rs
1use std::{borrow::Cow, fmt::Write};23use crate::{4	error::{ErrorKind::*, Result},5	throw, State, Val,6};78pub trait ManifestFormat {9	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()>;10	fn manifest(&self, val: Val) -> Result<String> {11		let mut out = String::new();12		self.manifest_buf(val, &mut out)?;13		Ok(out)14	}15	/// When outputing to file, is it safe to append a trailing newline (I.e newline won't change16	/// the meaning).17	///18	/// Default implementation returns `true`19	fn file_trailing_newline(&self) -> bool {20		true21	}22}23impl<T> ManifestFormat for Box<T>24where25	T: ManifestFormat + ?Sized,26{27	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {28		let inner = &**self;29		inner.manifest_buf(val, buf)30	}31	fn file_trailing_newline(&self) -> bool {32		let inner = &**self;33		inner.file_trailing_newline()34	}35}36impl<T> ManifestFormat for &'_ T37where38	T: ManifestFormat + ?Sized,39{40	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {41		let inner = &**self;42		inner.manifest_buf(val, buf)43	}44	fn file_trailing_newline(&self) -> bool {45		let inner = &**self;46		inner.file_trailing_newline()47	}48}4950#[derive(PartialEq, Eq, Clone, Copy)]51enum JsonFormatting {52	// Applied in manifestification53	Manifest,54	/// Used for std.manifestJson55	/// Empty array/objects extends to "[\n\n]" instead of "[ ]" as in manifest56	Std,57	/// No line breaks, used in `obj+''`58	ToString,59	/// Minified json60	Minify,61}6263pub struct JsonFormat<'s> {64	padding: Cow<'s, str>,65	mtype: JsonFormatting,66	newline: &'s str,67	key_val_sep: &'s str,68	#[cfg(feature = "exp-preserve-order")]69	preserve_order: bool,70	#[cfg(feature = "exp-bigint")]71	preserve_bigints: bool,72}7374impl<'s> JsonFormat<'s> {75	// Minifying format76	pub fn minify(#[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Self {77		Self {78			padding: Cow::Borrowed(""),79			mtype: JsonFormatting::Minify,80			newline: "\n",81			key_val_sep: ":",82			#[cfg(feature = "exp-preserve-order")]83			preserve_order,84			#[cfg(feature = "exp-bigint")]85			preserve_bigints: false,86		}87	}88	// Same format as std.toString89	pub fn std_to_string() -> Self {90		Self {91			padding: Cow::Borrowed(""),92			mtype: JsonFormatting::ToString,93			newline: "\n",94			key_val_sep: ": ",95			#[cfg(feature = "exp-preserve-order")]96			preserve_order: false,97			#[cfg(feature = "exp-bigint")]98			preserve_bigints: false,99		}100	}101	pub fn std_to_json(102		padding: String,103		newline: &'s str,104		key_val_sep: &'s str,105		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,106	) -> Self {107		Self {108			padding: Cow::Owned(padding),109			mtype: JsonFormatting::Std,110			newline,111			key_val_sep,112			#[cfg(feature = "exp-preserve-order")]113			preserve_order,114			#[cfg(feature = "exp-bigint")]115			preserve_bigints: false,116		}117	}118	// Same format as CLI manifestification119	pub fn cli(120		padding: usize,121		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,122	) -> Self {123		if padding == 0 {124			return Self::minify(125				#[cfg(feature = "exp-preserve-order")]126				preserve_order,127			);128		}129		Self {130			padding: Cow::Owned(" ".repeat(padding)),131			mtype: JsonFormatting::Manifest,132			newline: "\n",133			key_val_sep: ": ",134			#[cfg(feature = "exp-preserve-order")]135			preserve_order,136			#[cfg(feature = "exp-bigint")]137			preserve_bigints: false,138		}139	}140}141impl Default for JsonFormat<'static> {142	fn default() -> Self {143		Self {144			padding: Cow::Borrowed("    "),145			mtype: JsonFormatting::Manifest,146			newline: "\n",147			key_val_sep: ": ",148			#[cfg(feature = "exp-preserve-order")]149			preserve_order: false,150			#[cfg(feature = "exp-bigint")]151			preserve_bigints: false,152		}153	}154}155156pub fn manifest_json_ex(val: &Val, options: &JsonFormat<'_>) -> Result<String> {157	let mut out = String::new();158	manifest_json_ex_buf(val, &mut out, &mut String::new(), options)?;159	Ok(out)160}161fn manifest_json_ex_buf(162	val: &Val,163	buf: &mut String,164	cur_padding: &mut String,165	options: &JsonFormat<'_>,166) -> Result<()> {167	let mtype = options.mtype;168	match val {169		Val::Bool(v) => {170			if *v {171				buf.push_str("true");172			} else {173				buf.push_str("false");174			}175		}176		Val::Null => buf.push_str("null"),177		Val::Str(s) => escape_string_json_buf(&s.clone().into_flat(), buf),178		Val::Num(n) => write!(buf, "{n}").unwrap(),179		#[cfg(feature = "exp-bigint")]180		Val::BigInt(n) => if options.preserve_bigints {181			write!(buf, "{n}").unwrap()182		} else {183			write!(buf, "{:?}", n.to_string()).unwrap()184		},185		Val::Arr(items) => {186			buf.push('[');187			if !items.is_empty() {188				if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {189					buf.push_str(options.newline);190				}191192				let old_len = cur_padding.len();193				cur_padding.push_str(&options.padding);194				for (i, item) in items.iter().enumerate() {195					if i != 0 {196						buf.push(',');197						if mtype == JsonFormatting::ToString {198							buf.push(' ');199						} else if mtype != JsonFormatting::Minify {200							buf.push_str(options.newline);201						}202					}203					buf.push_str(cur_padding);204					manifest_json_ex_buf(&item?, buf, cur_padding, options)?;205				}206				cur_padding.truncate(old_len);207208				if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {209					buf.push_str(options.newline);210					buf.push_str(cur_padding);211				}212			} else if mtype == JsonFormatting::Std {213				buf.push_str(options.newline);214				buf.push_str(options.newline);215				buf.push_str(cur_padding);216			} else if mtype == JsonFormatting::ToString || mtype == JsonFormatting::Manifest {217				buf.push(' ');218			}219			buf.push(']');220		}221		Val::Obj(obj) => {222			obj.run_assertions()?;223			buf.push('{');224			let fields = obj.fields(225				#[cfg(feature = "exp-preserve-order")]226				options.preserve_order,227			);228			if !fields.is_empty() {229				if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {230					buf.push_str(options.newline);231				}232233				let old_len = cur_padding.len();234				cur_padding.push_str(&options.padding);235				for (i, field) in fields.into_iter().enumerate() {236					if i != 0 {237						buf.push(',');238						if mtype == JsonFormatting::ToString {239							buf.push(' ');240						} else if mtype != JsonFormatting::Minify {241							buf.push_str(options.newline);242						}243					}244					buf.push_str(cur_padding);245					escape_string_json_buf(&field, buf);246					buf.push_str(options.key_val_sep);247					State::push_description(248						|| format!("field <{}> manifestification", field.clone()),249						|| {250							let value = obj.get(field.clone())?.unwrap();251							manifest_json_ex_buf(&value, buf, cur_padding, options)?;252							Ok(())253						},254					)?;255				}256				cur_padding.truncate(old_len);257258				if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {259					buf.push_str(options.newline);260					buf.push_str(cur_padding);261				}262			} else if mtype == JsonFormatting::Std {263				buf.push_str(options.newline);264				buf.push_str(options.newline);265				buf.push_str(cur_padding);266			} else if mtype == JsonFormatting::ToString || mtype == JsonFormatting::Manifest {267				buf.push(' ');268			}269			buf.push('}');270		}271		Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),272	};273	Ok(())274}275276impl ManifestFormat for JsonFormat<'_> {277	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {278		manifest_json_ex_buf(&val, buf, &mut String::new(), self)279	}280}281282pub struct ToStringFormat;283impl ManifestFormat for ToStringFormat {284	fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {285		JsonFormat::std_to_string().manifest_buf(val, out)286	}287	fn file_trailing_newline(&self) -> bool {288		false289	}290}291pub struct StringFormat;292impl ManifestFormat for StringFormat {293	fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {294		let Val::Str(s) = val else {295			throw!(296				"output should be string for string manifest format, got {}",297				val.value_type()298			)299		};300		write!(out, "{s}").unwrap();301		Ok(())302	}303	fn file_trailing_newline(&self) -> bool {304		false305	}306}307308pub struct YamlStreamFormat<I>(pub I);309impl<I: ManifestFormat> ManifestFormat for YamlStreamFormat<I> {310	fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {311		let Val::Arr(arr) = val else {312			throw!(313				"output should be array for yaml stream format, got {}",314				val.value_type()315			)316		};317		if !arr.is_empty() {318			for v in arr.iter() {319				let v = v?;320				out.push_str("---\n");321				self.0.manifest_buf(v, out)?;322				out.push('\n');323			}324			out.push_str("...");325		}326		Ok(())327	}328}329330pub fn escape_string_json(s: &str) -> String {331	let mut buf = String::new();332	escape_string_json_buf(s, &mut buf);333	buf334}335336// Json string encoding was borrowed from https://github.com/serde-rs/json337338const BB: u8 = b'b'; // \x08339const TT: u8 = b't'; // \x09340const NN: u8 = b'n'; // \x0A341const FF: u8 = b'f'; // \x0C342const RR: u8 = b'r'; // \x0D343const QU: u8 = b'"'; // \x22344const BS: u8 = b'\\'; // \x5C345const UU: u8 = b'u'; // \x00...\x1F except the ones above346const __: u8 = 0;347348// Lookup table of escape sequences. A value of b'x' at index i means that byte349// i is escaped as "\x" in JSON. A value of 0 means that byte i is not escaped.350static ESCAPE: [u8; 256] = [351	//   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F352	UU, UU, UU, UU, UU, UU, UU, UU, BB, TT, NN, UU, FF, RR, UU, UU, // 0353	UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, // 1354	__, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2355	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 3356	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 4357	__, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, // 5358	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 6359	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7360	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8361	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9362	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A363	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B364	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C365	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D366	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E367	__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F368];369370pub fn escape_string_json_buf(value: &str, buf: &mut String) {371	// Safety: we only write correct utf-8 in this function372	let buf: &mut Vec<u8> = unsafe { &mut *(buf as *mut String).cast::<Vec<u8>>() };373	let bytes = value.as_bytes();374375	// Perfect for ascii strings, removes any reallocations376	buf.reserve(value.len() + 2);377378	buf.push(b'"');379380	let mut start = 0;381382	for (i, &byte) in bytes.iter().enumerate() {383		let escape = ESCAPE[byte as usize];384		if escape == __ {385			continue;386		}387388		if start < i {389			buf.extend_from_slice(&bytes[start..i]);390		}391		start = i + 1;392393		match escape {394			self::BB | self::TT | self::NN | self::FF | self::RR | self::QU | self::BS => {395				buf.extend_from_slice(&[b'\\', escape]);396			}397			self::UU => {398				static HEX_DIGITS: [u8; 16] = *b"0123456789abcdef";399				let bytes = &[400					b'\\',401					b'u',402					b'0',403					b'0',404					HEX_DIGITS[(byte >> 4) as usize],405					HEX_DIGITS[(byte & 0xF) as usize],406				];407				buf.extend_from_slice(bytes);408			}409			_ => unreachable!(),410		}411	}412413	if start == bytes.len() {414		buf.push(b'"');415		return;416	}417418	buf.extend_from_slice(&bytes[start..]);419	buf.push(b'"');420}