2018-05-01 21:50:34 +02:00
|
|
|
// Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-02-16 20:42:17 +01:00
|
|
|
#ifndef FORTRAN_PARSER_CHAR_BUFFER_H_
|
|
|
|
#define FORTRAN_PARSER_CHAR_BUFFER_H_
|
2018-01-30 20:47:17 +01:00
|
|
|
|
|
|
|
// Defines a simple expandable buffer suitable for efficiently accumulating
|
|
|
|
// a stream of bytes.
|
|
|
|
|
2018-03-20 18:59:07 +01:00
|
|
|
#include <cstddef>
|
2018-01-30 20:47:17 +01:00
|
|
|
#include <forward_list>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2018-05-02 22:48:12 +02:00
|
|
|
namespace Fortran::parser {
|
2018-01-30 20:47:17 +01:00
|
|
|
|
|
|
|
class CharBuffer {
|
2018-02-05 21:54:36 +01:00
|
|
|
public:
|
2018-01-30 20:47:17 +01:00
|
|
|
CharBuffer() {}
|
|
|
|
CharBuffer(CharBuffer &&that)
|
2018-02-05 23:29:26 +01:00
|
|
|
: blocks_(std::move(that.blocks_)), last_{that.last_}, bytes_{that.bytes_},
|
|
|
|
lastBlockEmpty_{that.lastBlockEmpty_} {
|
2018-01-30 20:47:17 +01:00
|
|
|
that.clear();
|
|
|
|
}
|
|
|
|
CharBuffer &operator=(CharBuffer &&that) {
|
|
|
|
blocks_ = std::move(that.blocks_);
|
|
|
|
last_ = that.last_;
|
|
|
|
bytes_ = that.bytes_;
|
|
|
|
lastBlockEmpty_ = that.lastBlockEmpty_;
|
|
|
|
that.clear();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-04-05 22:06:36 +02:00
|
|
|
bool empty() const { return bytes_ == 0; }
|
2018-03-20 18:59:07 +01:00
|
|
|
std::size_t size() const { return bytes_; }
|
2018-01-30 20:47:17 +01:00
|
|
|
|
|
|
|
void clear() {
|
|
|
|
blocks_.clear();
|
|
|
|
last_ = blocks_.end();
|
|
|
|
bytes_ = 0;
|
|
|
|
lastBlockEmpty_ = false;
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:59:07 +01:00
|
|
|
char *FreeSpace(std::size_t *);
|
|
|
|
void Claim(std::size_t);
|
|
|
|
void Put(const char *data, std::size_t n);
|
2018-01-30 20:47:17 +01:00
|
|
|
void Put(const std::string &);
|
|
|
|
void Put(char x) { Put(&x, 1); }
|
|
|
|
|
2018-02-05 21:54:36 +01:00
|
|
|
private:
|
2018-01-30 20:47:17 +01:00
|
|
|
struct Block {
|
2018-03-20 18:59:07 +01:00
|
|
|
static constexpr std::size_t capacity{1 << 20};
|
2018-01-30 20:47:17 +01:00
|
|
|
char data[capacity];
|
|
|
|
};
|
|
|
|
|
2018-02-05 21:54:36 +01:00
|
|
|
public:
|
2018-01-30 20:47:17 +01:00
|
|
|
class iterator {
|
2018-02-05 21:54:36 +01:00
|
|
|
public:
|
2018-01-30 20:47:17 +01:00
|
|
|
iterator() {}
|
|
|
|
iterator(std::forward_list<Block>::const_iterator block, int offset)
|
|
|
|
: block_{block}, offset_{offset} {}
|
|
|
|
iterator(const iterator &that)
|
|
|
|
: block_{that.block_}, offset_{that.offset_} {}
|
|
|
|
iterator &operator=(const iterator &that) {
|
|
|
|
block_ = that.block_;
|
|
|
|
offset_ = that.offset_;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
const char &operator*() const { return block_->data[offset_]; }
|
2018-04-05 23:39:55 +02:00
|
|
|
iterator &operator++(/*++prefix*/) {
|
2018-01-30 20:47:17 +01:00
|
|
|
if (++offset_ == Block::capacity) {
|
|
|
|
++block_;
|
|
|
|
offset_ = 0;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2018-04-05 23:39:55 +02:00
|
|
|
iterator operator++(int /*postfix++*/) {
|
2018-01-30 20:47:17 +01:00
|
|
|
iterator result{*this};
|
|
|
|
++*this;
|
|
|
|
return result;
|
|
|
|
}
|
2018-03-20 18:59:07 +01:00
|
|
|
iterator &operator+=(std::size_t n) {
|
2018-01-30 20:47:17 +01:00
|
|
|
while (n >= Block::capacity - offset_) {
|
|
|
|
n -= Block::capacity - offset_;
|
|
|
|
offset_ = 0;
|
|
|
|
++block_;
|
|
|
|
}
|
|
|
|
offset_ += n;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
bool operator==(const iterator &that) const {
|
|
|
|
return block_ == that.block_ && offset_ == that.offset_;
|
|
|
|
}
|
|
|
|
bool operator!=(const iterator &that) const {
|
|
|
|
return block_ != that.block_ || offset_ != that.offset_;
|
|
|
|
}
|
2018-02-05 23:29:26 +01:00
|
|
|
|
2018-02-05 21:54:36 +01:00
|
|
|
private:
|
2018-01-30 20:47:17 +01:00
|
|
|
std::forward_list<Block>::const_iterator block_;
|
|
|
|
int offset_;
|
|
|
|
};
|
|
|
|
|
|
|
|
iterator begin() const { return iterator(blocks_.begin(), 0); }
|
|
|
|
iterator end() const {
|
|
|
|
int offset = LastBlockOffset();
|
|
|
|
if (offset != 0 || lastBlockEmpty_) {
|
|
|
|
return iterator(last_, offset);
|
|
|
|
}
|
|
|
|
return iterator(blocks_.end(), 0);
|
|
|
|
}
|
|
|
|
|
2018-02-05 21:54:36 +01:00
|
|
|
private:
|
2018-01-30 20:47:17 +01:00
|
|
|
int LastBlockOffset() const { return bytes_ % Block::capacity; }
|
|
|
|
std::forward_list<Block> blocks_;
|
|
|
|
std::forward_list<Block>::iterator last_{blocks_.end()};
|
2018-03-20 18:59:07 +01:00
|
|
|
std::size_t bytes_{0};
|
2018-01-30 20:47:17 +01:00
|
|
|
bool lastBlockEmpty_{false};
|
|
|
|
};
|
2018-05-02 22:48:12 +02:00
|
|
|
|
|
|
|
} // namespace Fortran::parser
|
2018-02-16 20:42:17 +01:00
|
|
|
#endif // FORTRAN_PARSER_CHAR_BUFFER_H_
|