refactor: change buffer to Option<Vec<u8>> and drop it after binary detection
Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
@@ -4,7 +4,7 @@ use crate::meta_plugin::{MetaPlugin, MetaPluginResponse};
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct TextMetaPlugin {
|
||||
buffer: Vec<u8>,
|
||||
buffer: Option<Vec<u8>>,
|
||||
max_buffer_size: usize,
|
||||
is_finalized: bool,
|
||||
word_count: usize,
|
||||
@@ -37,7 +37,7 @@ impl TextMetaPlugin {
|
||||
.unwrap_or(PIPESIZE as u64) as usize;
|
||||
|
||||
TextMetaPlugin {
|
||||
buffer: Vec::new(),
|
||||
buffer: Some(Vec::new()),
|
||||
max_buffer_size,
|
||||
is_finalized: false,
|
||||
word_count: 0,
|
||||
@@ -124,14 +124,15 @@ impl MetaPlugin for TextMetaPlugin {
|
||||
|
||||
// If we haven't determined if content is binary yet, build buffer and check
|
||||
if self.is_binary_content.is_none() {
|
||||
if let Some(buffer) = &mut self.buffer {
|
||||
// Add data to our buffer up to max_buffer_size
|
||||
let remaining_capacity = self.max_buffer_size.saturating_sub(self.buffer.len());
|
||||
let remaining_capacity = self.max_buffer_size.saturating_sub(buffer.len());
|
||||
let bytes_to_take = std::cmp::min(data.len(), remaining_capacity);
|
||||
self.buffer.extend_from_slice(&data[..bytes_to_take]);
|
||||
buffer.extend_from_slice(&data[..bytes_to_take]);
|
||||
|
||||
// If we have enough data to make a binary determination, do it now
|
||||
if self.buffer.len() >= std::cmp::min(1024, self.max_buffer_size) {
|
||||
let is_binary_result = is_binary(&self.buffer);
|
||||
if buffer.len() >= std::cmp::min(1024, self.max_buffer_size) {
|
||||
let is_binary_result = is_binary(buffer);
|
||||
self.is_binary_content = Some(is_binary_result);
|
||||
|
||||
// Output text and binary status
|
||||
@@ -157,6 +158,7 @@ impl MetaPlugin for TextMetaPlugin {
|
||||
|
||||
// If it's binary, we're done with this plugin
|
||||
if is_binary_result {
|
||||
self.buffer = None; // Drop the buffer
|
||||
self.is_finalized = true;
|
||||
return MetaPluginResponse {
|
||||
metadata,
|
||||
@@ -167,14 +169,16 @@ impl MetaPlugin for TextMetaPlugin {
|
||||
// If it's text, count words and lines for this chunk and stop buffering
|
||||
self.count_text_stats(&data[..bytes_to_take]);
|
||||
|
||||
// If we've reached our buffer limit, we're finalized
|
||||
if self.buffer.len() >= self.max_buffer_size {
|
||||
// If we've reached our buffer limit, drop the buffer and finalize
|
||||
if buffer.len() >= self.max_buffer_size {
|
||||
self.buffer = None; // Drop the buffer
|
||||
self.is_finalized = true;
|
||||
}
|
||||
} else {
|
||||
// Still building up buffer, count words and lines for this chunk
|
||||
self.count_text_stats(&data[..bytes_to_take]);
|
||||
}
|
||||
}
|
||||
} else if self.is_binary_content == Some(false) {
|
||||
// We've already determined it's text, just count words and lines
|
||||
self.count_text_stats(data);
|
||||
@@ -206,8 +210,10 @@ impl MetaPlugin for TextMetaPlugin {
|
||||
let mut metadata = Vec::new();
|
||||
|
||||
// If we haven't determined binary status yet, do it now with whatever we have
|
||||
if self.is_binary_content.is_none() && !self.buffer.is_empty() {
|
||||
let is_binary_result = is_binary(&self.buffer);
|
||||
if self.is_binary_content.is_none() {
|
||||
if let Some(buffer) = &self.buffer {
|
||||
if !buffer.is_empty() {
|
||||
let is_binary_result = is_binary(buffer);
|
||||
self.is_binary_content = Some(is_binary_result);
|
||||
|
||||
// Output text and binary status
|
||||
@@ -232,6 +238,7 @@ impl MetaPlugin for TextMetaPlugin {
|
||||
|
||||
// If it's binary, we're done
|
||||
if is_binary_result {
|
||||
self.buffer = None; // Drop the buffer
|
||||
self.is_finalized = true;
|
||||
return MetaPluginResponse {
|
||||
metadata,
|
||||
@@ -239,6 +246,8 @@ impl MetaPlugin for TextMetaPlugin {
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If content is text, output word and line counts
|
||||
if self.is_binary_content == Some(false) {
|
||||
@@ -265,6 +274,9 @@ impl MetaPlugin for TextMetaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
// Drop the buffer since we're done with it
|
||||
self.buffer = None;
|
||||
|
||||
// Mark as finalized
|
||||
self.is_finalized = true;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user