lidavidm commented on a change in pull request #10726:
URL: https://github.com/apache/arrow/pull/10726#discussion_r670696529
##########
File path: cpp/src/arrow/util/enum.h
##########
@@ -43,68 +42,51 @@ constexpr bool CaseInsensitiveEquals(util::string_view l,
util::string_view r) {
return l.size() == r.size() && CaseInsensitiveEquals(l.data(), r.data(),
l.size());
}
-constexpr const char* SkipWhitespace(const char* raw) {
- return *raw == '\0' || !IsSpace(*raw) ? raw : SkipWhitespace(raw + 1);
-}
+} // namespace internal
-constexpr const char* SkipNonWhitespace(const char* raw) {
- return *raw == '\0' || IsSpace(*raw) ? raw : SkipNonWhitespace(raw + 1);
-}
+template <int N>
+struct EnumStrings {
+ template <int M>
+ static constexpr bool assert_count() {
+ static_assert(M == N, "Incorrect number of enum strings provided");
+ return false;
+ }
-constexpr size_t TokenSize(const char* token_start) {
- return SkipNonWhitespace(token_start) - token_start;
-}
+ template <typename... Strs>
+ constexpr EnumStrings(const Strs&... strs) // NOLINT runtime/explicit
+ : dummy_{assert_count<sizeof...(Strs)>()},
strings_{util::string_view(strs)...} {}
-constexpr size_t NextTokenStart(const char* raw, size_t token_start) {
- return SkipWhitespace(SkipNonWhitespace(raw + token_start)) - raw;
-}
+ constexpr int GetIndex(util::string_view repr, int i = 0) const {
+ return i == N ? -1
+ : internal::CaseInsensitiveEquals(strings_[i], repr)
+ ? i
+ : GetIndex(repr, i + 1);
+ }
-template <typename Raw, size_t... Offsets>
-struct EnumTypeImpl {
- static constexpr int kSize = sizeof...(Offsets);
+ using value_type = util::string_view;
+ using const_iterator = const util::string_view*;
- static constexpr util::string_view kValueStrs[sizeof...(Offsets)] = {
- {Raw::kValues + Offsets, TokenSize(Raw::kValues + Offsets)}...};
+ constexpr int size() const { return N; }
+ constexpr const util::string_view* data() const { return strings_; }
+ constexpr const_iterator begin() const { return data(); }
+ constexpr const_iterator end() const { return begin() + size(); }
+ constexpr util::string_view operator[](int i) const { return strings_[i]; }
- static constexpr int GetIndex(util::string_view repr, int i = 0) {
- return i == kSize
- ? -1
- : CaseInsensitiveEquals(kValueStrs[i], repr) ? i :
GetIndex(repr, i + 1);
- }
+ bool dummy_;
+ util::string_view strings_[N]; // NOLINT modernize
Review comment:
We can't use std::array since its methods aren't constexpr until C++17?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]