ScriptSegment consumeBuffer(String pBuffer) 
  throws ExParser {
    
    //Sanity check that the buffer starts with the correct sequence
    if(pBuffer.indexOf(mEscapeDelimiter.mStartSequence) != 0){
      //This is an internal error, this method should not have been called if the buffer is not in the correct state
      throw new ExInternal("Sequence " + mEscapeDelimiter + " should start at position 0");
    }
    
    //Find the corresponding terminating sequence
    int lEndPosition = pBuffer.indexOf(mEscapeDelimiter.mEndSequence, 1);
    int lEndEscSeqLength = mEscapeDelimiter.mEndSequence.length();
    
    //If the remaining string doesn't contain this segment's termination sequence, that is a problem
    if(lEndPosition == -1){
      if(mEscapeDelimiter.mRequiresEndDelimiter){
        throw new ExParser("Unterminated escape sequence " + mEscapeDelimiter); 
      }
      else {
        //For "--" sequence, end of file counts as a terminator
        lEndPosition = pBuffer.length()-1;
      }
    }
    
    //Record the contents of this segment
    setContents(pBuffer.substring(mEscapeDelimiter.mStartSequence.length(), lEndPosition));
    
    //Return a new unescaped segment which will start reading from the end of this segment
    return new UnescapedTextSegment(lEndPosition + lEndEscSeqLength); 
  }