[flang] Clean up: convert CharPointerWithLength into a proper class.

Original-commit: flang-compiler/f18@514823234f
This commit is contained in:
peter klausler 2018-01-30 12:21:25 -08:00
parent 880b7939c3
commit 33d7854c55
1 changed files with 20 additions and 16 deletions

View File

@ -24,23 +24,26 @@ class Prescanner;
// Just a const char pointer with an associated length; does not own the
// referenced data. Used to describe buffered tokens and hash table keys.
struct CharPointerWithLength {
class CharPointerWithLength {
public:
CharPointerWithLength() {}
CharPointerWithLength(const char *x, size_t n) : data{x}, bytes{n} {}
CharPointerWithLength(const char *x, size_t n) : data_{x}, bytes_{n} {}
CharPointerWithLength(const CharPointerWithLength &that)
: data{that.data}, bytes{that.bytes} {}
: data_{that.data_}, bytes_{that.bytes_} {}
CharPointerWithLength &operator=(const CharPointerWithLength &that) {
data = that.data;
bytes = that.bytes;
data_ = that.data_;
bytes_ = that.bytes_;
return *this;
}
bool empty() const { return bytes == 0; }
size_t size() const { return bytes; }
const char &operator[](size_t j) const { return data[j]; }
bool empty() const { return bytes_ == 0; }
size_t size() const { return bytes_; }
const char *data() const { return data_; }
const char &operator[](size_t j) const { return data_[j]; }
const char *data{nullptr};
size_t bytes{0};
private:
const char *data_{nullptr};
size_t bytes_{0};
};
} // namespace Fortran
@ -48,7 +51,7 @@ struct CharPointerWithLength {
template<> struct std::hash<Fortran::CharPointerWithLength> {
size_t operator()(const Fortran::CharPointerWithLength &x) const {
size_t hash{0};
const char *p{x.data}, *limit{p + x.bytes};
const char *p{x.data()}, *limit{p + x.size()};
for (; p < limit; ++p) {
hash = (hash * 31) ^ *p;
}
@ -59,10 +62,10 @@ template<> struct std::hash<Fortran::CharPointerWithLength> {
template<> struct std::equal_to<Fortran::CharPointerWithLength> {
bool operator()(const Fortran::CharPointerWithLength &x,
const Fortran::CharPointerWithLength &y) const {
return x.bytes == y.bytes &&
std::memcmp(static_cast<const void *>(x.data),
static_cast<const void *>(y.data),
x.bytes) == 0;
return x.size() == y.size() &&
std::memcmp(static_cast<const void *>(x.data()),
static_cast<const void *>(y.data()),
x.size()) == 0;
}
};
@ -134,7 +137,8 @@ class TokenSequence {
}
void push_back(const CharPointerWithLength &t) {
for (size_t j{0}; j < t.bytes; ++j) {
size_t bytes{t.size()};
for (size_t j{0}; j < bytes; ++j) {
AddChar(t[j]);
}
EndToken();