This is an automated email from the ASF dual-hosted git repository. cmcfarlen pushed a commit to branch 10.0.x in repository https://gitbox.apache.org/repos/asf/trafficserver.git
commit 897aeb7426243f0ab3d3eab88ea973ff154cd3cb Author: Leif Hedstrom <[email protected]> AuthorDate: Thu Apr 4 08:06:27 2024 -0600 Clean up some of Cripts strings with modern libSWOC (#11208) (cherry picked from commit d8f3acbd2b5821faec8886375c2d14d1fcb07282) --- example/cripts/example1.cc | 4 +++ include/cripts/Error.hpp | 2 +- include/cripts/Lulu.hpp | 79 ++++++++++++++-------------------------------- include/cripts/Matcher.hpp | 4 +-- include/cripts/Urls.hpp | 14 ++++---- 5 files changed, 38 insertions(+), 65 deletions(-) diff --git a/example/cripts/example1.cc b/example/cripts/example1.cc index b6d73c2043..945e9cca11 100644 --- a/example/cripts/example1.cc +++ b/example/cripts/example1.cc @@ -158,6 +158,10 @@ do_remap() CDebug("Path[1] is {}", url.path[1]); CDebug("Query is {}", url.query); + auto testing_trim = url.path.trim(); + + CDebug("Trimmed path is {}", testing_trim); + if (url.query["foo"] > 100) { CDebug("Query[foo] is > 100"); } diff --git a/include/cripts/Error.hpp b/include/cripts/Error.hpp index 1372d7fc35..efa87922dd 100644 --- a/include/cripts/Error.hpp +++ b/include/cripts/Error.hpp @@ -41,7 +41,7 @@ public: static void _set(Cript::Context *context, const Cript::string_view msg); - [[nodiscard]] Cript::StringViewWrapper + [[nodiscard]] Cript::string_view message() const { return {_message.c_str(), _message.size()}; diff --git a/include/cripts/Lulu.hpp b/include/cripts/Lulu.hpp index 02ed324a84..4b1ac6b984 100644 --- a/include/cripts/Lulu.hpp +++ b/include/cripts/Lulu.hpp @@ -70,11 +70,10 @@ public: constexpr StringViewMixin() = default; constexpr StringViewMixin(const char *s) { _value = mixin_type(s, strlen(s)); } constexpr StringViewMixin(const char *s, mixin_type::size_type count) { _value = mixin_type(s, count); } - constexpr StringViewMixin(Cript::string_view &str) { _value = str; } + constexpr StringViewMixin(const Cript::string_view &str) { _value = str; } virtual self_type &operator=(const mixin_type str) = 0; - // ToDo: We need the toFloat() and toBool() methods here operator integer() const { return integer_helper(_value); } [[nodiscard]] integer @@ -89,6 +88,18 @@ public: return integer(*this); } + [[nodiscard]] double + toFloat() const + { + return float(*this); + } + + [[nodiscard]] bool + toBool() const + { + return bool(*this); + } + std::vector<mixin_type> splitter(mixin_type input, char delim) { @@ -178,73 +189,64 @@ public: // ToDo: There are other members of std::string_view /swoc::TextView that we may want to incorporate here, // to make the mixin class more complete. - - // ToDo: These are additions made by us, would be in C++20 ChildT & ltrim(char c) { - _value.remove_prefix(_value.find_first_not_of(c)); + _value.ltrim(c); return *(static_cast<ChildT *>(this)); } ChildT & rtrim(char c) { - auto n = _value.find_last_not_of(c); - - _value.remove_suffix(_value.size() - (n == mixin_type::npos ? 0 : n + 1)); + _value.rtrim(c); return *(static_cast<ChildT *>(this)); } ChildT & trim(char c) { - this->ltrim(c); - this->rtrim(c); + _value.trim(c); return *(static_cast<ChildT *>(this)); } ChildT & ltrim(const char *chars = " \t\r\n") { - _value.remove_prefix(_value.find_first_not_of(chars)); + _value.ltrim(chars); return *(static_cast<ChildT *>(this)); } ChildT & rtrim(const char *chars = " \t\r\n") { - auto n = _value.find_last_not_of(chars); - - _value.remove_suffix(_value.size() - (n == mixin_type::npos ? 0 : n + 1)); + _value.rtrim(chars); return *(static_cast<ChildT *>(this)); } ChildT & trim(const char *chars = " \t") { - this->ltrim(chars); - this->rtrim(chars); - + _value.trim(chars); return *(static_cast<ChildT *>(this)); } [[nodiscard]] constexpr char const * data_end() const noexcept { - return _value.data() + _value.size(); + return _value.data_end(); } [[nodiscard]] bool ends_with(Cript::string_view const suffix) const { - return _value.size() >= suffix.size() && 0 == ::memcmp(this->data_end() - suffix.size(), suffix.data(), suffix.size()); + return _value.ends_with(suffix); } [[nodiscard]] bool starts_with(Cript::string_view const prefix) const { - return _value.size() >= prefix.size() && 0 == ::memcmp(_value.data(), prefix.data(), prefix.size()); + return _value.starts_with(prefix); } protected: @@ -258,24 +260,6 @@ private: mixin_type _value; }; // End class Cript::StringViewMixin -// ToDo: This is wonky right now, but once we're fully on ATS v10.0.0, we should just -// eliminate all this and use swoc::TextView directly and consistently. -class StringViewWrapper : public StringViewMixin<StringViewWrapper> -{ - using self_type = StringViewWrapper; - using super_type = StringViewMixin<self_type>; - -public: - using super_type::super_type; - StringViewWrapper & - operator=(const Cript::string_view str) override - { - _setSV(str); - return *this; - } -}; - -// ToDo: Should this be a mixin class too ? class string : public std::string { using super_type = std::string; @@ -316,9 +300,9 @@ public: string(super_type &&that) : super_type(std::move(that)) {} string(self_type &&that) : super_type(std::move(that)) {} - // ToDo: This seems to be ambiquous with STL implementation - // operator Cript::string_view() const { return {this->c_str(), this->size()}; } + operator Cript::string_view() const { return {this->c_str(), this->size()}; } + // Make sure to keep these updated with C++20 ... self_type & ltrim(char c = ' ') { @@ -638,19 +622,4 @@ template <> struct formatter<TSHttpStatus> { } }; -template <> struct formatter<Cript::StringViewWrapper> { - constexpr auto - parse(format_parse_context &ctx) -> decltype(ctx.begin()) - { - return ctx.begin(); - } - - template <typename FormatContext> - auto - format(const Cript::StringViewWrapper &sv, FormatContext &ctx) -> decltype(ctx.out()) - { - return fmt::format_to(ctx.out(), "{}", sv.getSV()); - } -}; - } // namespace fmt diff --git a/include/cripts/Matcher.hpp b/include/cripts/Matcher.hpp index 0b2a4c6582..6c0f266542 100644 --- a/include/cripts/Matcher.hpp +++ b/include/cripts/Matcher.hpp @@ -164,10 +164,10 @@ public: return matched(); } - Cript::StringViewWrapper + Cript::string_view operator[](size_t ix) const { - Cript::StringViewWrapper ret; + Cript::string_view ret; if ((count() > ix) && _ovector) { ret = {_subject.substr(_ovector[ix * 2], _ovector[ix * 2 + 1] - _ovector[ix * 2])}; diff --git a/include/cripts/Urls.hpp b/include/cripts/Urls.hpp index f357b05d9a..2456f3d0d4 100644 --- a/include/cripts/Urls.hpp +++ b/include/cripts/Urls.hpp @@ -86,37 +86,37 @@ class Url // This is not ideal, but best way I can think of for now to mixin the Cript::string_view mixin class // Remember to add things here when added to the Lulu.hpp file for the mixin class... :/ - Cript::StringViewWrapper & + Cript::string_view & ltrim(char c) { return _data.ltrim(c); } - Cript::StringViewWrapper & + Cript::string_view & rtrim(char c) { return _data.rtrim(c); } - Cript::StringViewWrapper & + Cript::string_view & trim(char c) { return _data.trim(c); } - Cript::StringViewWrapper & + Cript::string_view & ltrim(const char *chars = " \t\r\n") { return _data.ltrim(chars); } - Cript::StringViewWrapper & + Cript::string_view & rtrim(const char *chars = " \t\r\n") { return _data.rtrim(chars); } - Cript::StringViewWrapper & + Cript::string_view & trim(const char *chars = " \t") { return _data.trim(chars); @@ -141,7 +141,7 @@ class Url } protected: - mutable Cript::StringViewWrapper _data; + mutable Cript::string_view _data; Url *_owner = nullptr; bool _loaded = false;
