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