1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
use std::borrow::{Borrow, Cow};
use std::collections::HashMap;
use std::iter;

use once_cell::sync::Lazy;
use regex::bytes::{CaptureLocations, Regex};

use crate::CppNameStyle;

pub trait StringExt {
	fn replacen_in_place(&mut self, from: &str, limit: usize, to: &str) -> bool;
	fn replace_in_place(&mut self, from: &str, to: &str) -> bool;
	fn replacen_in_place_regex(&mut self, from: &Regex, limit: usize, to: &str) -> bool;
	fn replace_in_place_regex(&mut self, from: &Regex, to: &str) -> bool;
	fn replacen_in_place_regex_cb<'a>(
		&mut self,
		from: &Regex,
		limit: usize,
		replacer: impl FnMut(&str, &CaptureLocations) -> Option<Cow<'a, str>> + 'a,
	) -> bool;
	fn replace_in_place_regex_cb<'a>(
		&mut self,
		from: &Regex,
		replacer: impl FnMut(&str, &CaptureLocations) -> Option<Cow<'a, str>> + 'a,
	) -> bool;
	fn extend_join(&mut self, it: impl Iterator<Item = impl AsRef<str>>, sep: &str);
	fn extend_sep(&mut self, sep: &str, s: &str);
	fn push_indented_str(&mut self, indent: Indent, val: &str);
	fn bump_counter(&mut self);
	fn cleanup_name(&mut self);
}

impl StringExt for String {
	fn replacen_in_place(&mut self, from: &str, limit: usize, to: &str) -> bool {
		if from.is_empty() {
			return false;
		}
		let mut idx = 0;
		let mut count = 0;
		while let Some(start_idx) = self[idx..].find(from).map(|i| i + idx) {
			let end_idx = start_idx + from.len();
			self.replace_range(start_idx..end_idx, to);
			idx = start_idx + to.len();
			count += 1;
			if count == limit {
				break;
			}
		}
		count != 0
	}

	fn replace_in_place(&mut self, from: &str, to: &str) -> bool {
		self.replacen_in_place(from, 0, to)
	}

	fn replacen_in_place_regex(&mut self, from: &Regex, limit: usize, to: &str) -> bool {
		let mut idx = 0;
		if to.chars().any(|c| c == '$') {
			enum Elem<'a> {
				CaptureGroup(usize),
				Literal(&'a str),
			}

			#[inline(always)]
			fn compile_captures(rep: &str) -> Vec<Elem> {
				let mut out = Vec::with_capacity(10);
				let mut last_idx = 0;
				for (idx, _) in rep.match_indices('$') {
					if let Some((mut next_idx, next_char)) = rep[idx..].char_indices().nth(1) {
						next_idx += idx;
						if next_char == '$' {
							out.push(Elem::Literal(&rep[last_idx..next_idx]));
							last_idx = next_idx + 1;
							continue;
						}
						if let Some(mut num_end_idx) = rep[next_idx..]
							.char_indices()
							.take_while(|(_, c)| c.is_ascii_digit())
							.map(|(i, _)| i)
							.last()
						{
							num_end_idx += next_idx + 1;
							out.push(Elem::Literal(&rep[last_idx..idx]));
							out.push(Elem::CaptureGroup(
								rep[next_idx..num_end_idx].parse().expect("Can't parse as group number"),
							));
							last_idx = num_end_idx;
						}
					} else {
						break;
					}
				}
				out.push(Elem::Literal(&rep[last_idx..]));
				out
			}

			let rep = compile_captures(to);
			self.replacen_in_place_regex_cb(from, limit, |s, caps| {
				let cap_len = rep.iter().fold(0, |acc, x| {
					acc + match x {
						Elem::CaptureGroup(n) => {
							if let Some((start, end)) = caps.get(*n) {
								end - start
							} else {
								0
							}
						}
						Elem::Literal(s) => s.len(),
					}
				});
				let out = rep.iter().fold(String::with_capacity(cap_len), |out, x| {
					out + match x {
						Elem::CaptureGroup(n) => {
							if let Some((start, end)) = caps.get(*n) {
								&s[start..end]
							} else {
								""
							}
						}
						Elem::Literal(s) => s,
					}
				});
				Some(out.into())
			})
		} else {
			let mut count = 0;
			while let Some((start_idx, end_idx)) = from.find_at(self.as_bytes(), idx).map(|m| (m.start(), m.end())) {
				if start_idx == end_idx {
					return false;
				}
				self.replace_range(start_idx..end_idx, to);
				idx = start_idx + to.len();
				count += 1;
				if count == limit {
					break;
				}
			}
			count != 0
		}
	}

