refactor: optimize regex patterns for image and markdown processing
- Introduced pre-compiled regex patterns across various components, including streaming_helper, markdown_stream_formatter, and assistant_message_widget, to enhance performance during image extraction and markdown formatting. - Updated the AssistantMessageWidget to utilize these optimized patterns for TTS sanitization and image processing, reducing unnecessary regex evaluations. - Improved overall efficiency in handling markdown content by leveraging pre-compiled patterns for common markdown syntax detection.
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
// Pre-compiled regex patterns for markdown syntax detection (performance optimization)
|
||||
final _boldPattern = RegExp(r'\*\*');
|
||||
final _italicPattern = RegExp(r'(?<!\*)\*(?!\*)');
|
||||
|
||||
/// Maintains a raw markdown buffer for streaming content and generates
|
||||
/// preview-safe output by appending synthetic closing tokens when necessary.
|
||||
class MarkdownStreamFormatter {
|
||||
@@ -40,12 +44,12 @@ class MarkdownStreamFormatter {
|
||||
buffer.writeln('```');
|
||||
}
|
||||
|
||||
final boldCount = RegExp(r'\*\*').allMatches(content).length;
|
||||
final boldCount = _boldPattern.allMatches(content).length;
|
||||
if (boldCount.isOdd) {
|
||||
buffer.write('**');
|
||||
}
|
||||
|
||||
final italicCount = RegExp(r'(?<!\*)\*(?!\*)').allMatches(content).length;
|
||||
final italicCount = _italicPattern.allMatches(content).length;
|
||||
if (italicCount.isOdd) {
|
||||
buffer.write('*');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user