You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by bn...@apache.org on 2024/01/30 16:24:08 UTC

(trafficserver-libswoc) branch charset created (now 7592cb6)

This is an automated email from the ASF dual-hosted git repository.

bneradt pushed a change to branch charset
in repository https://gitbox.apache.org/repos/asf/trafficserver-libswoc.git


      at 7592cb6  Errata: fix is_ok

This branch includes the following new commits:

     new 3632bbc  Errata: Enable auto generated annotations.
     new 6ab1f57  Clang format.
     new 032c6a1  IPAddr: make copy_to const
     new 7592cb6  Errata: fix is_ok

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(trafficserver-libswoc) 03/04: IPAddr: make copy_to const

Posted by bn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

bneradt pushed a commit to branch charset
in repository https://gitbox.apache.org/repos/asf/trafficserver-libswoc.git

commit 032c6a12a84e161c20a69f16a432c08a483e0d8c
Author: Alan M. Carroll <am...@apache.org>
AuthorDate: Tue Nov 21 16:44:21 2023 -0600

    IPAddr: make copy_to const
---
 code/include/swoc/IPAddr.h | 2 +-
 code/src/swoc_ip.cc        | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/code/include/swoc/IPAddr.h b/code/include/swoc/IPAddr.h
index 690ee14..50d14af 100644
--- a/code/include/swoc/IPAddr.h
+++ b/code/include/swoc/IPAddr.h
@@ -579,7 +579,7 @@ public:
    * @param sa Destination.
    * @return @a sa
    */
-  sockaddr *copy_to(sockaddr *sa);
+  sockaddr *copy_to(sockaddr *sa) const;
 
   /** Parse a string and load the result in @a this.
    *
diff --git a/code/src/swoc_ip.cc b/code/src/swoc_ip.cc
index 67fc9fa..0ddc9bb 100644
--- a/code/src/swoc_ip.cc
+++ b/code/src/swoc_ip.cc
@@ -515,7 +515,7 @@ IPAddr::operator=(IPEndpoint const &addr) {
 }
 
 sockaddr *
-IPAddr::copy_to(sockaddr *sa) {
+IPAddr::copy_to(sockaddr *sa) const {
   if (this->is_ip4()) {
     _addr._ip4.copy_to(sa);
   } else if (this->is_ip6()) {


(trafficserver-libswoc) 02/04: Clang format.

Posted by bn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

bneradt pushed a commit to branch charset
in repository https://gitbox.apache.org/repos/asf/trafficserver-libswoc.git

commit 6ab1f574d2259c833f61c2514d3ce852300867af
Author: Alan M. Carroll <am...@apache.org>
AuthorDate: Tue Nov 21 16:38:30 2023 -0600

    Clang format.
---
 code/include/swoc/Errata.h   | 25 +++++++++++++------------
 code/include/swoc/TextView.h | 15 ++++++++++-----
 code/src/bw_format.cc        |  2 +-
 unit_tests/test_Errata.cc    | 27 ++++++++++-----------------
 4 files changed, 34 insertions(+), 35 deletions(-)

diff --git a/code/include/swoc/Errata.h b/code/include/swoc/Errata.h
index 8d3c129..944129d 100644
--- a/code/include/swoc/Errata.h
+++ b/code/include/swoc/Errata.h
@@ -86,8 +86,8 @@ public:
   /// This defaults to zero and no filtering is done unless it is overwritten.
   static Severity FILTER_SEVERITY;
 
-  static inline TextView AUTOTEXT_SEVERITY = "{}"; ///< Format for auto generated annotation with severity.
-  static inline TextView AUTOTEXT_CODE = "{}"; ///< Format for auto generated annotation with error code.
+  static inline TextView AUTOTEXT_SEVERITY      = "{}";     ///< Format for auto generated annotation with severity.
+  static inline TextView AUTOTEXT_CODE          = "{}";     ///< Format for auto generated annotation with error code.
   static inline TextView AUTOTEXT_SEVERITY_CODE = "{}: {}"; ///< Format for auto generate annotation with error code and severity.
 
   /// Mapping of severity to string.
@@ -200,15 +200,16 @@ protected:
     TextView _indent_text                   = DEFAULT_INDENT_TEXT;
     bool _glue_final_p                      = true; ///< Add glue after the last annotation?
 
-    std::optional<Severity> _severity;     ///< Severity.
-    code_type _code{DEFAULT_CODE}; ///< Message code / ID
-    Container _notes;                      ///< The message stack.
-    swoc::MemArena _arena;                 ///< Annotation text storage.
+    std::optional<Severity> _severity; ///< Severity.
+    code_type _code{DEFAULT_CODE};     ///< Message code / ID
+    Container _notes;                  ///< The message stack.
+    swoc::MemArena _arena;             ///< Annotation text storage.
   };
 
 public:
   /// Used to indicate automatically generated annotation text.
-  static constexpr struct AutoText {} AUTO {};
+  static constexpr struct AutoText {
+  } AUTO{};
 
   /// Default constructor - empty errata, very fast.
   Errata()                      = default;
@@ -224,7 +225,7 @@ public:
    *
    * No annotation is created.
    */
-  explicit Errata(code_type const& ec);
+  explicit Errata(code_type const &ec);
 
   /** Construct with an error code and generated annotation.
    *
@@ -233,7 +234,7 @@ public:
    * An annotation is created using the format @c AUTOTEXT_CODE with @a ec as the argument.
    * @see AUTOTEXT_CODE
    */