	fn replace_in_place_regex(&mut self, from: &Regex, to: &str) -> bool {
		self.replacen_in_place_regex(from, 0, to)
	}

	fn replacen_in_place_regex_cb<'a>(
		&mut self,
		from: &Regex,
		limit: usize,
		mut replacer: impl FnMut(&str, &CaptureLocations) -> Option<Cow<'a, str>> + 'a,
	) -> bool {
		let mut idx = 0;
		let mut caps = from.capture_locations();
		let mut count = 0;
		while let Some((start_idx, end_idx)) = from
			.captures_read_at(&mut caps, self.as_bytes(), idx)
			.map(|m| (m.start(), m.end()))
		{
			if start_idx == end_idx {
				return false;
			}
			if let Some(repl) = replacer(self, &caps) {
				self.replace_range(start_idx..end_idx, &repl);
				idx = start_idx + repl.len();
			} else {
				idx = end_idx;
			}
			count += 1;
			if count == limit {
				break;
			}
		}
		count != 0
	}

	fn replace_in_place_regex_cb<'a>(
		&mut self,
		from: &Regex,
		replacer: impl FnMut(&str, &CaptureLocations) -> Option<Cow<'a, str>> + 'a,
	) -> bool {
		self.replacen_in_place_regex_cb(from, 0, replacer)
	}

	fn extend_join(&mut self, it: impl IntoIterator<Item = impl AsRef<str>>, sep: &str) {
		let mut it = it.into_iter();
		let first = it.find(|e| !e.as_ref().is_empty());
		if let Some(first) = first {
			let first = first.as_ref();
			if !first.is_empty() {
				let needed_cap = it.size_hint().1.unwrap_or(8) * (first.len() + sep.len());
				if needed_cap > self.capacity() {
					self.reserve(needed_cap - self.capacity());
				}
				self.push_str(first);
				it.for_each(|part| {
					let part = part.as_ref();
					if !part.is_empty() {
						self.push_str(sep);
						self.push_str(part.as_ref());
					}
				})
			}
		}
	}

	fn extend_sep(&mut self, sep: &str, s: &str) {
		if !self.is_empty() {
			self.reserve(s.len() + sep.len());
			self.push_str(sep);
		}
		self.push_str(s);
	}

	fn push_indented_str(&mut self, indent: Indent, val: &str) {
		let mut lines = val.lines_with_nl();
		if let Some(line) = lines.next() {
			self.push_str(line);
		}
		for line in lines {
			self.extend(iter::repeat(indent.symbol).take(indent.len));
			self.push_str(line);
		}
	}

	fn bump_counter(&mut self) {
		let idx = self
			.rfind(|c: char| !c.is_ascii_digit())
			.map_or_else(|| self.len(), |idx| idx + 1);
		match self[idx..].parse::<u32>() {
			// parsing an empty string yields an error so that makes sure that [idx - 1] doesn't panic
			Ok(counter) if self.as_bytes()[idx - 1] == b'_' => self.replace_range(idx.., &(counter + 1).to_string()),
			_ => self.push_str("_1"),
		}
	}

	fn cleanup_name(&mut self) {
		// todo aho-corasick?
		self.replace_in_place(" ", "_");
		self.replace_in_place(">=", "GE");
		self.replace_in_place("<=", "LE");
		self.replace_in_place("<", "L");
		self.replace_in_place(">", "G");
		self.replace_in_place("(", "_");
		self.replace_in_place(")", "_");
		self.replace_in_place("*", "X");
		self.replace_in_place("&", "R");
		self.replace_in_place(",", "_");
		self.replace_in_place("[", "_");
		self.replace_in_place("]", "_");
		self.replace_in_place("::", "_");
		self.replace_in_place("+", "A");
		self.replace_in_place("-", "S");
		self.replace_in_place("/", "D");
		self.replace_in_place("==", "EQ");
		self.replace_in_place("!=", "NE");
		self.replace_in_place("|", "OR");
		self.replace_in_place("^", "XOR");
		self.replace_in_place("~", "NOTB");
		self.replace_in_place("=", "ST");
	}
}

pub struct LinesWithNl<'s> {
	string: &'s str,
	len: usize,
	idx: usize,
}

impl<'s> Iterator for LinesWithNl<'s> {
	type Item = &'s str;

	fn next(&mut self) -> Option<Self::Item> {
		if self.idx > self.len {
			None
		} else {
			let slice = &self.string[self.idx..];
			Some(if let Some(new_line_idx) = slice.find(|c| c == '\n') {
				self.idx += new_line_idx + 1;
				&slice[..=new_line_idx]
			} else {
				self.idx = self.len + 1;
				slice
			})
		}
	}
}

#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Indent {
	pub len: usize,
	pub symbol: char,
}

