You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by am...@apache.org on 2018/04/12 22:41:45 UTC

[trafficserver] branch master updated: bwformat: Add support for bool, and fix center align bug.

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 8e1cd19  bwformat: Add support for bool, and fix center align bug.
8e1cd19 is described below

commit 8e1cd19a5fb2ef298cf3265adec0840456ea5cf9
Author: Alan M. Carroll <am...@apache.org>
AuthorDate: Sun Mar 25 15:51:50 2018 -0500

    bwformat: Add support for bool, and fix center align bug.
---
 lib/ts/BufferWriter.h                        | 13 ++++++++++++
 lib/ts/BufferWriterFormat.cc                 | 31 ++++++++++++++--------------
 lib/ts/BufferWriterForward.h                 |  2 +-
 lib/ts/unit-tests/test_BufferWriterFormat.cc | 27 ++++++++++++++++++++++++
 4 files changed, 57 insertions(+), 16 deletions(-)

diff --git a/lib/ts/BufferWriter.h b/lib/ts/BufferWriter.h
index 061464c..b9fcbe6 100644
--- a/lib/ts/BufferWriter.h
+++ b/lib/ts/BufferWriter.h
@@ -654,6 +654,19 @@ bwformat(BufferWriter &w, BWFSpec const &, char c)
   return w.write(c);
 }
 
+inline BufferWriter &
+bwformat(BufferWriter &w, BWFSpec const &spec, bool f)
+{
+  if ('s' == spec._type) {
+    w.write(f ? "true"_sv : "false"_sv);
+  } else if ('S' == spec._type) {
+    w.write(f ? "TRUE"_sv : "FALSE"_sv);
+  } else {
+    bw_fmt::Format_Integer(w, spec, static_cast<uintmax_t>(f), false);
+  }
+  return w;
+}
+
 template <size_t N>
 BufferWriter &
 bwformat(BufferWriter &w, BWFSpec const &spec, const char (&a)[N])
diff --git a/lib/ts/BufferWriterFormat.cc b/lib/ts/BufferWriterFormat.cc
index 80d5e16..5cd1dc4 100644
--- a/lib/ts/BufferWriterFormat.cc
+++ b/lib/ts/BufferWriterFormat.cc
@@ -227,25 +227,17 @@ namespace bw_fmt
         }
         break;
       case BWFSpec::Align::CENTER:
-        d2 = (delta + 1) / 2;
-        if (d2 > 1) {
-          dst = base + d2; // move existing content to here.
-          if (dst < limit) {
-            last = dst + size; // amount of data to move.
-            if (last > limit) {
-              last = limit;
-            }
-            std::memmove(dst, base, last - dst);
-          }
-          dst  = base + size + d2;
-          last = base + delta / 2;
+        d2 = (delta + 1) / 2; // always > 0 because min > extent
+        // Move the original content right to make space to fill on the left.
+        dst = base + d2; // move existing content to here.
+        if (dst < limit) {
+          last = dst + size; // amount of data to move.
           if (last > limit) {
             last = limit;
           }
-          while (dst < last) {
-            *dst++ = spec._fill;
-          }
+          std::memmove(dst, base, last - dst); // move content.
         }
+        // Left fill.
         dst  = base;
         last = base + d2;
         if (last > limit) {
@@ -254,6 +246,15 @@ namespace bw_fmt
         while (dst < last) {
           *dst++ = spec._fill;
         }
+        // Right fill.
+        dst += size;
+        last = dst + delta / 2; // round down
+        if (last > limit) {
+          last = limit;
+        }
+        while (dst < last) {
+          *dst++ = spec._fill;
+        }
         break;
       default:
         // Everything else is equivalent to LEFT - distinction is for more specialized
diff --git a/lib/ts/BufferWriterForward.h b/lib/ts/BufferWriterForward.h
index 388e00b..eb69e45 100644
--- a/lib/ts/BufferWriterForward.h
+++ b/lib/ts/BufferWriterForward.h
@@ -94,7 +94,7 @@ BWFSpec::is_sign(char c)
 inline bool
 BWFSpec::is_type(char c)
 {
-  return 'x' == c || 'X' == c || 'o' == c || 'b' == c || 'B' == c || 'd' == c;
+  return 'x' == c || 'X' == c || 'o' == c || 'b' == c || 'B' == c || 'd' == c || 's' == c || 'S' == c;
 }
 
 class BWFormat;
diff --git a/lib/ts/unit-tests/test_BufferWriterFormat.cc b/lib/ts/unit-tests/test_BufferWriterFormat.cc
index 3deed41..ad4e1bc 100644
--- a/lib/ts/unit-tests/test_BufferWriterFormat.cc
+++ b/lib/ts/unit-tests/test_BufferWriterFormat.cc
@@ -204,6 +204,33 @@ TEST_CASE("BWFormat", "[bwprint][bwformat]")
   bw.print("|{:<0.2,5x}|", sv);
   REQUIRE(bw.view() == "|63313|");
 
+  bw.reduce(0);
+  bw.print("|{}|", true);
+  REQUIRE(bw.view() == "|1|");
+  bw.reduce(0);
+  bw.print("|{}|", false);
+  REQUIRE(bw.view() == "|0|");
+  bw.reduce(0);
+  bw.print("|{:s}|", true);
+  REQUIRE(bw.view() == "|true|");
+  bw.reduce(0);
+  bw.print("|{:S}|", false);
+  REQUIRE(bw.view() == "|FALSE|");
+  bw.reduce(0);
+  bw.print("|{:>9s}|", false);
+  REQUIRE(bw.view() == "|    false|");
+  bw.reduce(0);
+  bw.print("|{:=10s}|", true);
+  REQUIRE(bw.view() == "|   true   |");
+
+  // Test clipping a bit.
+  ts::LocalBufferWriter<20> bw20;
+  bw20.print("0123456789abc|{:=10s}|", true);
+  REQUIRE(bw20.view() == "0123456789abc|   tru");
+  bw20.reduce(0);
+  bw20.print("012345|{:=10s}|6789abc", true);
+  REQUIRE(bw20.view() == "012345|   true   |67");
+
   INK_MD5 md5;
   bw.reduce(0);
   bw.print("{}", md5);

-- 
To stop receiving notification emails like this one, please contact
amc@apache.org.