class Unparser::Writer::DynamicString::Segmented

def dispatch

def dispatch
  if children.empty?
    write('%()')
  else
    segments.each_with_index { |segment, index| emit_segment(segment, index) }
  end
end

def emit_segment(children, index)

def emit_segment(children, index)
  write(' ') unless index.zero?
  write('"')
  emit_segment_body(children)
  write('"')
end

def emit_segment_body(children)

def emit_segment_body(children)
  children.each_with_index do |child, index|
    case child.type
    when :begin
      write('#{')
      visit(child.children.first) if child.children.first
      write('}')
    when FLAT_INTERPOLATION
      write('#')
      visit(child)
    when :str
      visit_str(children, child, index)
    when :dstr
      emit_segment_body(child.children)
    end
  end
end

def visit_str(children, child, index)

def visit_str(children, child, index)
  string = child.children.first
  next_child = children.at(index.succ)
  if next_child && next_child.type.equal?(:str)
    write(string.gsub('"', '\\"'))
  else
    write(child.children.first.inspect[1..-2])
  end
end