2018-01-30 20:47:17 +01:00
|
|
|
#include "char-buffer.h"
|
|
|
|
#include "idioms.h"
|
|
|
|
#include <algorithm>
|
2018-03-20 18:59:07 +01:00
|
|
|
#include <cstddef>
|
2018-01-30 20:47:17 +01:00
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
namespace Fortran {
|
2018-02-07 21:04:42 +01:00
|
|
|
namespace parser {
|
2018-01-30 20:47:17 +01:00
|
|
|
|
2018-03-20 18:59:07 +01:00
|
|
|
char *CharBuffer::FreeSpace(std::size_t *n) {
|
2018-01-30 20:47:17 +01:00
|
|
|
int offset{LastBlockOffset()};
|
|
|
|
if (blocks_.empty()) {
|
|
|
|
blocks_.emplace_front();
|
|
|
|
last_ = blocks_.begin();
|
|
|
|
lastBlockEmpty_ = true;
|
|
|
|
} else if (offset == 0 && !lastBlockEmpty_) {
|
|
|
|
last_ = blocks_.emplace_after(last_);
|
|
|
|
lastBlockEmpty_ = true;
|
|
|
|
}
|
|
|
|
*n = Block::capacity - offset;
|
|
|
|
return last_->data + offset;
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:59:07 +01:00
|
|
|
void CharBuffer::Claim(std::size_t n) {
|
2018-01-30 20:47:17 +01:00
|
|
|
if (n > 0) {
|
|
|
|
bytes_ += n;
|
|
|
|
lastBlockEmpty_ = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:59:07 +01:00
|
|
|
void CharBuffer::Put(const char *data, std::size_t n) {
|
|
|
|
std::size_t chunk;
|
|
|
|
for (std::size_t at{0}; at < n; at += chunk) {
|
2018-01-30 20:47:17 +01:00
|
|
|
char *to{FreeSpace(&chunk)};
|
|
|
|
chunk = std::min(n - at, chunk);
|
|
|
|
Claim(chunk);
|
|
|
|
std::memcpy(to, data + at, chunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-05 23:29:26 +01:00
|
|
|
void CharBuffer::Put(const std::string &str) { Put(str.data(), str.size()); }
|
2018-02-07 21:04:42 +01:00
|
|
|
} // namespace parser
|
2018-01-30 20:47:17 +01:00
|
|
|
} // namespace Fortran
|