git.delta.rocks / jrsonnet / refs/commits / f51b87e6aff4

difftreelog

perf implement std.findSubstr in native

Yaroslav Bolyukin2022-08-07parent: #0831da3.patch.diff
in: master

4 files changed

modifiedCargo.tomldiffbeforeafterboth
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,6 +8,6 @@
 opt-level = 3
 lto = "fat"
 codegen-units = 1
-# debug = 0
+debug = 0
 panic = "abort"
-# strip = true
+strip = true
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -206,9 +206,13 @@
 	pub fn new_eager() -> Self {
 		Self::Eager(Cc::new(Vec::new()))
 	}
+	pub fn empty() -> Self {
+		Self::new_range(0, 0)
+	}
 
 	/// # Panics
 	/// If a > b
+	#[inline]
 	pub fn new_range(a: i32, b: i32) -> Self {
 		assert!(a <= b);
 		Self::Range(a, b)
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/lib.rs
1use std::{2	borrow::Cow,3	cell::{Ref, RefCell, RefMut},4	collections::HashMap,5	rc::Rc,6};78use jrsonnet_evaluator::{9	error::{Error::*, Result},10	function::{builtin::Builtin, ArgLike, CallLocation, FuncVal, TlaArg},11	gc::TraceBox,12	tb,13	typed::{Any, Either, Either2, Either4, VecVal, M1},14	val::ArrValue,15	Context, ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,16};17use jrsonnet_gcmodule::Cc;18use jrsonnet_macros::builtin;19use jrsonnet_parser::Source;2021mod expr;22mod types;23pub use types::*;24mod arrays;25pub use arrays::*;26mod math;27pub use math::*;28mod operator;29pub use operator::*;30mod sort;31pub use sort::*;32mod hash;33pub use hash::*;34mod encoding;35pub use encoding::*;36mod objects;37pub use objects::*;38mod manifest;39pub use manifest::*;40mod parse;41pub use parse::*;4243pub fn stdlib_uncached(s: State, settings: Rc<RefCell<Settings>>) -> ObjValue {44	let mut builder = ObjValueBuilder::new();4546	let expr = expr::stdlib_expr();47	let eval = jrsonnet_evaluator::evaluate(s.clone(), Context::default(), &expr)48		.expect("stdlib.jsonnet should have no errors")49		.as_obj()50		.expect("stdlib.jsonnet should evaluate to object");5152	builder.with_super(eval);5354	for (name, builtin) in [55		("length".into(), builtin_length::INST),56		// Types57		("type".into(), builtin_type::INST),58		("isString".into(), builtin_is_string::INST),59		("isNumber".into(), builtin_is_number::INST),60		("isBoolean".into(), builtin_is_boolean::INST),61		("isObject".into(), builtin_is_object::INST),62		("isArray".into(), builtin_is_array::INST),63		("isFunction".into(), builtin_is_function::INST),64		// Arrays65		("makeArray".into(), builtin_make_array::INST),66		("slice".into(), builtin_slice::INST),67		("map".into(), builtin_map::INST),68		("flatMap".into(), builtin_flatmap::INST),69		("filter".into(), builtin_filter::INST),70		("foldl".into(), builtin_foldl::INST),71		("foldr".into(), builtin_foldr::INST),72		("range".into(), builtin_range::INST),73		("join".into(), builtin_join::INST),74		("reverse".into(), builtin_reverse::INST),75		("any".into(), builtin_any::INST),76		("all".into(), builtin_all::INST),77		("member".into(), builtin_member::INST),78		("count".into(), builtin_count::INST),79		// Math80		("modulo".into(), builtin_modulo::INST),81		("floor".into(), builtin_floor::INST),82		("ceil".into(), builtin_ceil::INST),83		("log".into(), builtin_log::INST),84		("pow".into(), builtin_pow::INST),85		("sqrt".into(), builtin_sqrt::INST),86		("sin".into(), builtin_sin::INST),87		("cos".into(), builtin_cos::INST),88		("tan".into(), builtin_tan::INST),89		("asin".into(), builtin_asin::INST),90		("acos".into(), builtin_acos::INST),91		("atan".into(), builtin_atan::INST),92		("exp".into(), builtin_exp::INST),93		("mantissa".into(), builtin_mantissa::INST),94		("exponent".into(), builtin_exponent::INST),95		// Operator96		("mod".into(), builtin_mod::INST),97		("primitiveEquals".into(), builtin_primitive_equals::INST),98		("equals".into(), builtin_equals::INST),99		("format".into(), builtin_format::INST),100		// Sort101		("sort".into(), builtin_sort::INST),102		// Hash103		("md5".into(), builtin_md5::INST),104		// Encoding105		("encodeUTF8".into(), builtin_encode_utf8::INST),106		("decodeUTF8".into(), builtin_decode_utf8::INST),107		("base64".into(), builtin_base64::INST),108		("base64Decode".into(), builtin_base64_decode::INST),109		(110			"base64DecodeBytes".into(),111			builtin_base64_decode_bytes::INST,112		),113		// Objects114		("objectFieldsEx".into(), builtin_object_fields_ex::INST),115		("objectHasEx".into(), builtin_object_has_ex::INST),116		// Manifest117		("escapeStringJson".into(), builtin_escape_string_json::INST),118		("manifestJsonEx".into(), builtin_manifest_json_ex::INST),119		("manifestYamlDoc".into(), builtin_manifest_yaml_doc::INST),120		// Parsing121		("parseJson".into(), builtin_parse_json::INST),122		("parseYaml".into(), builtin_parse_yaml::INST),123		// Misc124		("codepoint".into(), builtin_codepoint::INST),125		("substr".into(), builtin_substr::INST),126		("char".into(), builtin_char::INST),127		("strReplace".into(), builtin_str_replace::INST),128		("splitLimit".into(), builtin_splitlimit::INST),129		("asciiUpper".into(), builtin_ascii_upper::INST),130		("asciiLower".into(), builtin_ascii_lower::INST),131	]132	.iter()133	.cloned()134	{135		builder136			.member(name)137			.hide()138			.value(s.clone(), Val::Func(FuncVal::StaticBuiltin(builtin)))139			.expect("no conflict");140	}141142	builder143		.member("extVar".into())144		.hide()145		.value(146			s.clone(),147			Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_ext_var {148				settings: settings.clone()149			})))),150		)151		.expect("no conflict");152	builder153		.member("native".into())154		.hide()155		.value(156			s.clone(),157			Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_native {158				settings: settings.clone()159			})))),160		)161		.expect("no conflict");162	builder163		.member("trace".into())164		.hide()165		.value(166			s.clone(),167			Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_trace { settings })))),168		)169		.expect("no conflict");170171	builder172		.member("id".into())173		.hide()174		.value(s, Val::Func(FuncVal::Id))175		.expect("no conflict");176177	builder.build()178}179180pub trait TracePrinter {181	fn print_trace(&self, s: State, loc: CallLocation, value: IStr);182}183184pub struct StdTracePrinter;185impl TracePrinter for StdTracePrinter {186	fn print_trace(&self, _s: State, loc: CallLocation, value: IStr) {187		eprint!("TRACE:");188		if let Some(loc) = loc.0 {189			let locs = loc.0.map_source_locations(&[loc.1]);190			eprint!(" {}:{}", loc.0.short_display(), locs[0].line);191		}192		eprintln!(" {}", value);193	}194}195196pub struct Settings {197	/// Used for `std.extVar`198	pub ext_vars: HashMap<IStr, TlaArg>,199	/// Used for `std.native`200	pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,201	/// Used for `std.trace`202	pub trace_printer: Box<dyn TracePrinter>,203}204205impl Default for Settings {206	fn default() -> Self {207		Self {208			ext_vars: Default::default(),209			ext_natives: Default::default(),210			trace_printer: Box::new(StdTracePrinter),211		}212	}213}214215pub fn extvar_source(name: &str, code: impl Into<IStr>) -> Source {216	let source_name = format!("<extvar:{}>", name);217	Source::new_virtual(Cow::Owned(source_name), code.into())218}219220pub struct ContextInitializer {221	// When we don't need to support legacy-this-file, we can reuse same context for all files222	#[cfg(not(feature = "legacy-this-file"))]223	context: Context,224	// Otherwise, we can only keep first stdlib layer, and then stack thisFile on top of it225	#[cfg(feature = "legacy-this-file")]226	stdlib_obj: ObjValue,227	settings: Rc<RefCell<Settings>>,228}229impl ContextInitializer {230	pub fn new(s: State) -> Self {231		let settings = Rc::new(RefCell::new(Settings::default()));232		Self {233			#[cfg(not(feature = "legacy-this-file"))]234			context: {235				let mut context = ContextBuilder::with_capacity(1);236				context.bind(237					"std".into(),238					Thunk::evaluated(Val::Obj(stdlib_uncached(s, settings.clone()))),239				);240				context.build()241			},242			#[cfg(feature = "legacy-this-file")]243			stdlib_obj: stdlib_uncached(s, settings.clone()),244			settings,245		}246	}247	pub fn settings(&self) -> Ref<Settings> {248		self.settings.borrow()249	}250	pub fn settings_mut(&self) -> RefMut<Settings> {251		self.settings.borrow_mut()252	}253	pub fn add_ext_var(&self, name: IStr, value: Val) {254		self.settings_mut()255			.ext_vars256			.insert(name, TlaArg::Val(value));257	}258	pub fn add_ext_str(&self, name: IStr, value: IStr) {259		self.settings_mut()260			.ext_vars261			.insert(name, TlaArg::String(value));262	}263	pub fn add_ext_code(&self, name: &str, code: impl Into<IStr>) -> Result<()> {264		let code = code.into();265		let source = extvar_source(name, code.clone());266		let parsed = jrsonnet_parser::parse(267			&code,268			&jrsonnet_parser::ParserSettings {269				file_name: source.clone(),270			},271		)272		.map_err(|e| ImportSyntaxError {273			path: source,274			error: Box::new(e),275		})?;276		// self.data_mut().volatile_files.insert(source_name, code);277		self.settings_mut()278			.ext_vars279			.insert(name.into(), TlaArg::Code(parsed));280		Ok(())281	}282	pub fn add_native(&self, name: IStr, cb: Cc<TraceBox<dyn Builtin>>) {283		self.settings_mut().ext_natives.insert(name, cb);284	}285}286impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {287	#[cfg(not(feature = "legacy-this-file"))]288	fn initialize(&self, _s: State, _source: Source) -> jrsonnet_evaluator::Context {289		self.context.clone()290	}291	#[cfg(feature = "legacy-this-file")]292	fn initialize(&self, s: State, source: Source) -> jrsonnet_evaluator::Context {293		let mut builder = ObjValueBuilder::new();294		builder.with_super(self.stdlib_obj.clone());295		builder296			.member("thisFile".into())297			.hide()298			.value(299				s,300				Val::Str(301					source302						.path()303						.map(|p| p.display().to_string())304						.unwrap_or_else(String::new)305						.into(),306				),307			)308			.expect("this object builder is empty");309		let stdlib_with_this_file = builder.build();310311		let mut context = ContextBuilder::with_capacity(1);312		context.bind(313			"std".into(),314			Thunk::evaluated(Val::Obj(stdlib_with_this_file)),315		);316		context.build()317	}318	unsafe fn as_any(&self) -> &dyn std::any::Any {319		self320	}321}322323#[builtin]324fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> Result<usize> {325	use Either4::*;326	Ok(match x {327		A(x) => x.chars().count(),328		B(x) => x.len(),329		C(x) => x.len(),330		D(f) => f.params_len(),331	})332}333334#[builtin]335const fn builtin_codepoint(str: char) -> Result<u32> {336	Ok(str as u32)337}338339#[builtin]340fn builtin_substr(str: IStr, from: usize, len: usize) -> Result<String> {341	Ok(str.chars().skip(from as usize).take(len as usize).collect())342}343344#[builtin(fields(345	settings: Rc<RefCell<Settings>>,346))]347fn builtin_ext_var(this: &builtin_ext_var, s: State, x: IStr) -> Result<Any> {348	let ctx = s.create_default_context(extvar_source(&x, ""));349	Ok(Any(this350		.settings351		.borrow()352		.ext_vars353		.get(&x)354		.cloned()355		.ok_or(UndefinedExternalVariable(x))?356		.evaluate_arg(s.clone(), ctx, true)?357		.evaluate(s)?))358}359360#[builtin(fields(361	settings: Rc<RefCell<Settings>>,362))]363fn builtin_native(this: &builtin_native, name: IStr) -> Result<Any> {364	Ok(Any(this365		.settings366		.borrow()367		.ext_natives368		.get(&name)369		.cloned()370		.map_or(Val::Null, |v| {371			Val::Func(FuncVal::Builtin(v.clone()))372		})))373}374375#[builtin]376fn builtin_char(n: u32) -> Result<char> {377	Ok(std::char::from_u32(n as u32).ok_or(InvalidUnicodeCodepointGot(n as u32))?)378}379380#[builtin(fields(381	settings: Rc<RefCell<Settings>>,382))]383fn builtin_trace(384	this: &builtin_trace,385	s: State,386	loc: CallLocation,387	str: IStr,388	rest: Thunk<Val>,389) -> Result<Any> {390	this.settings391		.borrow()392		.trace_printer393		.print_trace(s.clone(), loc, str);394	Ok(Any(rest.evaluate(s)?))395}396397#[builtin]398fn builtin_str_replace(str: String, from: IStr, to: IStr) -> Result<String> {399	Ok(str.replace(&from as &str, &to as &str))400}401402#[builtin]403fn builtin_splitlimit(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> Result<VecVal> {404	use Either2::*;405	Ok(VecVal(Cc::new(match maxsplits {406		A(n) => str407			.splitn(n + 1, &c as &str)408			.map(|s| Val::Str(s.into()))409			.collect(),410		B(_) => str.split(&c as &str).map(|s| Val::Str(s.into())).collect(),411	})))412}413414#[builtin]415fn builtin_ascii_upper(str: IStr) -> Result<String> {416	Ok(str.to_ascii_uppercase())417}418419#[builtin]420fn builtin_ascii_lower(str: IStr) -> Result<String> {421	Ok(str.to_ascii_lowercase())422}
after · crates/jrsonnet-stdlib/src/lib.rs
1use std::{2	borrow::Cow,3	cell::{Ref, RefCell, RefMut},4	collections::HashMap,5	rc::Rc,6};78use jrsonnet_evaluator::{9	error::{Error::*, Result},10	function::{builtin::Builtin, ArgLike, CallLocation, FuncVal, TlaArg},11	gc::TraceBox,12	tb,13	typed::{Any, Either, Either2, Either4, VecVal, M1},14	val::ArrValue,15	Context, ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,16};17use jrsonnet_gcmodule::Cc;18use jrsonnet_macros::builtin;19use jrsonnet_parser::Source;2021mod expr;22mod types;23pub use types::*;24mod arrays;25pub use arrays::*;26mod math;27pub use math::*;28mod operator;29pub use operator::*;30mod sort;31pub use sort::*;32mod hash;33pub use hash::*;34mod encoding;35pub use encoding::*;36mod objects;37pub use objects::*;38mod manifest;39pub use manifest::*;40mod parse;41pub use parse::*;4243pub fn stdlib_uncached(s: State, settings: Rc<RefCell<Settings>>) -> ObjValue {44	let mut builder = ObjValueBuilder::new();4546	let expr = expr::stdlib_expr();47	let eval = jrsonnet_evaluator::evaluate(s.clone(), Context::default(), &expr)48		.expect("stdlib.jsonnet should have no errors")49		.as_obj()50		.expect("stdlib.jsonnet should evaluate to object");5152	builder.with_super(eval);5354	for (name, builtin) in [55		("length".into(), builtin_length::INST),56		// Types57		("type".into(), builtin_type::INST),58		("isString".into(), builtin_is_string::INST),59		("isNumber".into(), builtin_is_number::INST),60		("isBoolean".into(), builtin_is_boolean::INST),61		("isObject".into(), builtin_is_object::INST),62		("isArray".into(), builtin_is_array::INST),63		("isFunction".into(), builtin_is_function::INST),64		// Arrays65		("makeArray".into(), builtin_make_array::INST),66		("slice".into(), builtin_slice::INST),67		("map".into(), builtin_map::INST),68		("flatMap".into(), builtin_flatmap::INST),69		("filter".into(), builtin_filter::INST),70		("foldl".into(), builtin_foldl::INST),71		("foldr".into(), builtin_foldr::INST),72		("range".into(), builtin_range::INST),73		("join".into(), builtin_join::INST),74		("reverse".into(), builtin_reverse::INST),75		("any".into(), builtin_any::INST),76		("all".into(), builtin_all::INST),77		("member".into(), builtin_member::INST),78		("count".into(), builtin_count::INST),79		// Math80		("modulo".into(), builtin_modulo::INST),81		("floor".into(), builtin_floor::INST),82		("ceil".into(), builtin_ceil::INST),83		("log".into(), builtin_log::INST),84		("pow".into(), builtin_pow::INST),85		("sqrt".into(), builtin_sqrt::INST),86		("sin".into(), builtin_sin::INST),87		("cos".into(), builtin_cos::INST),88		("tan".into(), builtin_tan::INST),89		("asin".into(), builtin_asin::INST),90		("acos".into(), builtin_acos::INST),91		("atan".into(), builtin_atan::INST),92		("exp".into(), builtin_exp::INST),93		("mantissa".into(), builtin_mantissa::INST),94		("exponent".into(), builtin_exponent::INST),95		// Operator96		("mod".into(), builtin_mod::INST),97		("primitiveEquals".into(), builtin_primitive_equals::INST),98		("equals".into(), builtin_equals::INST),99		("format".into(), builtin_format::INST),100		// Sort101		("sort".into(), builtin_sort::INST),102		// Hash103		("md5".into(), builtin_md5::INST),104		// Encoding105		("encodeUTF8".into(), builtin_encode_utf8::INST),106		("decodeUTF8".into(), builtin_decode_utf8::INST),107		("base64".into(), builtin_base64::INST),108		("base64Decode".into(), builtin_base64_decode::INST),109		(110			"base64DecodeBytes".into(),111			builtin_base64_decode_bytes::INST,112		),113		// Objects114		("objectFieldsEx".into(), builtin_object_fields_ex::INST),115		("objectHasEx".into(), builtin_object_has_ex::INST),116		// Manifest117		("escapeStringJson".into(), builtin_escape_string_json::INST),118		("manifestJsonEx".into(), builtin_manifest_json_ex::INST),119		("manifestYamlDoc".into(), builtin_manifest_yaml_doc::INST),120		// Parsing121		("parseJson".into(), builtin_parse_json::INST),122		("parseYaml".into(), builtin_parse_yaml::INST),123		// Misc124		("codepoint".into(), builtin_codepoint::INST),125		("substr".into(), builtin_substr::INST),126		("char".into(), builtin_char::INST),127		("strReplace".into(), builtin_str_replace::INST),128		("splitLimit".into(), builtin_splitlimit::INST),129		("asciiUpper".into(), builtin_ascii_upper::INST),130		("asciiLower".into(), builtin_ascii_lower::INST),131		("findSubstr".into(), builtin_find_substr::INST),132	]133	.iter()134	.cloned()135	{136		builder137			.member(name)138			.hide()139			.value(s.clone(), Val::Func(FuncVal::StaticBuiltin(builtin)))140			.expect("no conflict");141	}142143	builder144		.member("extVar".into())145		.hide()146		.value(147			s.clone(),148			Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_ext_var {149				settings: settings.clone()150			})))),151		)152		.expect("no conflict");153	builder154		.member("native".into())155		.hide()156		.value(157			s.clone(),158			Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_native {159				settings: settings.clone()160			})))),161		)162		.expect("no conflict");163	builder164		.member("trace".into())165		.hide()166		.value(167			s.clone(),168			Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_trace { settings })))),169		)170		.expect("no conflict");171172	builder173		.member("id".into())174		.hide()175		.value(s, Val::Func(FuncVal::Id))176		.expect("no conflict");177178	builder.build()179}180181pub trait TracePrinter {182	fn print_trace(&self, s: State, loc: CallLocation, value: IStr);183}184185pub struct StdTracePrinter;186impl TracePrinter for StdTracePrinter {187	fn print_trace(&self, _s: State, loc: CallLocation, value: IStr) {188		eprint!("TRACE:");189		if let Some(loc) = loc.0 {190			let locs = loc.0.map_source_locations(&[loc.1]);191			eprint!(" {}:{}", loc.0.short_display(), locs[0].line);192		}193		eprintln!(" {}", value);194	}195}196197pub struct Settings {198	/// Used for `std.extVar`199	pub ext_vars: HashMap<IStr, TlaArg>,200	/// Used for `std.native`201	pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,202	/// Used for `std.trace`203	pub trace_printer: Box<dyn TracePrinter>,204}205206impl Default for Settings {207	fn default() -> Self {208		Self {209			ext_vars: Default::default(),210			ext_natives: Default::default(),211			trace_printer: Box::new(StdTracePrinter),212		}213	}214}215216pub fn extvar_source(name: &str, code: impl Into<IStr>) -> Source {217	let source_name = format!("<extvar:{}>", name);218	Source::new_virtual(Cow::Owned(source_name), code.into())219}220221pub struct ContextInitializer {222	// When we don't need to support legacy-this-file, we can reuse same context for all files223	#[cfg(not(feature = "legacy-this-file"))]224	context: Context,225	// Otherwise, we can only keep first stdlib layer, and then stack thisFile on top of it226	#[cfg(feature = "legacy-this-file")]227	stdlib_obj: ObjValue,228	settings: Rc<RefCell<Settings>>,229}230impl ContextInitializer {231	pub fn new(s: State) -> Self {232		let settings = Rc::new(RefCell::new(Settings::default()));233		Self {234			#[cfg(not(feature = "legacy-this-file"))]235			context: {236				let mut context = ContextBuilder::with_capacity(1);237				context.bind(238					"std".into(),239					Thunk::evaluated(Val::Obj(stdlib_uncached(s, settings.clone()))),240				);241				context.build()242			},243			#[cfg(feature = "legacy-this-file")]244			stdlib_obj: stdlib_uncached(s, settings.clone()),245			settings,246		}247	}248	pub fn settings(&self) -> Ref<Settings> {249		self.settings.borrow()250	}251	pub fn settings_mut(&self) -> RefMut<Settings> {252		self.settings.borrow_mut()253	}254	pub fn add_ext_var(&self, name: IStr, value: Val) {255		self.settings_mut()256			.ext_vars257			.insert(name, TlaArg::Val(value));258	}259	pub fn add_ext_str(&self, name: IStr, value: IStr) {260		self.settings_mut()261			.ext_vars262			.insert(name, TlaArg::String(value));263	}264	pub fn add_ext_code(&self, name: &str, code: impl Into<IStr>) -> Result<()> {265		let code = code.into();266		let source = extvar_source(name, code.clone());267		let parsed = jrsonnet_parser::parse(268			&code,269			&jrsonnet_parser::ParserSettings {270				file_name: source.clone(),271			},272		)273		.map_err(|e| ImportSyntaxError {274			path: source,275			error: Box::new(e),276		})?;277		// self.data_mut().volatile_files.insert(source_name, code);278		self.settings_mut()279			.ext_vars280			.insert(name.into(), TlaArg::Code(parsed));281		Ok(())282	}283	pub fn add_native(&self, name: IStr, cb: Cc<TraceBox<dyn Builtin>>) {284		self.settings_mut().ext_natives.insert(name, cb);285	}286}287impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {288	#[cfg(not(feature = "legacy-this-file"))]289	fn initialize(&self, _s: State, _source: Source) -> jrsonnet_evaluator::Context {290		self.context.clone()291	}292	#[cfg(feature = "legacy-this-file")]293	fn initialize(&self, s: State, source: Source) -> jrsonnet_evaluator::Context {294		let mut builder = ObjValueBuilder::new();295		builder.with_super(self.stdlib_obj.clone());296		builder297			.member("thisFile".into())298			.hide()299			.value(300				s,301				Val::Str(302					source303						.path()304						.map(|p| p.display().to_string())305						.unwrap_or_else(String::new)306						.into(),307				),308			)309			.expect("this object builder is empty");310		let stdlib_with_this_file = builder.build();311312		let mut context = ContextBuilder::with_capacity(1);313		context.bind(314			"std".into(),315			Thunk::evaluated(Val::Obj(stdlib_with_this_file)),316		);317		context.build()318	}319	unsafe fn as_any(&self) -> &dyn std::any::Any {320		self321	}322}323324#[builtin]325fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> Result<usize> {326	use Either4::*;327	Ok(match x {328		A(x) => x.chars().count(),329		B(x) => x.len(),330		C(x) => x.len(),331		D(f) => f.params_len(),332	})333}334335#[builtin]336const fn builtin_codepoint(str: char) -> Result<u32> {337	Ok(str as u32)338}339340#[builtin]341fn builtin_substr(str: IStr, from: usize, len: usize) -> Result<String> {342	Ok(str.chars().skip(from as usize).take(len as usize).collect())343}344345#[builtin(fields(346	settings: Rc<RefCell<Settings>>,347))]348fn builtin_ext_var(this: &builtin_ext_var, s: State, x: IStr) -> Result<Any> {349	let ctx = s.create_default_context(extvar_source(&x, ""));350	Ok(Any(this351		.settings352		.borrow()353		.ext_vars354		.get(&x)355		.cloned()356		.ok_or(UndefinedExternalVariable(x))?357		.evaluate_arg(s.clone(), ctx, true)?358		.evaluate(s)?))359}360361#[builtin(fields(362	settings: Rc<RefCell<Settings>>,363))]364fn builtin_native(this: &builtin_native, name: IStr) -> Result<Any> {365	Ok(Any(this366		.settings367		.borrow()368		.ext_natives369		.get(&name)370		.cloned()371		.map_or(Val::Null, |v| {372			Val::Func(FuncVal::Builtin(v.clone()))373		})))374}375376#[builtin]377fn builtin_char(n: u32) -> Result<char> {378	Ok(std::char::from_u32(n as u32).ok_or(InvalidUnicodeCodepointGot(n as u32))?)379}380381#[builtin(fields(382	settings: Rc<RefCell<Settings>>,383))]384fn builtin_trace(385	this: &builtin_trace,386	s: State,387	loc: CallLocation,388	str: IStr,389	rest: Thunk<Val>,390) -> Result<Any> {391	this.settings392		.borrow()393		.trace_printer394		.print_trace(s.clone(), loc, str);395	Ok(Any(rest.evaluate(s)?))396}397398#[builtin]399fn builtin_str_replace(str: String, from: IStr, to: IStr) -> Result<String> {400	Ok(str.replace(&from as &str, &to as &str))401}402403#[builtin]404fn builtin_splitlimit(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> Result<VecVal> {405	use Either2::*;406	Ok(VecVal(Cc::new(match maxsplits {407		A(n) => str408			.splitn(n + 1, &c as &str)409			.map(|s| Val::Str(s.into()))410			.collect(),411		B(_) => str.split(&c as &str).map(|s| Val::Str(s.into())).collect(),412	})))413}414415#[builtin]416fn builtin_ascii_upper(str: IStr) -> Result<String> {417	Ok(str.to_ascii_uppercase())418}419420#[builtin]421fn builtin_ascii_lower(str: IStr) -> Result<String> {422	Ok(str.to_ascii_lowercase())423}424425#[builtin]426fn builtin_find_substr(pat: IStr, str: IStr) -> Result<ArrValue> {427	if pat.is_empty() || str.is_empty() || pat.len() > str.len() {428		return Ok(ArrValue::empty());429	}430431	let str = str.as_str();432	let pat = pat.as_bytes();433	let strb = str.as_bytes();434435	let max_pos = str.len() - pat.len();436437	let mut out: Vec<Val> = Vec::new();438	for (ch_idx, (i, _)) in str439		.char_indices()440		.take_while(|(i, _)| i <= &max_pos)441		.enumerate()442	{443		if &strb[i..i + pat.len()] == pat {444			out.push(Val::Num(ch_idx as f64))445		}446	}447	Ok(out.into())448}
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -477,19 +477,6 @@
     } else
       a,
 
-  findSubstr(pat, str)::
-    if !std.isString(pat) then
-      error 'findSubstr first parameter should be a string, got ' + std.type(pat)
-    else if !std.isString(str) then
-      error 'findSubstr second parameter should be a string, got ' + std.type(str)
-    else
-      local pat_len = std.length(pat);
-      local str_len = std.length(str);
-      if pat_len == 0 || str_len == 0 || pat_len > str_len then
-        []
-      else
-        std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),
-
   find(value, arr)::
     if !std.isArray(arr) then
       error 'find second parameter should be an array, got ' + std.type(arr)