impl Default for Indent {
	fn default() -> Self {
		Self { len: 0, symbol: '\t' }
	}
}

#[derive(Clone, Copy, Debug)]
enum Compiled<'s> {
	IntpLineStart(&'s str),
	IntpLiteral(&'s str),
	IntpLineEnd(&'s str),
	LiteralLine(&'s str),
	Var(&'s str),
}

#[derive(Clone, Debug)]
pub struct CompiledInterpolation<'s> {
	elems: Vec<Compiled<'s>>,
}

impl CompiledInterpolation<'_> {
	pub fn interpolate(&self, params: &HashMap<&str, impl AsRef<str>>) -> String {
		#[inline(always)]
		fn remove_trailing_empty_line(out: &mut String) -> bool {
			let last_line_start = out.rfind('\n').map_or(0, |i| i + 1);
			if out[last_line_start..].chars().all(char::is_whitespace) {
				out.drain(last_line_start..);
				true
			} else {
				false
			}
		}

		const INVALID_PARAM_NAME: &str = "<parameter not found>";

		let result_len = self.elems.iter().fold(0, |len, elem| {
			len + match elem {
				Compiled::IntpLineStart(s) | Compiled::IntpLiteral(s) => s.len(),
				Compiled::IntpLineEnd(s) | Compiled::LiteralLine(s) => s.len() + 1,
				Compiled::Var(name) => params
					.get(name)
					.map_or_else(|| INVALID_PARAM_NAME.len(), |x| x.as_ref().len()),
			}
		});
		let mut out = String::with_capacity(result_len);
		let mut line_indent = Indent::default();
		// interpolate vars keeping indent
		for elem in &self.elems {
			match elem {
				Compiled::IntpLineStart(s) => {
					line_indent = s.detect_indent();
					out += s;
				}
				Compiled::IntpLiteral(s) => out += s,
				Compiled::Var(name) => {
					out.push_indented_str(line_indent, params.get(name).map_or(INVALID_PARAM_NAME, |x| x.as_ref()))
				}
				Compiled::IntpLineEnd(s) => {
					out += s;
					if !remove_trailing_empty_line(&mut out) {
						out.push('\n');
					}
				}
				Compiled::LiteralLine(s) => {
					line_indent = s.detect_indent();
					out += s;
					out.push('\n');
				}
			}
		}
		if let Some('\n') = out.chars().next_back() {
			out.pop();
		}
		out
	}
}

pub trait StrExt {
	fn cpp_name_to_rust_fn_case(&self) -> Cow<str>;
	fn lines_with_nl(&self) -> LinesWithNl;
	fn detect_indent(&self) -> Indent;
	fn compile_interpolation(&self) -> CompiledInterpolation;
	fn trim_start_idx(&self) -> usize;
	fn trim_end_idx(&self) -> usize;
	/// For `cv::rapid::Rapid` returns `Rapid`
	fn localname(&self) -> &str;
	/// For `cv::rapid::Rapid` returns `cv::rapid`
	fn namespace(&self) -> &str;
	/// For `crate::rapid::Rapid` and `rapid::Rapid` returns `rapid`
	fn module(&self) -> &str;
	fn cpp_name_from_fullname(&self, style: CppNameStyle) -> &str;
}

impl StrExt for str {
	fn cpp_name_to_rust_fn_case(&self) -> Cow<str> {
		let mut out = String::with_capacity(self.len() + 8);
		#[derive(Copy, Clone)]
		enum State {
			StartOrLastUnderscore,
			LastLowercase,
			LastUppercase,
		}
		let mut state = State::StartOrLastUnderscore;
		let mut chars = self.as_bytes().iter().peekable();
		while let Some(&cur_c) = chars.next() {
			let (add_c, new_state) = match cur_c {
				_ if cur_c.is_ascii_uppercase() => {
					match state {
						State::StartOrLastUnderscore => {}
						State::LastLowercase => out.push('_'),
						State::LastUppercase => {
							// SVDValue => svd_value
							if chars.peek().map_or(false, |next_c| next_c.is_ascii_lowercase()) {
								out.push('_');
							}
						}
					}
					(cur_c.to_ascii_lowercase(), State::LastUppercase)
				}
				b'_' => (b'_', State::StartOrLastUnderscore),
				_ => (cur_c, State::LastLowercase),
			};
			out.push(char::from(add_c));
			state = new_state;
		}
		out.replacen_in_place("pn_p", 1, "pnp");
		out.replacen_in_place("p3_p", 1, "p3p");
		out.replacen_in_place("_u_mat", 1, "_umat");
		out.replacen_in_place("i_d3_d", 1, "id_3d_");
		out.replacen_in_place("d3_d", 1, "d3d");
		out.replacen_in_place("2_d", 1, "_2d");
		out.replacen_in_place("3_d", 1, "_3d");
		out.replacen_in_place("open_gl", 1, "opengl");
		out.replacen_in_place("open_cl", 1, "opencl");
		out.replacen_in_place("open_vx", 1, "openvx");
		out.replacen_in_place("aruco_3detect", 1, "aruco3_detect");
		out.into()
	}

	fn lines_with_nl(&self) -> LinesWithNl {
		LinesWithNl {
			string: self,
			len: self.len(),
			idx: 0,
		}
	}

	fn detect_indent(&self) -> Indent {
		self
			.char_indices()
			.take_while(|&(_, c)| c == ' ' || c == '\t')
			.last()
			.map_or_else(Indent::default, |(idx, chr)| Indent {
				len: idx + 1,
				symbol: chr,
			})
	}

	fn compile_interpolation(&self) -> CompiledInterpolation {
		static VARS: Lazy<Regex> = Lazy::new(|| Regex::new(r"\{\{\s*([^{}]+?)\s*}}").expect("Can't compile regex"));

		// trim leading newline
		let tpl = self.strip_prefix('\n').unwrap_or(self);

		// find minimum common indent
		let mut common_indent_len: Option<usize> = None;
		for line in tpl.lines_with_nl() {
			let Indent { len: new_indent, .. } = if let Some(len) = common_indent_len {
				line[..len.min(line.len())].detect_indent()
			} else {
				line.detect_indent()
			};
			// only take lines with something else than only whitespace into account
			if !line[new_indent..].trim_start().is_empty() {
				common_indent_len = Some(new_indent);
			}
		}

		let mut elems = vec![];
		// interpolate vars keeping indent
		if let Some(common_indent_len) = common_indent_len {
			for line in tpl.lines() {
				let line = &line[common_indent_len.min(line.len())..];
				let mut last_idx = 0;
				for cap in VARS.captures_iter(line.as_bytes()) {
					if let (Some(whole), Some(var)) = (cap.get(0), cap.get(1)) {
						if last_idx == 0 {
							elems.push(Compiled::IntpLineStart(&line[last_idx..whole.start()]));
						} else {
							elems.push(Compiled::IntpLiteral(&line[last_idx..whole.start()]));
						}
						last_idx = whole.end();
						elems.push(Compiled::Var(&line[var.start()..var.end()]));
					}
				}
				if last_idx == 0 {
					elems.push(Compiled::LiteralLine(&line[last_idx..]));
				} else {
					elems.push(Compiled::IntpLineEnd(&line[last_idx..]));
				}
			}
		} else {
			elems.push(Compiled::LiteralLine(""));
		}

		CompiledInterpolation { elems }
	}

	fn trim_start_idx(&self) -> usize {
		self
			.char_indices()
			.find(|(_, c)| !c.is_whitespace())
			.map_or_else(|| self.len(), |(i, _)| i)
	}

	fn trim_end_idx(&self) -> usize {
		self
			.char_indices()
			.rfind(|(_, c)| !c.is_whitespace())
			.map_or(0, |(i, _)| i + 1)
	}

	fn localname(&self) -> &str {
		self.rsplit("::").next().unwrap_or(self)
	}

	fn namespace(&self) -> &str {
		self.rsplit_once("::").map_or(self, |(left, _right)| left)
	}

	fn module(&self) -> &str {
		self
			.strip_prefix("crate::")
			.unwrap_or(self)
			.split("::")
			.next()
			.unwrap_or(self)
	}

	fn cpp_name_from_fullname(&self, style: CppNameStyle) -> &str {
		match style {
			CppNameStyle::Declaration => self.localname(),
			CppNameStyle::Reference => self,
		}
	}
}

pub trait CowMapBorrowedExt<'b, IN, OUT>
where
	IN: 'b + ToOwned + ?Sized,
	OUT: 'b + ToOwned + ?Sized,
{
	fn map_borrowed<F>(self, f: F) -> Cow<'b, OUT>
	where
		F: for<'f> FnOnce(&'f IN) -> Cow<'f, OUT>;
}

impl<'b, IN, OUT> CowMapBorrowedExt<'b, IN, OUT> for Cow<'b, IN>
where
	IN: 'b + ToOwned + ?Sized,
	OUT: 'b + ToOwned + ?Sized,
{
	#[inline(always)]
	fn map_borrowed<F>(self, f: F) -> Cow<'b, OUT>
	where
		F: for<'f> FnOnce(&'f IN) -> Cow<'f, OUT>,
	{
		match self {
			Cow::Borrowed(v) => f(v),
			Cow::Owned(v) => Cow::Owned(f(v.borrow()).into_owned()),
		}
	}
}