diff --git a/poc-memory/src/transcript.rs b/poc-memory/src/transcript.rs index 3919050..9a3dd71 100644 --- a/poc-memory/src/transcript.rs +++ b/poc-memory/src/transcript.rs @@ -32,14 +32,31 @@ impl<'a> Iterator for JsonlBackwardIter<'a> { type Item = &'a [u8]; fn next(&mut self) -> Option { - // Find the closing } of the next object - let close = loop { - let p = memrchr3(b'{', b'}', b'"', &self.data[..self.pos])?; - self.pos = p; - if self.data[p] == b'}' { - break p; + // Find the closing } of the next object, skipping } inside strings + let close = { + let mut in_string = false; + loop { + let p = memrchr3(b'{', b'}', b'"', &self.data[..self.pos])?; + self.pos = p; + let ch = self.data[p]; + + if in_string { + if ch == b'"' { + let mut bs = 0; + while p > bs + 1 && self.data[p - 1 - bs] == b'\\' { + bs += 1; + } + if bs % 2 == 0 { in_string = false; } + } + continue; + } + + match ch { + b'}' => break p, + b'"' => in_string = true, + _ => {} + } } - // Skip past any { or " that aren't our closing brace }; // Track brace depth to find matching { @@ -164,11 +181,31 @@ impl Iterator for TailMessages { fn next(&mut self) -> Option { loop { - // Find closing } - let close = loop { - let p = memrchr3(b'{', b'}', b'"', &self.mmap[..self.pos])?; - self.pos = p; - if self.mmap[p] == b'}' { break p; } + // Find closing }, skipping } inside strings + let close = { + let mut in_string = false; + loop { + let p = memrchr3(b'{', b'}', b'"', &self.mmap[..self.pos])?; + self.pos = p; + let ch = self.mmap[p]; + + if in_string { + if ch == b'"' { + let mut bs = 0; + while p > bs + 1 && self.mmap[p - 1 - bs] == b'\\' { + bs += 1; + } + if bs % 2 == 0 { in_string = false; } + } + continue; + } + + match ch { + b'}' => break p, + b'"' => in_string = true, + _ => {} + } + } }; // Track brace depth to find matching {