-  explicit Errata(code_type const& ec, AutoText);
+  explicit Errata(code_type const &ec, AutoText);
 
   /** Construct with a severity.
    *
@@ -270,7 +271,7 @@ public:
    * The annotation uses the format @c AUTOTEXT_SEVERITY_CODE with arguments @a severity , @a ec
    * @see AUTOTEXT_SEVERITY_CODE
    */
-  explicit Errata(code_type const& ec, Severity severity, AutoText auto_text);
+  explicit Errata(code_type const &ec, Severity severity, AutoText auto_text);
 
   /** Constructor.
    *
@@ -1030,7 +1031,7 @@ inline Errata::Errata(self_type &&that) noexcept {
   std::swap(_data, that._data);
 }
 
-inline Errata::Errata(code_type const& ec) {
+inline Errata::Errata(code_type const &ec) {
   this->data()->_code = ec;
 }
 
@@ -1044,7 +1045,7 @@ inline Errata::Errata(const code_type &ec, Severity severity) {
   d->_code     = ec;
 }
 
-inline Errata::Errata(code_type const& ec, AutoText) {
+inline Errata::Errata(code_type const &ec, AutoText) {
   this->data()->_code = ec;
   this->note(AUTOTEXT_CODE, ec);
 }
diff --git a/code/include/swoc/TextView.h b/code/include/swoc/TextView.h
index ed342ee..41ed9ba 100644
--- a/code/include/swoc/TextView.h
+++ b/code/include/swoc/TextView.h
@@ -41,9 +41,14 @@ class TextView;
  */
 class CharSet {
   using self_type = CharSet;
+
 public:
-  constexpr CharSet(TextView const& chars);
-  bool operator () (u_char idx) const { return _chars[idx]; }
+  constexpr CharSet(TextView const &chars);
+  bool
+  operator()(u_char idx) const {
+    return _chars[idx];
+  }
+
 protected:
   std::bitset<256> _chars;
 };
@@ -1081,8 +1086,8 @@ double svtod(TextView text, TextView *parsed = nullptr);
 // simpler plain @c TextView ? Because otherwise Doxygen can't match up the declaration and
 // definition and the reference documentation is messed up. Sigh.
 
