Skip to main content

luaur_analysis/methods/
ast_json_encoder_append_chunk.rs

1use crate::records::ast_json_encoder::AstJsonEncoder;
2
3impl AstJsonEncoder {
4    pub fn append_chunk(&mut self, sv: &str) {
5        const CHUNK_SIZE: usize = 4096;
6
7        if sv.len() > CHUNK_SIZE {
8            self.chunks.push(sv.to_string());
9            self.new_chunk();
10            return;
11        }
12
13        if self.chunks.is_empty() {
14            self.new_chunk();
15        }
16
17        let chunk = self.chunks.last_mut().unwrap();
18        if chunk.len() + sv.len() < CHUNK_SIZE {
19            chunk.push_str(sv);
20            return;
21        }
22
23        let prefix = CHUNK_SIZE - chunk.len();
24        chunk.push_str(&sv[..prefix]);
25        self.new_chunk();
26
27        self.chunks.last_mut().unwrap().push_str(&sv[prefix..]);
28    }
29}