Warning: This file is not a C or C++ file. It does not have highlighting.
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 | #include <vector> |
20 | |
21 | namespace Fortran::parser { |
22 | |
23 | class CharBuffer { |
24 | public: |
25 | CharBuffer() {} |
26 | CharBuffer(CharBuffer &&that) |
27 | : blocks_(std::move(that.blocks_)), bytes_{that.bytes_}, |
28 | lastBlockEmpty_{that.lastBlockEmpty_} { |
29 | that.clear(); |
30 | } |
31 | CharBuffer &operator=(CharBuffer &&that) { |
32 | blocks_ = std::move(that.blocks_); |
33 | bytes_ = that.bytes_; |
34 | lastBlockEmpty_ = that.lastBlockEmpty_; |
35 | that.clear(); |
36 | return *this; |
37 | } |
38 | |
39 | bool empty() const { return bytes_ == 0; } |
40 | std::size_t bytes() const { return bytes_; } |
41 | |
42 | void clear() { |
43 | blocks_.clear(); |
44 | bytes_ = 0; |
45 | lastBlockEmpty_ = false; |
46 | } |
47 | |
48 | char *FreeSpace(std::size_t &); |
49 | void Claim(std::size_t); |
50 | |
51 | // The return value is the byte offset of the new data, |
52 | // i.e. the value of size() before the call. |
53 | std::size_t Put(const char *data, std::size_t n); |
54 | std::size_t Put(const std::string &); |
55 | std::size_t Put(char x) { return Put(&x, 1); } |
56 | |
57 | std::string Marshal() const; |
58 | |
59 | private: |
60 | struct Block { |
61 | static constexpr std::size_t capacity{1 << 20}; |
62 | char data[capacity]; |
63 | }; |
64 | |
65 | int LastBlockOffset() const { return bytes_ % Block::capacity; } |
66 | std::list<Block> blocks_; |
67 | std::size_t bytes_{0}; |
68 | bool lastBlockEmpty_{false}; |
69 | }; |
70 | } // namespace Fortran::parser |
71 | #endif // FORTRAN_PARSER_CHAR_BUFFER_H_ |
72 |
Warning: This file is not a C or C++ file. It does not have highlighting.