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