-inline constexpr CharSet::CharSet(TextView const & chars) {
-  for ( auto c : chars) {
+inline constexpr CharSet::CharSet(TextView const &chars) {
+  for (auto c : chars) {
     _chars[u_char(c)] = true;
   }
 }
@@ -1510,7 +1515,7 @@ TextView::trim(char c) {
 
 inline TextView &
 TextView::ltrim(CharSet const &delimiters) {
-  const char *spot = this->data();
+  const char *spot  = this->data();
   const char *limit = this->data_end();
 
   while (spot < limit && delimiters(*spot)) {
diff --git a/code/src/bw_format.cc b/code/src/bw_format.cc
index 83f1689..60ceacc 100644
--- a/code/src/bw_format.cc
+++ b/code/src/bw_format.cc
@@ -905,7 +905,7 @@ bwformat(BufferWriter &w, bwf::Spec const &spec, std::error_code const &ec) {
 
   // This provides convenient safe access to the errno short name array.
   static const swoc::bwf::Format number_fmt{"[{}]"_sv}; // numeric value format.
-  if (spec.has_numeric_type()) {  // if numeric type, print just the numeric part.
+  if (spec.has_numeric_type()) {                        // if numeric type, print just the numeric part.
     bwformat(w, spec, ec.value());
   } else {
     if ((&ec.category() == G_CAT || &ec.category() == S_CAT) && swoc::ERRNO_RANGE.contains(ec.value())) {
diff --git a/unit_tests/test_Errata.cc b/unit_tests/test_Errata.cc
index da766ca..8ce82a5 100644
--- a/unit_tests/test_Errata.cc
+++ b/unit_tests/test_Errata.cc
@@ -30,11 +30,7 @@ std::array<swoc::TextView, 5> Severity_Names{
   {"Debug", "Diag", "Info", "Warn", "Error"}
 };
 
-enum class ECode {
-  ALPHA = 1,
-  BRAVO,
-  CHARLIE
-};
+enum class ECode { ALPHA = 1, BRAVO, CHARLIE };
 
 struct e_category : std::error_category {
   const char *name() const noexcept override;
@@ -44,27 +40,24 @@ struct e_category : std::error_category {
 e_category e_cat;
 
 const char *
-e_category::name() const noexcept
-{
+e_category::name() const noexcept {
   return "libswoc";
 }
 
 std::string
-e_category::message(int ev) const
-{
-  static swoc::Lexicon<ECode> lexicon {
-      {
-        { ECode::ALPHA , "Alpha" },
-        { ECode::BRAVO, "Bravo"},
-        { ECode::CHARLIE, "Charlie"}
-      }
-      , "Code out of range"
+e_category::message(int ev) const {
+  static swoc::Lexicon<ECode> lexicon{
+    {{ECode::ALPHA, "Alpha"}, {ECode::BRAVO, "Bravo"}, {ECode::CHARLIE, "Charlie"}},
+    "Code out of range"
   };
 
   return std::string(lexicon[ECode(ev)]);
 }
 
-inline std::error_code ecode(ECode c) { return { int(c) , e_cat }; }
+inline std::error_code
+ecode(ECode c) {
+  return {int(c), e_cat};
+}
 
 std::string ErrataSinkText;
 


(trafficserver-libswoc) 01/04: Errata: Enable auto generated annotations.

Posted by bn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

bneradt pushed a commit to branch charset
in repository https://gitbox.apache.org/repos/asf/trafficserver-libswoc.git

commit 3632bbcbcfd146fc6480a7226872e8e5b00a49f5
Author: Alan M. Carroll <am...@apache.org>
AuthorDate: Tue Nov 21 16:37:06 2023 -0600

    Errata: Enable auto generated annotations.
---
 code/include/swoc/Errata.h   | 87 +++++++++++++++++++++++++++++++++++++++-----
 code/include/swoc/TextView.h | 86 ++++++++++++++++++++++++++++++++-----------
 code/src/bw_format.cc        |  4 +-
 unit_tests/test_Errata.cc    | 46 +++++++++++++++++++++++
 4 files changed, 190 insertions(+), 33 deletions(-)

diff --git a/code/include/swoc/Errata.h b/code/include/swoc/Errata.h
index 59ec0d4..8d3c129 100644
--- a/code/include/swoc/Errata.h
+++ b/code/include/swoc/Errata.h
@@ -47,6 +47,7 @@
 #include "swoc/MemSpan.h"
 #include "swoc/MemArena.h"
 #include "swoc/bwf_base.h"
+#include "swoc/bwf_std.h"
 #include "swoc/IntrusiveDList.h"
 
 namespace swoc { inline namespace SWOC_VERSION_NS {
@@ -85,6 +86,10 @@ public:
   /// This defaults to zero and no filtering is done unless it is overwritten.
   static Severity FILTER_SEVERITY;
 
+  static inline TextView AUTOTEXT_SEVERITY = "{}"; ///< Format for auto generated annotation with severity.
+  static inline TextView AUTOTEXT_CODE = "{}"; ///< Format for auto generated annotation with error code.
+  static inline TextView AUTOTEXT_SEVERITY_CODE = "{}: {}"; ///< Format for auto generate annotation with error code and severity.
+
   /// Mapping of severity to string.
   /// Values larger than the span size will be rendered as numbers.
   /// Defaults to an empty span, meaning all severities will be printed as integers.
@@ -196,28 +201,77 @@ protected:
     bool _glue_final_p                      = true; ///< Add glue after the last annotation?
 
     std::optional<Severity> _severity;     ///< Severity.
-    code_type _code{Errata::DEFAULT_CODE}; ///< Message code / ID
+    code_type _code{DEFAULT_CODE}; ///< Message code / ID
     Container _notes;                      ///< The message stack.
     swoc::MemArena _arena;                 ///< Annotation text storage.
   };
 
 public:
+  /// Used to indicate automatically generated annotation text.
+  static constexpr struct AutoText {} AUTO {};
+
   /// Default constructor - empty errata, very fast.
   Errata()                      = default;
-  Errata(self_type const &that) = delete;               ///< No constant copy construction.
+  Errata(self_type const &that) = delete;               ///< No copy construction.
   Errata(self_type &&that) noexcept;                    ///< Move constructor.
-  self_type &operator=(self_type const &that) = delete; // no copy assignemnt.
+  self_type &operator=(self_type const &that) = delete; // no copy assignment.
   self_type &operator=(self_type &&that);               ///< Move assignment.
   ~Errata();                                            ///< Destructor.
 
+  /** Construct with an error code.
+   *
+   * @param ec Error code
+   *
+   * No annotation is created.
+   */
+  explicit Errata(code_type const& ec);
+
+  /** Construct with an error code and generated annotation.
+   *
+   * @param ec Error code
+   *
+   * An annotation is created using the format @c AUTOTEXT_CODE with @a ec as the argument.
+   * @see AUTOTEXT_CODE
+   */
+  explicit Errata(code_type const& ec, AutoText);
+
   /** Construct with a severity.
    *
    * @param severity Severity.
    *
-   * No annotations are created.
+   * No annotation is created.
    */
   explicit Errata(Severity severity);
 
+  /** Construct with a severity.
+   *
+   * @param severity Severity.
+   *
+   * An annotation is created using the format @c AUTO_TEXT_SEVERITY with @a severity as the argument.
+   * @see AUTOTEXT_SEVERITY
+   */
+  explicit Errata(Severity severity, AutoText);
+
+  /** Construct with error code and severity.
+   *
+   * @param ec Error code.
+   * @param severity Severity.
+   *
+   * No annotation is created.
+   */
+  Errata(code_type const &ec, Severity severity);
+
+  /** Construct with a severity and error code.
+   *
+   * @param severity Severity.
+   * @param ec Error code.
+   * @param auto_text If present, generate an annotation.
+   *
+   * The annotation uses the format @c AUTOTEXT_SEVERITY_CODE with arguments @a severity , @a ec
+   * @see AUTOTEXT_SEVERITY_CODE
+   */
+  explicit Errata(code_type const& ec, Severity severity, AutoText auto_text);
+
   /** Constructor.
    *
    * @param code Error code.
@@ -613,9 +667,6 @@ public:
   std::ostream &write(std::ostream &out) const;
 
 protected:
-  /// Construct with code and severity, but no annotations.
-  Errata(code_type const &code, Severity severity);
-
   /// Implementation instance.
   /// @internal Because this is used with a self-containing @c MemArena standard smart pointers do not
   /// work correctly. Instead the @c clear method must be used to release the memory.
@@ -979,14 +1030,32 @@ inline Errata::Errata(self_type &&that) noexcept {
   std::swap(_data, that._data);
 }
 
+inline Errata::Errata(code_type const& ec) {
+  this->data()->_code = ec;
+}
+
 inline Errata::Errata(Severity severity) {
   this->data()->_severity = severity;
 }
 
-inline Errata::Errata(const code_type &code, Severity severity) {
+inline Errata::Errata(const code_type &ec, Severity severity) {
   auto d       = this->data();
   d->_severity = severity;
-  d->_code     = code;
+  d->_code     = ec;
+}
+
+inline Errata::Errata(code_type const& ec, AutoText) {
+  this->data()->_code = ec;
+  this->note(AUTOTEXT_CODE, ec);
+}
+
+inline Errata::Errata(Severity severity, AutoText) {
+  this->data()->_severity = severity;
+  this->note(AUTOTEXT_SEVERITY, severity);
+}
+
+inline Errata::Errata(const code_type &ec, Severity severity, AutoText) : Errata(ec, severity) {
+  this->note(AUTOTEXT_SEVERITY_CODE, severity, ec);
 }
 
 inline Errata::Errata(const code_type &code, Severity severity, const std::string_view &text) : Errata(code, severity) {
diff --git a/code/include/swoc/TextView.h b/code/include/swoc/TextView.h
index b665f54..ed342ee 100644
--- a/code/include/swoc/TextView.h
+++ b/code/include/swoc/TextView.h
@@ -36,6 +36,18 @@ namespace swoc { inline namespace SWOC_VERSION_NS {
 
 class TextView;
 
+/** A set of characters.
+ *
+ */
+class CharSet {
+  using self_type = CharSet;
+public:
+  constexpr CharSet(TextView const& chars);
+  bool operator () (u_char idx) const { return _chars[idx]; }
+protected:
+  std::bitset<256> _chars;
+};
+
 /** A read only view of a contiguous piece of memory.
 
     A @c TextView does not own the memory to which it refers, it is simply a view of part of some
@@ -324,6 +336,12 @@ public:
    */
   self_type &ltrim(char c);
 
+  /** Remove bytes from the start of the view that are in @a delimiters.
+   *
+   * @return @a this
+   */
+  self_type &ltrim(CharSet const &delimiters);
+
   /** Remove bytes from the start of the view that are in @a delimiters.
    *
    * @return @a this
@@ -350,6 +368,12 @@ public:
    */
   self_type &rtrim(char c);
 
+  /** Remove bytes from the end of the view that are in @a delimiters.
+   *
+   * @return @a this
+   */
+  self_type &rtrim(CharSet const &delimiters);
+
   /** Remove bytes from the end of the view that are in @a delimiters.
    * @return @a this
    */
@@ -369,6 +393,11 @@ public:
    */
   self_type &trim(char c);
 
+  /** Remove bytes from the start and end of the view that are in @a delimiters.
+   * @return @a this
+   */
+  self_type &trim(CharSet const &delimiters);
+
   /** Remove bytes from the start and end of the view that are in @a delimiters.
    * @return @a this
    */
@@ -1052,6 +1081,12 @@ double svtod(TextView text, TextView *parsed = nullptr);
 // simpler plain @c TextView ? Because otherwise Doxygen can't match up the declaration and
 // definition and the reference documentation is messed up. Sigh.
 
+inline constexpr CharSet::CharSet(TextView const & chars) {
+  for ( auto c : chars) {
+    _chars[u_char(c)] = true;
+  }
+}
+
 // === TextView Implementation ===
 /// @cond TextView_INTERNAL
 // Doxygen doesn't match these up well due to various type and template issues.
@@ -1474,62 +1509,71 @@ TextView::trim(char c) {
 }
 
 inline TextView &
-TextView::ltrim(std::string_view const &delimiters) {
-  std::bitset<256> valid;
-  this->init_delimiter_set(delimiters, valid);
-  const char *spot;
-  const char *limit;
+TextView::ltrim(CharSet const &delimiters) {
+  const char *spot = this->data();
+  const char *limit = this->data_end();
 
-  for (spot = this->data(), limit = this->data_end(); spot < limit && valid[static_cast<uint8_t>(*spot)]; ++spot)
-    ;
+  while (spot < limit && delimiters(*spot)) {
+    ++spot;
+  }
   this->remove_prefix(spot - this->data());
 
   return *this;
 }
 
+inline TextView &
+TextView::ltrim(std::string_view const &delimiters) {
+  return this->ltrim(CharSet(delimiters));
+}
+
 inline TextView &
 TextView::ltrim(const char *delimiters) {
-  return this->ltrim(std::string_view(delimiters));
+  return this->ltrim(CharSet(delimiters));
 }
 
 inline TextView &
-TextView::rtrim(std::string_view const &delimiters) {
-  std::bitset<256> valid;
-  this->init_delimiter_set(delimiters, valid);
+TextView::rtrim(CharSet const &delimiters) {
   const char *spot  = this->data_end();
   const char *limit = this->data();
-
-  while (limit < spot-- && valid[static_cast<uint8_t>(*spot)])
-    ;
+  while (limit < spot-- && delimiters(*spot)) {
+  }
 
   this->remove_suffix(this->data_end() - (spot + 1));
   return *this;
 }
 
 inline TextView &
-TextView::trim(std::string_view const &delimiters) {
-  std::bitset<256> valid;
-  this->init_delimiter_set(delimiters, valid);
+TextView::rtrim(std::string_view const &delimiters) {
+  return this->rtrim(CharSet(delimiters));
+}
+
+inline TextView &
+TextView::trim(CharSet const &delimiters) {
   const char *spot;
   const char *limit;
 
   // Do this explicitly, so we don't have to initialize the character set twice.
-  for (spot = this->data(), limit = this->data_end(); spot < limit && valid[static_cast<uint8_t>(*spot)]; ++spot)
+  for (spot = this->data(), limit = this->data_end(); spot < limit && delimiters(*spot); ++spot)
     ;
   this->remove_prefix(spot - this->data());
 
   spot  = this->data_end();
   limit = this->data();
-  while (limit < spot-- && valid[static_cast<uint8_t>(*spot)])
-    ;
+  while (limit < spot-- && delimiters(*spot)) {
+  }
   this->remove_suffix(this->data_end() - (spot + 1));
 
   return *this;
 }
 
+inline TextView &
+TextView::trim(std::string_view const &delimiters) {
+  return this->trim(CharSet(delimiters));
+}
+
 inline TextView &
 TextView::trim(const char *delimiters) {
-  return this->trim(std::string_view(delimiters));
+  return this->trim(CharSet(delimiters));
 }
 
 template <typename F>
diff --git a/code/src/bw_format.cc b/code/src/bw_format.cc
index de62200..83f1689 100644
--- a/code/src/bw_format.cc
+++ b/code/src/bw_format.cc
@@ -905,8 +905,7 @@ bwformat(BufferWriter &w, bwf::Spec const &spec, std::error_code const &ec) {
 
   // This provides convenient safe access to the errno short name array.
   static const swoc::bwf::Format number_fmt{"[{}]"_sv}; // numeric value format.
-  if (spec.has_numeric_type()) {
-    // if numeric type, print just the numeric part.
+  if (spec.has_numeric_type()) {  // if numeric type, print just the numeric part.
     bwformat(w, spec, ec.value());
   } else {
     if ((&ec.category() == G_CAT || &ec.category() == S_CAT) && swoc::ERRNO_RANGE.contains(ec.value())) {
@@ -915,7 +914,6 @@ bwformat(BufferWriter &w, bwf::Spec const &spec, std::error_code const &ec) {
       w.write(ec.message());
     }
     if (spec._type != 's' && spec._type != 'S') {
-      bwformat(w, spec, ec.value());
       w.write(' ').write('[').format(spec, ec.value()).write(']');
     }
   }
diff --git a/unit_tests/test_Errata.cc b/unit_tests/test_Errata.cc
index acb41a0..da766ca 100644
--- a/unit_tests/test_Errata.cc
+++ b/unit_tests/test_Errata.cc
@@ -10,6 +10,7 @@
 #include "swoc/bwf_std.h"
 #include "swoc/bwf_ex.h"
 #include "swoc/swoc_file.h"
+#include "swoc/Lexicon.h"
 #include "catch.hpp"
 
 using swoc::Errata;
@@ -29,6 +30,42 @@ std::array<swoc::TextView, 5> Severity_Names{
   {"Debug", "Diag", "Info", "Warn", "Error"}
 };
 
+enum class ECode {
+  ALPHA = 1,
+  BRAVO,
+  CHARLIE
+};
+
+struct e_category : std::error_category {
+  const char *name() const noexcept override;
+  std::string message(int ev) const override;
+};
+
+e_category e_cat;
+
+const char *
+e_category::name() const noexcept
+{
+  return "libswoc";
+}
+
+std::string
+e_category::message(int ev) const
+{
+  static swoc::Lexicon<ECode> lexicon {
+      {
+        { ECode::ALPHA , "Alpha" },
+        { ECode::BRAVO, "Bravo"},
+        { ECode::CHARLIE, "Charlie"}
+      }
+      , "Code out of range"
+  };
+
+  return std::string(lexicon[ECode(ev)]);
+}
+
+inline std::error_code ecode(ECode c) { return { int(c) , e_cat }; }
+
 std::string ErrataSinkText;
 
 // Call from unit test main before starting tests.
@@ -376,3 +413,12 @@ TEST_CASE("Errata Wrapper", "[libswoc][errata]") {
     REQUIRE(errata.front().text().starts_with("ni itchi - EINVAL"));
   }
 }
+
+TEST_CASE("Errata Autotext", "[libswoc][errata]") {
+  Errata a{ERRATA_WARN, Errata::AUTO};
+  REQUIRE(a.front().text() == "Warn");
+  Errata b{ecode(ECode::BRAVO), Errata::AUTO};
+  REQUIRE(b.front().text() == "Bravo [2]");
+  Errata c{ecode(ECode::ALPHA), ERRATA_ERROR, Errata::AUTO};
+  REQUIRE(c.front().text() == "Error: Alpha [1]");
+}


(trafficserver-libswoc) 04/04: Errata: fix is_ok

Posted by bn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

bneradt pushed a commit to branch charset
in repository https://gitbox.apache.org/repos/asf/trafficserver-libswoc.git

commit 7592cb629f6a5886a3364c868de9c96378e6ae8a
Author: Alan M. Carroll <am...@apache.org>
AuthorDate: Mon Nov 27 13:38:52 2023 -0600

    Errata: fix is_ok
---
 code/include/swoc/Errata.h    | 12 +++----
 code/include/swoc/swoc_meta.h | 58 ++++++++++++++++++++++++++++++
 doc/code/TextView.en.rst      | 31 +++++++++++++---
 doc/index.rst                 | 13 +++++--
 tools/ats-drop.sh             | 82 -------------------------------------------
 unit_tests/test_Errata.cc     | 11 ++++++
 unit_tests/test_meta.cc       | 24 +++++++++++++
 7 files changed, 136 insertions(+), 95 deletions(-)

diff --git a/code/include/swoc/Errata.h b/code/include/swoc/Errata.h
index 944129d..d765ef7 100644
--- a/code/include/swoc/Errata.h
+++ b/code/include/swoc/Errata.h
@@ -504,10 +504,7 @@ public:
   bool is_ok() const;
 
   /// @return If there is top level severity.
-  bool
-  has_severity() const {
-    return _data && _data->_severity.has_value();
-  }
+  bool has_severity() const;
 
   /// @return Top level severity.
   Severity severity() const;
@@ -1124,9 +1121,12 @@ Errata::assign(code_type code) -> self_type & {
   return *this;
 }
 
+inline bool Errata::has_severity() const {
+  return _data && _data->_severity.has_value();
+}
 inline auto
 Errata::severity() const -> Severity {
-  return _data ? _data->_severity.value() : DEFAULT_SEVERITY;
+  return this->has_severity() ? _data->_severity.value() : DEFAULT_SEVERITY;
 }
 
 inline auto
@@ -1142,7 +1142,7 @@ Errata::length() const {
 
 inline bool
 Errata::is_ok() const {
-  return this->empty() || _data->_severity < FAILURE_SEVERITY;
+  return nullptr == _data || this->severity() < FAILURE_SEVERITY;
 }
 
 inline const Errata::Annotation &
diff --git a/code/include/swoc/swoc_meta.h b/code/include/swoc/swoc_meta.h
index 0d987b2..5c3855e 100644
--- a/code/include/swoc/swoc_meta.h
+++ b/code/include/swoc/swoc_meta.h
@@ -237,4 +237,62 @@ template <typename... Types> struct type_list {
   template <typename T> static constexpr bool contains = is_any_of<T, Types...>::value;
 };
 
+/** Scoped value change.
+ *
+ * The purpose of this class is to change the value of a variable in a scope and then change it
+ * back when the scope is exited. The old value will be moved to a cache variable and then moved
+ * back when the instance is destructed. This is very useful to temporarily tweak global variables
+ * which having to know what the current value is.
+ *
+ * @code
+ * {
+ *   let save(var, value);
+ *   // var now has value.
+ * }
+ * // var now has original value.
+ * @endcode
+ *
+ * @tparam T Type of variable to scope.
+ */
+template <typename T> struct let {
+  using self_type = let;
+
+  let(self_type const& that) = delete;
+  self_type & operator = (self_type const&) = delete;
+
+  T &_var;  ///< Reference to scoped variable.
+  T _value; ///< Original value.
+
+  /** Construct a scope.
+   *
+   * @param var Variable to scope.
+   * @param value temporary value to assign.
+   */
+  let(T &var, T const &value);
+
+  /** Construct a scope.
+   *
+   * @param var Variable to scope.
+   * @param value temporary value to assign.
+   */
+  let(T &var, T &&value);
+
+  ~let();
+};
+
+template <typename T> let<T>::let(T &var, T const &value) : _var(var), _value(std::move(var))
+{
+  _var = value;
+}
+template <typename T> let<T>::let(T &var, T &&value) : _var(var), _value(std::move(var))
+{
+  _var = std::move(value);
+}
+
+template <typename T> let<T>::~let()
+{
+  _var = std::move(_value);
+}
+
+
 }}} // namespace swoc::SWOC_VERSION_NS::meta
diff --git a/doc/code/TextView.en.rst b/doc/code/TextView.en.rst
index 740f85d..c2ede1b 100644
--- a/doc/code/TextView.en.rst
+++ b/doc/code/TextView.en.rst
@@ -86,6 +86,18 @@ E.g. the code to write a simple hash function [#]_ could be
       return hash;
    }
 
+Although alternatively, this can be done in a non-modifying way.
+
+.. code-block:: cpp
+
+   void hasher(TextView v) {
+      size_t hash = 0;
+      for ( auto c : v) {
+         hash = hash * 13 + c;
+      }
+      return hash;
+   }
+
 Because |TV| inherits from :code:`std::string_view` it can also be used as a container for range
 :code:`for` loops.
 
@@ -97,10 +109,21 @@ Because |TV| inherits from :code:`std::string_view` it can also be used as a con
       return hash;
    }
 
+The first approach enables dropping out of the loop on some condition with the view updated to
+no longer contain processed characters, making restart or other processing simple.
+
 The standard functions :code:`strcmp`, :code:`memcmp`, code:`memcpy`, and :code:`strcasecmp` are
 overloaded for |TV| so that a |TV| can be used as if it were a C-style string. The size is is taken
 from the |TV| and doesn't need to be passed in explicitly.
 
+.. class:: CharSet
+
+   :libswoc:`Reference documentation <swoc::CharSet>`.
+
+This is a simple class that contains a set of characters. This is intended primarily to make
+parsing faster and simpler. Rather than checking a list of delimiters the character can be checked
+with a single `std::bitset` lookup.
+
 Basic Operations
 ================
 
@@ -130,7 +153,7 @@ Searching
 ---------
 
 Because |TV| is a subclass of :code:`std::string_view` all of its search method work on a |TV|. The
-only search methods provided beyond those are :libswoc:`TextView::find_if` and
+only search methods provided beyond those in :code:`std::string` are :libswoc:`TextView::find_if` and
 :libswoc:`TextView::rfind_if` which search the view by a predicate. The predicate takes a single
 :code:`char` argument and returns a :code:`bool`. The search terminates on the first character for
 which the predicate returns :code:`true`.
@@ -281,12 +304,12 @@ developing |TV| parsing.
 
 The first was to minimize the need to allocate memory to hold intermediate results. For this reason, the normal
 style of use is a streaming / incremental one, where tokens are extracted from a source one by one
-and placed in |TV| instances, with the orignal source |TV| being reduced by each extraction until
+and placed in |TV| instances, with the original source |TV| being reduced by each extraction until
 it is empty.
 
 The second was to minimize cut and paste coding. Typical C or C++ parsing logic consists mostly of
 very generic code to handle pointer and size updates. The point of |TV| is to automate all of that
-so the resulting code is focused entirely on the parsing logic, not boiler plate string or view manipulation.
+yielding code focused entirely on the parsing logic, not boiler plate string or view manipulation.
 It is a common occurrence to not get such code exactly correct leading to hard to track bugs. Use
 of |TV| eliminates those problems.
 
@@ -313,7 +336,7 @@ are very cheap to copy. This is essentially the same as having a current pointer
 and checking for :code:`current >= end` except :code:`TextView` does all the work, leading to
 simpler and less buggy code.
 
-White space is dropped because of the calls to :code:`ltrim_if` and `rtrim_if`. By calling in the
+White space is dropped because of the calls to :code:`ltrim_if` and :code:`rtrim_if`. By calling in the
 loop condition, the loop exits if the remaining text is only whitespace and no token is processed.
 Alternatively :code:`trim_if` could be used after extraction. The performance will be *slightly*
 better because although :code:`trim_if` calls :code:`ltrim_if` and :code:`rtrim_if`, a final
diff --git a/doc/index.rst b/doc/index.rst
index b6f573e..f63bb30 100644
--- a/doc/index.rst
+++ b/doc/index.rst
@@ -18,9 +18,16 @@ Solid Wall of C++
 *****************
 
 The Solid Wall of C++ library is a collection of C++ classes and utilities. This code evolved out of
-infrastructure used in `Apache Traffic Server <https://trafficserver.apache.org>`__. The utilities
-had become useful enough there were requests to be able to use them in ATS plugins and other,
-unrelated projects. Hence this library. I hope you find it as useful as I have.
+infrastructure used in `Apache Traffic Server <https://trafficserver.apache.org>`__ as I strove to combine
+functionality, ease of use, and performance.
+The utilities had become useful enough there were requests to be able to use them in ATS plugins and other,
+unrelated projects. Hence this library. After much production use, this library has been imported back
+in to Traffic Server and can be used there in the core or any plugin.
+I hope you find it as useful as I have.
+
+Most of the library is dedicated to convenience, such as :class:`TextView` which provides Python like
+string manipulation on top of :code:`std::string_view`, and performance, such as :class:`IPSpace` which
+enables very fast IP address range storage.
 
 .. toctree::
    :maxdepth: 1
diff --git a/tools/ats-drop.sh b/tools/ats-drop.sh
index 839a07f..0048b29 100644
--- a/tools/ats-drop.sh
+++ b/tools/ats-drop.sh
@@ -23,9 +23,6 @@ else
   mkdir -p ${TARGET_SRC_DIR}
 fi
 
-cp code/CMakeLists.txt ${TARGET_BASE_DIR}/swoc
-(cd ${ATS}/${BASE_PATH}/swoc ; sed -i -e '/^if (LIBSWOC_INSTALL)/,/^endif/d' CMakeLists.txt ; git add CMakeLists.txt)
-
 cp code/src/*.cc ${TARGET_SRC_DIR}
 (cd ${ATS}; git add ${BASE_PATH}/swoc/CMakeLists.txt ; git add ${SRC_PATH}/*.cc)
 
@@ -48,82 +45,3 @@ cp code/include/swoc/*.h ${TARGET_INC_DIR}
 cp code/include/swoc/ext/*.h ${TARGET_INC_DIR}/ext
 cp code/include/swoc/ext/HashFNV.h ${TARGET_INC_DIR}
 (cd ${ATS}; git add ${INC_PATH}/*.h ; git add ${INC_PATH}/ext/*.h)
-
-# Build the source
-cat <<'TEXT' > ${ATS}/${BASE_PATH}/swoc/Makefile.am
-# swoc Makefile.am
-#
-#  Licensed to the Apache Software Foundation (ASF) under one
-#  or more contributor license agreements.  See the NOTICE file
-#  distributed with this work for additional information
-#  regarding copyright ownership.  The ASF licenses this file
-#  to you under the Apache License, Version 2.0 (the
-#  "License"); you may not use this file except in compliance
-#  with the License.  You may obtain a copy of the License at
-#
-#      http://www.apache.org/licenses/LICENSE-2.0
-#
-#  Unless required by applicable law or agreed to in writing, software
-#  distributed under the License is distributed on an "AS IS" BASIS,
-#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-#  See the License for the specific language governing permissions and
-#  limitations under the License.
-
-lib_LTLIBRARIES = libtsswoc.la
-
-library_includedir=$(includedir)/swoc
-
-AM_CPPFLAGS += @SWOC_INCLUDES@
-
-libtsswoc_la_LDFLAGS = @AM_LDFLAGS@ -no-undefined -release 1.5.8
-libtsswoc_la_SOURCES = \
-	src/ArenaWriter.cc  src/bw_format.cc  src/bw_ip_format.cc  src/Errata.cc  src/MemArena.cc  src/RBTree.cc  src/swoc_file.cc  src/swoc_ip.cc  src/TextView.cc src/string_view_util.cc
-
-if EXPORT_SWOC_HEADERS
-library_include_HEADERS = \
-        include/swoc/ArenaWriter.h \
-        include/swoc/BufferWriter.h \
-        include/swoc/bwf_base.h \
-        include/swoc/bwf_ex.h \
-        include/swoc/bwf_fwd.h \
-        include/swoc/bwf_ip.h \
-        include/swoc/bwf_std.h \
-        include/swoc/DiscreteRange.h \
-        include/swoc/Errata.h \
-        include/swoc/IntrusiveDList.h \
-        include/swoc/IntrusiveHashMap.h \
-        include/swoc/IPAddr.h \
-        include/swoc/IPEndpoint.h \
-        include/swoc/IPRange.h \
-        include/swoc/IPSrv.h \
-        include/swoc/Lexicon.h \
-        include/swoc/MemArena.h \
-        include/swoc/MemSpan.h \
-        include/swoc/RBTree.h \
-        include/swoc/Scalar.h \
-        include/swoc/swoc_file.h \
-        include/swoc/swoc_ip.h \
-        include/swoc/swoc_meta.h \
-        include/swoc/swoc_version.h\
-        include/swoc/string_view_util.h \
-        include/swoc/TextView.h \
-        include/swoc/Vectray.h \
-        include/swoc/HashFNV.h
-endif
-
-clean-local:
-
-clang-tidy-local: $(DIST_SOURCES)
-	$(CXX_Clang_Tidy)
-TEXT
-(cd ${ATS} ; git add ${BASE_PATH}/swoc/Makefile.am)
-
-if ! grep -q swoc ${ATS}/configure.ac ; then
-  sed -i -e 's!lib/yamlcpp/Makefile!lib/swoc/Makefile\n  &!' ${ATS}/configure.ac
-  (cd ${ATS} ; git add configure.ac)
-fi
-
-if ! grep -q swoc ${ATS}/${BASE_PATH}/Makefile.am ; then
-  sed -i -e '/SUBDIRS =/s!$! swoc!' ${ATS}/${BASE_PATH}/Makefile.am
-  (cd ${ATS} ; git add ${BASE_PATH}/Makefile.am)
-fi
diff --git a/unit_tests/test_Errata.cc b/unit_tests/test_Errata.cc
index 8ce82a5..e46bd07 100644
--- a/unit_tests/test_Errata.cc
+++ b/unit_tests/test_Errata.cc
@@ -414,4 +414,15 @@ TEST_CASE("Errata Autotext", "[libswoc][errata]") {
   REQUIRE(b.front().text() == "Bravo [2]");
   Errata c{ecode(ECode::ALPHA), ERRATA_ERROR, Errata::AUTO};
   REQUIRE(c.front().text() == "Error: Alpha [1]");
+
+  Errata d{ERRATA_ERROR};
+  REQUIRE_FALSE(d.is_ok());
+  Errata e{ERRATA_INFO};
+  REQUIRE(e.is_ok());
+  Errata f{ecode(ECode::BRAVO)};
+  REQUIRE_FALSE(f.is_ok());
+  // Change properties but need to restore them for other tests.
+  swoc::meta::let g1(Errata::DEFAULT_SEVERITY, ERRATA_WARN);
+  swoc::meta::let g2(Errata::FAILURE_SEVERITY, ERRATA_ERROR);
+  REQUIRE(f.is_ok());
 }
diff --git a/unit_tests/test_meta.cc b/unit_tests/test_meta.cc
index 55c9c3e..782bae4 100644
--- a/unit_tests/test_meta.cc
+++ b/unit_tests/test_meta.cc
@@ -93,3 +93,27 @@ TEST_CASE("Meta vary", "[meta][vary]") {
   v = "956"_tv;
   REQUIRE(std::visit(visitor, v) == 956);
 }
+
+TEST_CASE("Meta let", "[meta][let]") {
+  using swoc::meta::let;
+
+  unsigned x = 56;
+  {
+    REQUIRE(x == 56);
+    let guard(x, unsigned(3136));
+    REQUIRE(x == 3136);
+    // auto bogus = guard; // should not compile.
+  }
+  REQUIRE(x == 56);
+
+  // Checking move semantics - avoid reallocating the original string.
+  std::string s{"Evil Dave Rulz With An Iron Keyboard"}; // force allocation.
+  auto sptr = s.data();
+  {
+    char const * text = "Twas brillig and the slithy toves";
+    let guard(s, std::string(text));
+    REQUIRE(s == text);
+    REQUIRE(s.data() != sptr);
+  }
+  REQUIRE(s.data() == sptr);
+}