1 use crate::gen::include::HEADER;
2 use crate::gen::out::Content;
3
write(out: &mut Content, needed: bool, guard: &str)4 pub(super) fn write(out: &mut Content, needed: bool, guard: &str) {
5 let ifndef = format!("#ifndef {}", guard);
6 let define = format!("#define {}", guard);
7 let endif = format!("#endif // {}", guard);
8
9 let mut offset = 0;
10 loop {
11 let begin = find_line(offset, &ifndef);
12 let end = find_line(offset, &endif);
13 if let (Some(begin), Some(end)) = (begin, end) {
14 if !needed {
15 return;
16 }
17 out.next_section();
18 if offset == 0 {
19 writeln!(out, "{}", ifndef);
20 writeln!(out, "{}", define);
21 }
22 for line in HEADER[begin + ifndef.len()..end].trim().lines() {
23 if line != define && !line.trim_start().starts_with("//") {
24 writeln!(out, "{}", line);
25 }
26 }
27 offset = end + endif.len();
28 } else if offset == 0 {
29 panic!("not found in cxx.h header: {}", guard)
30 } else {
31 writeln!(out, "{}", endif);
32 return;
33 }
34 }
35 }
36
find_line(mut offset: usize, line: &str) -> Option<usize>37 fn find_line(mut offset: usize, line: &str) -> Option<usize> {
38 loop {
39 offset += HEADER[offset..].find(line)?;
40 let rest = &HEADER[offset + line.len()..];
41 if rest.starts_with('\n') || rest.starts_with('\r') {
42 return Some(offset);
43 }
44 offset += line.len();
45 }
46 }
47