void TextIteratorTextState::appendTextTo(ForwardsTextBuffer* output,
                                         unsigned position,
                                         unsigned lengthToAppend) const {
  ASSERT_WITH_SECURITY_IMPLICATION(position + lengthToAppend <=
                                   static_cast<unsigned>(length()));
  // Make sure there's no integer overflow.
  ASSERT_WITH_SECURITY_IMPLICATION(position + lengthToAppend >= position);
  if (!lengthToAppend)
    return;
  DCHECK(output);
  if (m_singleCharacterBuffer) {
    DCHECK_EQ(position, 0u);
    DCHECK_EQ(length(), 1);
    output->pushCharacters(m_singleCharacterBuffer, 1);
    return;
  }
  if (positionNode()) {
    flushPositionOffsets();
    unsigned offset = positionStartOffset() + position;
    if (string().is8Bit())
      output->pushRange(string().characters8() + offset, lengthToAppend);
    else
      output->pushRange(string().characters16() + offset, lengthToAppend);
    return;
  }
  // We shouldn't be attempting to append text that doesn't exist.
  NOTREACHED();
}
void TextIteratorTextState::appendTextToStringBuilder(StringBuilder& builder, unsigned position, unsigned maxLength) const
{
    unsigned lengthToAppend = std::min(static_cast<unsigned>(length()) - position, maxLength);
    if (!lengthToAppend)
        return;
    if (m_singleCharacterBuffer) {
        ASSERT(!position);
        builder.append(m_singleCharacterBuffer);
    } else {
        builder.append(string(), positionStartOffset() + position, lengthToAppend);
    }
}
UChar TextIteratorTextState::characterAt(unsigned index) const {
  ASSERT_WITH_SECURITY_IMPLICATION(index < static_cast<unsigned>(length()));
  if (!(index < static_cast<unsigned>(length())))
    return 0;

  if (m_singleCharacterBuffer) {
    DCHECK_EQ(index, 0u);
    DCHECK_EQ(length(), 1);
    return m_singleCharacterBuffer;
  }

  return string()[positionStartOffset() + index];
}
String TextIteratorTextState::substring(unsigned position, unsigned length) const
{
    ASSERT_WITH_SECURITY_IMPLICATION(position <= static_cast<unsigned>(this->length()));
    ASSERT_WITH_SECURITY_IMPLICATION(position + length <= static_cast<unsigned>(this->length()));
    if (!length)
        return emptyString();
    if (m_singleCharacterBuffer) {
        ASSERT(!position);
        ASSERT(length == 1);
        return String(&m_singleCharacterBuffer, 1);
    }
    return string().substring(positionStartOffset() + position, length);
}