summaryrefslogtreecommitdiff
path: root/xpcom/ds/IncrementalTokenizer.cpp
blob: 429428516136fc2ab6f5df685299c1ab99bbce50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/IncrementalTokenizer.h"

#include "mozilla/AutoRestore.h"

#include "nsIInputStream.h"
#include "IncrementalTokenizer.h"
#include <algorithm>

namespace mozilla {

IncrementalTokenizer::IncrementalTokenizer(Consumer aConsumer,
                                           const char * aWhitespaces,
                                           const char * aAdditionalWordChars,
                                           uint32_t aRawMinBuffered)
  : TokenizerBase(aWhitespaces, aAdditionalWordChars)
#ifdef DEBUG
  , mConsuming(false)
#endif
  , mNeedMoreInput(false)
  , mRollback(false)
  , mInputCursor(0)
  , mConsumer(aConsumer)
{
  mInputFinished = false;
  mMinRawDelivery = aRawMinBuffered;
}

nsresult IncrementalTokenizer::FeedInput(const nsACString & aInput)
{
  NS_ENSURE_TRUE(mConsumer, NS_ERROR_NOT_INITIALIZED);
  MOZ_ASSERT(!mInputFinished);

  mInput.Cut(0, mInputCursor);
  mInputCursor = 0;

  mInput.Append(aInput);

  return Process();
}

nsresult IncrementalTokenizer::FeedInput(nsIInputStream * aInput, uint32_t aCount)
{
  NS_ENSURE_TRUE(mConsumer, NS_ERROR_NOT_INITIALIZED);
  MOZ_ASSERT(!mInputFinished);
  MOZ_ASSERT(!mConsuming);

  mInput.Cut(0, mInputCursor);
  mInputCursor = 0;

  nsresult rv = NS_OK;
  while (NS_SUCCEEDED(rv) && aCount) {
    nsCString::index_type remainder = mInput.Length();
    nsCString::index_type load =
      std::min<nsCString::index_type>(aCount, PR_UINT32_MAX - remainder);

    if (!load) {
      // To keep the API simple, we fail if the input data buffer if filled.
      // It's highly unlikely there will ever be such amout of data cumulated
      // unless a logic fault in the consumer code.
      NS_ERROR("IncrementalTokenizer consumer not reading data?");
      return NS_ERROR_OUT_OF_MEMORY;
    }

    if (!mInput.SetLength(remainder + load, fallible)) {
      return NS_ERROR_OUT_OF_MEMORY;
    }

    nsCString::char_iterator buffer = mInput.BeginWriting() + remainder;

    uint32_t read;
    rv = aInput->Read(buffer, load, &read);
    if (NS_SUCCEEDED(rv)) {
      // remainder + load fits the uint32_t size, so must remainder + read.
      mInput.SetLength(remainder + read);
      aCount -= read;

      rv = Process();
    }
  }

  return rv;
}

nsresult IncrementalTokenizer::FinishInput()
{
  NS_ENSURE_TRUE(mConsumer, NS_ERROR_NOT_INITIALIZED);
  MOZ_ASSERT(!mInputFinished);
  MOZ_ASSERT(!mConsuming);

  mInput.Cut(0, mInputCursor);
  mInputCursor = 0;

  mInputFinished = true;
  nsresult rv = Process();
  mConsumer = nullptr;
  return rv;
}

bool IncrementalTokenizer::Next(Token & aToken)
{
  // Assert we are called only from the consumer callback
  MOZ_ASSERT(mConsuming);

  if (mPastEof) {
    return false;
  }

  nsACString::const_char_iterator next = Parse(aToken);
  mPastEof = aToken.Type() == TOKEN_EOF;
  if (next == mCursor && !mPastEof) {
    // Not enough input to make a deterministic decision.
    return false;
  }

  AssignFragment(aToken, mCursor, next);
  mCursor = next;
  return true;
}

void IncrementalTokenizer::NeedMoreInput()
{
  // Assert we are called only from the consumer callback
  MOZ_ASSERT(mConsuming);

  // When the input has been finished, we can't set the flag to prevent
  // indefinite wait for more input (that will never come)
  mNeedMoreInput = !mInputFinished;
}

void IncrementalTokenizer::Rollback()
{
  // Assert we are called only from the consumer callback
  MOZ_ASSERT(mConsuming);

  mRollback = true;
}

nsresult IncrementalTokenizer::Process()
{
#ifdef DEBUG
  // Assert we are not re-entered
  MOZ_ASSERT(!mConsuming);

  AutoRestore<bool> consuming(mConsuming);
  mConsuming = true;
#endif

  MOZ_ASSERT(!mPastEof);

  nsresult rv = NS_OK;

  mInput.BeginReading(mCursor);
  mCursor += mInputCursor;
  mInput.EndReading(mEnd);

  while (NS_SUCCEEDED(rv) && !mPastEof) {
    Token token;
    nsACString::const_char_iterator next = Parse(token);
    mPastEof = token.Type() == TOKEN_EOF;
    if (next == mCursor && !mPastEof) {
      // Not enough input to make a deterministic decision.
      break;
    }

    AssignFragment(token, mCursor, next);

    nsACString::const_char_iterator rollback = mCursor;
    mCursor = next;

    mNeedMoreInput = mRollback = false;

    rv = mConsumer(token, *this);
    if (NS_FAILED(rv)) {
      break;
    }
    if (mNeedMoreInput || mRollback) {
      mCursor = rollback;
      mPastEof = false;
      if (mNeedMoreInput) {
        break;
      }
    }
  }

  mInputCursor = mCursor - mInput.BeginReading();
  return rv;
}

} // mozilla