# File lib/edi4r/edifact.rb, line 94
  def edi_split( str, s, e, max=0 )
    results, item, start = [], '', 0
    while start < str.length do
      # match_at = index of next separator, or -1 if none found
      match_at = ((start...str.length).find{|i| str[i] == s}) || str.length
      item += str[start...match_at]
      # Count escapes in front of separator. No real separator if odd!
      escapes = count_escapes( item, e )
      if escapes & 1 == 1 # odd
        raise EDISyntaxError, "Pending escape char in #{str}" if match_at == str.length
        (escapes/2+1).times {item.chop!} # chop off duplicate escapes
        item << s # add separator as regular character
      else # even
        (escapes/2).times {item.chop!}  # chop off duplicate escapes
        results << item
        item = ''
      end
      start = match_at + 1
    end
    #
    # Do not return trailing empty items
    #
    results << item unless item.empty?
    return results if results.empty?
    while results.last.empty?; results.pop; end
    results
  end