1/*
2 * Copyright 2017, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "word_stream_impl.h"
18
19namespace android {
20namespace spirit {
21
22WordStreamImpl::WordStreamImpl() {}
23
24WordStreamImpl::WordStreamImpl(const std::vector<uint32_t> &words)
25    : mWords(words), mIter(mWords.begin()) {}
26
27WordStreamImpl::WordStreamImpl(std::vector<uint32_t> &&words)
28    : mWords(words), mIter(mWords.begin()) {}
29
30WordStreamImpl &WordStreamImpl::operator<<(const std::string &str) {
31  const size_t len = str.length();
32  const uint32_t *begin = (uint32_t *)str.c_str();
33  const uint32_t *end = begin + (len / 4);
34  mWords.insert(mWords.end(), begin, end);
35
36  uint32_t lastWord = *end;
37  uint32_t mask = 0xFF;
38  bool clear = false;
39  for (int i = 0; i < 4; i++, mask <<= 8) {
40    if (clear) {
41      lastWord &= ~mask;
42    } else {
43      clear = ((lastWord & mask) == 0);
44    }
45  }
46  mWords.push_back(lastWord);
47  return *this;
48}
49
50WordStreamImpl &WordStreamImpl::operator>>(std::string *str) {
51  const char *s = (const char *)&*mIter;
52  str->assign(s);
53  while (*mIter++ & 0xFF000000) {
54  }
55  return *this;
56}
57
58} // namespace spirit
59} // namespace android
60