1 //===-- lib/Parser/char-buffer.cpp ----------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "flang/Parser/char-buffer.h"
10 #include "flang/Common/idioms.h"
15 namespace Fortran::parser
{
17 char *CharBuffer::FreeSpace(std::size_t &n
) {
18 int offset
{LastBlockOffset()};
19 if (blocks_
.empty()) {
20 blocks_
.emplace_front();
21 lastBlockEmpty_
= true;
22 } else if (offset
== 0 && !lastBlockEmpty_
) {
23 blocks_
.emplace_back();
24 lastBlockEmpty_
= true;
26 n
= Block::capacity
- offset
;
27 return blocks_
.back().data
+ offset
;
30 void CharBuffer::Claim(std::size_t n
) {
33 lastBlockEmpty_
= false;
37 std::size_t CharBuffer::Put(const char *data
, std::size_t n
) {
39 for (std::size_t at
{0}; at
< n
; at
+= chunk
) {
40 char *to
{FreeSpace(chunk
)};
41 chunk
= std::min(n
- at
, chunk
);
43 std::memcpy(to
, data
+ at
, chunk
);
48 std::size_t CharBuffer::Put(const std::string
&str
) {
49 return Put(str
.data(), str
.size());
52 std::string
CharBuffer::Marshal() const {
54 std::size_t bytes
{bytes_
};
55 result
.reserve(bytes
);
56 for (const Block
&block
: blocks_
) {
57 std::size_t chunk
{std::min(bytes
, Block::capacity
)};
58 for (std::size_t j
{0}; j
< chunk
; ++j
) {
59 result
+= block
.data
[j
];
63 result
.shrink_to_fit();
64 CHECK(result
.size() == bytes_
);
67 } // namespace Fortran::parser