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/02/13 14:16:34 UTC

[trafficserver] branch master updated: TextView: Fix svtoi for other bases, operator!, add verification tests.

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 3b279f4  TextView: Fix svtoi for other bases, operator!, add verification tests.
3b279f4 is described below

commit 3b279f483cd4eb6f73291a898dbda8dcb3cba035
Author: Alan M. Carroll <am...@apache.org>
AuthorDate: Tue Jan 23 20:22:19 2018 -0600

    TextView: Fix svtoi for other bases, operator!, add verification tests.
---
 lib/ts/TextView.cc                 | 19 +++++++++++++++----
 lib/ts/TextView.h                  |  5 ++---
 lib/ts/unit-tests/test_TextView.cc | 35 ++++++++++++++++++++++++++++++++++-
 3 files changed, 51 insertions(+), 8 deletions(-)

diff --git a/lib/ts/TextView.cc b/lib/ts/TextView.cc
index 28d8207..8b09cc8 100644
--- a/lib/ts/TextView.cc
+++ b/lib/ts/TextView.cc
@@ -26,7 +26,6 @@
 #include <ts/TextView.h>
 #include <sstream>
 #include <cctype>
-#include <ts/ink_platform.h>
 
 int
 ts::memcmp(TextView const &lhs, TextView const &rhs)
@@ -96,10 +95,10 @@ ts::svtoi(TextView src, TextView *out, int base)
   if (out) {
     out->clear();
   }
-  if (!(1 < base && base <= 36)) {
+  if (!(0 <= base && base <= 36)) {
     return 0;
   }
-  if (src.ltrim_if(&isspace)) {
+  if (src.ltrim_if(&isspace) && src) {
     const char *start = src.data();
     int8_t v;
     bool neg = false;
@@ -107,7 +106,19 @@ ts::svtoi(TextView src, TextView *out, int base)
       ++src;
       neg = true;
     }
-    while (src.size() && (-1 != (v = convert[static_cast<unsigned char>(*src)]))) {
+    // If base is 0, it wasn't specified - check for standard base prefixes
+    if (0 == base) {
+      base = 10;
+      if ('0' == *src) {
+        ++src;
+        base = 8;
+        if (src && ('x' == *src || 'X' == *src)) {
+          ++src;
+          base = 16;
+        }
+      }
+    }
+    while (src.size() && (0 <= (v = convert[static_cast<unsigned char>(*src)])) && v < base) {
       zret = zret * base + v;
       ++src;
     }
diff --git a/lib/ts/TextView.h b/lib/ts/TextView.h
index 79ca4be..dce8e51 100644
--- a/lib/ts/TextView.h
+++ b/lib/ts/TextView.h
@@ -79,7 +79,7 @@ using ::strcasecmp; // Make this an overload, not an override.
     - If the number starts with a literal '0' then it is treated as base 8.
     - If the number starts with the literal characters '0x' or '0X' then it is treated as base 16.
 */
-intmax_t svtoi(TextView src, TextView *parsed = nullptr, int base = 10);
+intmax_t svtoi(TextView src, TextView *parsed = nullptr, int base = 0);
 
 /** A read only view of contiguous piece of memory.
 
@@ -553,7 +553,7 @@ inline char TextView::operator*() const
 
 inline bool TextView::operator!() const
 {
-  return !this->empty();
+  return this->empty();
 }
 
 inline TextView::operator bool() const
@@ -1032,7 +1032,6 @@ extern template std::ostream &TextView::stream_write(std::ostream &, const TextV
 namespace std
 {
 ostream &operator<<(ostream &os, const ts::TextView &b);
-ostream &operator<<(ostream &os, const ts::TextView &b);
 }
 
 #if 0
diff --git a/lib/ts/unit-tests/test_TextView.cc b/lib/ts/unit-tests/test_TextView.cc
index e8bdb02..ffe87f8 100644
--- a/lib/ts/unit-tests/test_TextView.cc
+++ b/lib/ts/unit-tests/test_TextView.cc
@@ -25,6 +25,8 @@
 #include <catch.hpp>
 #include <iostream>
 
+using TV = ts::TextView;
+
 TEST_CASE("TextView Constructor", "[libts][TextView]")
 {
   static std::string base = "Evil Dave Rulez!";
@@ -37,12 +39,21 @@ TEST_CASE("TextView Constructor", "[libts][TextView]")
 
 TEST_CASE("TextView Operations", "[libts][TextView]")
 {
-  ts::TextView tv{"Evil Dave Rulez"};
+  TV tv{"Evil Dave Rulez"};
+  TV nothing;
   size_t off;
 
   REQUIRE(tv.find('l') == 3);
   off = tv.find_if([](char c) { return c == 'D'; });
   REQUIRE(off == tv.find('D'));
+
+  REQUIRE(tv);
+  REQUIRE(!tv == false);
+  if (nothing) {
+    REQUIRE(nullptr == "bad operator bool on TextView");
+  }
+  REQUIRE(!nothing == true);
+  REQUIRE(nothing.empty() == true);
 }
 
 TEST_CASE("TextView Trimming", "[libts][TextView]")
@@ -180,3 +191,25 @@ TEST_CASE("TextView Formatting", "[libts][TextView]")
     REQUIRE(buff.str() == "|01234567____|");
   }
 }
+
+TEST_CASE("TextView Conversions", "[libts][TextView]")
+{
+  TV n  = "   956783";
+  TV n2 = n;
+  TV n3 = "031";
+  TV n4 = "13f8q";
+  TV n5 = "0x13f8";
+  TV n6 = "0X13f8";
+  TV x;
+  n2.ltrim_if(&isspace);
+
+  REQUIRE(956783 == svtoi(n));
+  REQUIRE(956783 == svtoi(n2));
+  REQUIRE(0x13f8 == svtoi(n4, &x, 16));
+  REQUIRE(x == "13f8");
+  REQUIRE(0x13f8 == svtoi(n5));
+  REQUIRE(0x13f8 == svtoi(n6));
+
+  REQUIRE(25 == svtoi(n3));
+  REQUIRE(31 == svtoi(n3, nullptr, 10));
+}

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