Class: OllamaAgent::Skills::JsonExtractor::BalancedScan

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama_agent/skills/json_extractor.rb

Overview

Walks the buffer once tracking bracket depth while ignoring brackets nested inside JSON string literals. Returns the slice length that closes the initial opener, or nil when the input is unbalanced.

Instance Method Summary collapse

Constructor Details

#initialize(buffer) ⇒ BalancedScan

Returns a new instance of BalancedScan.



45
46
47
48
49
50
51
52
# File 'lib/ollama_agent/skills/json_extractor.rb', line 45

def initialize(buffer)
  @buffer = buffer
  @opener = buffer[0]
  @closer = CLOSERS.fetch(@opener)
  @depth = 0
  @in_string = false
  @escape = false
end

Instance Method Details

#lengthObject



54
55
56
57
58
59
60
# File 'lib/ollama_agent/skills/json_extractor.rb', line 54

def length
  @buffer.each_char.with_index do |char, idx|
    consume(char)
    return idx + 1 if !@in_string && @depth.zero? && idx.positive?
  end
  nil
end