From 15bcd37834628c941db5613fa62770426dff29e0 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Thu, 2 Nov 2023 16:02:40 -0400 Subject: [PATCH] make argparse work on c++ 17 --- include/blt/compatibility.h | 2 ++ include/blt/parse/argparse.h | 16 +++++++++++----- include/blt/std/string.h | 36 ++++++++++++++++++++++++++++++++++-- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/include/blt/compatibility.h b/include/blt/compatibility.h index c49e4ea..359cd6b 100644 --- a/include/blt/compatibility.h +++ b/include/blt/compatibility.h @@ -22,10 +22,12 @@ #if __cplusplus >= 202002L #define BLT_CONTAINS(container, value) container.contains(value) #define BLT_CPP20_CONSTEXPR constexpr + #define BLT_USE_CPP20 #else #include #define BLT_CONTAINS(container, value) std::find(container.begin(), container.end(), value) != container.end() #define BLT_CPP20_CONSTEXPR + #undef BLT_USE_CPP20 #endif #define INCLUDE_FS \ diff --git a/include/blt/parse/argparse.h b/include/blt/parse/argparse.h index 78cc880..f39b7e0 100755 --- a/include/blt/parse/argparse.h +++ b/include/blt/parse/argparse.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -118,7 +119,6 @@ namespace blt struct arg_properties_t { - private: public: arg_vector_t a_flags; arg_action_t a_action = arg_action_t::STORE; @@ -130,6 +130,11 @@ namespace blt std::string a_version{}; std::string a_metavar{}; bool a_required = true; + + arg_properties_t() = delete; + + explicit arg_properties_t(arg_vector_t flags): a_flags(std::move(flags)) + {} }; class arg_builder @@ -237,13 +242,13 @@ namespace blt // returns true if the current arg is a flag inline bool isFlag() { - return args[currentIndex].starts_with('-'); + return blt::string::starts_with(args[currentIndex], '-'); } // returns true if we have next and the next arg is a flag inline bool isNextFlag() { - return hasNext() && args[currentIndex + 1].starts_with('-'); + return hasNext() && blt::string::starts_with(args[currentIndex + 1], '-'); } // advances to the next flag @@ -306,6 +311,7 @@ namespace blt return static_cast(std::stoll(s)); return static_cast(std::stoull(s)); } + private: struct { @@ -359,9 +365,9 @@ namespace blt inline bool contains(const std::string& key) { - if (key.starts_with("--")) + if (blt::string::starts_with(key, "--")) return data.find(key.substr(2)) != data.end(); - if (key.starts_with('-')) + if (blt::string::starts_with(key, '-')) return data.find(key.substr(1)) != data.end(); return data.find(key) != data.end(); } diff --git a/include/blt/std/string.h b/include/blt/std/string.h index 9581328..df5e3b4 100755 --- a/include/blt/std/string.h +++ b/include/blt/std/string.h @@ -63,6 +63,9 @@ namespace blt::string static inline BLT_CPP20_CONSTEXPR bool starts_with(const std::string& string, const std::string& search) { +#ifdef BLT_USE_CPP20 + return string.starts_with(search); +#else if (search.length() > string.length()) return false; auto chars = string.c_str(); @@ -73,10 +76,25 @@ namespace blt::string return false; } return true; +#endif + } + + static inline BLT_CPP20_CONSTEXPR bool starts_with(const std::string& string, char search) + { +#ifdef BLT_USE_CPP20 + return string.starts_with(search); +#else + if (string.empty()) + return false; + return string[0] == search; +#endif } static inline BLT_CPP20_CONSTEXPR bool ends_with(const std::string& string, const std::string& search) { +#ifdef BLT_USE_CPP20 + return string.ends_with(search); +#else if (search.length() > string.length()) return false; auto chars = string.c_str(); @@ -88,6 +106,18 @@ namespace blt::string return false; } return true; +#endif + } + + static inline BLT_CPP20_CONSTEXPR bool ends_with(const std::string& string, char search) + { +#ifdef BLT_USE_CPP20 + return string.ends_with(search); +#else + if (string.empty()) + return false; + return string[string.size() - 1] == search; +#endif } static inline BLT_CPP20_CONSTEXPR bool contains(const std::string& string, const char search) @@ -97,7 +127,8 @@ namespace blt::string return c == search; }); #else - for (const char c : string){ + for (const char c : string) + { if (c == search) return true; } @@ -278,7 +309,8 @@ namespace blt::string return std::isdigit(c); }); #else - for (const char c : s){ + for (const char c : s) + { if (!std::isdigit(c)) return false; }