From 33d7854c5517c7f571855635f265f5992ef0d507 Mon Sep 17 00:00:00 2001 From: peter klausler Date: Tue, 30 Jan 2018 12:21:25 -0800 Subject: [PATCH] [flang] Clean up: convert CharPointerWithLength into a proper class. Original-commit: flang-compiler/f18@514823234fefd422dce2c9f7407c208982e459de --- flang/preprocessor.h | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/flang/preprocessor.h b/flang/preprocessor.h index eda9839..fd20d85 100644 --- a/flang/preprocessor.h +++ b/flang/preprocessor.h @@ -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 { 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 { template<> struct std::equal_to { bool operator()(const Fortran::CharPointerWithLength &x, const Fortran::CharPointerWithLength &y) const { - return x.bytes == y.bytes && - std::memcmp(static_cast(x.data), - static_cast(y.data), - x.bytes) == 0; + return x.size() == y.size() && + std::memcmp(static_cast(x.data()), + static_cast(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(); -- 2.7.4