Skip to main content

luaur_bytecode/methods/
bytecode_builder_set_dump_source.rs

1use crate::records::bytecode_builder::BytecodeBuilder;
2
3impl BytecodeBuilder {
4    pub fn set_dump_source(&mut self, source: &str) {
5        self.dump_source.clear();
6
7        // Faithful to C++ `while (pos != npos)`: each iteration pushes exactly one line
8        // (the chunk up to the next '\n', or the remainder), and the loop exits only AFTER
9        // pushing the final chunk when no '\n' remains. A trailing '\n' therefore yields a
10        // final EMPTY line — which dumpSourceRemarks relies on to emit the trailing newline.
11        // The previous `if pos == len { break }` guard dropped that empty line.
12        let mut pos: Option<usize> = Some(0);
13
14        while let Some(p) = pos {
15            match source[p..].find('\n') {
16                None => {
17                    self.dump_source.push(source[p..].to_owned());
18                    pos = None;
19                }
20                Some(idx) => {
21                    let next = p + idx;
22                    self.dump_source.push(source[p..next].to_owned());
23                    pos = Some(next + 1);
24                }
25            }
26
27            if let Some(last) = self.dump_source.last_mut() {
28                if !last.is_empty() && last.as_bytes()[last.len() - 1] == b'\r' {
29                    last.pop();
30                }
31            }
32        }
33    }
34}