This is an automated email from the ASF dual-hosted git repository.
amc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/master by this push:
new 6be18d5 IpMap: Add bwformat.
6be18d5 is described below
commit 6be18d59f9c6169ba42404e8637e35b63fe1dbcf
Author: Alan M. Carroll <[email protected]>
AuthorDate: Wed Feb 20 11:54:37 2019 -0600
IpMap: Add bwformat.
---
include/tscore/IpMap.h | 22 ++++++++++---
src/tscore/IpMap.cc | 66 +++++++++++++++++++++++++++++--------
src/tscore/unit_tests/test_IpMap.cc | 10 ++++++
3 files changed, 79 insertions(+), 19 deletions(-)
diff --git a/include/tscore/IpMap.h b/include/tscore/IpMap.h
index ae57329..754f506 100644
--- a/include/tscore/IpMap.h
+++ b/include/tscore/IpMap.h
@@ -32,6 +32,7 @@
#include "tscore/ink_inet.h"
#include "tscpp/util/IntrusiveDList.h"
#include "tscore/ink_assert.h"
+#include "tscore/BufferWriterForward.h"
namespace ts
{
@@ -194,7 +195,7 @@ public:
Node *_node = nullptr; //!< Current node.
};
- IpMap(); ///< Default constructor.
+ IpMap() = default; ///< Default constructor.
IpMap(self_type const &that) = delete;
IpMap(self_type &&that) noexcept;
~IpMap(); ///< Destructor.
@@ -362,9 +363,13 @@ public:
*/
void validate();
- /// Print all spans.
- /// @return This map.
- // self& print();
+ /** Generate formatted output.
+ *
+ * @param w Destination of formatted output.
+ * @param spec Formatting specification.
+ * @return @a w
+ */
+ ts::BufferWriter &describe(ts::BufferWriter &w, ts::BWFSpec const &spec)
const;
protected:
/// Force the IPv4 map to exist.
@@ -487,4 +492,11 @@ inline IpMap::iterator::pointer
IpMap::iterator::operator->() const
return _node;
}
-inline IpMap::IpMap() : _m4(nullptr), _m6(nullptr) {}
+namespace ts
+{
+inline BufferWriter &
+bwformat(BufferWriter &w, BWFSpec const &spec, IpMap const &map)
+{
+ return map.describe(w, spec);
+}
+} // namespace ts
diff --git a/src/tscore/IpMap.cc b/src/tscore/IpMap.cc
index d2583f2..4a51962 100644
--- a/src/tscore/IpMap.cc
+++ b/src/tscore/IpMap.cc
@@ -45,6 +45,7 @@
#include "tscore/IpMap.h"
#include "tscore/ink_inet.h"
+#include "tscore/BufferWriter.h"
namespace ts
{
@@ -226,9 +227,13 @@ namespace detail
/// @return The number of distinct ranges.
size_t count() const;
- /// Print all spans.
- /// @return This map.
- self_type &print();
+ /** Generate formatted output.
+ *
+ * @param w Destination of the output.
+ * @param spec Format specification.
+ * @return @a w.
+ */
+ ts::BufferWriter &describe(ts::BufferWriter &w, ts::BWFSpec const &spec)
const;
// Helper methods.
N *
@@ -731,18 +736,21 @@ namespace detail
}
template <typename N>
- IpMapBase<N> &
- IpMapBase<N>::print()
+ ts::BufferWriter &
+ IpMapBase<N>::describe(ts::BufferWriter &w, ts::BWFSpec const &spec) const
{
-#if 0
- for ( Node* n = _list.head() ; n ; n = n->_next ) {
- std::cout
- << n << ": " << n->_min << '-' << n->_max << " [" << n->_data << "] "
- << (n->_color == Node::BLACK ? "Black " : "Red ") << "P=" <<
n->_parent << " L=" << n->_left << " R=" << n->_right
- << std::endl;
- }
-#endif
- return *this;
+ auto pos = w.extent();
+ for (auto const &rb_node : _list) {
+ N const &n{static_cast<N const &>(rb_node)};
+ if (w.extent() > pos) {
+ w.write(',');
+ }
+ w.print("{::a}-{::a}={}", n.min(), n.max(), n._data);
+ if (std::string_view::npos != spec._ext.find('x')) {
+ w.print("[{};^{};<{};>{}]", n._color == N::BLACK ? "Black" : "Red",
n._parent, n._left, n._right);
+ }
+ }
+ return w;
}
template <typename N> IpMapBase<N>::~IpMapBase() { this->clear(); }
@@ -1090,6 +1098,14 @@ namespace detail
friend class ::IpMap;
};
} // namespace detail
+
+template <typename N>
+inline BufferWriter &
+bwformat(BufferWriter &w, BWFSpec const &spec, detail::IpMapBase<N> const &map)
+{
+ return map.describe(w, spec);
+}
+
} // namespace ts
//----------------------------------------------------------------------------
IpMap::IpMap(IpMap::self_type &&that) noexcept : _m4(that._m4), _m6(that._m6)
@@ -1290,5 +1306,27 @@ IpMap::iterator::operator--()
return *this;
}
+ts::BufferWriter &
+IpMap::describe(ts::BufferWriter &w, ts::BWFSpec const &spec) const
+{
+ w.write("IPv4 ");
+ if (_m4) {
+ bwformat(w, spec, *_m4);
+ } else {
+ w.write("N/A");
+ }
+ w.write("\n");
+
+ w.write("IPv6 ");
+ if (_m6) {
+ bwformat(w, spec, *_m6);
+ } else {
+ w.write("N/A");
+ }
+ w.write("\n");
+
+ return w;
+}
+
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
diff --git a/src/tscore/unit_tests/test_IpMap.cc
b/src/tscore/unit_tests/test_IpMap.cc
index 20a8231..07debf8 100644
--- a/src/tscore/unit_tests/test_IpMap.cc
+++ b/src/tscore/unit_tests/test_IpMap.cc
@@ -24,6 +24,7 @@
#include "tscore/IpMap.h"
#include <sstream>
#include <catch.hpp>
+#include <tscore/BufferWriter.h>
std::ostream &
operator<<(std::ostream &s, IpEndpoint const &addr)
@@ -620,4 +621,13 @@ TEST_CASE("IpMap CloseIntersection", "[libts][ipmap]")
CHECK_THAT(m2, IsMarkedWith(c_3_m, markC));
CHECK_THAT(m2, IsMarkedWith(d_2_l, markD));
CHECK(m2.count() == 13);
+
+#if 0
+ ts::LocalBufferWriter<1024> w;
+ std::cout << "Basic map dump" << std::endl;
+ std::cout << w.print("{}", m2).view() << std::endl;
+ w.reset();
+ std::cout << "With tree detail" << std::endl;
+ std::cout << w.print("{::x}", m2).view() << std::endl;
+#endif
};