Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/ctioga2/utils.rb

Instance Method Summary collapse

Instance Method Details

#split_at_toplevel(regexp) ⇒ Object

Splits a string into substrings at the given regexp, but only if the splitting occurs at top-level with respect to parentheses.



548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# File 'lib/ctioga2/utils.rb', line 548

def split_at_toplevel(regexp)
  # Groups
  grps = {} 
  
  sz = 0
  s = self.dup
  while true
    s.gsub!(/\([^()]+\)/) do |x|
      idx = grps.size
      rep = "__#{idx}__"
      grps[rep] = x
      rep
    end
    if sz == grps.size
      break
    else
      sz = grps.size
    end
  end
  
  splitted = s.split(regexp)
  
  while grps.size > 0
    for s in splitted
      s.gsub!(/__\d+__/) do |x|
        rep = grps[x]
        grps.delete(x)
        rep
      end
    end
  end
  return splitted
end