ab_riscv_macros_common/
code_utils.rs1const FROM_TRAIT_CONST_1: &str = "const trait ";
5const TO_TRAIT_CONST_1: &str = "trait _const";
6const FROM_TRAIT_CONST_2: &str = "const unsafe trait ";
7const TO_TRAIT_CONST_2: &str = "unsafe trait _const";
8const FROM_IMPL_CONST_1: &str = "> const ";
9const TO_IMPL_CONST_1: &str = "> cnst::";
10const FROM_IMPL_CONST_2: &str = "> const ";
11const TO_IMPL_CONST_2: &str = "> cnst ::";
12const FROM_IMPL_CONST_REVERT_1: &str = "pub cnst::";
13const TO_IMPL_CONST_REVERT_1: &str = "pub const ";
14const FROM_IMPL_CONST_REVERT_2: &str = "pub cnst ::";
15const TO_IMPL_CONST_REVERT_2: &str = "pub const ";
16const FROM_IMPL_CONST_REVERT_3: &str = ") cnst::";
17const TO_IMPL_CONST_REVERT_3: &str = ") const ";
18const FROM_IMPL_CONST_REVERT_4: &str = ") cnst ::";
19const TO_IMPL_CONST_REVERT_4: &str = ") const ";
20const FROM_IMPL_CONST_REVERT_5: &str = "cnst::fn";
21const TO_IMPL_CONST_REVERT_5: &str = "const fn";
22
23const FROM_BRACKETS_CONST_1: &str = "[const] ";
24const TO_BRACKETS_CONST_1: &str = "BRCONST+";
25const FROM_BRACKETS_CONST_2: &str = " [const] ";
26const TO_BRACKETS_CONST_2: &str = "BRCONST +";
27
28pub fn pre_process_rust_code(s: &mut str) {
31 replace_inplace(s, FROM_TRAIT_CONST_1, TO_TRAIT_CONST_1);
32 replace_inplace(s, FROM_TRAIT_CONST_2, TO_TRAIT_CONST_2);
33 replace_inplace(s, FROM_IMPL_CONST_1, TO_IMPL_CONST_1);
34 replace_inplace(s, FROM_IMPL_CONST_2, TO_IMPL_CONST_2);
35 replace_inplace(s, FROM_IMPL_CONST_REVERT_1, TO_IMPL_CONST_REVERT_1);
36 replace_inplace(s, FROM_IMPL_CONST_REVERT_2, TO_IMPL_CONST_REVERT_2);
37 replace_inplace(s, FROM_IMPL_CONST_REVERT_3, TO_IMPL_CONST_REVERT_3);
38 replace_inplace(s, FROM_IMPL_CONST_REVERT_4, TO_IMPL_CONST_REVERT_4);
39 replace_inplace(s, FROM_IMPL_CONST_REVERT_5, TO_IMPL_CONST_REVERT_5);
40 replace_inplace(s, FROM_BRACKETS_CONST_1, TO_BRACKETS_CONST_1);
41 replace_inplace(s, FROM_BRACKETS_CONST_2, TO_BRACKETS_CONST_2);
42}
43
44pub fn post_process_rust_code(s: &mut str) {
46 replace_inplace(s, TO_TRAIT_CONST_2, FROM_TRAIT_CONST_2);
47 replace_inplace(s, TO_TRAIT_CONST_1, FROM_TRAIT_CONST_1);
48 replace_inplace(s, TO_IMPL_CONST_1, FROM_IMPL_CONST_1);
49 replace_inplace(s, TO_IMPL_CONST_2, FROM_IMPL_CONST_2);
50 replace_inplace(s, TO_BRACKETS_CONST_1, FROM_BRACKETS_CONST_1);
51 replace_inplace(s, TO_BRACKETS_CONST_2, FROM_BRACKETS_CONST_2);
52}
53
54fn replace_inplace(mut s: &mut str, from: &str, to: &str) {
55 assert_eq!(from.len(), to.len(), "`{from}` != `{to}`");
56
57 if from.is_empty() {
58 return;
59 }
60
61 while let Some(found) = s.find(from) {
62 let start = found;
63 let end = found + from.len();
64
65 unsafe { s.as_bytes_mut() }
67 .get_mut(start..end)
68 .expect("Just found a string of the desired length; qed")
69 .copy_from_slice(to.as_bytes());
70
71 s = &mut s[end..];
72 }
73}