FLANG
char-buffer.h
1//===-- include/flang/Parser/char-buffer.h ----------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef FORTRAN_PARSER_CHAR_BUFFER_H_
10#define FORTRAN_PARSER_CHAR_BUFFER_H_
11
12// Defines a simple expandable buffer suitable for efficiently accumulating
13// a stream of bytes.
14
15#include <cstddef>
16#include <list>
17#include <string>
18#include <utility>
19
20namespace Fortran::parser {
21
22class CharBuffer {
23public:
24 CharBuffer() {}
25 CharBuffer(CharBuffer &&that)
26 : blocks_(std::move(that.blocks_)), bytes_{that.bytes_},
27 lastBlockEmpty_{that.lastBlockEmpty_} {
28 that.clear();
29 }
30 CharBuffer &operator=(CharBuffer &&that) {
31 blocks_ = std::move(that.blocks_);
32 bytes_ = that.bytes_;
33 lastBlockEmpty_ = that.lastBlockEmpty_;
34 that.clear();
35 return *this;
36 }
37
38 bool empty() const { return bytes_ == 0; }
39 std::size_t bytes() const { return bytes_; }
40
41 void clear() {
42 blocks_.clear();
43 bytes_ = 0;
44 lastBlockEmpty_ = false;
45 }
46
47 char *FreeSpace(std::size_t &);
48 void Claim(std::size_t);
49
50 // The return value is the byte offset of the new data,
51 // i.e. the value of size() before the call.
52 std::size_t Put(const char *data, std::size_t n);
53 std::size_t Put(const std::string &);
54 std::size_t Put(char x) { return Put(&x, 1); }
55
56 std::string Marshal() const;
57
58private:
59 struct Block {
60 static constexpr std::size_t capacity{1 << 20};
61 char data[capacity];
62 };
63
64 int LastBlockOffset() const { return bytes_ % Block::capacity; }
65 std::list<Block> blocks_;
66 std::size_t bytes_{0};
67 bool lastBlockEmpty_{false};
68};
69} // namespace Fortran::parser
70#endif // FORTRAN_PARSER_CHAR_BUFFER_H_
Definition check-expression.h:19